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 d39f577..8bdcdfe 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 @@ -3,6 +3,7 @@ package com.gmail.artemis.the.gr8.playerstats.api; import com.gmail.artemis.the.gr8.playerstats.enums.Unit; import com.gmail.artemis.the.gr8.playerstats.msg.components.ComponentUtils; import com.gmail.artemis.the.gr8.playerstats.msg.msgutils.NumberFormatter; +import com.gmail.artemis.the.gr8.playerstats.msg.msgutils.StringUtils; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.text.TextComponent; import org.bukkit.Statistic; @@ -32,6 +33,13 @@ public interface ApiFormatter { return new NumberFormatter(); } + /** Replace "_" with " " and capitalize each first letter of the input. + @param rawEnumString String to prettify, case-insensitive + @return the transformed String. For example: animals_bred becomes Animals Bred*/ + default String BukkitEnumToString(String rawEnumString) { + return StringUtils.prettify(rawEnumString); + } + /** Gets the default prefix PlayerStats uses. @return [PlayerStats]*/ TextComponent getPluginPrefix(); diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatAction.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatAction.java index ac24e78..ea50e2f 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatAction.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatAction.java @@ -63,17 +63,15 @@ final class StatAction extends RecursiveTask> String playerName = iterator.next(); MyLogger.actionRunning(Thread.currentThread().getName(), playerName, 2); OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(playerName); - if (player != null) { - int statistic = 0; - switch (requestSettings.getStatistic().getType()) { - case UNTYPED -> statistic = player.getStatistic(requestSettings.getStatistic()); - case ENTITY -> statistic = player.getStatistic(requestSettings.getStatistic(), requestSettings.getEntity()); - case BLOCK -> statistic = player.getStatistic(requestSettings.getStatistic(), requestSettings.getBlock()); - case ITEM -> statistic = player.getStatistic(requestSettings.getStatistic(), requestSettings.getItem()); - } - if (statistic > 0) { - allStats.put(playerName, statistic); - } + int statistic = 0; + switch (requestSettings.getStatistic().getType()) { + case UNTYPED -> statistic = player.getStatistic(requestSettings.getStatistic()); + case ENTITY -> statistic = player.getStatistic(requestSettings.getStatistic(), requestSettings.getEntity()); + case BLOCK -> statistic = player.getStatistic(requestSettings.getStatistic(), requestSettings.getBlock()); + case ITEM -> statistic = player.getStatistic(requestSettings.getStatistic(), requestSettings.getItem()); + } + if (statistic > 0) { + allStats.put(playerName, statistic); } } while (iterator.hasNext()); } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatCalculator.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatCalculator.java index 62521a4..aad1a71 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatCalculator.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatCalculator.java @@ -22,18 +22,14 @@ public final class StatCalculator { this.offlinePlayerHandler = offlinePlayerHandler; } - public int getPlayerStat(RequestSettings requestSettings) { OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(requestSettings.getPlayerName()); - if (player != null) { - return switch (requestSettings.getStatistic().getType()) { - case UNTYPED -> player.getStatistic(requestSettings.getStatistic()); - case ENTITY -> player.getStatistic(requestSettings.getStatistic(), requestSettings.getEntity()); - case BLOCK -> player.getStatistic(requestSettings.getStatistic(), requestSettings.getBlock()); - case ITEM -> player.getStatistic(requestSettings.getStatistic(), requestSettings.getItem()); - }; - } - return 0; + return switch (requestSettings.getStatistic().getType()) { + case UNTYPED -> player.getStatistic(requestSettings.getStatistic()); + case ENTITY -> player.getStatistic(requestSettings.getStatistic(), requestSettings.getEntity()); + case BLOCK -> player.getStatistic(requestSettings.getStatistic(), requestSettings.getBlock()); + case ITEM -> player.getStatistic(requestSettings.getStatistic(), requestSettings.getItem()); + }; } public LinkedHashMap getTopStats(RequestSettings requestSettings) { diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/RequestHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/RequestHandler.java index d4045fe..71ec4ed 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/RequestHandler.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/RequestHandler.java @@ -68,12 +68,14 @@ public final class RequestHandler { else { throw new IllegalArgumentException("Either this statistic is not of Type.Block or Type.Item, or no valid block or item has been provided"); } + requestSettings.setStatistic(statistic); requestSettings.setSubStatEntryName(material.toString()); return requestSettings; } public RequestSettings entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException { if (statistic.getType() == Statistic.Type.ENTITY) { + requestSettings.setStatistic(statistic); requestSettings.setSubStatEntryName(entityType.toString()); requestSettings.setEntity(entityType); return requestSettings; diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OfflinePlayerHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OfflinePlayerHandler.java index bccbce3..e85e0d0 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OfflinePlayerHandler.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/OfflinePlayerHandler.java @@ -2,7 +2,6 @@ package com.gmail.artemis.the.gr8.playerstats.utils; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; -import org.jetbrains.annotations.Nullable; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -48,16 +47,19 @@ public final class OfflinePlayerHandler { /** * Uses the playerName to get the player's UUID from a private HashMap, and uses the UUID to get the corresponding OfflinePlayer Object. - * @param playerName name of the target player - * @return OfflinePlayer (if this player is on the list, otherwise null) + * @param playerName name of the target player (case-sensitive) + * @return OfflinePlayer + * @throws IllegalArgumentException if this player is not on the list of players that should be included in statistic calculations */ - public @Nullable OfflinePlayer getOfflinePlayer(String playerName) { + public OfflinePlayer getOfflinePlayer(String playerName) throws IllegalArgumentException { if (offlinePlayerUUIDs.get(playerName) != null) { return Bukkit.getOfflinePlayer(offlinePlayerUUIDs.get(playerName)); } else { - MyLogger.logMsg("Cannot calculate statistics for player-name: " + playerName, true); - return null; + MyLogger.logMsg("Cannot calculate statistics for player-name: " + playerName + + "! Double-check if the name is spelled correctly (including capital letters), " + + "or if any of your config settings exclude them", true); + throw new IllegalArgumentException("Cannot convert this player-name into a valid Player to calculate statistics for"); } } } \ No newline at end of file