Done with top stats formatting (#22, #9, #1)

This commit is contained in:
Artemis-the-gr8 2022-05-15 17:33:31 +02:00
parent 3db21f5970
commit a9dca1db83
6 changed files with 68 additions and 79 deletions

View File

@ -19,7 +19,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.18-R0.1-SNAPSHOT</version>
<version>1.18.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
@ -29,12 +29,6 @@
<version>23.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
<properties>

View File

@ -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<String, ChatColor> getChatColors() {
HashMap<String, ChatColor> 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();

View File

@ -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));

View File

@ -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<String, Integer> 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)));
}

View File

@ -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<String, ChatColor> 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<String, Integer> 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<String> 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;
}

View File

@ -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