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").setTabCompleter(new TabCompleter(
enumHandler, statManager,this));
this.getCommand("statisticreload").setExecutor(new ReloadCommand(config));
this.getCommand("statisticreload").setExecutor(new ReloadCommand(config, outputFormatter));
Bukkit.getPluginManager().registerEvents(new JoinListener(), this);
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
public int getStatistic(String statName, String subStatEntryName, String playerName) throws IllegalArgumentException, NullPointerException {
long time = System.currentTimeMillis();
OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(playerName);
plugin.getLogger().info("StatManager 51: " + (System.currentTimeMillis() - time));

View File

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

View File

@ -10,7 +10,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.time.Instant;
public class StatCommand implements CommandExecutor {
@ -30,7 +29,8 @@ public class StatCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
long time = System.currentTimeMillis();
plugin.getLogger().info("onCommand 33: " + time);
long startTime = System.currentTimeMillis();
if (args.length >= 2) {
String statName = null;
@ -84,10 +84,18 @@ public class StatCommand implements CommandExecutor {
try {
plugin.getLogger().info("onCommand 85: " + (System.currentTimeMillis() - time));
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));
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) {
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;
}
}

View File

@ -11,7 +11,7 @@ public class OfflinePlayerHandler {
private static OfflinePlayerHandler instance;
private List<OfflinePlayer> offlinePlayers;
private List<String> offlinePlayerNames;
private HashMap<String, UUID> offlinePlayerUUIDs;
private HashMap<String, OfflinePlayer> offlinePlayerMap;
private OfflinePlayerHandler() {
updateOfflinePlayers();
@ -30,15 +30,10 @@ public class OfflinePlayerHandler {
public OfflinePlayer getOfflinePlayer(String playerName) {
long time = System.currentTimeMillis();
System.out.println(("OfflinePlayerHandler 34: " + (System.currentTimeMillis() - time)));
time = System.currentTimeMillis();
Optional<OfflinePlayer> player = offlinePlayers.stream().filter(offlinePlayer ->
offlinePlayer.getName() != null && offlinePlayer.getName().equalsIgnoreCase(playerName)).findAny();
System.out.println(("OfflinePlayerHandler 40: " + (System.currentTimeMillis() - time)));
time = System.currentTimeMillis();
return player.orElse(null);
OfflinePlayer player = offlinePlayerMap.get(playerName);
System.out.println(("OfflinePlayerHandler 35: " + (System.currentTimeMillis() - time)));
return player;
}
public List<OfflinePlayer> getAllOfflinePlayers() {
@ -50,9 +45,10 @@ public class OfflinePlayerHandler {
}
public void updateOfflinePlayers() {
offlinePlayerMap = new HashMap<>();
offlinePlayers = Arrays.stream(Bukkit.getOfflinePlayers()).filter(offlinePlayer ->
offlinePlayer.getName() != null && offlinePlayer.hasPlayedBefore()).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;
import com.gmail.artemis.the.gr8.playerstats.ConfigHandler;
import com.gmail.artemis.the.gr8.playerstats.Main;
import org.bukkit.ChatColor;
import java.util.HashMap;
@ -13,11 +14,12 @@ public class OutputFormatter {
//statNames(Ranked)
//subStatNames(Ranked)
//numbers(Ranked)
private final HashMap<String, ChatColor> chatColors;
private final ConfigHandler config;
private HashMap<String, ChatColor> chatColors;
public OutputFormatter(ConfigHandler c) {
chatColors = c.getChatColors();
config = c;
updateOutputColors();
}
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) {
long time = System.currentTimeMillis();
System.out.println("OutputFormatter 33: " + (System.currentTimeMillis() - time));
time = System.currentTimeMillis();
String subStat = subStatEntryName != null ?
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;
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
individual-statistics:
player-names: gold
stat-names: dark_aqua
sub-stat-names: blue
stat-names: yellow
sub-stat-names: yellow
numbers: white
ranked-list:
player-names: gold
stat-names: dark_aqua
sub-stat-names: blue
stat-names: yellow
sub-stat-names: yellow
numbers: white