From 8ae089f22260b3da60649817dbab876c1c196cec Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Mon, 18 Jul 2022 13:01:25 +0200 Subject: [PATCH] Decided on formatting for shared stat-results in hover-text (#92, #52), added random name-colors for shared-by-player, improved ComponentFactory and MessageWriter (fixed gray brackets not being gray) --- .../gr8/playerstats/enums/PluginColor.java | 38 ++++++++++++++++++- .../msg/BukkitConsoleComponentFactory.java | 8 +++- .../gr8/playerstats/msg/ComponentFactory.java | 26 +++++++------ .../gr8/playerstats/msg/MessageWriter.java | 21 +++++----- .../gr8/playerstats/msg/OutputManager.java | 5 ++- .../msg/msgutils/ExampleMessage.java | 3 +- 6 files changed, 75 insertions(+), 26 deletions(-) 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 39cc825..44c66ad 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 @@ -3,6 +3,8 @@ package com.gmail.artemis.the.gr8.playerstats.enums; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextColor; +import java.util.Random; + /** This enum represents the colorscheme PlayerStats uses in its output messages.

GRAY: ChatColor Gray

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

@@ -22,7 +24,16 @@ public enum PluginColor { GOLD (NamedTextColor.GOLD), //#FFAA00 MEDIUM_GOLD (TextColor.fromHexString("#FFD52B")), LIGHT_GOLD (TextColor.fromHexString("#FFEA40")), - LIGHT_YELLOW (TextColor.fromHexString("#FFFF8E")); + LIGHT_YELLOW (TextColor.fromHexString("#FFFF8E")), + + NAME_1 (NamedTextColor.BLUE), //#5555FF - blue + NAME_2 (TextColor.fromHexString("#4287F5")), //between blue and medium_blue + NAME_3 (TextColor.fromHexString("#55AAFF")), //same as medium_blue + NAME_4 (TextColor.fromHexString("#D65DB1")), //magenta-purple + NAME_5 (TextColor.fromHexString("#EE8A19")), //dark orange + NAME_6 (NamedTextColor.GOLD), //same as gold + NAME_7 (TextColor.fromHexString("#01C1A7")), //aqua-cyan-green-ish + NAME_8 (TextColor.fromHexString("46D858")); //light green private final TextColor color; @@ -38,4 +49,27 @@ public enum PluginColor { public TextColor getConsoleColor() { return NamedTextColor.nearestTo(color); } -} + + public static TextColor getRandomNameColor() { + return getRandomNameColor(false); + } + + public static TextColor getRandomNameColor(boolean isConsole) { + Random randomizer = new Random(); + PluginColor color = switch (randomizer.nextInt(8)) { + case 0 -> NAME_1; + case 2 -> NAME_3; + case 3 -> NAME_4; + case 4 -> NAME_5; + case 5 -> NAME_6; + case 6 -> NAME_7; + case 7 -> NAME_8; + default -> NAME_2; + }; + return getCorrespondingColor(color, isConsole); + } + + private static TextColor getCorrespondingColor(PluginColor nameColor, boolean isConsole) { + return isConsole ? nameColor.getConsoleColor() : nameColor.getColor(); + } +} \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/BukkitConsoleComponentFactory.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/BukkitConsoleComponentFactory.java index e2ff040..e7474b4 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/BukkitConsoleComponentFactory.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/BukkitConsoleComponentFactory.java @@ -4,6 +4,7 @@ import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler; import com.gmail.artemis.the.gr8.playerstats.enums.DebugLevel; import com.gmail.artemis.the.gr8.playerstats.enums.PluginColor; import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger; +import net.kyori.adventure.text.format.TextColor; public class BukkitConsoleComponentFactory extends ComponentFactory { @@ -25,4 +26,9 @@ public class BukkitConsoleComponentFactory extends ComponentFactory { HOVER_MSG = PluginColor.LIGHT_BLUE.getConsoleColor(); HOVER_ACCENT = PluginColor.LIGHT_GOLD.getConsoleColor(); } -} + + @Override + public TextColor getRandomNameColor() { + return PluginColor.getRandomNameColor(true); + } +} \ No newline at end of file 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 4d70cec..3f18972 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 @@ -44,6 +44,7 @@ public class ComponentFactory { protected static TextColor CLICKED_MSG; //light_purple protected static TextColor HOVER_ACCENT; //light_gold + public ComponentFactory(ConfigHandler c) { config = c; prepareColors(); @@ -64,6 +65,10 @@ public class ComponentFactory { HOVER_ACCENT = PluginColor.LIGHT_GOLD.getColor(); } + public TextColor getRandomNameColor() { + return PluginColor.getRandomNameColor(); + } + public TextColor prefix() { return PREFIX; } @@ -263,15 +268,14 @@ public class ComponentFactory { if (!(unitName == null && unitKey == null)) { TextComponent.Builder statUnitBuilder = getComponentBuilder(null, getColorFromString(config.getSubStatNameDecoration(selection, false)), - getStyleFromString(config.getSubStatNameDecoration(selection, true))) - .append(text("[")); + getStyleFromString(config.getSubStatNameDecoration(selection, true))); if (unitKey != null) { statUnitBuilder.append(translatable() .key(unitKey)); } else { statUnitBuilder.append(text(unitName)); } - return statUnitBuilder.append(text("]")).build(); + return surroundingBracketComponent(statUnitBuilder.build()); } else { return Component.empty(); @@ -314,25 +318,25 @@ public class ComponentFactory { public TextComponent messageSharedComponent(String playerName) { return messageSharedComponent( text(playerName) - .color(MSG_ACCENT)); + .color(getRandomNameColor())); } public TextComponent messageSharedComponent(Component playerName) { return surroundingBracketComponent( - text("Shared by") - .color(CLICKED_MSG) + text().append(text("Shared by") + .color(BRACKETS) + .decorate(TextDecoration.ITALIC)) .append(space()) .append(playerName) - .append(text("!"))); + .append(text("!")) + .build()); } private TextComponent surroundingBracketComponent(TextComponent component) { - return Component.text() - .color(BRACKETS) + return getComponent(null, BRACKETS, null) .append(text("[")) .append(component) - .append(text("]")) - .build(); + .append(text("]")); } public TextComponent titleComponent(String content, Target selection) { 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 8c7b9c3..6c2a30c 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 @@ -166,16 +166,18 @@ public class MessageWriter { .append(getStatUnitComponent(request.getStatistic(), request.getSelection(), request.isConsoleSender())); //space is provided by statUnitComponent return shareCode -> { + TextComponent.Builder playerBuilder = text().append(playerStat); + //if we're adding a share-button if (shareCode != null) { - playerStat.append(space()) + playerBuilder.append(space()) .append(componentFactory.shareButtonComponent(shareCode)); } - return playerStat.build(); + return playerBuilder.build(); }; } public Function formattedServerStatFunction(long stat, @NotNull StatRequest request) { - TextComponent.Builder serverStat = Component.text() + TextComponent.Builder serverStat = text() .append(componentFactory.titleComponent(config.getServerTitle(), Target.SERVER)) .append(space()) .append(componentFactory.serverNameComponent(config.getServerName())) @@ -186,11 +188,13 @@ public class MessageWriter { .append(getStatUnitComponent(request.getStatistic(), request.getSelection(), request.isConsoleSender())); //space is provided by statUnit return shareCode -> { + TextComponent.Builder serverBuilder = text().append(serverStat); + //if we're adding a share-button if (shareCode != null) { - serverStat.append(space()) + serverBuilder.append(space()) .append(componentFactory.shareButtonComponent(shareCode)); } - return serverStat.build(); + return serverBuilder.build(); }; } public Function formattedTopStatFunction(@NotNull LinkedHashMap topStats, @NotNull StatRequest request) { @@ -198,8 +202,9 @@ public class MessageWriter { TextComponent list = getTopStatList(topStats, request); return shareCode -> { - TextComponent.Builder topBuilder = Component.text(); - if (shareCode != null) { //if we're adding a share-button + TextComponent.Builder topBuilder = text(); + //if we're adding a share-button + if (shareCode != null) { TextComponent newLineTitle = newline().append(title); topBuilder .append(newLineTitle) @@ -271,8 +276,6 @@ public class MessageWriter { return topList.build(); } - - /** Depending on the config settings, return either a TranslatableComponent representing the statName (and potential subStatName), or a TextComponent with capitalized English names.*/ private TextComponent getStatNameComponent(StatRequest request) { diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/OutputManager.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/OutputManager.java index 5294c94..c986929 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/OutputManager.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/OutputManager.java @@ -91,8 +91,9 @@ public class OutputManager { totalResult = msg.startWithNewLine(totalResult); adventure.players() - .filterAudience(onlinePlayer -> !onlinePlayer.get(Identity.NAME) - .orElse("").equalsIgnoreCase(sender.getName())).sendMessage(totalResult); +// .filterAudience(onlinePlayer -> !onlinePlayer.get(Identity.NAME) +// .orElse("").equalsIgnoreCase(sender.getName())) + .sendMessage(totalResult); adventure.sender(sender).sendMessage(msg.messageShared(statResult)); } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/msgutils/ExampleMessage.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/msgutils/ExampleMessage.java index eb38afa..486c49a 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/msgutils/ExampleMessage.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/msgutils/ExampleMessage.java @@ -45,7 +45,8 @@ public class ExampleMessage implements TextComponent { .append(text("/statistic ") .append(text("deaths ").color(componentFactory.msgAccent()) .append(text("player ").color(componentFactory.msgAccent2()) - .append(text("Artemis_the_gr8")))))); + .append(text("Artemis_the_gr8") + .color(componentFactory.getRandomNameColor())))))); } @Override