Drastically improved performance by storing player+playerobject in HashMap in onEnable

This commit is contained in:
Artemis-the-gr8 2022-05-09 22:15:07 +02:00
parent eb334c074c
commit 17a7d10a4c
7 changed files with 51 additions and 25 deletions

View File

@ -24,7 +24,7 @@ public class Main extends JavaPlugin {
this.getCommand("statistic").setExecutor(new StatCommand(outputFormatter, statManager, this)); this.getCommand("statistic").setExecutor(new StatCommand(outputFormatter, statManager, this));
this.getCommand("statistic").setTabCompleter(new TabCompleter( this.getCommand("statistic").setTabCompleter(new TabCompleter(
enumHandler, statManager,this)); enumHandler, statManager,this));
this.getCommand("statisticreload").setExecutor(new ReloadCommand(config)); this.getCommand("statisticreload").setExecutor(new ReloadCommand(config, outputFormatter));
Bukkit.getPluginManager().registerEvents(new JoinListener(), this); Bukkit.getPluginManager().registerEvents(new JoinListener(), this);
this.getLogger().info("Enabled PlayerStats!"); this.getLogger().info("Enabled PlayerStats!");

View File

@ -45,6 +45,7 @@ public class StatManager {
//returns the integer associated with a certain statistic for a player //returns the integer associated with a certain statistic for a player
public int getStatistic(String statName, String subStatEntryName, String playerName) throws IllegalArgumentException, NullPointerException { public int getStatistic(String statName, String subStatEntryName, String playerName) throws IllegalArgumentException, NullPointerException {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(playerName); OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(playerName);
plugin.getLogger().info("StatManager 51: " + (System.currentTimeMillis() - time)); plugin.getLogger().info("StatManager 51: " + (System.currentTimeMillis() - time));

View File

@ -1,6 +1,7 @@
package com.gmail.artemis.the.gr8.playerstats.commands; package com.gmail.artemis.the.gr8.playerstats.commands;
import com.gmail.artemis.the.gr8.playerstats.ConfigHandler; import com.gmail.artemis.the.gr8.playerstats.ConfigHandler;
import com.gmail.artemis.the.gr8.playerstats.utils.OutputFormatter;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -10,14 +11,17 @@ import org.jetbrains.annotations.NotNull;
public class ReloadCommand implements CommandExecutor { public class ReloadCommand implements CommandExecutor {
private final ConfigHandler config; private final ConfigHandler config;
private final OutputFormatter outputFormatter;
public ReloadCommand(ConfigHandler c) { public ReloadCommand(ConfigHandler c, OutputFormatter o) {
outputFormatter = o;
config = c; config = c;
} }
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
if (config.reloadConfig()) { if (config.reloadConfig()) {
outputFormatter.updateOutputColors();
sender.sendMessage(ChatColor.GREEN + "Config reloaded!"); sender.sendMessage(ChatColor.GREEN + "Config reloaded!");
return true; return true;
} }

View File

@ -10,7 +10,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.time.Instant;
public class StatCommand implements CommandExecutor { public class StatCommand implements CommandExecutor {
@ -30,7 +29,8 @@ public class StatCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
plugin.getLogger().info("onCommand 33: " + time); long startTime = System.currentTimeMillis();
if (args.length >= 2) { if (args.length >= 2) {
String statName = null; String statName = null;
@ -84,10 +84,18 @@ public class StatCommand implements CommandExecutor {
try { try {
plugin.getLogger().info("onCommand 85: " + (System.currentTimeMillis() - time)); plugin.getLogger().info("onCommand 85: " + (System.currentTimeMillis() - time));
time = System.currentTimeMillis(); time = System.currentTimeMillis();
sender.sendMessage(outputFormatter.formatPlayerStat(playerName, statName, subStatEntry,
statManager.getStatistic(statName, subStatEntry, playerName))); int stat = statManager.getStatistic(statName, subStatEntry, playerName);
plugin.getLogger().info("onCommand 89: " + (System.currentTimeMillis() - time)); plugin.getLogger().info("onCommand 89: " + (System.currentTimeMillis() - time));
time = System.currentTimeMillis(); time = System.currentTimeMillis();
String msg = outputFormatter.formatPlayerStat(playerName, statName, subStatEntry, stat);
plugin.getLogger().info("onCommand 93: " + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
sender.sendMessage(msg);
plugin.getLogger().info("onCommand 97: " + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
} }
catch (Exception e) { catch (Exception e) {
sender.sendMessage(e.toString()); sender.sendMessage(e.toString());
@ -95,7 +103,8 @@ public class StatCommand implements CommandExecutor {
} }
} }
plugin.getLogger().info("onCommand 98: " + (System.currentTimeMillis() - time)); plugin.getLogger().info("onCommand 106: " + (System.currentTimeMillis() - time));
plugin.getLogger().info("Total time elapsed: " + (System.currentTimeMillis() - startTime));
return true; return true;
} }
} }

View File

@ -11,7 +11,7 @@ public class OfflinePlayerHandler {
private static OfflinePlayerHandler instance; private static OfflinePlayerHandler instance;
private List<OfflinePlayer> offlinePlayers; private List<OfflinePlayer> offlinePlayers;
private List<String> offlinePlayerNames; private List<String> offlinePlayerNames;
private HashMap<String, UUID> offlinePlayerUUIDs; private HashMap<String, OfflinePlayer> offlinePlayerMap;
private OfflinePlayerHandler() { private OfflinePlayerHandler() {
updateOfflinePlayers(); updateOfflinePlayers();
@ -30,15 +30,10 @@ public class OfflinePlayerHandler {
public OfflinePlayer getOfflinePlayer(String playerName) { public OfflinePlayer getOfflinePlayer(String playerName) {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
System.out.println(("OfflinePlayerHandler 34: " + (System.currentTimeMillis() - time)));
time = System.currentTimeMillis();
Optional<OfflinePlayer> player = offlinePlayers.stream().filter(offlinePlayer -> OfflinePlayer player = offlinePlayerMap.get(playerName);
offlinePlayer.getName() != null && offlinePlayer.getName().equalsIgnoreCase(playerName)).findAny(); System.out.println(("OfflinePlayerHandler 35: " + (System.currentTimeMillis() - time)));
return player;
System.out.println(("OfflinePlayerHandler 40: " + (System.currentTimeMillis() - time)));
time = System.currentTimeMillis();
return player.orElse(null);
} }
public List<OfflinePlayer> getAllOfflinePlayers() { public List<OfflinePlayer> getAllOfflinePlayers() {
@ -50,9 +45,10 @@ public class OfflinePlayerHandler {
} }
public void updateOfflinePlayers() { public void updateOfflinePlayers() {
offlinePlayerMap = new HashMap<>();
offlinePlayers = Arrays.stream(Bukkit.getOfflinePlayers()).filter(offlinePlayer -> offlinePlayers = Arrays.stream(Bukkit.getOfflinePlayers()).filter(offlinePlayer ->
offlinePlayer.getName() != null && offlinePlayer.hasPlayedBefore()).collect(Collectors.toList()); offlinePlayer.getName() != null && offlinePlayer.hasPlayedBefore()).collect(Collectors.toList());
offlinePlayerNames = offlinePlayers.stream().map(OfflinePlayer::getName).collect(Collectors.toList()); offlinePlayerNames = offlinePlayers.stream().map(OfflinePlayer::getName).collect(Collectors.toList());
offlinePlayers.forEach(offlinePlayer -> offlinePlayerUUIDs.put(offlinePlayer.getName(), offlinePlayer.getUniqueId())); offlinePlayers.forEach(offlinePlayer -> offlinePlayerMap.put(offlinePlayer.getName(), offlinePlayer));
} }
} }

View File

@ -1,6 +1,7 @@
package com.gmail.artemis.the.gr8.playerstats.utils; package com.gmail.artemis.the.gr8.playerstats.utils;
import com.gmail.artemis.the.gr8.playerstats.ConfigHandler; import com.gmail.artemis.the.gr8.playerstats.ConfigHandler;
import com.gmail.artemis.the.gr8.playerstats.Main;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import java.util.HashMap; import java.util.HashMap;
@ -13,11 +14,12 @@ public class OutputFormatter {
//statNames(Ranked) //statNames(Ranked)
//subStatNames(Ranked) //subStatNames(Ranked)
//numbers(Ranked) //numbers(Ranked)
private final ConfigHandler config;
private final HashMap<String, ChatColor> chatColors; private HashMap<String, ChatColor> chatColors;
public OutputFormatter(ConfigHandler c) { public OutputFormatter(ConfigHandler c) {
chatColors = c.getChatColors(); config = c;
updateOutputColors();
} }
public String formatTopStats(LinkedHashMap<String, Integer> topStats) { public String formatTopStats(LinkedHashMap<String, Integer> topStats) {
@ -29,10 +31,24 @@ public class OutputFormatter {
} }
public String formatPlayerStat(String playerName, String statName, String subStatEntryName, int stat) { public String formatPlayerStat(String playerName, String statName, String subStatEntryName, int stat) {
long time = System.currentTimeMillis();
System.out.println("OutputFormatter 33: " + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
String subStat = subStatEntryName != null ? String subStat = subStatEntryName != null ?
chatColors.get("subStatNames") + " (" + subStatEntryName.toLowerCase().replace("_", " ") + ")" : ""; chatColors.get("subStatNames") + " (" + subStatEntryName.toLowerCase().replace("_", " ") + ")" : "";
return chatColors.get("playerNames") + playerName + chatColors.get("numbers") + ": " + stat + " " + System.out.println("OutputFormatter 39: " + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
String msg = chatColors.get("playerNames") + playerName + chatColors.get("numbers") + ": " + stat + " " +
chatColors.get("statNames") + statName.toLowerCase().replace("_", " ") + subStat; chatColors.get("statNames") + statName.toLowerCase().replace("_", " ") + subStat;
System.out.println("OutputFormatter 45: " + (System.currentTimeMillis() - time));
return msg;
}
public void updateOutputColors() {
chatColors = config.getChatColors();
} }
} }

View File

@ -4,12 +4,12 @@
# supports: all default Minecraft colors # supports: all default Minecraft colors
individual-statistics: individual-statistics:
player-names: gold player-names: gold
stat-names: dark_aqua stat-names: yellow
sub-stat-names: blue sub-stat-names: yellow
numbers: white numbers: white
ranked-list: ranked-list:
player-names: gold player-names: gold
stat-names: dark_aqua stat-names: yellow
sub-stat-names: blue sub-stat-names: yellow
numbers: white numbers: white