Started reworking OutputFormatter and ConfigHandler to work with adventure components instead

This commit is contained in:
Artemis-the-gr8 2022-05-26 02:58:53 +02:00
parent dab141f4ca
commit 270dea4ab5
10 changed files with 322 additions and 488 deletions

View File

@ -6,7 +6,7 @@ import com.gmail.artemis.the.gr8.playerstats.commands.TabCompleter;
import com.gmail.artemis.the.gr8.playerstats.filehandlers.ConfigHandler;
import com.gmail.artemis.the.gr8.playerstats.listeners.JoinListener;
import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
import com.gmail.artemis.the.gr8.playerstats.utils.OutputFormatter;
import com.gmail.artemis.the.gr8.playerstats.utils.MessageFactory;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
@ -16,7 +16,6 @@ import org.jetbrains.annotations.NotNull;
public class Main extends JavaPlugin {
private static boolean enableHexColors;
private BukkitAudiences adventure;
public @NotNull BukkitAudiences adventure() {
@ -33,31 +32,20 @@ public class Main extends JavaPlugin {
//initialize the Adventure library
adventure = BukkitAudiences.create(this);
//check if Spigot ChatColors can be used, and prepare accordingly
try {
Class.forName("net.md_5.bungee.api.ChatColor");
enableHexColors = true;
this.getLogger().info("Hex Color support enabled!");
}
catch (ClassNotFoundException e) {
enableHexColors = false;
this.getLogger().info("Hex Colors are not supported for this server type, proceeding with default Chat Colors...");
}
//get instances of the classes that should be initialized
ConfigHandler config = new ConfigHandler(this);
OutputFormatter outputFormatter = new OutputFormatter(config, enableHexColors);
MessageFactory messageFactory = new MessageFactory(config);
OfflinePlayerHandler offlinePlayerHandler = new OfflinePlayerHandler(config);
getLogger().info("Amount of offline players: " + offlinePlayerHandler.getOfflinePlayerCount());
//register the commands
PluginCommand statcmd = this.getCommand("statistic");
if (statcmd != null) {
statcmd.setExecutor(new StatCommand(config, offlinePlayerHandler, outputFormatter, this));
statcmd.setExecutor(new StatCommand(adventure(), config, offlinePlayerHandler, messageFactory, this));
statcmd.setTabCompleter(new TabCompleter(offlinePlayerHandler));
}
PluginCommand reloadcmd = this.getCommand("statisticreload");
if (reloadcmd != null) reloadcmd.setExecutor(new ReloadCommand(adventure(), config, offlinePlayerHandler, outputFormatter, this));
if (reloadcmd != null) reloadcmd.setExecutor(new ReloadCommand(config, offlinePlayerHandler, messageFactory, this));
//register the listener
Bukkit.getPluginManager().registerEvents(new JoinListener(offlinePlayerHandler), this);
@ -75,10 +63,6 @@ public class Main extends JavaPlugin {
this.getLogger().info("Disabled PlayerStats!");
}
public static boolean hexEnabled() {
return enableHexColors;
}
public long logTimeTaken(String className, String methodName, long previousTime) {
getLogger().info(className + " " + methodName + ": " + (System.currentTimeMillis() - previousTime));
return System.currentTimeMillis();

View File

@ -3,7 +3,7 @@ package com.gmail.artemis.the.gr8.playerstats;
import com.gmail.artemis.the.gr8.playerstats.filehandlers.ConfigHandler;
import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler;
import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
import com.gmail.artemis.the.gr8.playerstats.utils.OutputFormatter;
import com.gmail.artemis.the.gr8.playerstats.utils.MessageFactory;
import org.bukkit.OfflinePlayer;
import org.bukkit.Statistic;
import org.bukkit.command.CommandSender;
@ -21,16 +21,16 @@ public class StatThread extends Thread {
private final ConfigHandler config;
private final OfflinePlayerHandler offlinePlayerHandler;
private final OutputFormatter outputFormatter;
private final MessageFactory messageFactory;
private final Main plugin;
//constructor (called on thread creation)
public StatThread(StatRequest s, ConfigHandler c, OfflinePlayerHandler of, OutputFormatter o, Main p) {
public StatThread(StatRequest s, ConfigHandler c, OfflinePlayerHandler of, MessageFactory o, Main p) {
request = s;
config = c;
offlinePlayerHandler = of;
outputFormatter = o;
messageFactory = o;
plugin = p;
plugin.getLogger().info("StatThread created!");
}
@ -40,7 +40,7 @@ public class StatThread extends Thread {
public void run() throws IllegalStateException, NullPointerException {
long time = System.currentTimeMillis();
if (outputFormatter == null || plugin == null) {
if (messageFactory == null || plugin == null) {
throw new IllegalStateException("Not all classes off the plugin are running!");
}
if (request == null) {
@ -56,25 +56,25 @@ public class StatThread extends Thread {
if (playerName != null) {
try {
sender.sendMessage(
outputFormatter.formatPlayerStat(
messageFactory.formatPlayerStat(
playerName, statName, subStatEntry, getStatistic(
statName, subStatEntry, playerName)));
plugin.logTimeTaken("StatThread", "calculated individual stat", time);
} catch (Exception e) {
sender.sendMessage(outputFormatter.formatExceptions(e.toString()));
sender.sendMessage(messageFactory.formatExceptions(e.toString()));
e.printStackTrace();
}
} else if (topFlag) {
try {
sender.sendMessage(outputFormatter.formatTopStats(
sender.sendMessage(messageFactory.formatTopStats(
getTopStatistics(statName, subStatEntry), statName, subStatEntry));
plugin.logTimeTaken("StatThread", "calculated top stat", time);
} catch (Exception e) {
sender.sendMessage(outputFormatter.formatExceptions(e.toString()));
sender.sendMessage(messageFactory.formatExceptions(e.toString()));
e.printStackTrace();
}
}

View File

@ -2,15 +2,9 @@ package com.gmail.artemis.the.gr8.playerstats.commands;
import com.gmail.artemis.the.gr8.playerstats.Main;
import com.gmail.artemis.the.gr8.playerstats.filehandlers.ConfigHandler;
import com.gmail.artemis.the.gr8.playerstats.utils.ComponentFactory;
import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
import com.gmail.artemis.the.gr8.playerstats.utils.OutputFormatter;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.title.Title;
import com.gmail.artemis.the.gr8.playerstats.utils.MessageFactory;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -19,17 +13,15 @@ import org.jetbrains.annotations.NotNull;
public class ReloadCommand implements CommandExecutor {
private final BukkitAudiences adventure;
private final ConfigHandler config;
private final OfflinePlayerHandler offlinePlayerHandler;
private final OutputFormatter outputFormatter;
private final MessageFactory messageFactory;
private final Main plugin;
public ReloadCommand(BukkitAudiences b, ConfigHandler c, OfflinePlayerHandler of, OutputFormatter o, Main p) {
adventure = b;
public ReloadCommand(ConfigHandler c, OfflinePlayerHandler of, MessageFactory o, Main p) {
offlinePlayerHandler = of;
outputFormatter = o;
messageFactory = o;
config = c;
plugin = p;
}
@ -37,19 +29,10 @@ public class ReloadCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
if (config.reloadConfig()) {
outputFormatter.updateOutputColors();
offlinePlayerHandler.updateOfflinePlayerList();
plugin.getLogger().info("Amount of players: " + offlinePlayerHandler.getOfflinePlayerCount());
TextComponent t = Component.text("Hello :D").color(TextColor.fromHexString("#fc4e03"));
TextComponent subt = Component.text("Red ").color(NamedTextColor.RED)
.append(Component.text(" - for comparison - ").color(TextColor.fromHexString("#fc4e03")))
.append(Component.text(" Gold").color(NamedTextColor.GOLD));
Title title = Title.title(t, subt);
adventure.player(offlinePlayerHandler.getOfflinePlayerUUID("Artemis_the_gr8")).showTitle(title);
adventure.sender(sender).sendMessage(ComponentFactory.helpMsg());
sender.sendMessage(OutputFormatter.getPluginPrefix() + ChatColor.GREEN + "Config reloaded!");
sender.sendMessage(MessageFactory.getPluginPrefix() + ChatColor.GREEN + "Config reloaded!");
return true;
}
return false;

View File

@ -6,7 +6,8 @@ import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler;
import com.gmail.artemis.the.gr8.playerstats.StatRequest;
import com.gmail.artemis.the.gr8.playerstats.StatThread;
import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
import com.gmail.artemis.the.gr8.playerstats.utils.OutputFormatter;
import com.gmail.artemis.the.gr8.playerstats.utils.MessageFactory;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
@ -16,15 +17,17 @@ import org.jetbrains.annotations.NotNull;
public class StatCommand implements CommandExecutor {
private final BukkitAudiences adventure;
private final ConfigHandler config;
private final OfflinePlayerHandler offlinePlayerHandler;
private final OutputFormatter outputFormatter;
private final MessageFactory messageFactory;
private final Main plugin;
public StatCommand(ConfigHandler c, OfflinePlayerHandler of, OutputFormatter o, Main p) {
public StatCommand(BukkitAudiences b, ConfigHandler c, OfflinePlayerHandler of, MessageFactory o, Main p) {
adventure = b;
config = c;
offlinePlayerHandler = of;
outputFormatter = o;
messageFactory = o;
plugin = p;
}
@ -66,28 +69,18 @@ public class StatCommand implements CommandExecutor {
//part 2: sending the information to the StatThread
if (isValidStatRequest(request)) {
StatThread statThread = new StatThread(request, config, offlinePlayerHandler, outputFormatter, plugin);
StatThread statThread = new StatThread(request, config, offlinePlayerHandler, messageFactory, plugin);
statThread.start();
return true;
}
else {
if (Main.hexEnabled()) {
//sender.spigot().sendMessage(outputFormatter.formatHelpSpigot());
}
else {
sender.sendMessage(outputFormatter.formatHelpBukkit());
}
adventure.sender(sender).sendMessage(messageFactory.getHelpMsg());
return false;
}
}
else {
if (Main.hexEnabled()) {
//sender.spigot().sendMessage(outputFormatter.formatHelpSpigot());
}
else {
sender.sendMessage(outputFormatter.formatHelpBukkit());
}
adventure.sender(sender).sendMessage(messageFactory.getHelpMsg());
return false;
}
}

View File

@ -1,14 +1,13 @@
package com.gmail.artemis.the.gr8.playerstats.filehandlers;
import com.gmail.artemis.the.gr8.playerstats.Main;
import org.bukkit.ChatColor;
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;
public class ConfigHandler {
@ -91,113 +90,85 @@ public class ConfigHandler {
}
}
//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) {
individual.getKeys(false).forEach(path -> styling.put(path, getStyleOption(individual, path)));
}
ConfigurationSection top = config.getConfigurationSection("top-list-style");
if (top != null) {
top.getKeys(false).forEach(path -> styling.put(path + "-top", getStyleOption(top, path)));
}
return styling;
public String getPlayerNamesColor(boolean topStat) {
ConfigurationSection color = getRelevantSection(topStat, false);
return color != null ? color.getString("player-names") : null;
}
private ChatColor getStyleOption(ConfigurationSection section, String path) {
ChatColor style;
public String getPlayerNamesStyle(boolean topStat) {
ConfigurationSection style = getRelevantSection(topStat, true);
return style != null ? style.getString("player-names") : null;
}
public boolean playerNamesStyleIsBold() {
ConfigurationSection style = getRelevantSection(true, true);
String styleString = null;
if (style != null) styleString = style.getString("player-names");
return styleString != null && styleString.equalsIgnoreCase("bold");
}
public String getStatNamesColor(boolean topStat) {
ConfigurationSection color = getRelevantSection(topStat, false);
return color != null ? color.getString("stat-names") : "";
}
public String getStatNamesStyle(boolean topStat) {
ConfigurationSection style = getRelevantSection(topStat, true);
return style != null ? style.getString("stat-names") : "";
}
public String getSubStatNamesColor(boolean topStat) {
ConfigurationSection color = getRelevantSection(topStat, false);
return color != null ? color.getString("sub-stat-names") : "";
}
public String getSubStatNamesStyle(boolean topStat) {
ConfigurationSection style = getRelevantSection(topStat, true);
return style != null ? style.getString("sub-stat-names") : "";
}
public String getStatNumbersColor(boolean topStat) {
ConfigurationSection color = getRelevantSection(topStat, false);
return color != null ? color.getString("stat-numbers") : "";
}
public String getStatNumbersStyle(boolean topStat) {
ConfigurationSection style = getRelevantSection(topStat, true);
return style != null ? style.getString("stat-numbers") : "";
}
public String getListNumbersColor() {
ConfigurationSection color = getRelevantSection(true, false);
return color != null ? color.getString("list-numbers") : "";
}
public String getListNumbersStyle() {
ConfigurationSection style = getRelevantSection(true, true);
return style != null ? style.getString("list-numbers") : "";
}
public String getDotsColor() {
ConfigurationSection section = getRelevantSection(true, false);
return section != null ? section.getString("dots") : "";
}
private ConfigurationSection getRelevantSection(boolean topStat, boolean isStyle) {
ConfigurationSection section;
try {
String entry = section.getString(path);
if (entry == null || entry.equalsIgnoreCase("none")) {
style = null;
if (!topStat) {
if (!isStyle) section = config.getConfigurationSection("individual-statistics-color");
else section = config.getConfigurationSection("individual-statistics-style");
}
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)
public HashMap<String, ChatColor> getChatColors() {
HashMap<String, ChatColor> chatColors = new HashMap<>();
ConfigurationSection individual = config.getConfigurationSection("individual-statistics");
if (individual != null) {
individual.getKeys(false).forEach(path -> chatColors.put(path, getChatColor(individual, path)));
}
ConfigurationSection top = config.getConfigurationSection("top-list");
if (top != null) {
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 if there is no (or a bad) entry
private ChatColor getChatColor(ConfigurationSection section, String path) {
ChatColor color;
try {
String colorText = section.getString(path);
if (colorText != null) {
color = ChatColor.valueOf(colorText.toUpperCase().replace(" ", "_"));
}
else {
color = null;
if (!isStyle) section = config.getConfigurationSection("top-list-color");
else section = config.getConfigurationSection("top-list-style");
}
return section;
}
catch (IllegalArgumentException | NullPointerException exception) {
plugin.getLogger().warning(exception.toString());
color = null;
return 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) {
individual.getKeys(false).forEach(path -> chatColors.put(path, getHexChatColor(individual, path)));
}
ConfigurationSection top = config.getConfigurationSection("top-list");
if (top != null) {
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, 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 {
String colorText = section.getString(path);
if (colorText != null) {
color = net.md_5.bungee.api.ChatColor.of(colorText);
}
else {
color = null;
}
}
catch (IllegalArgumentException | NullPointerException exception) {
plugin.getLogger().warning(exception.toString());
color = null;
}
return color;
}
//create a config file if none exists yet (from the config.yml in the plugin's resources)

View File

@ -1,85 +0,0 @@
package com.gmail.artemis.the.gr8.playerstats.utils;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import static net.kyori.adventure.text.Component.newline;
import static net.kyori.adventure.text.Component.text;
public class ComponentFactory {
public static TextComponent helpMsg() {
TextComponent spaces = text(" ");
TextComponent underscores = text("____________").color(TextColor.fromHexString("#6E3485"));
TextComponent arrow = text("").color(NamedTextColor.GOLD);
//the builder
TextComponent helpMsg = Component.newline()
.append(underscores).append(spaces).append(text(OutputFormatter.getPluginPrefix())).append(spaces).append(underscores)
.append(newline())
.append(text("Hover over the arguments for more information!").color(NamedTextColor.GRAY).decorate(TextDecoration.ITALIC))
.append(newline())
.append(text("Usage: ").color(NamedTextColor.GOLD)).append(text("/statistic").color(NamedTextColor.YELLOW))
.append(newline())
.append(spaces).append(arrow)
.append(text("name").color(NamedTextColor.YELLOW)
.hoverEvent(HoverEvent.showText(text("The name of the statistic").color(TextColor.fromHexString("#FFD52B"))
.append(newline())
.append(text("Example: ").color(TextColor.fromHexString("#FFD52B")))
.append(text("\"mine_block\"").color(NamedTextColor.YELLOW)))))
.append(newline())
.append(spaces).append(arrow)
.append(text("sub-statistic"));
return helpMsg;
}
/*
public BaseComponent[] formatHelpSpigot() {
String spaces = " ";
String underscores = "____________";
ComponentBuilder underscore = new ComponentBuilder(underscores).color(net.md_5.bungee.api.ChatColor.of("#6E3485"));
TextComponent arrow = new TextComponent("");
arrow.setColor(net.md_5.bungee.api.ChatColor.GOLD);
TextComponent statName = new TextComponent("name");
statName.setColor(net.md_5.bungee.api.ChatColor.YELLOW);
statName.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new Text("The name of the statistic (Example: \"mine_block\")")));
TextComponent subStatName = new TextComponent("sub-statistic");
subStatName.setColor(net.md_5.bungee.api.ChatColor.YELLOW);
subStatName.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new Text("Some statistics require an item, block or entity as sub-statistic (example: \"mine_block diorite\")")));
TextComponent target = new TextComponent("me | player | top");
target.setColor(net.md_5.bungee.api.ChatColor.YELLOW);
target.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new Text("Choose whether you want to see your own statistic, another player's, or the top " + config.getTopListMaxSize())));
TextComponent playerName = new TextComponent("player-name");
playerName.setColor(net.md_5.bungee.api.ChatColor.YELLOW);
playerName.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new Text("In case you selected \"player\", specify the player's name here")));
ComponentBuilder help = new ComponentBuilder()
.append("\n").append(underscore.create()).append(spaces).append(pluginPrefix).append(spaces).append(underscore.create()).append("\n")
.append("Hover over the arguments for more information!").color(net.md_5.bungee.api.ChatColor.GRAY).italic(true).append("\n")
.append("Usage: ").color(net.md_5.bungee.api.ChatColor.GOLD).italic(false)
.append("/statistic ").color(net.md_5.bungee.api.ChatColor.YELLOW).append("\n")
.append(spaces).append(arrow).append(statName).append("\n").reset()
.append(spaces).append(arrow).append(subStatName).append("\n").reset()
.append(spaces).append(arrow).append(target).append("\n").reset()
.append(spaces).append(arrow).append(playerName);
return help.create();
}
*/
}

View File

@ -0,0 +1,214 @@
package com.gmail.artemis.the.gr8.playerstats.utils;
import com.gmail.artemis.the.gr8.playerstats.filehandlers.ConfigHandler;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.ChatColor;
import org.bukkit.map.MinecraftFont;
import java.util.*;
import static net.kyori.adventure.text.Component.newline;
import static net.kyori.adventure.text.Component.text;
public class MessageFactory {
private static ConfigHandler config;
public MessageFactory(ConfigHandler c) {
config = c;
}
public static String getPluginPrefix() {
return ChatColor.GRAY + "[" + ChatColor.GOLD + "PlayerStats" + ChatColor.GRAY + "] " + ChatColor.RESET;
}
public String formatExceptions(String exception) {
return getPluginPrefix() + exception;
}
public TextComponent getHelpMsg() {
TextComponent spaces = text(" ");
TextComponent underscores = text("____________").color(TextColor.fromHexString("#6E3485"));
TextComponent arrow = text("").color(NamedTextColor.GOLD);
TextColor arguments = NamedTextColor.YELLOW;
TextColor hoverDescription = NamedTextColor.GOLD;
TextColor hoverExample1 = TextColor.fromHexString("#FFD52B");
TextColor hoverExample2 = NamedTextColor.YELLOW;
return Component.newline()
.append(underscores).append(spaces).append(text(MessageFactory.getPluginPrefix())).append(spaces).append(underscores)
.append(newline())
.append(text("Hover over the arguments for more information!").color(NamedTextColor.GRAY).decorate(TextDecoration.ITALIC))
.append(newline())
.append(text("Usage: ").color(NamedTextColor.GOLD)).append(text("/statistic").color(arguments))
.append(newline())
.append(spaces).append(arrow)
.append(text("name").color(arguments)
.hoverEvent(HoverEvent.showText(text("The name of the statistic").color(hoverDescription)
.append(newline())
.append(text("Example: ").color(hoverExample1))
.append(text("\"mine_block\"").color(hoverExample2)))))
.append(newline())
.append(spaces).append(arrow)
.append(text("sub-statistic").color(arguments)
.hoverEvent(HoverEvent.showText(
text("Some statistics require an item, block or entity as sub-statistic").color(hoverDescription)
.append(newline())
.append(text("Example: ").color(hoverExample1)
.append(text("\"mine_block diorite\"").color(hoverExample2))))))
.append(newline())
.append(spaces).append(arrow)
.append(text("me | player | top").color(arguments)
.hoverEvent(HoverEvent.showText(
text("Choose whether you want to see your own statistic, another player's, or the top ").color(hoverDescription)
.append(text(config.getTopListMaxSize()).color(hoverDescription)))))
.append(newline())
.append(spaces).append(arrow)
.append(text("player-name").color(arguments)
.hoverEvent(HoverEvent.showText(
text("In case you selected ").color(hoverDescription)
.append(text("\"player\"").color(hoverExample2)
.append(text(", specify the player's name here").color(hoverDescription))))));
}
public String formatPlayerStat(String playerName, String statName, String subStatEntryName, int stat) {
StringBuilder singleStat = new StringBuilder();
String subStat = subStatEntryName != null ?
" (" + subStatEntryName.toLowerCase().replace("_", " ") + ")" : "";
singleStat.append(config.getPlayerNamesColor(false)).append(playerName).append(": ")
.append(config.getStatNumbersColor(false)).append(stat).append(" ")
.append(config.getStatNamesColor(false))
.append(statName.toLowerCase().replace("_", " "))
.append(config.getSubStatNamesColor(false)).append(subStat);
return singleStat.toString();
}
public String formatTopStats(LinkedHashMap<String, Integer> topStats, String statName, String subStatEntryName) {
StringBuilder topList = new StringBuilder();
String subStat = subStatEntryName != null ?
" (" + subStatEntryName.toLowerCase().replace("_", " ") + ")" : "";
topList.append("\n").append(getPluginPrefix())
.append(config.getStatNamesColor(true)).append("Top ")
.append(config.getListNumbersColor()).append(topStats.size())
.append(config.getStatNamesColor(true)).append(" ")
.append(statName.toLowerCase().replace("_", " "))
.append(config.getSubStatNamesColor(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;
topList.append("\n")
.append(config.getListNumbersColor()).append(count).append(". ")
.append(config.getPlayerNamesColor(true)).append(playerName);
if (useDots) {
topList.append(config.getDotsColor()).append(" ");
int dots = (int) Math.round((130.0 - font.getWidth(count + ". " + playerName))/2);
if (config.playerNamesStyleIsBold()) {
dots = (int) Math.round((130.0 - font.getWidth(count + ". ") - (font.getWidth(playerName) * 1.19))/2);
}
if (dots >= 1) {
topList.append(".".repeat(dots));
}
}
else {
topList.append(":");
}
topList.append(" ").append(config.getStatNumbersColor(true)).append(topStats.get(playerName).toString());
}
return topList.toString();
}
//try to get the hex color or ChatColor from config String, substitute green if both fail, and try to apply style if necessary
private TextComponent playerNameComponent(String playerName, boolean topStat) {
ChatColor defaultColor = topStat ? ChatColor.GREEN : ChatColor.GOLD;
TextComponent player = applyColor(
config.getPlayerNamesColor(topStat), playerName, defaultColor);
return applyStyle(config.getPlayerNamesStyle(topStat), player);
}
private TextComponent statNameComponent(String statName, boolean topStat) {
TextComponent stat = applyColor(
config.getStatNamesColor(topStat), statName, ChatColor.YELLOW);
return applyStyle(config.getStatNamesStyle(topStat), stat);
}
private TextComponent subStatNameComponent(String subStatName, boolean topStat) {
TextComponent subStat = applyColor(
config.getSubStatNamesColor(topStat), subStatName, ChatColor.YELLOW);
return applyStyle(config.getSubStatNamesStyle(topStat), subStat);
}
private TextComponent statNumberComponent(int statNumber, boolean topStat) {
TextComponent number = applyColor(
config.getStatNumbersColor(topStat), statNumber + "", ChatColor.LIGHT_PURPLE);
return applyStyle(config.getStatNumbersStyle(topStat), number);
}
private TextComponent listNumberComponent(int listNumber) {
TextComponent list = applyColor(config.getListNumbersColor(), listNumber + ".", ChatColor.GOLD);
return applyStyle(config.getListNumbersStyle(), list);
}
private TextComponent dotsComponent() {
return applyColor(config.getDotsColor(), "", ChatColor.DARK_GRAY);
}
private TextComponent applyColor(String configString, String content, ChatColor defaultColor) {
TextComponent component = Component.text().build();
ChatColor color = defaultColor;
if (configString != null) {
if (configString.contains("#")) {
return component.content(content).color(TextColor.fromHexString(configString));
}
else {
try {
color = ChatColor.valueOf(configString.toUpperCase().replace(" ", "_"));
}
catch (IllegalArgumentException | NullPointerException exception) {
exception.printStackTrace();
}
}
}
return component.content(content + color);
}
private TextComponent applyStyle(String configString, TextComponent component) {
if (configString != null) {
if (configString.equalsIgnoreCase("none")) {
return component;
}
else if (configString.equalsIgnoreCase("bold")) {
return component.decorate(TextDecoration.BOLD);
}
else if (configString.equalsIgnoreCase("italic")) {
return component.decorate(TextDecoration.ITALIC);
}
else if (configString.equalsIgnoreCase("magic")) {
return component.decorate(TextDecoration.OBFUSCATED);
}
else if (configString.equalsIgnoreCase("strikethrough")) {
return component.decorate(TextDecoration.STRIKETHROUGH);
}
else if (configString.equalsIgnoreCase("underlined")) {
return component.decorate(TextDecoration.UNDERLINED);
}
}
return component;
}
}

View File

@ -51,8 +51,4 @@ public class OfflinePlayerHandler {
public OfflinePlayer getOfflinePlayer(String playerName) {
return Bukkit.getOfflinePlayer(offlinePlayerUUIDs.get(playerName));
}
public UUID getOfflinePlayerUUID(String playerName) {
return offlinePlayerUUIDs.get(playerName);
}
}

View File

@ -1,222 +0,0 @@
package com.gmail.artemis.the.gr8.playerstats.utils;
import com.gmail.artemis.the.gr8.playerstats.filehandlers.ConfigHandler;
import org.bukkit.ChatColor;
import org.bukkit.map.MinecraftFont;
import java.util.*;
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 static ConfigHandler config;
private HashMap<String, ChatColor> chatColors;
private HashMap<String, ChatColor> styleOptions;
private HashMap<String, net.md_5.bungee.api.ChatColor> hexChatColors;
private static String pluginPrefix;
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 static String getPluginPrefix() {
return pluginPrefix;
}
public String formatExceptions(String exception) {
return pluginPrefix + exception;
}
/*
public BaseComponent[] formatHelpSpigot() {
String spaces = " ";
String underscores = "____________";
ComponentBuilder underscore = new ComponentBuilder(underscores).color(net.md_5.bungee.api.ChatColor.of("#6E3485"));
TextComponent arrow = new TextComponent("");
arrow.setColor(net.md_5.bungee.api.ChatColor.GOLD);
TextComponent statName = new TextComponent("name");
statName.setColor(net.md_5.bungee.api.ChatColor.YELLOW);
statName.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new Text("The name of the statistic (example: \"mine_block\")")));
TextComponent subStatName = new TextComponent("sub-statistic");
subStatName.setColor(net.md_5.bungee.api.ChatColor.YELLOW);
subStatName.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new Text("Some statistics require an item, block or entity as sub-statistic (example: \"mine_block diorite\")")));
TextComponent target = new TextComponent("me | player | top");
target.setColor(net.md_5.bungee.api.ChatColor.YELLOW);
target.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new Text("Choose whether you want to see your own statistic, another player's, or the top " + config.getTopListMaxSize())));
TextComponent playerName = new TextComponent("player-name");
playerName.setColor(net.md_5.bungee.api.ChatColor.YELLOW);
playerName.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new Text("In case you selected \"player\", specify the player's name here")));
ComponentBuilder help = new ComponentBuilder()
.append("\n").append(underscore.create()).append(spaces).append(pluginPrefix).append(spaces).append(underscore.create()).append("\n")
.append("Hover over the arguments for more information!").color(net.md_5.bungee.api.ChatColor.GRAY).italic(true).append("\n")
.append("Usage: ").color(net.md_5.bungee.api.ChatColor.GOLD).italic(false)
.append("/statistic ").color(net.md_5.bungee.api.ChatColor.YELLOW).append("\n")
.append(spaces).append(arrow).append(statName).append("\n").reset()
.append(spaces).append(arrow).append(subStatName).append("\n").reset()
.append(spaces).append(arrow).append(target).append("\n").reset()
.append(spaces).append(arrow).append(playerName);
return help.create();
}
*/
public String formatHelpBukkit() {
String spaces = " ";
String underscores = ChatColor.GRAY + "____________";
String arrow = spaces + ChatColor.GOLD + "";
ChatColor argumentColor = ChatColor.YELLOW;
ChatColor descriptionColor = ChatColor.WHITE;
return "\n" +
underscores + spaces + pluginPrefix + spaces + underscores + "\n" +
ChatColor.GRAY + ChatColor.ITALIC + "A list of all the required and optional arguments" + ChatColor.RESET + "\n" +
ChatColor.GOLD + "Usage: " + argumentColor + "/statistic " + "\n" +
arrow + argumentColor + "name: " +
descriptionColor + "The name of the statistic (example: \"mine_block\")" + "\n" +
arrow + argumentColor + "sub-statistic: " +
descriptionColor + "Some statistics require an item, block or entity as sub-statistic (example: \"mine_block diorite\")" + "\n" +
arrow + argumentColor + "me | player | top: " +
descriptionColor + "Choose whether you want to see your own statistic, another player's, or the top " + config.getTopListMaxSize() + "\n" +
arrow + argumentColor + "player-name: " +
descriptionColor + "In case you selected \"player\", specify the player's name here";
}
public String formatPlayerStat(String playerName, String statName, String subStatEntryName, int stat) {
StringBuilder singleStat = new StringBuilder();
String subStat = subStatEntryName != null ?
" (" + subStatEntryName.toLowerCase().replace("_", " ") + ")" : "";
singleStat.append(getPlayerFormatting(false)).append(playerName).append(": ")
.append(getStatNumberFormatting(false)).append(stat).append(" ")
.append(getStatNameFormatting(false))
.append(statName.toLowerCase().replace("_", " "))
.append(getSubStatNameFormatting(false)).append(subStat);
return singleStat.toString();
}
public String formatTopStats(LinkedHashMap<String, Integer> topStats, String statName, String subStatEntryName) {
StringBuilder topList = new StringBuilder();
String subStat = subStatEntryName != null ?
" (" + subStatEntryName.toLowerCase().replace("_", " ") + ")" : "";
topList.append("\n").append(pluginPrefix)
.append(getStatNameFormatting(true)).append("Top ")
.append(getListNumberFormatting()).append(topStats.size())
.append(getStatNameFormatting(true)).append(" ")
.append(statName.toLowerCase().replace("_", " "))
.append(getSubStatNameFormatting(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;
topList.append("\n")
.append(getListNumberFormatting()).append(count).append(". ")
.append(getPlayerFormatting(true)).append(playerName);
if (useDots) {
topList.append(getDotColor()).append(" ");
int dots = (int) Math.round((130.0 - font.getWidth(count + ". " + playerName))/2);
if (topPlayerStyleIsBold()) {
dots = (int) Math.round((130.0 - font.getWidth(count + ". ") - (font.getWidth(playerName) * 1.19))/2);
}
if (dots >= 1) {
topList.append(".".repeat(dots));
}
}
else {
topList.append(":");
}
topList.append(" ").append(getStatNumberFormatting(true)).append(topStats.get(playerName).toString());
}
return topList.toString();
}
private String getPlayerFormatting(boolean isTopStat) {
return getColor("player-names", false, isTopStat) + "" +
getColor("player-names", true, isTopStat);
}
private boolean topPlayerStyleIsBold() {
return getColor("player-names", true, true).equals(ChatColor.BOLD);
}
private String getStatNameFormatting(boolean isTopStat) {
return getColor("stat-names", false, isTopStat) + "" +
getColor("stat-names", true, isTopStat);
}
private String getSubStatNameFormatting(boolean isTopStat) {
return getColor("sub-stat-names", false, isTopStat) + "" +
getColor("sub-stat-names", true, isTopStat);
}
private String getStatNumberFormatting(boolean isTopStat) {
return getColor("stat-numbers", false, isTopStat) + "" +
getColor("stat-numbers", true, isTopStat);
}
private String getListNumberFormatting() {
return getColor("list-numbers", false, true) + "" +
getColor("list-numbers", true, true);
}
private String getDotColor() {
return getColor("dots", false,true) + "";
}
//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;
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();
}
else {
chatColors = config.getChatColors();
}
}
}

View File

@ -21,20 +21,12 @@ use-dots: true
# ------ Color Options ---------
# 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:
individual-statistics-color:
player-names: gold
stat-names: yellow
sub-stat-names: yellow
stat-numbers: white
top-list:
player-names: green
stat-names: yellow
sub-stat-names: yellow
stat-numbers: dark_aqua
list-numbers: gold
dots: dark_gray
# This provides additional styling options (italic/underline/bold, and yes, even magic)
individual-statistics-style:
player-names: none
@ -42,6 +34,14 @@ individual-statistics-style:
sub-stat-names: none
stat-numbers: none
top-list-color:
player-names: green
stat-names: yellow
sub-stat-names: yellow
stat-numbers: '#40b1f7'
list-numbers: gold
dots: dark_gray
# Note that making the player-names bold could cause the alignment of the stat numbers to shift slightly
top-list-style:
player-names: none