From b2bc509933b9cf1f0480a73de14bc29160ca0e75 Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Tue, 28 Jun 2022 22:31:57 +0200 Subject: [PATCH] Finished rewrite of MessageWriter and ComponentFactory --- .../artemis/the/gr8/playerstats/Main.java | 6 +- .../gr8/playerstats/commands/StatCommand.java | 14 ++ .../gr8/playerstats/config/ConfigHandler.java | 2 +- .../config/ConfigUpdateHandler.java | 9 +- .../gr8/playerstats/enums/PluginColor.java | 16 ++- .../gr8/playerstats/msg/ComponentFactory.java | 26 +++- .../gr8/playerstats/msg/MessageWriter.java | 131 ++++++++---------- ...actory.java => PrideComponentFactory.java} | 10 +- src/main/resources/config.yml | 4 +- 9 files changed, 123 insertions(+), 95 deletions(-) rename src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/{PrideMessageFactory.java => PrideComponentFactory.java} (87%) 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 27b64ca..81c1fb2 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 @@ -7,7 +7,7 @@ import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler; import com.gmail.artemis.the.gr8.playerstats.listeners.JoinListener; import com.gmail.artemis.the.gr8.playerstats.msg.LanguageKeyHandler; import com.gmail.artemis.the.gr8.playerstats.msg.MessageWriter; -import com.gmail.artemis.the.gr8.playerstats.msg.PrideMessageFactory; +import com.gmail.artemis.the.gr8.playerstats.msg.PrideComponentFactory; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import org.bukkit.Bukkit; import org.bukkit.command.PluginCommand; @@ -35,8 +35,8 @@ public class Main extends JavaPlugin { ConfigHandler config = new ConfigHandler(this); LanguageKeyHandler language = new LanguageKeyHandler(); - //for now always use the PrideMessageFactory (it'll use the regular formatting when needed) - MessageWriter messageWriter = new PrideMessageFactory(config, language); + //for now always use the PrideComponentFactory (it'll use the regular formatting when needed) + MessageWriter messageWriter = new MessageWriter(config, language); //initialize the threadManager ThreadManager threadManager = new ThreadManager(adventure(), config, messageWriter, 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 a06cdfe..5cfd80d 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 @@ -1,6 +1,7 @@ package com.gmail.artemis.the.gr8.playerstats.commands; import com.gmail.artemis.the.gr8.playerstats.ThreadManager; +import com.gmail.artemis.the.gr8.playerstats.enums.PluginColor; import com.gmail.artemis.the.gr8.playerstats.enums.Target; import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler; import com.gmail.artemis.the.gr8.playerstats.statistic.StatRequest; @@ -8,6 +9,7 @@ import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; import com.gmail.artemis.the.gr8.playerstats.msg.MessageWriter; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.format.TextColor; import org.bukkit.Material; import org.bukkit.Statistic; import org.bukkit.command.Command; @@ -19,6 +21,8 @@ import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import static net.kyori.adventure.text.Component.text; + public class StatCommand implements CommandExecutor { @@ -41,6 +45,16 @@ public class StatCommand implements CommandExecutor { args[0].equalsIgnoreCase("example")) { //in case of "statistic examples", show examples adventure.sender(sender).sendMessage(messageWriter.usageExamples(sender instanceof ConsoleCommandSender)); } + else if (args[0].equalsIgnoreCase("test")) { + TextComponent msg = text("Tier 1").color(PluginColor.GOLD.getColor()) + .append(text("Tier 2").color(PluginColor.MEDIUM_GOLD.getColor()) + .append(text("Tier 3").color(TextColor.fromHexString("#FFEA40")) + .append(text("Tier 4").color(PluginColor.YELLOW.getColor())) + .append(text("Tier 3?"))) + .append(text("Tier 2?"))) + .append(text("Tier 1?")); + adventure.sender(sender).sendMessage(msg); + } else { StatRequest request = generateRequest(sender, args); TextComponent issues = checkRequest(request); diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigHandler.java index ed0c2b1..5ccc252 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigHandler.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigHandler.java @@ -23,7 +23,7 @@ public class ConfigHandler { saveDefaultConfig(); config = YamlConfiguration.loadConfiguration(configFile); - configVersion = 4; + configVersion = 4.1; checkConfigVersion(); MyLogger.setDebugLevel(debugLevel()); diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigUpdateHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigUpdateHandler.java index 2f5d910..36d5c96 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigUpdateHandler.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigUpdateHandler.java @@ -34,11 +34,14 @@ public class ConfigUpdateHandler { } } - /** Adjusts some of the default colors to migrate from versions 2 or 3 to version 4.*/ + /** Adjusts some of the default colors to migrate from versions 2 or 3 to version 4.1.*/ private void updateDefaultColors(YamlConfiguration configuration) { - updateColor(configuration, "top-list.title", "yellow", "#FFD52B"); - updateColor(configuration, "top-list.stat-names", "yellow", "#FFD52B"); + updateColor(configuration, "top-list.title", "yellow", "#FFEA40"); + updateColor(configuration, "top-list-title", "#FFD52B", "#FFEA40"); + updateColor(configuration, "top-list.stat-names", "yellow", "#FFEA40"); + updateColor(configuration, "top-list-stat-names", "#FFD52B", "#FFEA40"); updateColor(configuration, "top-list.sub-stat-names", "#FFD52B", "yellow"); + updateColor(configuration, "individual-statistics.stat-names", "yellow", "#FFD52B"); updateColor(configuration, "individual-statistics.sub-stat-names", "#FFD52B", "yellow"); updateColor(configuration, "total-server.title", "gold", "#55AAFF"); diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/enums/PluginColor.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/enums/PluginColor.java index 008f15c..1495da5 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/enums/PluginColor.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/enums/PluginColor.java @@ -8,19 +8,21 @@ import net.kyori.adventure.text.format.TextColor;

DARK_PURPLE: #6E3485 (used for default sub-titles, title-underscores and brackets)

MEDIUM_BLUE: #55AAFF (used for all plain feedback and error messages)

LIGHT_BLUE: #55C6FF (used for default hover-text)

-

DARK_GOLD: ChatColor Gold (used for first parts of usage messages and for first parts of hover-text accent)

-

MEDIUM_GOLD: #FFB80E (used for second parts of usage messages and for second parts of hover-text accent)

+

GOLD: ChatColor Gold (used for first parts of usage messages and for first parts of hover-text accent)

+

LIGHT_GOLD: #FFB80E (used for second parts of usage messages and for second parts of hover-text accent)

YELLOW: ChatColor Yellow (used for third parts of usage messages)

*/ public enum PluginColor { GRAY (NamedTextColor.GRAY), DARK_PURPLE (TextColor.fromHexString("#6E3485")), - MEDIUM_BLUE(TextColor.fromHexString("#55AAFF")), + MEDIUM_BLUE (TextColor.fromHexString("#55AAFF")), LIGHT_BLUE (TextColor.fromHexString("#55C6FF")), - DARK_GOLD (NamedTextColor.GOLD), + GOLD (NamedTextColor.GOLD), MEDIUM_GOLD (TextColor.fromHexString("#FFD52B")), - YELLOW (NamedTextColor.YELLOW); + YELLOW (NamedTextColor.YELLOW), + LIGHT_YELLOW (TextColor.fromHexString("#FFFF8E")); + //TODO try "#55AAFF" for LIGHT_BLUE private final TextColor color; PluginColor(TextColor color) { @@ -30,4 +32,8 @@ public enum PluginColor { public TextColor getColor() { return color; } + + public TextColor getConsoleColor() { + return NamedTextColor.nearestTo(color); + } } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/ComponentFactory.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/ComponentFactory.java index 7f5ca38..94ea0ac 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/ComponentFactory.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/ComponentFactory.java @@ -35,7 +35,7 @@ public class ComponentFactory { public TextComponent pluginPrefix(boolean isConsoleSender) { return text("[") .color(PluginColor.GRAY.getColor()) - .append(text("PlayerStats").color(PluginColor.DARK_GOLD.getColor())) + .append(text("PlayerStats").color(PluginColor.GOLD.getColor())) .append(text("]")) .append(space()); } @@ -79,7 +79,7 @@ public class ComponentFactory { TextComponent.Builder msg = Component.text(); if (part1 != null) { msg.append(text(part1) - .color(PluginColor.DARK_GOLD.getColor())); + .color(PluginColor.GOLD.getColor())); if (part2 != null || part3 != null || part4 != null) { msg.append(space()); } @@ -105,6 +105,22 @@ public class ComponentFactory { return msg.build(); } + public TextComponent simpleHoverPart(String plainText, String hoverText, PluginColor hoverColor) { + return simpleHoverPart(plainText, null, hoverText, hoverColor); + } + + public TextComponent simpleHoverPart(String plainText, @Nullable PluginColor plainColor, String hoverText, PluginColor hoverColor) { + TextComponent.Builder msg = Component.text() + .append(text(plainText)) + .hoverEvent(HoverEvent.showText( + text(hoverText) + .color(hoverColor.getColor()))); + if (plainColor != null) { + msg.color(plainColor.getColor()); + } + return msg.build(); + } + /** Returns a TextComponent with hover-text that can consist of three different parts, divided over two different lines. Each part has its own designated color. If all the input Strings are null, it will return an empty Component. @@ -114,7 +130,7 @@ public class ComponentFactory { @param hoverLineTwoA text on the second line, with color DARK_GOLD @param hoverLineTwoB text on the second part of the second line, with color MEDIUM_GOLD */ - public TextComponent hoverMsgPart(@NotNull String plainText, @NotNull PluginColor color, @Nullable String hoverLineOne, @Nullable String hoverLineTwoA, @Nullable String hoverLineTwoB) { + public TextComponent complexHoverPart(@NotNull String plainText, @NotNull PluginColor color, @Nullable String hoverLineOne, @Nullable String hoverLineTwoA, @Nullable String hoverLineTwoB) { TextComponent base = Component.text(plainText).color(color.getColor()); TextComponent.Builder hoverText = Component.text(); if (hoverLineOne != null) { @@ -126,7 +142,7 @@ public class ComponentFactory { } if (hoverLineTwoA != null) { hoverText.append(text(hoverLineTwoA) - .color(PluginColor.DARK_GOLD.getColor())); + .color(PluginColor.GOLD.getColor())); if (hoverLineTwoB != null) { hoverText.append(space()); } @@ -320,4 +336,4 @@ public class ComponentFactory { } } -} +} \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/MessageWriter.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/MessageWriter.java index f2893e2..2e9b2bf 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/MessageWriter.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/MessageWriter.java @@ -7,7 +7,6 @@ import com.gmail.artemis.the.gr8.playerstats.statistic.StatRequest; 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 org.bukkit.Bukkit; import org.bukkit.Statistic; @@ -26,17 +25,9 @@ public class MessageWriter { private static ConfigHandler config; private static ComponentFactory componentFactory; - private final TextColor hoverBaseColor; //light blue - one shade lighter than msgColor - private final TextColor accentColor1; //gold - one shade lighter than standard gold - private final TextColor accentColor2; //yellow - a few shades darker than standard yellow - public MessageWriter(ConfigHandler c, LanguageKeyHandler l) { config = c; componentFactory = new ComponentFactory(c, l); - - hoverBaseColor = TextColor.fromHexString("#55C6FF"); - accentColor1 = TextColor.fromHexString("#FFB80E"); - accentColor2 = TextColor.fromHexString("#FFD52B"); } public TextComponent reloadedConfig(boolean isConsoleSender) { @@ -182,29 +173,33 @@ public class MessageWriter { } public TextComponent usageExamples(boolean isConsoleSender) { - TextComponent spaces = text(" "); //4 spaces - TextComponent arrow = text("→ ").color(NamedTextColor.GOLD); - TextColor accentColor = TextColor.fromHexString("#FFE339"); - - if (isConsoleSender && Bukkit.getName().equalsIgnoreCase("CraftBukkit")) { - arrow = text("-> ").color(NamedTextColor.GOLD); - accentColor = NamedTextColor.YELLOW; - } + boolean isBukkitConsole = (isConsoleSender && Bukkit.getName().equalsIgnoreCase("CraftBukkit")); + TextColor mainColor = isBukkitConsole ? PluginColor.GOLD.getConsoleColor() : PluginColor.GOLD.getColor(); + TextColor accentColor1 = isBukkitConsole ? PluginColor.MEDIUM_GOLD.getConsoleColor() : PluginColor.MEDIUM_GOLD.getColor(); + TextColor accentColor3 = isBukkitConsole ? PluginColor.LIGHT_YELLOW.getConsoleColor() : PluginColor.LIGHT_YELLOW.getColor(); + String arrow = isBukkitConsole ? " -> " : " → "; //4 spaces, alt + 26, 1 space return Component.newline() .append(componentFactory.prefixTitle(isConsoleSender)) .append(newline()) - .append(text("Examples: ").color(NamedTextColor.GOLD)) - .append(newline()) - .append(spaces).append(arrow) - .append(text("/statistic animals_bred top").color(accentColor)) - .append(newline()) - .append(spaces).append(arrow) - .append(text("/statistic mine_block diorite me").color(accentColor)) - .append(newline()) - .append(spaces).append(arrow) - .append(text("/statistic deaths player Artemis_the_gr8").color(accentColor)) - .append(newline()); + .append(text("Examples: ").color(mainColor) + .append(newline()) + .append(text(arrow)) + .append(text("/statistic ").color(mainColor) + .append(text("animals_bred ").color(accentColor1) + .append(text("top").color(accentColor3)))) + .append(newline()) + .append(text(arrow)) + .append(text("/statistic ").color(mainColor) + .append(text("mine_block diorite ").color(accentColor1) + .append(text("me").color(accentColor3)))) + .append(newline()) + .append(text(arrow)) + .append(text("/statistic ").color(mainColor) + .append(text("deaths ").color(accentColor1) + .append(text("player ").color(accentColor3) + .append(text("Artemis_the_gr8"))))) + .append(newline())); } public TextComponent helpMsg(boolean isConsoleSender) { @@ -219,7 +214,6 @@ public class MessageWriter { /** Returns the usage-explanation with hovering text */ private TextComponent helpMsgHover() { String arrow = " → "; //4 spaces, alt + 26, 1 space - return Component.newline() .append(componentFactory.prefixTitle(false)) .append(newline()) @@ -228,47 +222,44 @@ public class MessageWriter { .append(componentFactory.msgPart("Usage:", null, "/statistic", null)) .append(newline()) .append(componentFactory.msgPart(arrow, null, null, null) - .append(componentFactory.hoverMsgPart("name", PluginColor.YELLOW, + .append(componentFactory.complexHoverPart("name", PluginColor.YELLOW, "The name that describes the statistic", "Example:", "\"animals_bred\""))) .append(newline()) - .append(spaces).append(arrow) - .append(text("sub-statistic").color(arguments) - .hoverEvent(HoverEvent.showText( - text("Some statistics need an item, block or entity as extra input").color(hoverBaseColor) - .append(newline()) - .append(text("Example: ").color(accentColor1) - .append(text("\"mine_block diorite\"").color(accentColor2)))))) + .append(componentFactory.msgPart(arrow, null, null, null) + .append(componentFactory.complexHoverPart("sub-statistic", PluginColor.YELLOW, + "Some statistics need an item, block or entity as extra input", + "Example:", + "\"mine_block diorite\""))) .append(newline()) - .append(spaces) - .append(text("→").color(NamedTextColor.GOLD) - .hoverEvent(HoverEvent.showText( - text("Choose one").color(TextColor.fromHexString("#6E3485"))))) - .append(space()) - .append(text("me").color(arguments) - .hoverEvent(HoverEvent.showText( - text("See your own statistic").color(hoverBaseColor)))) - .append(text(" | ").color(arguments)) - .append(text("player").color(arguments) - .hoverEvent(HoverEvent.showText( - text("Choose any player that has played on your server").color(hoverBaseColor)))) - .append(text(" | ").color(arguments)) - .append(text("server").color(arguments) - .hoverEvent(HoverEvent.showText( - text("See the combined total for everyone on your server").color(hoverBaseColor)))) - .append(text(" | ").color(arguments)) - .append(text("top").color(arguments) - .hoverEvent(HoverEvent.showText( - text("See the top ").color(hoverBaseColor) - .append(text(config.getTopListMaxSize()).color(hoverBaseColor))))) + .append(text(" ").color(PluginColor.YELLOW.getColor()) + .append(componentFactory.simpleHoverPart( + "→", PluginColor.GOLD, + "Choose one", PluginColor.DARK_PURPLE)) + .append(space()) + .append(componentFactory.simpleHoverPart( + "me", + "See your own statistic", PluginColor.LIGHT_BLUE)) + .append(text(" | ")) + .append(componentFactory.simpleHoverPart( + "player", + "Choose any player that has played on your server", PluginColor.LIGHT_BLUE)) + .append(text(" | ")) + .append(componentFactory.simpleHoverPart( + "server", + "See the combined total for everyone on your server", PluginColor.LIGHT_BLUE)) + .append(text(" | ")) + .append(componentFactory.simpleHoverPart( + "top", + "See the top " + config.getTopListMaxSize(), PluginColor.LIGHT_BLUE))) .append(newline()) - .append(spaces).append(arrow) - .append(text("player-name").color(arguments) - .hoverEvent(HoverEvent.showText( - text("In case you typed ").color(hoverBaseColor) - .append(text("\"player\"").color(accentColor2) - .append(text(", add the player's name").color(hoverBaseColor)))))); + .append(componentFactory.msgPart(arrow, null, null, null) + .append(text("player-name").color(PluginColor.YELLOW.getColor()) + .hoverEvent(HoverEvent.showText( + text("In case you typed ").color(PluginColor.LIGHT_BLUE.getColor()) + .append(text("\"player\"").color(PluginColor.MEDIUM_GOLD.getColor())) + .append(text(", add the player's name")))))); } //TODO create ConsoleComponentFactory for Bukkit @@ -276,12 +267,10 @@ public class MessageWriter { /** Returns the usage-explanation without any hovering text. If BukkitVersion is CraftBukkit, this doesn't use unicode symbols or hex colors */ private TextComponent helpMsgPlain(boolean isConsoleSender) { - String arrow = " →"; //4 spaces, alt + 26 - String bullet = " •"; //8 spaces, alt + 7 - if (isConsoleSender && Bukkit.getName().equalsIgnoreCase("CraftBukkit")) { - arrow = " ->"; - bullet = " *"; - } + boolean isBukkitConsole = (isConsoleSender && Bukkit.getName().equalsIgnoreCase("CraftBukkit")); + String arrow = isBukkitConsole ? " ->" : " →"; //4 spaces, alt + 26 + String bullet = isBukkitConsole ? " *" : " •"; //8 spaces, alt + 7 + return Component.newline() .append(componentFactory.prefixTitle(isConsoleSender)) .append(newline()) @@ -303,6 +292,6 @@ public class MessageWriter { .append(newline()) .append(componentFactory.msgPart(bullet, "top:", null, "the top " + config.getTopListMaxSize())) .append(newline()) - .append(componentFactory.msgPart(arrow, null, "{player-name", null)); + .append(componentFactory.msgPart(arrow, null, "{player-name}", null)); } } \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/PrideMessageFactory.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/PrideComponentFactory.java similarity index 87% rename from src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/PrideMessageFactory.java rename to src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/PrideComponentFactory.java index 2773bb3..3e9a031 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/PrideMessageFactory.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/PrideComponentFactory.java @@ -12,20 +12,20 @@ import java.time.Month; import static net.kyori.adventure.text.Component.*; -public class PrideMessageFactory extends MessageWriter { +public class PrideComponentFactory extends ComponentFactory { private static ConfigHandler config; - public PrideMessageFactory(ConfigHandler c, LanguageKeyHandler l) { + public PrideComponentFactory(ConfigHandler c, LanguageKeyHandler l) { super(c, l); config = c; } @Override - protected TextComponent getPrefixAsTitle(boolean isConsoleSender) { + public TextComponent prefixTitle(boolean isConsoleSender) { if (cancelRainbow(isConsoleSender)) { - return super.getPrefixAsTitle(isConsoleSender); + return super.prefixTitle(isConsoleSender); } else { String title = "____________ [PlayerStats] ____________"; //12 underscores @@ -36,7 +36,7 @@ public class PrideMessageFactory extends MessageWriter { } @Override - protected TextComponent pluginPrefix(boolean isConsoleSender) { + public TextComponent pluginPrefix(boolean isConsoleSender) { if (cancelRainbow(isConsoleSender)) { return super.pluginPrefix(isConsoleSender); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 9154e48..77f2a7d 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -74,13 +74,13 @@ your-server-name: 'this server' # # ------------------------------ # # top-list: - title: '#FFD52B' + title: '#FFEA40' title-style: none title-number: gold title-number-style: none - stat-names: '#FFD52B' + stat-names: '#FFEA40' stat-names-style: none sub-stat-names: yellow