Made sure ChatColors work correctly for Bukkit and Spigot+ (#26), added more styling options and hex color support (#20)

This commit is contained in:
Artemis-the-gr8 2022-05-18 21:34:03 +02:00
parent 7a65f4ec57
commit dc364a567e
5 changed files with 137 additions and 89 deletions

View File

@ -31,11 +31,14 @@ public class StatThread extends Thread {
enumHandler = e;
outputFormatter = o;
plugin = p;
plugin.getLogger().info("StatThread created!");
}
//what the thread will do once started
@Override
public void run() throws IllegalStateException, NullPointerException {
plugin.getLogger().info("Name: " + this.getName());
plugin.getLogger().info("ID: " + this.getId());
long time = System.currentTimeMillis();
if (outputFormatter == null || plugin == null) {

View File

@ -15,7 +15,7 @@ import org.jetbrains.annotations.NotNull;
public class StatCommand implements CommandExecutor {
private final OutputFormatter outputFormatter;
private static OutputFormatter outputFormatter;
private final EnumHandler enumHandler;
private final Main plugin;

View File

@ -38,9 +38,8 @@ public class ConfigHandler {
//returns the config setting for use-dots, or the default value "true" if no value can be retrieved
public boolean useDots() {
ConfigurationSection ranked = config.getConfigurationSection("top-list");
try {
return ranked == null || ranked.getBoolean("use-dots");
return config.getBoolean("use-dots");
}
catch (Exception e) {
e.printStackTrace();
@ -48,10 +47,47 @@ public class ConfigHandler {
}
}
public HashMap<String, ChatColor> getStylingOptions() {
//returns a HashMap with the available (Bukkit) style choices, null if no style was chosen, and ChatColor.RESET if the entry was not valid
public HashMap<String, ChatColor> getStyleOptions() {
HashMap<String, ChatColor> styling = new HashMap<>();
ConfigurationSection individual = config.getConfigurationSection("individual-statistics-style");
if (individual != null) {
plugin.getLogger().info("individual-statistics-style: " + individual.getKeys(false));
individual.getKeys(false).forEach(path -> {
styling.put(path, getStyleOption(individual, path));
});
}
ConfigurationSection top = config.getConfigurationSection("top-list-style");
if (top != null) {
plugin.getLogger().info("top-list-style: " + top.getKeys(false));
top.getKeys(false).forEach(path -> {
styling.put(path + "-top", getStyleOption(top, path));
});
}
return styling;
}
private ChatColor getStyleOption(ConfigurationSection section, String path) {
ChatColor style;
try {
String entry = section.getString(path);
if (entry == null || entry.equalsIgnoreCase("none")) {
style = null;
}
else {
style = getChatColor(section, path);
}
}
catch (NullPointerException ignored) {
style = null;
}
catch (IllegalArgumentException e) {
plugin.getLogger().warning(e.toString());
style = null;
}
return style;
}
//returns a HashMap with all the available (Bukkit) color choices (entries contain ChatColor.RESET if no colors were found)
@ -59,65 +95,25 @@ public class ConfigHandler {
HashMap<String, ChatColor> chatColors = new HashMap<>();
ConfigurationSection individual = config.getConfigurationSection("individual-statistics");
chatColors.put("player-names", getChatColor(individual, "player-names"));
chatColors.put("stat-names", getChatColor(individual, "stat-names"));
chatColors.put("sub-stat-names", getChatColor(individual, "sub-stat-names"));
chatColors.put("stat-numbers", getChatColor(individual, "stat-numbers"));
ConfigurationSection top = config.getConfigurationSection("top-list");
chatColors.put("player-names-top", getChatColor(top, "player-names"));
chatColors.put("stat-names-top", getChatColor(top, "stat-names"));
chatColors.put("sub-stat-names-top", getChatColor(top, "sub-stat-names"));
chatColors.put("stat-numbers-top", getChatColor(top, "stat-numbers"));
chatColors.put("list-numbers-top", getChatColor(top, "list-numbers"));
chatColors.put("dots-top", getChatColor(top, "dots"));
return chatColors;
}
//returns a HashMap with all the available (Spigot) color choices (entries contain ChatColor.RESET if no colors were found)
public HashMap<String, net.md_5.bungee.api.ChatColor> getHexChatColors() {
HashMap<String, net.md_5.bungee.api.ChatColor> chatColors = new HashMap<>();
fillSpigotHashMap(chatColors, config.getConfigurationSection("individual-statistics"), false);
/*
ConfigurationSection individual = config.getConfigurationSection("individual-statistics");
chatColors.put("player-names", getHexChatColor(individual, "player-names"));
chatColors.put("stat-names", getHexChatColor(individual, "stat-names"));
chatColors.put("sub-stat-names", getHexChatColor(individual, "sub-stat-names"));
chatColors.put("stat-numbers", getHexChatColor(individual, "stat-numbers"));
ConfigurationSection top = config.getConfigurationSection("top-list");
chatColors.put("player-names-top", getHexChatColor(top, "player-names"));
chatColors.put("stat-names-top", getHexChatColor(top, "stat-names"));
chatColors.put("sub-stat-names-top", getHexChatColor(top, "sub-stat-names"));
chatColors.put("stat-numbers-top", getHexChatColor(top, "stat-numbers"));
chatColors.put("list-numbers-top", getHexChatColor(top, "list-numbers"));
chatColors.put("dots-top", getHexChatColor(top, "dots"));
*/
return chatColors;
}
//fill the provided HashMap with either Bukkit or Spigot ChatColors for the given ConfigurationSection (individual or top)
private void fillSpigotHashMap(HashMap<String, net.md_5.bungee.api.ChatColor> hashMap, ConfigurationSection section, boolean isTopSection) {
if (section != null) {
section.getKeys(false).forEach(path -> {
String hashMapKey = isTopSection ? path + "-top" : path;
hashMap.put(hashMapKey, getHexChatColor(section, path));
if (individual != null) {
plugin.getLogger().info("individual-statistics: " + individual.getKeys(false));
individual.getKeys(false).forEach(path -> {
chatColors.put(path, getChatColor(individual, path));
});
}
}
//fill the provided HashMap with either Bukkit or Spigot ChatColors for the given ConfigurationSection (individual or top)
private void fillBukkitHashMap(HashMap<String, ChatColor> hashMap, ConfigurationSection section, boolean isTopSection) {
if (section != null) {
section.getKeys(false).forEach(path -> {
String hashMapKey = isTopSection ? path + "-top" : path;
hashMap.put(hashMapKey, getChatColor(section, path));
ConfigurationSection top = config.getConfigurationSection("top-list");
if (top != null) {
plugin.getLogger().info("top-list: " + top.getKeys(false));
top.getKeys(false).forEach(path -> {
chatColors.put(path + "-top", getChatColor(top, path));
});
}
return chatColors;
}
//turns the requested entry from the provided configuration section into a (Bukkit) ChatColor
//returns null if section does not exist, and ChatColor.RESET if there is no entry
//returns null if section does not exist, and if there is no (or a bad) entry
private ChatColor getChatColor(ConfigurationSection section, String path) {
ChatColor color;
try {
@ -126,18 +122,40 @@ public class ConfigHandler {
color = ChatColor.valueOf(colorText.toUpperCase().replace(" ", "_"));
}
else {
color = ChatColor.RESET;
color = null;
}
}
catch (IllegalArgumentException | NullPointerException exception) {
plugin.getLogger().warning(exception.toString());
color = ChatColor.RESET;
color = null;
}
return color;
}
//returns a HashMap with all the available (Spigot) color choices (entries contain ChatColor.RESET if no colors were found)
public HashMap<String, net.md_5.bungee.api.ChatColor> getHexChatColors() {
HashMap<String, net.md_5.bungee.api.ChatColor> chatColors = new HashMap<>();
ConfigurationSection individual = config.getConfigurationSection("individual-statistics");
if (individual != null) {
plugin.getLogger().info("individual-statistics: " + individual.getKeys(false));
individual.getKeys(false).forEach(path -> {
chatColors.put(path, getHexChatColor(individual, path));
});
}
ConfigurationSection top = config.getConfigurationSection("top-list");
if (top != null) {
plugin.getLogger().info("top-list: " + top.getKeys(false));
top.getKeys(false).forEach(path -> {
chatColors.put(path + "-top", getHexChatColor(top, path));
});
}
return chatColors;
}
//turns the requested entry from the provided configuration section into a (Spigot) ChatColor
//returns null if section does not exist, and ChatColor.RESET if there is no entry
//returns null if section does not exist, or if there is no (or a bad) entry
private net.md_5.bungee.api.ChatColor getHexChatColor(ConfigurationSection section, String path) {
net.md_5.bungee.api.ChatColor color;
try {
@ -146,12 +164,12 @@ public class ConfigHandler {
color = net.md_5.bungee.api.ChatColor.of(colorText);
}
else {
color = net.md_5.bungee.api.ChatColor.RESET;
color = null;
}
}
catch (IllegalArgumentException | NullPointerException exception) {
plugin.getLogger().warning(exception.toString());
color = net.md_5.bungee.api.ChatColor.RESET;
color = null;
}
return color;
}

View File

@ -10,8 +10,9 @@ public class OutputFormatter {
//keys for the HashMaps are the same as the config options (so e.g. player-names/player-names-ranked)
private final boolean useHex;
private final ConfigHandler config;
private static ConfigHandler config;
private HashMap<String, ChatColor> chatColors;
private HashMap<String, ChatColor> styleOptions;
private HashMap<String, net.md_5.bungee.api.ChatColor> hexChatColors;
private final String pluginPrefix;
@ -36,10 +37,11 @@ public class OutputFormatter {
String subStat = subStatEntryName != null ?
" (" + subStatEntryName.toLowerCase().replace("_", " ") + ")" : "";
msg.append(getPlayerColor(false)).append(playerName).append(": ")
.append(getStatNumberColor(false)).append(stat).append(" ")
.append(getStatNameColor(false)).append(statName.toLowerCase().replace("_", " "))
.append(getSubStatNameColor(false)).append(subStat);
msg.append(getPlayerColor(false)).append(getPlayerStyle(false)).append(playerName).append(": ")
.append(getStatNumberColor(false)).append(getStatNumberStyle(false)).append(stat).append(" ")
.append(getStatNameColor(false)).append(getStatNameStyle(false))
.append(statName.toLowerCase().replace("_", " "))
.append(getSubStatNameColor(false)).append(getSubStatNameStyle(false)).append(subStat);
return msg.toString();
}
@ -50,26 +52,32 @@ public class OutputFormatter {
" (" + subStatEntryName.toLowerCase().replace("_", " ") + ")" : "";
msg.append("\n").append(pluginPrefix)
.append(getStatNameColor(true)).append("Top ")
.append(getListNumberColor()).append(topStats.size())
.append(getStatNameColor(true)).append(" ").append(statName.toLowerCase().replace("_", " "))
.append(getSubStatNameColor(true)).append(subStat);
.append(getStatNameColor(true)).append(getStatNameStyle(true)).append("Top ")
.append(getListNumberColor()).append(getListNumberStyle()).append(topStats.size())
.append(getStatNameColor(true)).append(getStatNameStyle(true)).append(" ")
.append(statName.toLowerCase().replace("_", " "))
.append(getSubStatNameColor(true)).append(getSubStatNameStyle(true)).append(subStat);
boolean useDots = config.useDots();
Set<String> playerNames = topStats.keySet();
MinecraftFont font = new MinecraftFont();
int count = 0;
for (String playerName : playerNames) {
count = count+1;
msg.append("\n")
.append(getListNumberColor()).append(count).append(". ")
.append(getPlayerColor(true)).append(playerName);
.append(getListNumberColor()).append(getListNumberStyle()).append(count).append(". ")
.append(getPlayerColor(true)).append(getPlayerStyle(true)).append(playerName);
if (useDots) {
msg.append(getDotColor()).append(" ");
int dots = (int) Math.round((130.0 - font.getWidth(count + ". " + playerName))/2);
if (getPlayerStyle(true).equals(ChatColor.BOLD)) {
dots = (int) Math.round((130.0 - font.getWidth(count + ". ") - (font.getWidth(playerName) * 1.19))/2);
}
if (dots >= 1) {
msg.append(".".repeat(dots));
}
@ -77,42 +85,61 @@ public class OutputFormatter {
else {
msg.append(":");
}
msg.append(" ").append(getStatNumberColor(true)).append(topStats.get(playerName).toString());
msg.append(" ").append(getStatNumberColor(true)).append(getStatNumberStyle(true)).append(topStats.get(playerName).toString());
}
return msg.toString();
}
private Object getPlayerColor(boolean isTopStat) {
return getColor("player-names", isTopStat);
}
private Object getPlayerColor(boolean isTopStat) {return getColor("player-names", false, isTopStat);}
private Object getPlayerStyle(boolean isTopStat) {return getColor("player-names", true, isTopStat);}
private Object getStatNameColor(boolean isTopStat) {
return getColor("stat-names", isTopStat);
return getColor("stat-names", false, isTopStat);
}
private Object getStatNameStyle(boolean isTopStat) {return getColor("stat-names", true, isTopStat);}
private Object getSubStatNameColor(boolean isTopStat) {
return getColor("sub-stat-names", isTopStat);
return getColor("sub-stat-names", false, isTopStat);
}
private Object getSubStatNameStyle(boolean isTopStat) {return getColor("sub-stat-names", true, isTopStat);}
private Object getStatNumberColor(boolean isTopStat) {
return getColor("stat-numbers", isTopStat);
return getColor("stat-numbers", false, isTopStat);
}
private Object getStatNumberStyle(boolean isTopStat) {return getColor("stat-numbers", true, isTopStat);}
private Object getListNumberColor() {
return getColor("list-numbers", true);
return getColor("list-numbers", false, true);
}
private Object getListNumberStyle() {return getColor("list-numbers", true, true);}
private Object getDotColor() {
return getColor("dots", true);
return getColor("dots", false,true);
}
//gets the appropriate ChatColor object, depending on whether the Spigot ChatColor is available or not
private Object getColor(String path, boolean isTopStat) {
//gets the appropriate ChatColor object (or empty string), depending on whether the Spigot ChatColor is available or not
//the HashMap keys are config paths (with -top for the top-list variants)
private Object getColor(String path, boolean isStyleOption, boolean isTopStat) {
path = isTopStat ? path + "-top" : path;
return useHex ? hexChatColors.get(path) : chatColors.get(path);
if (isStyleOption) {
return styleOptions.get(path) != null ? styleOptions.get(path) : "";
}
else if (useHex){
return hexChatColors.get(path) != null ? hexChatColors.get(path) : "";
}
else {
return chatColors.get(path) != null ? chatColors.get(path) : "";
}
}
private void updateOutPutColors(boolean useHex) {
styleOptions = config.getStyleOptions();
if (useHex) {
hexChatColors = config.getHexChatColors();
}

View File

@ -26,7 +26,7 @@ top-list:
list-numbers: gold
dots: dark_gray
# This provides additional styling options such as italic/underlined/bold
# This provides additional styling options such as italic/underline/bold
individual-statistics-style:
player-names: none
stat-names: none
@ -34,11 +34,11 @@ individual-statistics-style:
stat-numbers: none
top-list-style:
player-names-style: none
stat-names-style: none
sub-stat-names-style: none
stat-numbers-style: none
list-numbers-style: none
player-names: none
stat-names: none
sub-stat-names: none
stat-numbers: none
list-numbers: none