From 86e9a7827eb58bb87647f2f5fd731d67d5d6fd09 Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Wed, 10 Aug 2022 13:50:38 +0200 Subject: [PATCH] Fixed minor formatting bug, added 3 new formatting methods for server-stat and top-stat to the ApiFormatter --- .../the/gr8/playerstats/api/ApiFormatter.java | 39 ++++-- .../gr8/playerstats/api/ApiOutputManager.java | 118 ++++++++++++------ .../the/gr8/playerstats/utils/MyLogger.java | 4 +- 3 files changed, 117 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/ApiFormatter.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/ApiFormatter.java index 544184f..d39f577 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/ApiFormatter.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/ApiFormatter.java @@ -32,25 +32,50 @@ public interface ApiFormatter { return new NumberFormatter(); } - /** @return [PlayerStats]*/ + /** Gets the default prefix PlayerStats uses. + @return [PlayerStats]*/ TextComponent getPluginPrefix(); + /** Gets the special rainbow version of PlayerStats' prefix. + @return [PlayerStats] in rainbow colors*/ TextComponent getRainbowPluginPrefix(); - /** @return ________ [PlayerStats] ________*/ + /** Gets the version of the prefix that is surrounded by underscores. This is + meant to be used as a title above a message or statistic display. + @return ________ [PlayerStats] ________ */ TextComponent getPluginPrefixAsTitle(); + /** Gets the special rainbow version of the title-prefix. + @return ________ [PlayerStats] ________ in rainbow colors*/ TextComponent getRainbowPluginPrefixAsTitle(); + /** Gets the default top-stat-title for a Statistic of Type.Untyped. + @return Top [topStatSize] [stat-name]*/ TextComponent getTopStatTitle(int topStatSize, Statistic statistic); - TextComponent getTopStatTitle(int topStatSize, Statistic statistic, String subStatisticName); + /** Gets the top-stat-title for a statistic that has a sub-statistic (block, item or entity). + @return Top [topStatSize] [stat-name] [sub-stat-name] */ + TextComponent getTopStatTitle(int topStatSize, Statistic statistic, String subStatName); + /** Gets the top-stat-title with the specified {@link Unit} in the title. + @return Top [topStatSize] [stat-name] [unit-name] */ TextComponent getTopStatTitle(int topStatSize, Statistic statistic, Unit unit); - /** @return a single line from a top-x statistic: - *
x. Player-name ......... number */ - TextComponent getFormattedTopStatLine(int positionInTopList, String playerName, long statNumber, Unit unit); + /** Formats the input into a single top-statistic line. + @return a single line from a top-x statistic: + *
[positionInTopList]. [player-name] ......... [stat-number] */ + TextComponent getFormattedTopStatLine(int positionInTopList, String playerName, long statNumber, Statistic statistic); - TextComponent getFormattedServerStat(long statNumber, Unit unit); + /** Formats the input into a server statistic message. + @return [Total on this server]: [stat-number] [stat-name] */ + TextComponent getFormattedServerStat(long statNumber, Statistic statistic); + + /** Formats the input into a server statistic message for a statistic that has a + sub-statistic (block, item or entity). + @return [Total on this server]: [stat-number] [stat-name] [sub-stat-name]*/ + TextComponent getFormattedServerStat(long statNumber, Statistic statistic, String subStatName); + + /** Formats the input into a server statistic message with the specified {@link Unit}. + @return [Total on this server]: [stat-number] [stat-name] [unit-name]*/ + TextComponent getFormattedServerStat(long statNumber, Statistic statistic, Unit unit); } \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/ApiOutputManager.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/ApiOutputManager.java index c22417a..8441850 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/ApiOutputManager.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/ApiOutputManager.java @@ -17,13 +17,15 @@ import static net.kyori.adventure.text.Component.space; public class ApiOutputManager implements ApiFormatter { + private static ConfigHandler config; private final ComponentFactory componentFactory; private final PrideComponentFactory prideComponentFactory; private final NumberFormatter numberFormatter; - public ApiOutputManager(ConfigHandler config) { - componentFactory = new ComponentFactory(config); - prideComponentFactory = new PrideComponentFactory(config); + public ApiOutputManager(ConfigHandler configHandler) { + config = configHandler; + componentFactory = new ComponentFactory(configHandler); + prideComponentFactory = new PrideComponentFactory(configHandler); numberFormatter = new NumberFormatter(); } @@ -53,7 +55,7 @@ public class ApiOutputManager implements ApiFormatter { } @Override - public TextComponent getTopStatTitle(int topListSize, Statistic statistic, @Nullable String subStatName) { + public TextComponent getTopStatTitle(int topListSize, Statistic statistic, String subStatName) { return getTopStatTitle(topListSize, statistic, subStatName, null); } @@ -62,7 +64,67 @@ public class ApiOutputManager implements ApiFormatter { return getTopStatTitle(topListSize, statistic, null, unit); } - private TextComponent getTopStatTitle(int topListSize, Statistic statistic, String subStatName, Unit unit) { + @Override + public TextComponent getFormattedTopStatLine(int positionInTopList, String playerName, long statNumber, Statistic statistic) { + TextComponent.Builder topStatLineBuilder = Component.text() + .append(space()) + .append(componentFactory.rankNumber(positionInTopList)) + .append(space()) + .append(componentFactory.playerName(playerName, Target.TOP)) + .append(space()); + + int dots = FontUtils.getNumberOfDotsToAlign(positionInTopList + ". " + playerName); + if (dots >= 1) { + topStatLineBuilder.append(componentFactory.dots(".".repeat(dots))); + } + + Unit.Type statUnitType = Unit.getTypeFromStatistic(statistic); + TextComponent numberComponent = getStatNumberComponent(statNumber, statUnitType, Target.TOP); + + return topStatLineBuilder + .append(space()) + .append(numberComponent) + .build(); + } + + @Override + public TextComponent getFormattedServerStat(long statNumber, Statistic statistic) { + return getFormattedServerStat(statNumber, statistic, null, null); + } + + @Override + public TextComponent getFormattedServerStat(long statNumber, Statistic statistic, String subStatName) { + return getFormattedServerStat(statNumber, statistic, subStatName, null); + } + + @Override + public TextComponent getFormattedServerStat(long statNumber, Statistic statistic, Unit unit) { + return getFormattedServerStat(statNumber, statistic, null, unit); + } + + private TextComponent getFormattedServerStat(long statNumber, Statistic statistic, @Nullable String subStatName, @Nullable Unit unit) { + String serverTitle = config.getServerTitle(); + String serverName = config.getServerName(); + String prettyStatName = StringUtils.prettify(statistic.toString()); + Unit.Type unitType = Unit.getTypeFromStatistic(statistic); + + TextComponent.Builder serverStatBuilder = Component.text() + .append(componentFactory.title(serverTitle, Target.SERVER)) + .append(space()) + .append(componentFactory.serverName(serverName)) + .append(space()) + .append(getStatNumberComponent(statNumber, unitType, Target.SERVER)) + .append(space()) + .append(componentFactory.statAndSubStatName(prettyStatName, subStatName, Target.SERVER)); + + if (unit != null) { + serverStatBuilder.append(space()) + .append(componentFactory.statUnit(unit.getLabel(), Target.SERVER)); + } + return serverStatBuilder.build(); + } + + private TextComponent getTopStatTitle(int topListSize, Statistic statistic, @Nullable String subStatName, @Nullable Unit unit) { String prettyStatName = StringUtils.prettify(statistic.toString()); TextComponent.Builder titleBuilder = Component.text() .append(componentFactory.title("Top", Target.TOP)) @@ -78,36 +140,22 @@ public class ApiOutputManager implements ApiFormatter { return titleBuilder.build(); } - @Override - public TextComponent getFormattedTopStatLine(int positionInTopList, String playerName, long statNumber, Unit unit) { - TextComponent.Builder topStatLineBuilder = Component.text() - .append(space()) - .append(componentFactory.rankNumber(positionInTopList)) - .append(space()); - - int dots = FontUtils.getNumberOfDotsToAlign(positionInTopList + ". " + playerName); - if (dots >= 1) { - topStatLineBuilder.append(componentFactory.dots(".".repeat(dots))); - } - - TextComponent numberComponent = getTopStatNumberComponent(unit, statNumber); - return topStatLineBuilder - .append(space()) - .append(numberComponent) - .build(); - } - - @Override - public TextComponent getFormattedServerStat(long statNumber, Unit unit) { - - } - - private TextComponent getTopStatNumberComponent(Unit unit, long statNumber) { - return switch (unit.getType()) { - case DISTANCE -> componentFactory.distanceNumber(numberFormatter.formatDistanceNumber(statNumber, unit), Target.TOP); - case DAMAGE -> componentFactory.damageNumber(numberFormatter.formatDamageNumber(statNumber, unit), Target.TOP); - case TIME -> componentFactory.timeNumber(numberFormatter.formatTimeNumber(statNumber, unit, unit), Target.TOP); - default -> componentFactory.statNumber(numberFormatter.formatNumber(statNumber), Target.TOP); + private TextComponent getStatNumberComponent(long statNumber, Unit.Type unitType, Target target) { + return switch (unitType) { + case DISTANCE -> { + Unit unit = Unit.getMostSuitableUnit(Unit.Type.DISTANCE, statNumber); + yield componentFactory.distanceNumber(numberFormatter.formatDistanceNumber(statNumber, unit), target); + } + case DAMAGE -> { + Unit unit = Unit.getMostSuitableUnit(Unit.Type.DAMAGE, statNumber); + yield componentFactory.damageNumber(numberFormatter.formatDamageNumber(statNumber, unit), target); + } + case TIME -> { + Unit bigUnit = Unit.getMostSuitableUnit(Unit.Type.TIME, statNumber); + Unit smallUnit = bigUnit.getSmallerUnit(1); + yield componentFactory.timeNumber(numberFormatter.formatTimeNumber(statNumber, bigUnit, smallUnit), target); + } + default -> componentFactory.statNumber(numberFormatter.formatNumber(statNumber), target); }; } } \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/MyLogger.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/MyLogger.java index 67fc2f2..7fe13b4 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/MyLogger.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/MyLogger.java @@ -101,9 +101,9 @@ public final class MyLogger { } } - /** If DebugLevel is MEDIUM or HIGH, logs when the while loop in MessageBuilder, getLanguageKey is being run. */ + /** If DebugLevel is HIGH, logs when the while loop in MessageBuilder, getLanguageKey is being run. */ public static void replacingUnderscores() { - if (debugLevel != DebugLevel.LOW) { + if (debugLevel == DebugLevel.HIGH) { logger.info("Replacing underscores and capitalizing names..."); } }