Experimenting with a way to ensure compatibility with both Bukkit and Spigot ChatColors

This commit is contained in:
Artemis-the-gr8 2022-05-18 17:03:18 +02:00
parent 0c93fa85c9
commit 7a65f4ec57
5 changed files with 225 additions and 65 deletions

6
.idea/encodings.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

View File

@ -15,9 +15,19 @@ public class Main extends JavaPlugin {
@Override
public void onEnable() {
boolean enableHexColors = false;
try {
Class.forName("net.md_5.bungee.api.ChatColor");
enableHexColors = true;
this.getLogger().info("Hex Color support enabled!");
}
catch (ClassNotFoundException e) {
this.getLogger().info("Hex Colors are not supported for this server type, proceeding with default Chat Colors...");
}
ConfigHandler config = new ConfigHandler(this);
EnumHandler enumHandler = new EnumHandler(this);
OutputFormatter outputFormatter = new OutputFormatter(config);
OutputFormatter outputFormatter = new OutputFormatter(config, enableHexColors);
//prepare private hashMap of offline players
OfflinePlayerHandler.updateOfflinePlayers();
@ -27,6 +37,10 @@ public class Main extends JavaPlugin {
this.getCommand("statisticreload").setExecutor(new ReloadCommand(config, outputFormatter, this));
Bukkit.getPluginManager().registerEvents(new JoinListener(), this);
this.getLogger().info("Bukkit name: " + Bukkit.getName());
this.getLogger().info("Bukkit getServer name: " + Bukkit.getServer().getName());
this.getLogger().info("Bukkit version: " + Bukkit.getVersion());
this.getLogger().info("Bukkit getBukkitVersion: " + Bukkit.getBukkitVersion());
this.getLogger().info("Enabled PlayerStats!");
}

View File

@ -6,6 +6,7 @@ import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.util.HashMap;
@ -20,38 +21,6 @@ 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<>();
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 ranked = config.getConfigurationSection("ranked-list");
chatColors.put("player-names-ranked", getChatColor(ranked, "player-names"));
chatColors.put("list-title", getChatColor(ranked, "list-title"));
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("dots", getChatColor(ranked, "dots"));
return chatColors;
}
//reload the config after changes have been made to it
public boolean reloadConfig() {
try {
@ -67,7 +36,88 @@ public class ConfigHandler {
}
}
//returns the requested entry from the provided configuration section, null if section does not exist, and ChatColor.RESET if there is no entry
//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");
}
catch (Exception e) {
e.printStackTrace();
return true;
}
}
public HashMap<String, ChatColor> getStylingOptions() {
HashMap<String, ChatColor> styling = new HashMap<>();
}
//returns a HashMap with all the available (Bukkit) color choices (entries contain ChatColor.RESET if no colors were found)
public HashMap<String, ChatColor> getChatColors() {
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));
});
}
}
//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));
});
}
}
//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
private ChatColor getChatColor(ConfigurationSection section, String path) {
ChatColor color;
try {
@ -86,6 +136,26 @@ public class ConfigHandler {
return color;
}
//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
private net.md_5.bungee.api.ChatColor getHexChatColor(ConfigurationSection section, String path) {
net.md_5.bungee.api.ChatColor color;
try {
String colorText = section.getString(path);
if (colorText != null) {
color = net.md_5.bungee.api.ChatColor.of(colorText);
}
else {
color = net.md_5.bungee.api.ChatColor.RESET;
}
}
catch (IllegalArgumentException | NullPointerException exception) {
plugin.getLogger().warning(exception.toString());
color = net.md_5.bungee.api.ChatColor.RESET;
}
return color;
}
//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

@ -8,68 +8,116 @@ import java.util.*;
public class OutputFormatter {
//keys for the HashMap are the same as the config options (so e.g. player-names/player-names-ranked)
//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 HashMap<String, ChatColor> chatColors;
private HashMap<String, net.md_5.bungee.api.ChatColor> hexChatColors;
private final String pluginPrefix;
public OutputFormatter(ConfigHandler c) {
public OutputFormatter(ConfigHandler c, boolean enableHexColors) {
config = c;
pluginPrefix = ChatColor.GRAY + "[" + ChatColor.GOLD + "PlayerStats" + ChatColor.GRAY + "] " + ChatColor.RESET;
useHex = enableHexColors;
updateOutputColors();
}
public void updateOutputColors() {
updateOutPutColors(useHex);
}
public String formatExceptions(String exception) {
return pluginPrefix + exception;
}
public String formatPlayerStat(String playerName, String statName, String subStatEntryName, int stat) {
StringBuilder msg = new StringBuilder();
String subStat = subStatEntryName != null ?
chatColors.get("sub-stat-names") + " (" + subStatEntryName.toLowerCase().replace("_", " ") + ")" : "";
" (" + subStatEntryName.toLowerCase().replace("_", " ") + ")" : "";
return chatColors.get("player-names") + playerName + chatColors.get("stat-numbers") + ": " + stat + " " +
chatColors.get("stat-names") + statName.toLowerCase().replace("_", " ") + subStat;
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);
return msg.toString();
}
public String formatTopStats(LinkedHashMap<String, Integer> topStats, String statName, String subStatEntryName) {
StringBuilder msg = new StringBuilder();
String subStat = subStatEntryName != null ?
chatColors.get("sub-stat-names-ranked") + " (" + subStatEntryName.toLowerCase().replace("_", " ") + ")" : "";
String topCount = chatColors.get("list-numbers") + " " + topStats.size();
String title = "\n" + pluginPrefix + chatColors.get("list-title") + "Top" + topCount + chatColors.get("list-title") + " " +
statName.toLowerCase().replace("_", " ") + subStat;
" (" + subStatEntryName.toLowerCase().replace("_", " ") + ")" : "";
boolean useDots = config.getUseDots();
int count = 0;
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);
boolean useDots = config.useDots();
Set<String> playerNames = topStats.keySet();
MinecraftFont font = new MinecraftFont();
StringBuilder rankList = new StringBuilder();
int count = 0;
for (String playerName : playerNames) {
count = count+1;
rankList.append("\n")
.append(chatColors.get("list-numbers")).append(count).append(". ")
.append(chatColors.get("player-names-ranked")).append(playerName)
.append(chatColors.get("dots"));
msg.append("\n")
.append(getListNumberColor()).append(count).append(". ")
.append(getPlayerColor(true)).append(playerName);
if (useDots) {
rankList.append(" ");
int dots = (int) Math.round((125.0 - font.getWidth(count + ". " + playerName))/2);
msg.append(getDotColor()).append(" ");
int dots = (int) Math.round((130.0 - font.getWidth(count + ". " + playerName))/2);
if (dots >= 1) {
rankList.append(".".repeat(dots));
msg.append(".".repeat(dots));
}
}
else {
rankList.append(":");
msg.append(":");
}
rankList.append(" ").append(chatColors.get("stat-numbers-ranked")).append(topStats.get(playerName).toString());
msg.append(" ").append(getStatNumberColor(true)).append(topStats.get(playerName).toString());
}
return title + rankList;
return msg.toString();
}
public void updateOutputColors() {
chatColors = config.getChatColors();
private Object getPlayerColor(boolean isTopStat) {
return getColor("player-names", isTopStat);
}
private Object getStatNameColor(boolean isTopStat) {
return getColor("stat-names", isTopStat);
}
private Object getSubStatNameColor(boolean isTopStat) {
return getColor("sub-stat-names", isTopStat);
}
private Object getStatNumberColor(boolean isTopStat) {
return getColor("stat-numbers", isTopStat);
}
private Object getListNumberColor() {
return getColor("list-numbers", true);
}
private Object getDotColor() {
return getColor("dots", true);
}
//gets the appropriate ChatColor object, depending on whether the Spigot ChatColor is available or not
private Object getColor(String path, boolean isTopStat) {
path = isTopStat ? path + "-top" : path;
return useHex ? hexChatColors.get(path) : chatColors.get(path);
}
private void updateOutPutColors(boolean useHex) {
if (useHex) {
hexChatColors = config.getHexChatColors();
}
else {
chatColors = config.getChatColors();
}
}
}

View File

@ -4,20 +4,42 @@
# --- Format & Color Options ---
# --------- format -------------
# If true, the top list of statistics will be aligned so that they are all underneath each other
use-dots: true
# ---------- color -------------
# The colors below can be chat colors or hex codes if your server supports those (Bukkit does not, Spigot and further forks do)
# Make sure to put hex codes between quotation marks! (format: '#xxxxxx')
individual-statistics:
player-names: gold
stat-names: yellow
sub-stat-names: yellow
stat-numbers: white
ranked-list:
top-list:
player-names: green
list-title: yellow
stat-names: yellow
sub-stat-names: yellow
stat-numbers: white
list-numbers: gold
# If true, the statistics will be aligned so that they are all underneath each other
use-dots: true
dots: dark_gray
# This provides additional styling options such as italic/underlined/bold
individual-statistics-style:
player-names: none
stat-names: none
sub-stat-names: none
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