From bf35582b1317c765a0e6c4d86ffa91795b6f6744 Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Tue, 6 Dec 2022 20:29:04 +0100 Subject: [PATCH] Added way to get excluded player with corresponding config setting (#128, #129) --- .../gr8/playerstats/core/commands/StatCommand.java | 1 + .../gr8/playerstats/core/config/ConfigHandler.java | 10 +++++++++- .../playerstats/core/multithreading/StatAction.java | 2 +- .../playerstats/core/statrequest/RequestManager.java | 11 ++++++++++- .../playerstats/core/utils/OfflinePlayerHandler.java | 11 +++++++++-- src/main/resources/config.yml | 7 ++++++- 6 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/artemis/the/gr8/playerstats/core/commands/StatCommand.java b/src/main/java/com/artemis/the/gr8/playerstats/core/commands/StatCommand.java index c885ea6..91f7844 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/core/commands/StatCommand.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/core/commands/StatCommand.java @@ -170,6 +170,7 @@ public final class StatCommand implements CommandExecutor { switch (targetArg) { case "me" -> { if (sender instanceof Player) { + //TODO this is where an excluded player can sneak in target = Target.PLAYER; playerName = sender.getName(); } else { diff --git a/src/main/java/com/artemis/the/gr8/playerstats/core/config/ConfigHandler.java b/src/main/java/com/artemis/the/gr8/playerstats/core/config/ConfigHandler.java index 68ca9f5..d64de0b 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/core/config/ConfigHandler.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/core/config/ConfigHandler.java @@ -21,7 +21,7 @@ public final class ConfigHandler extends FileHandler { super("config.yml"); config = super.getFileConfiguration(); - configVersion = 6; + configVersion = 7; checkAndUpdateConfigVersion(); MyLogger.setDebugLevel(getDebugLevel()); } @@ -132,6 +132,14 @@ public final class ConfigHandler extends FileHandler { return config.getInt("number-of-days-since-last-joined", 0); } + /** + * Whether to allow the /stat player command for excluded players. + * @return the config setting (default: true) + */ + public boolean allowPlayerLookupsForExcludedPlayers() { + return config.getBoolean("allow-player-lookups-for-excluded-players", true); + } + /** * Whether to use TranslatableComponents wherever possible. * diff --git a/src/main/java/com/artemis/the/gr8/playerstats/core/multithreading/StatAction.java b/src/main/java/com/artemis/the/gr8/playerstats/core/multithreading/StatAction.java index df62967..e0e5c9b 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/core/multithreading/StatAction.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/core/multithreading/StatAction.java @@ -64,7 +64,7 @@ final class StatAction extends RecursiveTask> do { String playerName = iterator.next(); MyLogger.actionRunning(Thread.currentThread().getName()); - OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(playerName); + OfflinePlayer player = offlinePlayerHandler.getLoadedOfflinePlayer(playerName); int statistic = 0; switch (requestSettings.getStatistic().getType()) { case UNTYPED -> statistic = player.getStatistic(requestSettings.getStatistic()); diff --git a/src/main/java/com/artemis/the/gr8/playerstats/core/statrequest/RequestManager.java b/src/main/java/com/artemis/the/gr8/playerstats/core/statrequest/RequestManager.java index 7723439..d83d15e 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/core/statrequest/RequestManager.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/core/statrequest/RequestManager.java @@ -4,6 +4,7 @@ import com.artemis.the.gr8.playerstats.api.RequestGenerator; import com.artemis.the.gr8.playerstats.api.StatManager; import com.artemis.the.gr8.playerstats.api.StatRequest; import com.artemis.the.gr8.playerstats.api.StatResult; +import com.artemis.the.gr8.playerstats.core.config.ConfigHandler; import com.artemis.the.gr8.playerstats.core.msg.msgutils.FormattingFunction; import com.artemis.the.gr8.playerstats.core.msg.OutputManager; import com.artemis.the.gr8.playerstats.core.multithreading.ThreadManager; @@ -85,10 +86,12 @@ public final class RequestManager implements StatManager { private final class RequestProcessor { + private static ConfigHandler config; private static OutputManager outputManager; private static ShareManager shareManager; public RequestProcessor(OutputManager outputManager) { + RequestProcessor.config = ConfigHandler.getInstance(); RequestProcessor.outputManager = outputManager; RequestProcessor.shareManager = ShareManager.getInstance(); } @@ -121,7 +124,13 @@ public final class RequestManager implements StatManager { } private int getPlayerStat(@NotNull StatRequest.Settings requestSettings) { - OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(requestSettings.getPlayerName()); + OfflinePlayer player; + if (offlinePlayerHandler.isExcludedPlayer(requestSettings.getPlayerName()) && + config.allowPlayerLookupsForExcludedPlayers()) { + player = offlinePlayerHandler.getExcludedOfflinePlayer(requestSettings.getPlayerName()); + } else { + player = offlinePlayerHandler.getLoadedOfflinePlayer(requestSettings.getPlayerName()); + } return switch (requestSettings.getStatistic().getType()) { case UNTYPED -> player.getStatistic(requestSettings.getStatistic()); case ENTITY -> player.getStatistic(requestSettings.getStatistic(), requestSettings.getEntity()); diff --git a/src/main/java/com/artemis/the/gr8/playerstats/core/utils/OfflinePlayerHandler.java b/src/main/java/com/artemis/the/gr8/playerstats/core/utils/OfflinePlayerHandler.java index 4f02db9..b22da42 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/core/utils/OfflinePlayerHandler.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/core/utils/OfflinePlayerHandler.java @@ -132,7 +132,7 @@ public final class OfflinePlayerHandler extends FileHandler { * @throws IllegalArgumentException if this player is not on the list * of players that should be included in statistic calculations */ - public @NotNull OfflinePlayer getOfflinePlayer(String playerName) throws IllegalArgumentException { + public @NotNull OfflinePlayer getLoadedOfflinePlayer(String playerName) throws IllegalArgumentException { if (includedPlayerUUIDs.get(playerName) != null) { return Bukkit.getOfflinePlayer(includedPlayerUUIDs.get(playerName)); } @@ -140,10 +140,17 @@ public final class OfflinePlayerHandler extends FileHandler { MyLogger.logWarning("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"); - throw new IllegalArgumentException("Cannot convert this player-name into a valid Player to calculate statistics for"); + throw new IllegalArgumentException("PlayerStats does not know a player by this name"); } } + public @NotNull OfflinePlayer getExcludedOfflinePlayer(String playerName) throws IllegalArgumentException { + if (excludedPlayerUUIDs.get(playerName) != null) { + return Bukkit.getOfflinePlayer(excludedPlayerUUIDs.get(playerName)); + } + throw new IllegalArgumentException("There is no player on the exclude-list with this name"); + } + private void loadOfflinePlayers() { Executors.newSingleThreadExecutor().execute(() -> { loadExcludedPlayerNames(); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 27c6b78..9065560 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,7 +1,7 @@ # ------------------------------------------------------------------------------------------------------ # # PlayerStats Configuration # # ------------------------------------------------------------------------------------------------------ # -config-version: 6 +config-version: 7 # # ------------------------------- # # @@ -33,6 +33,11 @@ exclude-banned-players: false # Leave this on 0 to include all players number-of-days-since-last-joined: 0 +# Players that are excluded through the previous settings or the excluded-players-file will not +# show up in top or server statistics. This setting controls whether you can still see their stats with +# the /stat player command +allow-player-lookups-for-excluded-players: true + # # ------------------------------- # # # # Format & Display # #