From 85ceb0b8c76d9a3765f4ff93889637c704650bc0 Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Tue, 2 Aug 2022 22:30:18 +0200 Subject: [PATCH] Made top-list-size configurable for API-requests, improved MessageBuilder --- .../gr8/playerstats/config/ConfigHandler.java | 30 ++-- .../gr8/playerstats/enums/PluginColor.java | 2 + .../the/gr8/playerstats/enums/Target.java | 2 +- .../gr8/playerstats/msg/MessageBuilder.java | 169 ++++++++++------- .../gr8/playerstats/msg/OutputManager.java | 80 +++++---- .../BukkitConsoleComponentFactory.java | 1 + .../msg/components/ComponentFactory.java | 170 +++++++++++------- .../msg/components/PrideComponentFactory.java | 1 + .../msg/msgutils/NumberFormatter.java | 22 +-- .../gr8/playerstats/reload/ReloadThread.java | 2 +- .../statistic/request/StatRequest.java | 26 ++- .../statistic/request/StatRequestHandler.java | 10 +- 12 files changed, 295 insertions(+), 220 deletions(-) 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 5ede3cd..60a07d4 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 @@ -191,53 +191,53 @@ public final class ConfigHandler { /** Returns the unit that should be used for distance-related statistics.
Default: Blocks for plain text, km for hover-text
*/ - public String getDistanceUnit(boolean isHoverText) { - return getUnitString(isHoverText, "blocks", "km", "distance-unit"); + public String getDistanceUnit(boolean isUnitForHoverText) { + return getUnitString(isUnitForHoverText, "blocks", "km", "distance-unit"); } /** Returns the unit that should be used for damage-based statistics.
Default: Hearts for plain text, HP for hover-text.
*/ - public String getDamageUnit(boolean isHoverText) { - return getUnitString(isHoverText, "hearts", "hp", "damage-unit"); + public String getDamageUnit(boolean isUnitForHoverText) { + return getUnitString(isUnitForHoverText, "hearts", "hp", "damage-unit"); } /** Whether PlayerStats should automatically detect the most suitable unit to use for time-based statistics.
Default: true
*/ - public boolean autoDetectTimeUnit(boolean isHoverText) { + public boolean autoDetectTimeUnit(boolean isUnitForHoverText) { String path = "auto-detect-biggest-time-unit"; - if (isHoverText) { + if (isUnitForHoverText) { path = path + "-for-hover-text"; } - boolean defaultValue = !isHoverText; + boolean defaultValue = !isUnitForHoverText; return config.getBoolean(path, defaultValue); } /** How many additional units should be displayed next to the most suitable largest unit for time-based statistics.
Default: 1 for plain text, 0 for hover-text
*/ - public int getNumberOfExtraTimeUnits(boolean isHoverText) { + public int getNumberOfExtraTimeUnits(boolean isUnitForHoverText) { String path = "number-of-extra-units"; - if (isHoverText) { + if (isUnitForHoverText) { path = path + "-for-hover-text"; } - int defaultValue = isHoverText ? 0 : 1; + int defaultValue = isUnitForHoverText ? 0 : 1; return config.getInt(path, defaultValue); } /** Returns the unit that should be used for time-based statistics. (this will return the largest unit that should be used).
Default: days for plain text, hours for hover-text
*/ - public String getTimeUnit(boolean isHoverText) { - return getTimeUnit(isHoverText, false); + public String getTimeUnit(boolean isUnitForHoverText) { + return getTimeUnit(isUnitForHoverText, false); } /** Returns the unit that should be used for time-based statistics. If the optional smallUnit flag is true, this will return the smallest unit (and otherwise the largest).
Default: hours for plain text, seconds for hover-text
*/ - public String getTimeUnit(boolean isHoverText, boolean smallUnit) { + public String getTimeUnit(boolean isUnitForHoverText, boolean smallUnit) { if (smallUnit) { - return getUnitString(isHoverText, "hours", "seconds", "smallest-time-unit"); + return getUnitString(isUnitForHoverText, "hours", "seconds", "smallest-time-unit"); } - return getUnitString(isHoverText, "days", "hours", "biggest-time-unit"); + return getUnitString(isUnitForHoverText, "days", "hours", "biggest-time-unit"); } /** Returns an integer between 0 and 100 that represents how much lighter a hoverColor should be. 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 6bc3d23..806a986 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 @@ -40,6 +40,8 @@ public enum PluginColor { /** A Light Yellow that is used for final accents in the example message (#FFFF8E).*/ LIGHT_YELLOW (TextColor.fromHexString("#FFFF8E")), + /** The color of vanilla Minecraft hearts (#FF1313). */ + RED (TextColor.fromHexString("#FF1313")), /** ChatColor Blue (#5555FF)*/ NAME_1 (NamedTextColor.BLUE), //#5555FF - blue diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/enums/Target.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/enums/Target.java index d055f44..1d4affa 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/enums/Target.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/enums/Target.java @@ -1,6 +1,6 @@ package com.gmail.artemis.the.gr8.playerstats.enums; -/** This enum represents the targets PlayerStats accepts for a stat-lookup.*/ +/** This enum represents the targets PlayerStats accepts for a stat-lookup (Player, Server and Top).*/ public enum Target { PLAYER, SERVER, TOP } \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/MessageBuilder.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/MessageBuilder.java index fa6930d..3bffbe3 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/MessageBuilder.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/MessageBuilder.java @@ -31,6 +31,7 @@ import static net.kyori.adventure.text.Component.*; public final class MessageBuilder { private static ConfigHandler config; + private boolean useHoverText; private final ComponentFactory componentFactory; private final LanguageKeyHandler languageKeyHandler; @@ -42,6 +43,7 @@ public final class MessageBuilder { private MessageBuilder(ConfigHandler configHandler, ComponentFactory factory) { config = configHandler; + useHoverText = config.useHoverText(); componentFactory = factory; formatter = new NumberFormatter(); @@ -57,6 +59,12 @@ public final class MessageBuilder { return new MessageBuilder(config, factory); } + /** Set whether this {@link MessageBuilder} should use hoverText. + By default, this follows the setting specified in the {@link ConfigHandler}. */ + public void toggleHoverUse(boolean desiredSetting) { + useHoverText = desiredSetting; + } + public TextComponent reloadedConfig() { return componentFactory.pluginPrefix() .append(space()) @@ -157,7 +165,7 @@ public final class MessageBuilder { public TextComponent helpMsg(boolean isConsoleSender) { int listSize = config.getTopListMaxSize(); - if (!isConsoleSender && config.useHoverText()) { + if (!isConsoleSender && useHoverText) { return HelpMessage.constructHoverMsg(componentFactory, listSize); } else { return HelpMessage.constructPlainMsg(componentFactory, listSize); @@ -174,7 +182,7 @@ public final class MessageBuilder { .append(componentFactory.playerName(statRequest.getPlayerName(), Target.PLAYER) .append(text(":")) .append(space())) - .append(getStatNumberComponent(statRequest.getStatistic(), stat, Target.PLAYER, statRequest.isConsoleSender())) + .append(getStatNumberComponent(statRequest, stat)) .append(space()) .append(getStatNameComponent(statRequest)) .append(getStatUnitComponent(statRequest.getStatistic(), statRequest.getTarget(), statRequest.isConsoleSender())) //space is provided by statUnitComponent @@ -194,7 +202,7 @@ public final class MessageBuilder { .append(space()) .append(componentFactory.serverName(config.getServerName())) .append(space()) - .append(getStatNumberComponent(statRequest.getStatistic(), stat, Target.SERVER, statRequest.isConsoleSender())) + .append(getStatNumberComponent(statRequest, stat)) .append(space()) .append(getStatNameComponent(statRequest)) .append(getStatUnitComponent(statRequest.getStatistic(), statRequest.getTarget(), statRequest.isConsoleSender())) //space is provided by statUnit @@ -255,9 +263,9 @@ public final class MessageBuilder { }; } - private BiFunction getFormattingFunction(@NotNull TextComponent statResult, Target selection) { - boolean useEnters = config.useEnters(selection, false); - boolean useEntersForShared = config.useEnters(selection, true); + private BiFunction getFormattingFunction(@NotNull TextComponent statResult, Target target) { + boolean useEnters = config.useEnters(target, false); + boolean useEntersForShared = config.useEnters(target, true); return (shareCode, sender) -> { TextComponent.Builder statBuilder = text(); @@ -343,7 +351,7 @@ public final class MessageBuilder { else { topList.append(playerNameBuilder.append(text(":"))); } - topList.append(space()).append(getStatNumberComponent(statRequest.getStatistic(), topStats.get(playerName), Target.TOP, statRequest.isConsoleSender())); + topList.append(space()).append(getStatNumberComponent(statRequest, topStats.get(playerName))); } return topList.build(); } @@ -373,60 +381,114 @@ public final class MessageBuilder { } } - private TextComponent getStatNumberComponent(Statistic statistic, long statNumber, Target selection, boolean isConsoleSender) { - Unit.Type type = Unit.getTypeFromStatistic(statistic); - Unit statUnit; - switch (type) { - case DISTANCE -> statUnit = Unit.fromString(config.getDistanceUnit(false)); - case DAMAGE -> statUnit = Unit.fromString(config.getDamageUnit(false)); - case TIME -> { - return getTimeNumberComponent(statNumber, selection, getTimeUnitRange(statNumber)); - } - default -> statUnit = Unit.NUMBER; - } - String prettyNumber = formatter.format(statNumber, statUnit); - if (!config.useHoverText() || statUnit == Unit.NUMBER) { - return componentFactory.statNumber(prettyNumber, selection); - } - Unit hoverUnit = type == Unit.Type.DISTANCE ? Unit.fromString(config.getDistanceUnit(true)) : - Unit.fromString(config.getDamageUnit(true)); - String prettyHoverNumber = formatter.format(statNumber, hoverUnit); - MyLogger.logMsg("mainNumber: " + prettyNumber + ", hoverNumber: " + prettyHoverNumber, DebugLevel.HIGH); + private TextComponent getStatNumberComponent(StatRequest request, long statNumber) { + Unit.Type statUnitType = Unit.getTypeFromStatistic(request.getStatistic()); + Target target = request.getTarget(); + return switch (statUnitType) { + case DISTANCE -> getDistanceNumberComponent(statNumber, target); + case DAMAGE -> getDamageNumberComponent(statNumber, target); + case TIME -> getTimeNumberComponent(statNumber, target); + default -> getDefaultNumberComponent(statNumber, target); + }; + } - if (hoverUnit == Unit.HEART) { - return componentFactory.damageNumberWithHoverText( - prettyNumber, prettyHoverNumber, - componentFactory.heart(isConsoleSender, true), selection); + private TextComponent getDistanceNumberComponent(long statNumber, Target target) { + Unit statUnit = Unit.fromString(config.getDistanceUnit(false)); + String prettyNumber = formatter.formatDistanceNumber(statNumber, statUnit); + if (!useHoverText) { + return componentFactory.distanceNumber(prettyNumber, target); } + + Unit hoverUnit = Unit.fromString(config.getDistanceUnit(true)); + String hoverNumber = formatter.formatDistanceNumber(statNumber, hoverUnit); if (config.useTranslatableComponents()) { String unitKey = languageKeyHandler.getUnitKey(hoverUnit); if (unitKey != null) { - return componentFactory.statNumberWithHoverText(prettyNumber, prettyHoverNumber, null, unitKey, selection); + return componentFactory.distanceNumberWithTranslatableHoverText(prettyNumber, hoverNumber, unitKey, target); } } - return componentFactory.statNumberWithHoverText(prettyNumber, prettyHoverNumber, hoverUnit.getLabel(), null, selection); + return componentFactory.distanceNumberWithHoverText(prettyNumber, hoverNumber, hoverUnit.getLabel(), target); } - private TextComponent getTimeNumberComponent(long statNumber, Target selection, ArrayList unitRange) { - if (unitRange.size() <= 1 || (config.useHoverText() && unitRange.size() <= 3)) { + private TextComponent getDamageNumberComponent(long statNumber, Target target) { + Unit statUnit = Unit.fromString(config.getDamageUnit(false)); + String prettyNumber = formatter.formatDamageNumber(statNumber, statUnit); + if (!useHoverText) { + return componentFactory.damageNumber(prettyNumber, target); + } + + Unit hoverUnit = Unit.fromString(config.getDamageUnit(true)); + String prettyHoverNumber = formatter.formatDamageNumber(statNumber, hoverUnit); + if (hoverUnit == Unit.HEART) { + return componentFactory.damageNumberWithHeartUnitInHoverText(prettyNumber, prettyHoverNumber, target); + } + return componentFactory.damageNumberWithHoverText(prettyNumber, prettyHoverNumber, hoverUnit.getLabel(), target); + } + + private TextComponent getTimeNumberComponent(long statNumber, Target target) { + ArrayList unitRange = getTimeUnitRange(statNumber); + if (unitRange.size() <= 1 || (useHoverText && unitRange.size() <= 3)) { MyLogger.logMsg( "There is something wrong with the time-units you specified, please check your config!", true); - return componentFactory.statNumber("-", selection); + return componentFactory.statNumber("-", target); } else { - String mainNumber = formatter.format(statNumber, unitRange.get(0), unitRange.get(1)); - if (!config.useHoverText()) { - return componentFactory.statNumber(mainNumber, selection); + String mainNumber = formatter.formatTimeNumber(statNumber, unitRange.get(0), unitRange.get(1)); + if (!useHoverText) { + return componentFactory.statNumber(mainNumber, target); } else { - String hoverNumber = formatter.format(statNumber, unitRange.get(2), unitRange.get(3)); + String hoverNumber = formatter.formatTimeNumber(statNumber, unitRange.get(2), unitRange.get(3)); MyLogger.logMsg("mainNumber: " + mainNumber + ", hoverNumber: " + hoverNumber, DebugLevel.HIGH); return componentFactory.statNumberWithHoverText(mainNumber, hoverNumber, - null, null, selection); + null, null, target); } } } + private TextComponent getDefaultNumberComponent(long statNumber, Target target) { + return componentFactory.statNumber(formatter.formatNumber(statNumber), target); + } + + private TextComponent getStatUnitComponent(Statistic statistic, Target target, boolean isConsoleSender) { + return switch (Unit.getTypeFromStatistic(statistic)) { + case DAMAGE -> getDamageUnit(target, isConsoleSender); + case DISTANCE -> getDistanceUnit(target); + default -> Component.empty(); + }; + } + + private TextComponent getDistanceUnit(Target target) { + Unit statUnit = Unit.fromString(config.getDistanceUnit(false)); + if (config.useTranslatableComponents()) { + String unitKey = languageKeyHandler.getUnitKey(statUnit); + if (unitKey != null) { + return Component.space() + .append(componentFactory.statUnitTranslatable(unitKey, target)); + } + } + return Component.space() + .append(componentFactory.statUnit(statUnit.getLabel(), target)); + } + + private TextComponent getDamageUnit(Target target, boolean isConsoleSender) { + Unit statUnit = Unit.fromString(config.getDamageUnit(false)); + if (statUnit == Unit.HEART) { + TextComponent heartUnit; + if (isConsoleSender) { + heartUnit = componentFactory.consoleHeart(); + } else if (useHoverText) { + heartUnit = componentFactory.clientHeartWithHoverText(); + } else { + heartUnit = componentFactory.clientHeart(false); + } + return Component.space() + .append(heartUnit); + } + return Component.space() + .append(componentFactory.statUnit(statUnit.getLabel(), target)); + } + /** Get an ArrayList consisting of 2 or 4 timeUnits. The order of items is:

0. maxUnit

1. minUnit

@@ -443,7 +505,7 @@ public final class MessageBuilder { unitRange.add(bigUnit); unitRange.add(bigUnit.getSmallerUnit(config.getNumberOfExtraTimeUnits(false))); } - if (config.useHoverText()) { + if (useHoverText) { if (!config.autoDetectTimeUnit(true)) { unitRange.add(Unit.fromString(config.getTimeUnit(true))); unitRange.add(Unit.fromString(config.getTimeUnit(true, true))); @@ -458,31 +520,6 @@ public final class MessageBuilder { return unitRange; } - private TextComponent getStatUnitComponent(Statistic statistic, Target selection, boolean isConsoleSender) { - Unit statUnit; - switch (Unit.getTypeFromStatistic(statistic)) { - case DAMAGE -> statUnit = Unit.fromString(config.getDamageUnit(false)); - case DISTANCE -> statUnit = Unit.fromString(config.getDistanceUnit(false)); - default -> { - return Component.empty(); - } - } - if (config.useTranslatableComponents()) { - String unitKey = languageKeyHandler.getUnitKey(statUnit); - if (unitKey != null) { - return Component.space() - .append(componentFactory.statUnit(null, unitKey, selection)); - } - } - String statName = statUnit.getLabel(); - if (statUnit == Unit.HEART) { //console can do u2665, u2764 looks better in-game - return Component.space() - .append(componentFactory.heart(isConsoleSender, false)); - } - return Component.space() - .append(componentFactory.statUnit(statName, null, selection)); - } - /** Returns "block", "entity", "item", or "sub-statistic" if the provided Type is null. */ public static String getSubStatTypeName(Statistic.Type statType) { String subStat = "sub-statistic"; 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 d6ec0de..7565f50 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 @@ -31,28 +31,30 @@ import static com.gmail.artemis.the.gr8.playerstats.enums.StandardMessage.*; public final class OutputManager implements StatFormatter { private static BukkitAudiences adventure; + private static ConfigHandler config; private static ShareManager shareManager; - private static MessageBuilder writer; - private static MessageBuilder consoleWriter; + private static MessageBuilder messageBuilder; + private static MessageBuilder consoleMessageBuilder; private static EnumMap> standardMessages; public OutputManager(BukkitAudiences adventure, ConfigHandler config, ShareManager shareManager) { OutputManager.adventure = adventure; + OutputManager.config = config; OutputManager.shareManager = shareManager; - getMessageWriters(config); + getMessageBuilders(); prepareFunctions(); } - public static void updateMessageWriters(ConfigHandler config) { - getMessageWriters(config); + public static void updateMessageBuilders() { + getMessageBuilders(); } @Override public TextComponent formatPlayerStat(@NotNull StatRequest statRequest, int playerStat) { BiFunction playerStatFunction = - getWriter(statRequest).formattedPlayerStatFunction(playerStat, statRequest); + getMessageBuilder(statRequest).formattedPlayerStatFunction(playerStat, statRequest); return processFunction(statRequest.getCommandSender(), playerStatFunction); } @@ -60,7 +62,7 @@ public final class OutputManager implements StatFormatter { @Override public TextComponent formatServerStat(@NotNull StatRequest statRequest, long serverStat) { BiFunction serverStatFunction = - getWriter(statRequest).formattedServerStatFunction(serverStat, statRequest); + getMessageBuilder(statRequest).formattedServerStatFunction(serverStat, statRequest); return processFunction(statRequest.getCommandSender(), serverStatFunction); } @@ -68,7 +70,7 @@ public final class OutputManager implements StatFormatter { @Override public TextComponent formatTopStat(@NotNull StatRequest statRequest, @NotNull LinkedHashMap topStats) { BiFunction topStatFunction = - getWriter(statRequest).formattedTopStatFunction(topStats, statRequest); + getMessageBuilder(statRequest).formattedTopStatFunction(topStats, statRequest); return processFunction(statRequest.getCommandSender(), topStatFunction); } @@ -76,17 +78,17 @@ public final class OutputManager implements StatFormatter { public void sendFeedbackMsg(@NotNull CommandSender sender, StandardMessage message) { if (message != null) { adventure.sender(sender).sendMessage(standardMessages.get(message) - .apply(getWriter(sender))); + .apply(getMessageBuilder(sender))); } } public void sendFeedbackMsgWaitAMoment(@NotNull CommandSender sender, boolean longWait) { - adventure.sender(sender).sendMessage(getWriter(sender) + adventure.sender(sender).sendMessage(getMessageBuilder(sender) .waitAMoment(longWait)); } public void sendFeedbackMsgMissingSubStat(@NotNull CommandSender sender, Statistic.Type statType) { - adventure.sender(sender).sendMessage(getWriter(sender) + adventure.sender(sender).sendMessage(getMessageBuilder(sender) .missingSubStatName(statType)); } @@ -94,18 +96,18 @@ public final class OutputManager implements StatFormatter { if (subStatName == null) { sendFeedbackMsgMissingSubStat(sender, statType); } else { - adventure.sender(sender).sendMessage(getWriter(sender) + adventure.sender(sender).sendMessage(getMessageBuilder(sender) .wrongSubStatType(statType, subStatName)); } } public void sendExamples(@NotNull CommandSender sender) { - adventure.sender(sender).sendMessage(getWriter(sender) + adventure.sender(sender).sendMessage(getMessageBuilder(sender) .usageExamples()); } public void sendHelp(@NotNull CommandSender sender) { - adventure.sender(sender).sendMessage(getWriter(sender) + adventure.sender(sender).sendMessage(getMessageBuilder(sender) .helpMsg(sender instanceof ConsoleCommandSender)); } @@ -132,33 +134,47 @@ public final class OutputManager implements StatFormatter { } } - private MessageBuilder getWriter(CommandSender sender) { - return sender instanceof ConsoleCommandSender ? consoleWriter : writer; + private MessageBuilder getMessageBuilder(CommandSender sender) { + return sender instanceof ConsoleCommandSender ? consoleMessageBuilder : messageBuilder; } - private MessageBuilder getWriter(StatRequest statRequest) { + private MessageBuilder getMessageBuilder(StatRequest statRequest) { if (statRequest.isAPIRequest() || !statRequest.isConsoleSender()) { - return writer; + return messageBuilder; } else { - return consoleWriter; + return consoleMessageBuilder; } } - private static void getMessageWriters(ConfigHandler config) { - boolean isBukkit = Bukkit.getName().equalsIgnoreCase("CraftBukkit"); - if (config.useRainbowMode() || - (config.useFestiveFormatting() && LocalDate.now().getMonth().equals(Month.JUNE))) { - writer = MessageBuilder.fromComponentFactory(config, new PrideComponentFactory(config)); - } - else { - writer = MessageBuilder.defaultBuilder(config); - } + private static void getMessageBuilders() { + messageBuilder = getClientMessageBuilder(); + consoleMessageBuilder = getConsoleMessageBuilder(); + } - if (!isBukkit) { - consoleWriter = writer; - } else { - consoleWriter = MessageBuilder.fromComponentFactory(config, new BukkitConsoleComponentFactory(config)); + private static MessageBuilder getClientMessageBuilder() { + if (useRainbowStyle()) { + return MessageBuilder.fromComponentFactory(config, new PrideComponentFactory(config)); } + return MessageBuilder.defaultBuilder(config); + } + + private static MessageBuilder getConsoleMessageBuilder() { + MessageBuilder consoleBuilder; + if (isBukkit()) { + consoleBuilder = MessageBuilder.fromComponentFactory(config, new BukkitConsoleComponentFactory(config)); + } else { + consoleBuilder = getClientMessageBuilder(); + } + consoleBuilder.toggleHoverUse(false); + return consoleBuilder; + } + + private static boolean useRainbowStyle() { + return config.useRainbowMode() || (config.useFestiveFormatting() && LocalDate.now().getMonth().equals(Month.JUNE)); + } + + private static boolean isBukkit() { + return Bukkit.getName().equalsIgnoreCase("CraftBukkit"); } private void prepareFunctions() { diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/components/BukkitConsoleComponentFactory.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/components/BukkitConsoleComponentFactory.java index 22776b2..05a9f72 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/components/BukkitConsoleComponentFactory.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/components/BukkitConsoleComponentFactory.java @@ -24,6 +24,7 @@ public class BukkitConsoleComponentFactory extends ComponentFactory { PREFIX = PluginColor.GOLD.getConsoleColor(); BRACKETS = PluginColor.GRAY.getConsoleColor(); UNDERSCORE = PluginColor.DARK_PURPLE.getConsoleColor(); + HEARTS = PluginColor.RED.getConsoleColor(); MSG_MAIN = PluginColor.MEDIUM_BLUE.getConsoleColor(); MSG_ACCENT = PluginColor.BLUE.getConsoleColor(); diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/components/ComponentFactory.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/components/ComponentFactory.java index 7e07be2..61c36c5 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/components/ComponentFactory.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/components/ComponentFactory.java @@ -20,9 +20,7 @@ import org.bukkit.Bukkit; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; - import static net.kyori.adventure.text.Component.*; -import static net.kyori.adventure.text.Component.text; /** Creates Components with the desired formatting for the {@link MessageBuilder} to build messages with. This class can put Strings into formatted Components with TextColor @@ -34,6 +32,7 @@ public class ComponentFactory { protected TextColor PREFIX; //gold protected TextColor BRACKETS; //gray protected TextColor UNDERSCORE; //dark_purple + protected TextColor HEARTS; //red protected TextColor MSG_MAIN; //medium_blue protected TextColor MSG_ACCENT; //blue @@ -56,6 +55,7 @@ public class ComponentFactory { PREFIX = PluginColor.GOLD.getColor(); BRACKETS = PluginColor.GRAY.getColor(); UNDERSCORE = PluginColor.DARK_PURPLE.getColor(); + HEARTS = PluginColor.RED.getColor(); MSG_MAIN = PluginColor.MEDIUM_BLUE.getColor(); MSG_ACCENT = PluginColor.BLUE.getColor(); @@ -109,10 +109,10 @@ public class ComponentFactory { return text().color(MSG_ACCENT).build(); } - public TextComponent title(String content, Target selection) { + public TextComponent title(String content, Target target) { return getComponent(content, - getColorFromString(config.getTitleDecoration(selection, false)), - getStyleFromString(config.getTitleDecoration(selection, true))); + getColorFromString(config.getTitleDecoration(target, false)), + getStyleFromString(config.getTitleDecoration(target, true))); } public TextComponent titleNumber(int number) { @@ -141,10 +141,10 @@ public class ComponentFactory { .append(colon); } - public TextComponent playerName(String playerName, Target selection) { + public TextComponent playerName(String playerName, Target target) { return getComponent(playerName, - getColorFromString(config.getPlayerNameDecoration(selection, false)), - getStyleFromString(config.getPlayerNameDecoration(selection, true))); + getColorFromString(config.getPlayerNameDecoration(target, false)), + getStyleFromString(config.getPlayerNameDecoration(target, true))); } public TextComponent sharerName(String sharerName) { @@ -154,7 +154,7 @@ public class ComponentFactory { } public TextComponent shareButton(int shareCode) { - return surroundingBrackets( + return surroundWithBrackets( text("Share") .color(MSG_HOVER) .clickEvent(ClickEvent.runCommand("/statshare " + shareCode)) @@ -163,7 +163,7 @@ public class ComponentFactory { } public TextComponent sharedByMessage(Component playerName) { - return surroundingBrackets( + return surroundWithBrackets( text().append( getComponent("Shared by", getColorFromString(config.getSharedByTextDecoration(false)), @@ -174,7 +174,7 @@ public class ComponentFactory { } public TextComponent statResultInHoverText(TextComponent statResult) { - return surroundingBrackets( + return surroundWithBrackets( text().append(text("Hover Here") .color(MSG_CLICKED) .decorate(TextDecoration.ITALIC) @@ -184,27 +184,27 @@ public class ComponentFactory { /** @param prettyStatName a statName with underscores removed and each word capitalized @param prettySubStatName if present, a subStatName with underscores removed and each word capitalized*/ - public TextComponent statAndSubStatName(String prettyStatName, @Nullable String prettySubStatName, Target selection) { + public TextComponent statAndSubStatName(String prettyStatName, @Nullable String prettySubStatName, Target target) { TextComponent.Builder totalStatNameBuilder = getComponentBuilder(prettyStatName, - getColorFromString(config.getStatNameDecoration(selection, false)), - getStyleFromString(config.getStatNameDecoration(selection, true))); - TextComponent subStat = subStatName(prettySubStatName, selection); + getColorFromString(config.getStatNameDecoration(target, false)), + getStyleFromString(config.getStatNameDecoration(target, true))); + TextComponent subStat = subStatName(prettySubStatName, target); if (!subStat.equals(Component.empty())) { totalStatNameBuilder .append(space().decorations(TextDecoration.NAMES.values(), false)) - .append(subStatName(prettySubStatName, selection)); + .append(subStatName(prettySubStatName, target)); } return totalStatNameBuilder.build(); } /** Returns a TextComponent with TranslatableComponent as a child.*/ - public TextComponent statAndSubStatNameTranslatable(String statKey, String subStatKey, Target selection) { + public TextComponent statAndSubStatNameTranslatable(String statKey, String subStatKey, Target target) { TextComponent.Builder totalStatNameBuilder = getComponentBuilder(null, - getColorFromString(config.getStatNameDecoration(selection, false)), - getStyleFromString(config.getStatNameDecoration(selection, true))); + getColorFromString(config.getStatNameDecoration(target, false)), + getStyleFromString(config.getStatNameDecoration(target, true))); - TextComponent subStat = subStatNameTranslatable(subStatKey, selection); + TextComponent subStat = subStatNameTranslatable(subStatKey, target); if (LanguageKeyHandler.isKeyForKillEntity(statKey)) { return totalStatNameBuilder.append(killEntityBuilder(subStat)).build(); } @@ -222,63 +222,95 @@ public class ComponentFactory { } } - public TextComponent statNumber(String prettyNumber, Target selection) { + public TextComponent statNumber(String prettyNumber, Target target) { return getComponent(prettyNumber, - getColorFromString(config.getStatNumberDecoration(selection, false)), - getStyleFromString(config.getStatNumberDecoration(selection, true))); + getColorFromString(config.getStatNumberDecoration(target, false)), + getStyleFromString(config.getStatNumberDecoration(target, true))); } - public TextComponent statNumberWithHoverText(String mainNumber, String hoverNumber, @Nullable String hoverUnitName, @Nullable String hoverUnitKey, Target selection) { - return statNumberWithHoverText(mainNumber, hoverNumber, hoverUnitName, hoverUnitKey, null, selection); + public TextComponent statNumberWithHoverText(String mainNumber, String hoverNumber, @Nullable String hoverUnitName, @Nullable String hoverUnitKey, Target target) { + return statNumberWithHoverText(mainNumber, hoverNumber, hoverUnitName, hoverUnitKey, null, target); } - public TextComponent damageNumberWithHoverText(String mainNumber, String hoverNumber, TextComponent heart, Target selection) { - return statNumberWithHoverText(mainNumber, hoverNumber, null, null, heart, selection); + public TextComponent damageNumber(String prettyNumber, Target target) { + return statNumber(prettyNumber, target); + } + public TextComponent damageNumberWithHoverText(String mainNumber, String hoverNumber, String hoverUnitName, Target target) { + return statNumberWithHoverText(mainNumber, hoverNumber, hoverUnitName, null, null, target); } - public TextComponent statUnit(String unitName, String unitKey, Target selection) { - if (!(unitName == null && unitKey == null)) { - TextComponent.Builder statUnitBuilder = getComponentBuilder(null, - getColorFromString(config.getSubStatNameDecoration(selection, false)), - getStyleFromString(config.getSubStatNameDecoration(selection, true))); - if (unitKey != null) { - statUnitBuilder.append(translatable() - .key(unitKey)); - } else { - statUnitBuilder.append(text(unitName)); - } - return surroundingBrackets(statUnitBuilder.build()); - } - else { - return Component.empty(); - } + public TextComponent damageNumberWithHeartUnitInHoverText(String mainNumber, String hoverNumber, Target target) { + return statNumberWithHoverText(mainNumber, hoverNumber, null, null, clientHeart(true), target); } - public TextComponent heart(boolean isConsoleSender, boolean isHoverUnit) { - TextColor heartColor = TextColor.fromHexString("#FF1313"); - char heart = isConsoleSender ? '\u2665' : '\u2764'; - if (isHoverUnit) { - return Component.text(heart).color(heartColor); + public TextComponent distanceNumber(String prettyNumber, Target target) { + return statNumber(prettyNumber, target); + } + + public TextComponent distanceNumberWithHoverText(String mainNumber, String hoverNumber, String hoverUnitName, Target target) { + return statNumberWithHoverText(mainNumber, hoverNumber, hoverUnitName, null, target); + } + + public TextComponent distanceNumberWithTranslatableHoverText(String mainNumber, String hoverNumber, String hoverUnitKey, Target target) { + return statNumberWithHoverText(mainNumber, hoverNumber, null, hoverUnitKey, target); + } + + public TextComponent statUnit(String unitName, Target target) { + TextComponent statUnit = getComponentBuilder(unitName, + getColorFromString(config.getSubStatNameDecoration(target, false)), + getStyleFromString(config.getSubStatNameDecoration(target, true))) + .build(); + return surroundWithBrackets(statUnit); + } + + public TextComponent statUnitTranslatable(String unitKey, Target target) { + TextComponent statUnit = getComponentBuilder(null, + getColorFromString(config.getSubStatNameDecoration(target, false)), + getStyleFromString(config.getSubStatNameDecoration(target, true))) + .append(translatable() + .key(unitKey)) + .build(); + return surroundWithBrackets(statUnit); + } + + public TextComponent clientHeart(boolean isDisplayedInHoverText) { + TextComponent basicHeartComponent = basicHeartComponent('\u2764'); + if (isDisplayedInHoverText) { + return basicHeartComponent; } - TextComponent.Builder heartComponent = Component.text() - .content(String.valueOf(heart)) - .color(heartColor); - if (config.useHoverText()) { - heartComponent.hoverEvent(HoverEvent.showText( - text(Unit.HEART.getLabel()) - .color(MSG_HOVER_ACCENT))); - } - return surroundingBrackets(heartComponent.build()); + return surroundWithBrackets(basicHeartComponent); + } + + public TextComponent clientHeartWithHoverText() { + TextComponent basicHeartComponent = basicHeartComponent('\u2764') + .toBuilder() + .hoverEvent(HoverEvent.showText( + text(Unit.HEART.getLabel()) + .color(MSG_HOVER_ACCENT))) + .build(); + return surroundWithBrackets(basicHeartComponent); + } + + public TextComponent consoleHeart() { + return surroundWithBrackets(basicHeartComponent('\u2665')); + } + + //console can do u2665, u2764 looks better in-game + private TextComponent basicHeartComponent(char heartChar) { + return Component.text() + .content(String.valueOf(heartChar)) + .color(HEARTS) + .build(); } /** Returns a TextComponent for the subStatName, or an empty component.*/ - private TextComponent subStatName(@Nullable String prettySubStatName, Target selection) { + private TextComponent subStatName(@Nullable String prettySubStatName, Target target) { if (prettySubStatName == null) { return Component.empty(); } else { return getComponentBuilder(null, - getColorFromString(config.getSubStatNameDecoration(selection, false)), - getStyleFromString(config.getSubStatNameDecoration(selection, true))) + getColorFromString(config.getSubStatNameDecoration(target, false)), + getStyleFromString(config.getSubStatNameDecoration(target, true))) .append(text("(")) .append(text(prettySubStatName)) .append(text(")")) @@ -287,11 +319,11 @@ public class ComponentFactory { } /** Returns a TranslatableComponent for the subStatName, or an empty component.*/ - private TextComponent subStatNameTranslatable(String subStatKey, Target selection) { + private TextComponent subStatNameTranslatable(String subStatKey, Target target) { if (subStatKey != null) { return getComponentBuilder(null, - getColorFromString(config.getSubStatNameDecoration(selection, false)), - getStyleFromString(config.getSubStatNameDecoration(selection, true))) + getColorFromString(config.getSubStatNameDecoration(target, false)), + getStyleFromString(config.getSubStatNameDecoration(target, true))) .append(text("(")) .append(translatable() .key(subStatKey)) @@ -322,14 +354,14 @@ public class ComponentFactory { .args(subStat)); } - private TextComponent statNumberWithHoverText(String mainNumber, String hoverNumber, @Nullable String hoverUnitName, @Nullable String hoverUnitKey, @Nullable TextComponent heart, Target selection) { - TextColor baseColor = getColorFromString(config.getStatNumberDecoration(selection, false)); - TextDecoration style = getStyleFromString(config.getStatNumberDecoration(selection, true)); + private TextComponent statNumberWithHoverText(String mainNumber, String hoverNumber, @Nullable String hoverUnitName, @Nullable String hoverUnitKey, @Nullable TextComponent heartComponent, Target target) { + TextColor baseColor = getColorFromString(config.getStatNumberDecoration(target, false)); + TextDecoration style = getStyleFromString(config.getStatNumberDecoration(target, true)); TextComponent.Builder hoverText = getComponentBuilder(hoverNumber, getLighterColor(baseColor), style); - if (heart != null) { + if (heartComponent != null) { hoverText.append(space()) - .append(heart); + .append(heartComponent); } else if (hoverUnitKey != null) { hoverText.append(space()) @@ -342,7 +374,7 @@ public class ComponentFactory { return getComponent(mainNumber, baseColor, style).hoverEvent(HoverEvent.showText(hoverText)); } - private TextComponent surroundingBrackets(TextComponent component) { + private TextComponent surroundWithBrackets(TextComponent component) { return getComponent(null, BRACKETS, null) .append(text("[")) .append(component) diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/components/PrideComponentFactory.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/components/PrideComponentFactory.java index 12f3896..7f3b8f2 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/components/PrideComponentFactory.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/components/PrideComponentFactory.java @@ -23,6 +23,7 @@ public class PrideComponentFactory extends ComponentFactory { PREFIX = PluginColor.GOLD.getColor(); BRACKETS = PluginColor.GRAY.getColor(); UNDERSCORE = PluginColor.DARK_PURPLE.getColor(); + HEARTS = PluginColor.RED.getColor(); MSG_MAIN = PluginColor.GRAY.getColor(); //difference 1 MSG_ACCENT = PluginColor.LIGHT_GOLD.getColor(); //difference 2 diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/msgutils/NumberFormatter.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/msgutils/NumberFormatter.java index f849646..3d1ae47 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/msgutils/NumberFormatter.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/msgutils/NumberFormatter.java @@ -20,25 +20,13 @@ public final class NumberFormatter { /** Turns the input number into a more readable format depending on its type (number-of-times, time-, damage- or distance-based) according to the corresponding config settings, and adds commas in groups of 3.*/ - public String format(long number, Unit statUnit) { - return format(number, statUnit, null); - } - - public String format(long number, Unit statUnit, Unit smallTimeUnit) { - if (smallTimeUnit == null) { - return switch (statUnit.getType()) { - case DISTANCE -> formatDistance(number, statUnit); - case DAMAGE -> formatDamage(number, statUnit); - default -> format.format(number); - }; - } else { - return formatTime(number, statUnit, smallTimeUnit); - } + public String formatNumber(long number) { + return format.format(number); } /** The unit of damage-based statistics is half a heart by default. This method turns the number into hearts. */ - private String formatDamage(long number, Unit statUnit) { //7 statistics + public String formatDamageNumber(long number, Unit statUnit) { //7 statistics if (statUnit == Unit.HEART) { return format.format(Math.round(number / 2.0)); } else { @@ -48,7 +36,7 @@ public final class NumberFormatter { /** The unit of distance-based statistics is cm by default. This method turns it into blocks by default, and turns it into km or leaves it as cm otherwise, depending on the config settings. */ - private String formatDistance(long number, Unit statUnit) { //15 statistics + public String formatDistanceNumber(long number, Unit statUnit) { //15 statistics switch (statUnit) { case CM -> { return format.format(number); @@ -66,7 +54,7 @@ public final class NumberFormatter { } /** The unit of time-based statistics is ticks by default.*/ - private String formatTime(long number, Unit bigUnit, Unit smallUnit) { //5 statistics + public String formatTimeNumber(long number, Unit bigUnit, Unit smallUnit) { //5 statistics if (number == 0) { return "-"; } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/reload/ReloadThread.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/reload/ReloadThread.java index 76012e3..218ec91 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/reload/ReloadThread.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/reload/ReloadThread.java @@ -81,7 +81,7 @@ public final class ReloadThread extends Thread { private void reloadEverything() { MyLogger.setDebugLevel(config.getDebugLevel()); - OutputManager.updateMessageWriters(config); + OutputManager.updateMessageBuilders(); OfflinePlayerHandler.updateOfflinePlayerList(loadOfflinePlayers()); ShareManager.updateSettings(config); } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/StatRequest.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/StatRequest.java index 33b7b6a..ff1ac08 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/StatRequest.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/StatRequest.java @@ -2,6 +2,7 @@ package com.gmail.artemis.the.gr8.playerstats.statistic.request; import com.gmail.artemis.the.gr8.playerstats.api.RequestGenerator; import com.gmail.artemis.the.gr8.playerstats.enums.Target; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Statistic; import org.bukkit.command.CommandSender; @@ -40,34 +41,31 @@ public final class StatRequest { private Material item; private boolean playerFlag; - /** Create a new {@link StatRequest} with default values: -
- CommandSender sender (provided) -
- Target target = {@link Target#TOP} -
- boolean playerFlag = false -
- boolean isAPIRequest = false - - @param sender the CommandSender who prompted this RequestGenerator - */ - public StatRequest(@NotNull CommandSender sender) { - this(sender, false); - } - /** Create a new {@link StatRequest} with default values:
- CommandSender sender (provided)
- Target target = {@link Target#TOP} +
- int topListSize = 10
- boolean playerFlag = false -
- boolean isAPIRequest (provided) +
- boolean isAPIRequest @param sender the CommandSender who prompted this RequestGenerator @param isAPIRequest whether this RequestGenerator is coming through the API or the onCommand */ - public StatRequest(@NotNull CommandSender sender, boolean isAPIRequest) { + private StatRequest(@NotNull CommandSender sender, boolean isAPIRequest) { this.sender = sender; this.isAPIRequest = isAPIRequest; target = Target.TOP; playerFlag = false; } + public static StatRequest getBasicRequest(CommandSender sender) { + return new StatRequest(sender, false); + } + + public static StatRequest getBasicAPIRequest() { + return new StatRequest(Bukkit.getConsoleSender(), true); + } + public void setAPIRequest() { this.isAPIRequest = true; } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/StatRequestHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/StatRequestHandler.java index b56489a..92042b5 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/StatRequestHandler.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/StatRequestHandler.java @@ -5,7 +5,6 @@ import com.gmail.artemis.the.gr8.playerstats.api.RequestGenerator; 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.utils.OfflinePlayerHandler; -import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Statistic; import org.bukkit.command.CommandSender; @@ -17,20 +16,20 @@ import org.jetbrains.annotations.NotNull; public record StatRequestHandler(StatRequest statRequest) implements RequestGenerator { public static StatRequestHandler playerRequestHandler(String playerName) { - StatRequest request = new StatRequest(Bukkit.getConsoleSender(), true); + StatRequest request = StatRequest.getBasicAPIRequest(); request.setTarget(Target.PLAYER); request.setPlayerName(playerName); return new StatRequestHandler(request); } public static StatRequestHandler serverRequestHandler() { - StatRequest request = new StatRequest(Bukkit.getConsoleSender(), true); + StatRequest request = StatRequest.getBasicAPIRequest(); request.setTarget(Target.SERVER); return new StatRequestHandler(request); } public static StatRequestHandler topRequestHandler(int topListSize) { - StatRequest request = new StatRequest(Bukkit.getConsoleSender(), true); + StatRequest request = StatRequest.getBasicAPIRequest(); request.setTarget(Target.TOP); request.setTopListSize(topListSize != 0 ? topListSize : Main.getConfigHandler().getTopListMaxSize()); return new StatRequestHandler(request); @@ -40,7 +39,8 @@ public record StatRequestHandler(StatRequest statRequest) implements RequestGene @param sender the CommandSender that requested this specific statistic */ public static StatRequestHandler internalRequestHandler(CommandSender sender) { - StatRequest request = new StatRequest(sender); + StatRequest request = StatRequest.getBasicRequest(sender); + request.setTopListSize(Main.getConfigHandler().getTopListMaxSize()); return new StatRequestHandler(request); }