From a9dca1db836e8d12430a521f661ea1e904fcdd49 Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Sun, 15 May 2022 17:33:31 +0200 Subject: [PATCH] Done with top stats formatting (#22, #9, #1) --- pom.xml | 8 +-- .../the/gr8/playerstats/ConfigHandler.java | 44 +++++++----- .../artemis/the/gr8/playerstats/Main.java | 3 +- .../gr8/playerstats/commands/StatCommand.java | 15 +++- .../playerstats/utils/OutputFormatter.java | 68 +++++-------------- src/main/resources/config.yml | 9 ++- 6 files changed, 68 insertions(+), 79 deletions(-) diff --git a/pom.xml b/pom.xml index 48e1b7e..d71520b 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ org.spigotmc spigot-api - 1.18-R0.1-SNAPSHOT + 1.18.2-R0.1-SNAPSHOT provided @@ -29,12 +29,6 @@ 23.0.0 compile - - - org.apache.commons - commons-lang3 - 3.12.0 - diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/ConfigHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/ConfigHandler.java index 46c3fb6..1f6b76d 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/ConfigHandler.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/ConfigHandler.java @@ -19,6 +19,18 @@ public class ConfigHandler { saveDefaultConfig(); } + //returns the config setting for use-dots, or the default value "true" if no value can be retrieved + public boolean getUseDots() { + ConfigurationSection ranked = config.getConfigurationSection("ranked-list"); + try { + return ranked == null || ranked.getBoolean("use-dots"); + } + catch (Exception e) { + e.printStackTrace(); + return true; + } + } + //returns a HashMap with all the available color choices, or a ChatColor.RESET if no colors were found public HashMap getChatColors() { HashMap chatColors = new HashMap<>(); @@ -35,10 +47,25 @@ public class ConfigHandler { chatColors.put("sub-stat-names-ranked", getChatColor(ranked, "sub-stat-names")); chatColors.put("stat-numbers-ranked", getChatColor(ranked, "stat-numbers")); chatColors.put("list-numbers", getChatColor(ranked, "list-numbers")); - chatColors.put("underscores", getChatColor(ranked, "underscores")); + chatColors.put("dots", getChatColor(ranked, "dots")); return chatColors; } + //reload the config after changes have been made to it + public boolean reloadConfig() { + try { + if (!configFile.exists()) { + saveDefaultConfig(); + } + config = YamlConfiguration.loadConfiguration(configFile); + return true; + } + catch (Exception e) { + e.printStackTrace(); + return false; + } + } + //returns the requested entry from the provided configuration section, null if section does not exist, and ChatColor.RESET if there is no entry private ChatColor getChatColor(ConfigurationSection section, String path) { ChatColor color; @@ -58,21 +85,6 @@ public class ConfigHandler { return color; } - //reload the config after changes have been made to it - public boolean reloadConfig() { - try { - if (!configFile.exists()) { - saveDefaultConfig(); - } - config = YamlConfiguration.loadConfiguration(configFile); - return true; - } - catch (Exception e) { - e.printStackTrace(); - return false; - } - } - //create a config file if none exists yet (from the config.yml in the plugin's resources) private void saveDefaultConfig() { config = plugin.getConfig(); diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java index 8191a71..169b3eb 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java @@ -13,11 +13,10 @@ public class Main extends JavaPlugin { @Override public void onEnable() { - ConfigHandler config = new ConfigHandler(this); EnumHandler enumHandler = new EnumHandler(); - OutputFormatter outputFormatter = new OutputFormatter(config, this); + OutputFormatter outputFormatter = new OutputFormatter(config); StatManager statManager = new StatManager(enumHandler, this); this.getCommand("statistic").setExecutor(new StatCommand(outputFormatter, statManager, this)); diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java index cf4f9e9..2943a00 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java @@ -4,6 +4,11 @@ import com.gmail.artemis.the.gr8.playerstats.Main; import com.gmail.artemis.the.gr8.playerstats.StatManager; import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; import com.gmail.artemis.the.gr8.playerstats.utils.OutputFormatter; +import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.ComponentBuilder; +import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -81,7 +86,11 @@ public class StatCommand implements CommandExecutor { LinkedHashMap topStats2 = statManager.getTopStatistics2(statName, subStatEntry); time = plugin.logTimeTaken("StatCommand", "onCommand", time, 82); - sender.sendMessage(outputFormatter.formatTopStats(topStats, statName, subStatEntry)); + String top = outputFormatter.formatTopStats(topStats, statName, subStatEntry); + String top2 = outputFormatter.formatTopStats(topStats2, statName, subStatEntry); + sender.sendMessage(top); + sender.sendMessage(top2); + return true; } catch (Exception e) { @@ -93,6 +102,10 @@ public class StatCommand implements CommandExecutor { else if (playerName != null) { try { + BaseComponent[] component = new ComponentBuilder("hi?").color(ChatColor.of("#4a32a8")).create(); + sender.spigot().sendMessage(component); + String msg = ChatColor.of("#f27d07") + "... hi"; + sender.sendMessage(msg); sender.sendMessage(outputFormatter.formatPlayerStat(playerName, statName, subStatEntry, statManager.getStatistic (statName, subStatEntry, playerName))); } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OutputFormatter.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OutputFormatter.java index 5f42ae6..c35fcaf 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OutputFormatter.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OutputFormatter.java @@ -1,36 +1,23 @@ 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 org.bukkit.map.MinecraftFont; import java.util.*; -import org.apache.commons.lang3.StringUtils; - public class OutputFormatter { - //keys for the HashMap are the same as the config options: - //player-names(-ranked) - //stat-names OR list-title - //sub-stat-names(-ranked) - //stat-numbers(-ranked) - //list-numbers + //keys for the HashMap are the same as the config options (so e.g. player-names/player-names-ranked) private final ConfigHandler config; - private final Main plugin; private HashMap chatColors; - private String pluginPrefix; - private String className; + private final String pluginPrefix; - public OutputFormatter(ConfigHandler c, Main p) { + public OutputFormatter(ConfigHandler c) { config = c; - plugin = p; pluginPrefix = ChatColor.GRAY + "[" + ChatColor.GOLD + "PlayerStats" + ChatColor.GRAY + "] " + ChatColor.RESET; - updateOutputColors(); - className = "OutputFormatter"; } public String formatExceptions(String exception) { @@ -38,42 +25,25 @@ public class OutputFormatter { } public String formatPlayerStat(String playerName, String statName, String subStatEntryName, int stat) { - String methodName = "formatPlayerStats"; - long time = System.currentTimeMillis(); - time = plugin.logTimeTaken(className, methodName, time, 39); - String subStat = subStatEntryName != null ? chatColors.get("sub-stat-names") + " (" + subStatEntryName.toLowerCase().replace("_", " ") + ")" : ""; - time = plugin.logTimeTaken(className, methodName, time, 43); - String msg = chatColors.get("player-names") + playerName + chatColors.get("stat-numbers") + ": " + stat + " " + + return chatColors.get("player-names") + playerName + chatColors.get("stat-numbers") + ": " + stat + " " + chatColors.get("stat-names") + statName.toLowerCase().replace("_", " ") + subStat; - plugin.logTimeTaken(className, methodName, time, 47); - return msg; } public String formatTopStats(LinkedHashMap topStats, String statName, String subStatEntryName) { String subStat = subStatEntryName != null ? chatColors.get("sub-stat-names-ranked") + " (" + subStatEntryName.toLowerCase().replace("_", " ") + ")" : ""; String topCount = chatColors.get("list-numbers") + " " + topStats.size(); - String title = pluginPrefix + chatColors.get("list-title") + "Top" + topCount + chatColors.get("list-title") + " " + + String title = "\n" + pluginPrefix + chatColors.get("list-title") + "Top" + topCount + chatColors.get("list-title") + " " + statName.toLowerCase().replace("_", " ") + subStat; + boolean useDots = config.getUseDots(); int count = 0; - Set playerNames = topStats.keySet(); MinecraftFont font = new MinecraftFont(); - int max = 130; - boolean useWidth = true; - /*try { - //https://stackoverflow.com/questions/43034015/how-do-i-properly-align-using-string-format-in-java - max = playerNames.stream().map(font::getWidth).max(Integer::compareTo).orElseThrow(); - } - catch (NoSuchElementException e) { - useWidth = false; - } - */ - String hairSpace = "\u200A"; + StringBuilder rankList = new StringBuilder(); for (String playerName : playerNames) { count = count+1; @@ -81,22 +51,20 @@ public class OutputFormatter { rankList.append("\n") .append(chatColors.get("list-numbers")).append(count).append(". ") .append(chatColors.get("player-names-ranked")).append(playerName).append(" ") - .append(chatColors.get("underscores")); - StringBuilder underscores = new StringBuilder(); + .append(chatColors.get("dots")); - int i = 0; - while (font.getWidth(count + ". " + playerName + " " + underscores) < 124) { - underscores.append("_"); - i++; + if (useDots) { + rankList.append(" "); + int dots = (125 - font.getWidth(count + ". " + playerName + " ")); + if (dots >= 1) { + rankList.append(".".repeat(dots)); + } + } + else { + rankList.append(":"); } - int extraSpaces = 129 - font.getWidth(count + ". " + playerName + " " + underscores); - hairSpace = hairSpace.repeat(extraSpaces); - - plugin.getLogger().info("while loop executed [" + i + "] times"); - rankList.append(underscores) - .append(hairSpace) - .append(chatColors.get("stat-numbers-ranked")).append(topStats.get(playerName).toString()); + rankList.append(" ").append(chatColors.get("stat-numbers-ranked")).append(topStats.get(playerName).toString()); } return title + rankList; } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index bec9ad6..942b7dc 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,7 +1,7 @@ # PlayerStats Configuration -# --- Color Options --- -# supports: all default Minecraft colors +# --- Format Options --- + individual-statistics: player-names: gold stat-names: yellow @@ -14,5 +14,8 @@ ranked-list: sub-stat-names: yellow stat-numbers: white list-numbers: gold - underscores: gray + +# If true, the statistics will be aligned so that they are all underneath each other + use-dots: true + dots: gray