From eeb164e194c593f43024f566b07cb2418546db7f Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Mon, 8 Aug 2022 17:24:22 +0200 Subject: [PATCH] Improved Request-business inside API --- .../artemis/the/gr8/playerstats/Main.java | 32 ++- .../the/gr8/playerstats/ThreadManager.java | 26 +-- .../the/gr8/playerstats/api/PlayerStats.java | 3 +- .../gr8/playerstats/api/PlayerStatsAPI.java | 30 +-- .../gr8/playerstats/api/RequestExecutor.java | 20 -- .../gr8/playerstats/api/RequestGenerator.java | 18 +- .../gr8/playerstats/api/StatFormatter.java | 11 +- .../the/gr8/playerstats/api/StatManager.java | 43 ++-- .../gr8/playerstats/commands/StatCommand.java | 28 +-- .../gr8/playerstats/msg/MessageBuilder.java | 66 +++--- .../gr8/playerstats/msg/OutputManager.java | 24 +-- .../gr8/playerstats/reload/ReloadThread.java | 4 +- .../gr8/playerstats/statistic/StatAction.java | 24 +-- ...StatRetriever.java => StatCalculator.java} | 40 ++-- .../gr8/playerstats/statistic/StatThread.java | 40 ++-- .../statistic/request/PlayerStatRequest.java | 33 ++- ...equestHandler.java => RequestHandler.java} | 117 ++++++----- .../statistic/request/RequestSettings.java | 186 +++++++++++++++++ .../statistic/request/ServerStatRequest.java | 33 ++- .../statistic/request/StatRequest.java | 194 +++--------------- .../statistic/request/TopStatRequest.java | 29 ++- .../statistic/result/InternalStatResult.java | 2 +- .../statistic/result/PlayerStatResult.java | 2 +- .../statistic/result/ServerStatResult.java | 2 +- .../statistic/result/StatResult.java | 4 +- .../statistic/result/TopStatResult.java | 2 +- 26 files changed, 518 insertions(+), 495 deletions(-) delete mode 100644 src/main/java/com/gmail/artemis/the/gr8/playerstats/api/RequestExecutor.java rename src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/{StatRetriever.java => StatCalculator.java} (64%) rename src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/{StatRequestHandler.java => RequestHandler.java} (51%) create mode 100644 src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/RequestSettings.java diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java index 8f230e1..4389821 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java @@ -2,6 +2,7 @@ package com.gmail.artemis.the.gr8.playerstats; import com.gmail.artemis.the.gr8.playerstats.api.PlayerStats; import com.gmail.artemis.the.gr8.playerstats.api.PlayerStatsAPI; +import com.gmail.artemis.the.gr8.playerstats.api.StatFormatter; import com.gmail.artemis.the.gr8.playerstats.commands.ReloadCommand; import com.gmail.artemis.the.gr8.playerstats.commands.ShareCommand; import com.gmail.artemis.the.gr8.playerstats.commands.StatCommand; @@ -9,7 +10,7 @@ import com.gmail.artemis.the.gr8.playerstats.commands.TabCompleter; import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler; import com.gmail.artemis.the.gr8.playerstats.listeners.JoinListener; import com.gmail.artemis.the.gr8.playerstats.msg.OutputManager; -import com.gmail.artemis.the.gr8.playerstats.statistic.StatRetriever; +import com.gmail.artemis.the.gr8.playerstats.statistic.StatCalculator; import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler; import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; import net.kyori.adventure.platform.bukkit.BukkitAudiences; @@ -29,6 +30,7 @@ public final class Main extends JavaPlugin { private static OutputManager outputManager; private static ShareManager shareManager; + private static StatCalculator statCalculator; private static ThreadManager threadManager; private static PlayerStats playerStatsAPI; @@ -82,6 +84,13 @@ public final class Main extends JavaPlugin { return config; } + public static @NotNull OfflinePlayerHandler getOfflinePlayerHandler() throws IllegalStateException { + if (offlinePlayerHandler == null) { + throw new IllegalStateException("PlayerStats does not seem to be fully loaded!"); + } + return offlinePlayerHandler; + } + public static @NotNull EnumHandler getEnumHandler() { if (enumHandler == null) { enumHandler = new EnumHandler(); @@ -89,11 +98,18 @@ public final class Main extends JavaPlugin { return enumHandler; } - public static @NotNull OfflinePlayerHandler getOfflinePlayerHandler() throws IllegalStateException { - if (offlinePlayerHandler == null) { - throw new IllegalStateException("PlayerStats does not seem to be fully loaded!"); + public static @NotNull StatCalculator getStatCalculator() throws IllegalStateException { + if (statCalculator == null) { + throw new IllegalStateException("PlayerStats does not seem to be loaded!"); } - return offlinePlayerHandler; + return statCalculator; + } + + public static @NotNull StatFormatter getStatFormatter() throws IllegalStateException { + if (outputManager == null) { + throw new IllegalStateException("PlayerStats does not seem to be loaded!"); + } + return outputManager; } public static @NotNull PlayerStats getPlayerStatsAPI() throws IllegalStateException { @@ -112,9 +128,9 @@ public final class Main extends JavaPlugin { shareManager = new ShareManager(config); outputManager = new OutputManager(getAdventure(), config, shareManager); - StatRetriever statRetriever = new StatRetriever(offlinePlayerHandler); - threadManager = new ThreadManager(config, statRetriever, outputManager); + statCalculator = new StatCalculator(offlinePlayerHandler); + threadManager = new ThreadManager(config, statCalculator, outputManager); - playerStatsAPI = new PlayerStatsAPI(statRetriever, outputManager, offlinePlayerHandler); + playerStatsAPI = new PlayerStatsAPI(outputManager, offlinePlayerHandler); } } \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/ThreadManager.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/ThreadManager.java index ac316e8..c68dcf6 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/ThreadManager.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/ThreadManager.java @@ -2,10 +2,10 @@ package com.gmail.artemis.the.gr8.playerstats; import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler; import com.gmail.artemis.the.gr8.playerstats.enums.StandardMessage; -import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest; +import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestSettings; import com.gmail.artemis.the.gr8.playerstats.msg.OutputManager; import com.gmail.artemis.the.gr8.playerstats.reload.ReloadThread; -import com.gmail.artemis.the.gr8.playerstats.statistic.StatRetriever; +import com.gmail.artemis.the.gr8.playerstats.statistic.StatCalculator; import com.gmail.artemis.the.gr8.playerstats.statistic.StatThread; import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger; import org.bukkit.command.CommandSender; @@ -25,17 +25,17 @@ public final class ThreadManager { private static ConfigHandler config; private static OutputManager outputManager; - private static StatRetriever statRetriever; + private static StatCalculator statCalculator; private ReloadThread lastActiveReloadThread; private StatThread lastActiveStatThread; private final HashMap statThreads; private static long lastRecordedCalcTime; - public ThreadManager(ConfigHandler config, StatRetriever statRetriever, OutputManager outputManager) { + public ThreadManager(ConfigHandler config, StatCalculator statCalculator, OutputManager outputManager) { ThreadManager.config = config; ThreadManager.outputManager = outputManager; - ThreadManager.statRetriever = statRetriever; + ThreadManager.statCalculator = statCalculator; statThreads = new HashMap<>(); statThreadID = 0; @@ -61,19 +61,19 @@ public final class ThreadManager { } } - public void startStatThread(StatRequest statRequest) { + public void startStatThread(RequestSettings requestSettings) { statThreadID += 1; - String cmdSender = statRequest.getCommandSender().getName(); + String cmdSender = requestSettings.getCommandSender().getName(); if (config.limitStatRequests() && statThreads.containsKey(cmdSender)) { Thread runningThread = statThreads.get(cmdSender); if (runningThread.isAlive()) { - outputManager.sendFeedbackMsg(statRequest.getCommandSender(), StandardMessage.REQUEST_ALREADY_RUNNING); + outputManager.sendFeedbackMsg(requestSettings.getCommandSender(), StandardMessage.REQUEST_ALREADY_RUNNING); } else { - startNewStatThread(statRequest); + startNewStatThread(requestSettings); } } else { - startNewStatThread(statRequest); + startNewStatThread(requestSettings); } } @@ -89,9 +89,9 @@ public final class ThreadManager { return lastRecordedCalcTime; } - private void startNewStatThread(StatRequest statRequest) { - lastActiveStatThread = new StatThread(outputManager, statRetriever, statThreadID, statRequest, lastActiveReloadThread); - statThreads.put(statRequest.getCommandSender().getName(), lastActiveStatThread); + private void startNewStatThread(RequestSettings requestSettings) { + lastActiveStatThread = new StatThread(outputManager, statCalculator, statThreadID, requestSettings, lastActiveReloadThread); + statThreads.put(requestSettings.getCommandSender().getName(), lastActiveStatThread); lastActiveStatThread.start(); } } \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStats.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStats.java index 8adcf70..eba26de 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStats.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStats.java @@ -1,6 +1,7 @@ package com.gmail.artemis.the.gr8.playerstats.api; import com.gmail.artemis.the.gr8.playerstats.Main; +import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @@ -9,7 +10,7 @@ import org.jetbrains.annotations.NotNull; {@link PlayerStatsAPI}. You can then use this object to access any of the further methods.

Since calculating a top or server statistics can take some time, I strongly - encourage you to call all the serverStat() and topStat() methods asynchronously. + encourage you to call {@link StatRequest#execute()} asynchronously. Otherwise, the main Thread will have to wait until all calculations are done, and this can severely impact server performance. */ diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStatsAPI.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStatsAPI.java index 4fb44e9..506d64d 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStatsAPI.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStatsAPI.java @@ -1,7 +1,5 @@ package com.gmail.artemis.the.gr8.playerstats.api; - -import com.gmail.artemis.the.gr8.playerstats.statistic.StatRetriever; import com.gmail.artemis.the.gr8.playerstats.statistic.request.*; import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; @@ -11,24 +9,14 @@ import static org.jetbrains.annotations.ApiStatus.Internal; public final class PlayerStatsAPI implements PlayerStats, StatManager { private final OfflinePlayerHandler offlinePlayerHandler; - private static StatRetriever statRetriever; private static StatFormatter statFormatter; @Internal - public PlayerStatsAPI(StatRetriever stat, StatFormatter format, OfflinePlayerHandler offlinePlayers) { - statRetriever = stat; + public PlayerStatsAPI(StatFormatter format, OfflinePlayerHandler offlinePlayers) { statFormatter = format; offlinePlayerHandler = offlinePlayers; } - static StatRetriever statCalculator() { - return statRetriever; - } - - static StatFormatter statFormatter() { - return statFormatter; - } - @Override public Formatter getFormatter() { return statFormatter; @@ -40,26 +28,26 @@ public final class PlayerStatsAPI implements PlayerStats, StatManager { } @Override - public RequestGenerator getPlayerStat(String playerName) { - StatRequest request = StatRequestHandler.getBasicPlayerStatRequest(playerName); + public RequestGenerator playerStatRequest(String playerName) { + RequestSettings request = RequestHandler.getBasicPlayerStatRequest(playerName); return new PlayerStatRequest(request); } @Override - public ServerStatRequest calculateServerStat() { - StatRequest request = StatRequestHandler.getBasicServerStatRequest(); + public ServerStatRequest serverStatRequest() { + RequestSettings request = RequestHandler.getBasicServerStatRequest(); return new ServerStatRequest(request); } @Override - public TopStatRequest calculateTopStat(int topListSize) { - StatRequest request = StatRequestHandler.getBasicTopStatRequest(topListSize); + public TopStatRequest topStatRequest(int topListSize) { + RequestSettings request = RequestHandler.getBasicTopStatRequest(topListSize); return new TopStatRequest(request); } @Override - public TopStatRequest calculateTotalTopStatList() { + public TopStatRequest totalTopStatListRequest() { int playerCount = offlinePlayerHandler.getOfflinePlayerCount(); - return calculateTopStat(playerCount); + return topStatRequest(playerCount); } } \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/RequestExecutor.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/RequestExecutor.java deleted file mode 100644 index 88c068f..0000000 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/RequestExecutor.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.gmail.artemis.the.gr8.playerstats.api; - -import com.gmail.artemis.the.gr8.playerstats.statistic.StatRetriever; -import com.gmail.artemis.the.gr8.playerstats.statistic.result.StatResult; -import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest; - -/** Completes a basic {@link StatRequest} provided by the {@link PlayerStatsAPI} - and performs a statistic lookup with the information that is stored inside this StatRequest.*/ -public interface RequestExecutor { - - static StatRetriever getStatCalculator() { - return PlayerStatsAPI.statCalculator(); - } - - static StatFormatter getStatFormatter() { - return PlayerStatsAPI.statFormatter(); - } - - StatResult execute(); -} \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/RequestGenerator.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/RequestGenerator.java index 8c46191..2f0d0f2 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/RequestGenerator.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/RequestGenerator.java @@ -1,25 +1,25 @@ package com.gmail.artemis.the.gr8.playerstats.api; -import com.gmail.artemis.the.gr8.playerstats.statistic.StatRetriever; +import com.gmail.artemis.the.gr8.playerstats.statistic.StatCalculator; import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest; import org.bukkit.Material; import org.bukkit.Statistic; import org.bukkit.entity.EntityType; import org.jetbrains.annotations.NotNull; -/** Turns user input into a completed {@link StatRequest}. This StatRequest should hold all - the information PlayerStats needs to work with, and is used by the {@link StatRetriever} +/** Is responsible for creating an executable {@link StatRequest}. This Request holds all + the information PlayerStats needs to work with, and is used by the {@link StatCalculator} to get the desired statistic data.*/ public interface RequestGenerator { - /** Gets a StatRequest for a Statistic of Statistic.Type {@code Untyped}. + /** Gets an executable Request object for a Statistic of Statistic.Type {@code Untyped}. @param statistic a Statistic of Type.Untyped @return a {@link StatRequest} @throws IllegalArgumentException if statistic is not of Type.Untyped*/ - RequestExecutor untyped(@NotNull Statistic statistic) throws IllegalArgumentException; + StatRequest untyped(@NotNull Statistic statistic) throws IllegalArgumentException; - /** Gets a StatRequest for a Statistic of Statistic.Type Block or Item. + /** Gets an executable Request object for a Statistic of Statistic.Type Block or Item. @param statistic a Statistic of Type.Block or Type.Item @param material a block if the statistic is of Type.Block, @@ -28,13 +28,13 @@ public interface RequestGenerator { @throws IllegalArgumentException if statistic is not of Type.Block (with a block as material), or statistic is not of Type.Item (with an item as material) */ - RequestExecutor blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException; + StatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException; - /** Gets a StatRequest for a Statistic of Statistic.Type Entity. + /** Gets an executable Request object for a Statistic of Statistic.Type Entity. @param statistic a Statistic of Type.Entity @param entityType an EntityType @return a {@link StatRequest} @throws IllegalArgumentException if statistic is not of Type.Entity*/ - RequestExecutor entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException; + StatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException; } \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/StatFormatter.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/StatFormatter.java index 170fd36..24cf509 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/StatFormatter.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/StatFormatter.java @@ -1,6 +1,7 @@ package com.gmail.artemis.the.gr8.playerstats.api; -import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest; +import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestSettings; +import com.gmail.artemis.the.gr8.playerstats.statistic.StatCalculator; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.text.*; import org.jetbrains.annotations.ApiStatus.Internal; @@ -8,7 +9,7 @@ import org.jetbrains.annotations.ApiStatus.Internal; import java.util.LinkedHashMap; /** The {@link StatFormatter} formats raw numbers into pretty messages. - This Formatter takes a {@link StatRequest} and combines it with the raw number(s) + This Formatter takes a {@link RequestSettings} object and combines it with the raw number(s) returned by the {@link StatCalculator}, and transforms those into a pretty message (by default a TextComponent) with all the relevant information in it.
@@ -28,16 +29,16 @@ interface StatFormatter extends Formatter { /** @return a TextComponent with the following parts:
[player-name]: [number] [stat-name] {sub-stat-name}*/ - TextComponent formatPlayerStat(StatRequest statRequest, int playerStat); + TextComponent formatPlayerStat(RequestSettings requestSettings, int playerStat); /** @return a TextComponent with the following parts:
[Total on] [server-name]: [number] [stat-name] [sub-stat-name]*/ - TextComponent formatServerStat(StatRequest statRequest, long serverStat); + TextComponent formatServerStat(RequestSettings requestSettings, long serverStat); /** @return a TextComponent with the following parts:
[PlayerStats] [Top 10] [stat-name] [sub-stat-name]
[1.] [player-name] [number]
[2.] [player-name] [number]
[3.] etc...*/ - TextComponent formatTopStat(StatRequest statRequest, LinkedHashMap topStats); + TextComponent formatTopStat(RequestSettings requestSettings, LinkedHashMap topStats); } \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/StatManager.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/StatManager.java index eb3c4bf..1211bb1 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/StatManager.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/StatManager.java @@ -4,32 +4,33 @@ import java.util.LinkedHashMap; public interface StatManager { - /** Gets a StatRequest object that can be used to look up a player-statistic. - This StatRequest will have all default settings already configured, - and will be processed as soon as you call one of its methods. + /** Gets a RequestGenerator that can be used to create a PlayerStatRequest. + This RequestGenerator will make sure all default settings + for a player-statistic-lookup are configured. - @return a PlayerStatRequest that can be used to look up a statistic for the - Player whose name is provided*/ - RequestGenerator getPlayerStat(String playerName); + @param playerName the player whose statistic is being requested + @return the RequestGenerator */ + RequestGenerator playerStatRequest(String playerName); - /** Gets a StatRequest object that can be used to look up a server-statistic. - This StatRequest will have all default settings already configured, - and will be processed as soon as you call one of its methods. -
-
Don't call this from the main Thread! (see class description) + /** Gets a RequestGenerator that can be used to create a ServerStatRequest. + This RequestGenerator will make sure all default settings + for a server-statistic-lookup are configured. - @return a ServerStatRequest that can be used to look up a server total*/ - RequestGenerator calculateServerStat(); + @return the RequestGenerator*/ + RequestGenerator serverStatRequest(); - /** Gets a StatRequest object that can be used to look up a top-x-statistic. - This StatRequest will have all default settings already configured, and will be - processed as soon as you call one of its methods. -
-
Don't call this from the main Thread! (see class description) + /** Gets a RequestGenerator that can be used to create a TopStatRequest + for a top-list of the specified size. This RequestGenerator will + make sure all default settings for a top-statistic-lookup are configured. @param topListSize how big the top-x should be (10 by default) - @return a TopStatRequest that can be used to look up a top statistic*/ - RequestGenerator> calculateTopStat(int topListSize); + @return the RequestGenerator*/ + RequestGenerator> topStatRequest(int topListSize); - RequestGenerator> calculateTotalTopStatList(); + /** Gets a RequestGenerator that can be used to create a TopStatRequest + for all offline players on the server. This RequestGenerator will make sure + all default settings for a top-statistic-lookup are configured. + + @return the RequestGenerator*/ + RequestGenerator> totalTopStatListRequest(); } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java index 00bccb1..1df6e9a 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java @@ -3,8 +3,8 @@ package com.gmail.artemis.the.gr8.playerstats.commands; import com.gmail.artemis.the.gr8.playerstats.ThreadManager; import com.gmail.artemis.the.gr8.playerstats.enums.StandardMessage; import com.gmail.artemis.the.gr8.playerstats.enums.Target; -import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequestHandler; -import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest; +import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestSettings; +import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestHandler; import com.gmail.artemis.the.gr8.playerstats.msg.OutputManager; import org.bukkit.Statistic; import org.bukkit.command.Command; @@ -32,10 +32,10 @@ public final class StatCommand implements CommandExecutor { outputManager.sendExamples(sender); } else { - StatRequest baseRequest = StatRequestHandler.getBasicInternalStatRequest(sender); - StatRequestHandler statRequestHandler = new StatRequestHandler(baseRequest); + RequestSettings baseRequest = RequestHandler.getBasicInternalStatRequest(sender); + RequestHandler requestHandler = new RequestHandler(baseRequest); - StatRequest completedRequest = statRequestHandler.getRequestFromArgs(args); + RequestSettings completedRequest = requestHandler.getRequestFromArgs(args); if (completedRequest.isValid()) { threadManager.startStatThread(completedRequest); } else { @@ -46,7 +46,7 @@ public final class StatCommand implements CommandExecutor { return true; } - /** If a given {@link StatRequest} does not result in a valid statistic look-up, + /** If a given {@link RequestSettings} does not result in a valid statistic look-up, this will send a feedback message to the CommandSender that made the request.
The following is checked:
    @@ -54,23 +54,23 @@ public final class StatCommand implements CommandExecutor {
  • Is a subStatEntry needed, and if so, is a corresponding Material/EntityType present?
  • If the target is Player, is a valid playerName provided?
- @param statRequest the StatRequest to give feedback on + @param requestSettings the RequestSettings to give feedback on */ - private void sendFeedback(StatRequest statRequest) { - CommandSender sender = statRequest.getCommandSender(); + private void sendFeedback(RequestSettings requestSettings) { + CommandSender sender = requestSettings.getCommandSender(); - if (statRequest.getStatistic() == null) { + if (requestSettings.getStatistic() == null) { outputManager.sendFeedbackMsg(sender, StandardMessage.MISSING_STAT_NAME); } - else if (statRequest.getTarget() == Target.PLAYER && statRequest.getPlayerName() == null) { + else if (requestSettings.getTarget() == Target.PLAYER && requestSettings.getPlayerName() == null) { outputManager.sendFeedbackMsg(sender, StandardMessage.MISSING_PLAYER_NAME); } else { - Statistic.Type type = statRequest.getStatistic().getType(); - if (type != Statistic.Type.UNTYPED && statRequest.getSubStatEntryName() == null) { + Statistic.Type type = requestSettings.getStatistic().getType(); + if (type != Statistic.Type.UNTYPED && requestSettings.getSubStatEntryName() == null) { outputManager.sendFeedbackMsgMissingSubStat(sender, type); } else { - outputManager.sendFeedbackMsgWrongSubStat(sender, type, statRequest.getSubStatEntryName()); + outputManager.sendFeedbackMsgWrongSubStat(sender, type, requestSettings.getSubStatEntryName()); } } } 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 fc62b5a..96f62a5 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 @@ -5,7 +5,7 @@ import com.gmail.artemis.the.gr8.playerstats.enums.Target; import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler; import com.gmail.artemis.the.gr8.playerstats.enums.Unit; -import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest; +import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestSettings; import com.gmail.artemis.the.gr8.playerstats.msg.components.ComponentFactory; import com.gmail.artemis.the.gr8.playerstats.msg.components.ExampleMessage; import com.gmail.artemis.the.gr8.playerstats.msg.components.HelpMessage; @@ -182,15 +182,15 @@ public final class MessageBuilder {

- Integer shareCode: if a shareCode is provided, a clickable "share" button will be added.
- CommandSender sender: if a sender is provided, a signature with "shared by sender-name" will be added.

- If both parameters are null, the formattedValue will be returned as is.
*/ - public BiFunction formattedPlayerStatFunction(int stat, @NotNull StatRequest statRequest) { + public BiFunction formattedPlayerStatFunction(int stat, @NotNull RequestSettings requestSettings) { TextComponent playerStat = Component.text() - .append(componentFactory.playerName(statRequest.getPlayerName(), Target.PLAYER) + .append(componentFactory.playerName(requestSettings.getPlayerName(), Target.PLAYER) .append(text(":")) .append(space())) - .append(getStatNumberComponent(statRequest, stat)) + .append(getStatNumberComponent(requestSettings, stat)) .append(space()) - .append(getStatNameComponent(statRequest)) - .append(getStatUnitComponent(statRequest.getStatistic(), statRequest.getTarget())) //space is provided by statUnitComponent + .append(getStatNameComponent(requestSettings)) + .append(getStatUnitComponent(requestSettings.getStatistic(), requestSettings.getTarget())) //space is provided by statUnitComponent .build(); return getFormattingFunction(playerStat, Target.PLAYER); @@ -201,16 +201,16 @@ public final class MessageBuilder {

- Integer shareCode: if a shareCode is provided, a clickable "share" button will be added.
- CommandSender sender: if a sender is provided, a signature with "shared by sender-name" will be added.

- If both parameters are null, the formattedValue will be returned as is.
*/ - public BiFunction formattedServerStatFunction(long stat, @NotNull StatRequest statRequest) { + public BiFunction formattedServerStatFunction(long stat, @NotNull RequestSettings requestSettings) { TextComponent serverStat = text() .append(componentFactory.title(config.getServerTitle(), Target.SERVER)) .append(space()) .append(componentFactory.serverName(config.getServerName())) .append(space()) - .append(getStatNumberComponent(statRequest, stat)) + .append(getStatNumberComponent(requestSettings, stat)) .append(space()) - .append(getStatNameComponent(statRequest)) - .append(getStatUnitComponent(statRequest.getStatistic(), statRequest.getTarget())) //space is provided by statUnit + .append(getStatNameComponent(requestSettings)) + .append(getStatUnitComponent(requestSettings.getStatistic(), requestSettings.getTarget())) //space is provided by statUnit .build(); return getFormattingFunction(serverStat, Target.SERVER); @@ -221,10 +221,10 @@ public final class MessageBuilder {

- Integer shareCode: if a shareCode is provided, a clickable "share" button will be added.
- CommandSender sender: if a sender is provided, a signature with "shared by sender-name" will be added.

- If both parameters are null, the formattedValue will be returned as is.
*/ - public BiFunction formattedTopStatFunction(@NotNull LinkedHashMap topStats, @NotNull StatRequest statRequest) { - final TextComponent title = getTopStatsTitleComponent(statRequest, topStats.size()); - final TextComponent shortTitle = getTopStatDescription(statRequest, topStats.size()); - final TextComponent list = getTopStatListComponent(topStats, statRequest); + public BiFunction formattedTopStatFunction(@NotNull LinkedHashMap topStats, @NotNull RequestSettings requestSettings) { + final TextComponent title = getTopStatsTitleComponent(requestSettings, topStats.size()); + final TextComponent shortTitle = getTopStatDescription(requestSettings, topStats.size()); + final TextComponent list = getTopStatListComponent(topStats, requestSettings); final boolean useEnters = config.useEnters(Target.TOP, false); final boolean useEntersForShared = config.useEnters(Target.TOP, true); @@ -296,25 +296,25 @@ public final class MessageBuilder { return componentFactory.sharerName(sender.getName()); } - private TextComponent getTopStatsTitleComponent(StatRequest statRequest, int statListSize) { + private TextComponent getTopStatsTitleComponent(RequestSettings requestSettings, int statListSize) { return Component.text() .append(componentFactory.pluginPrefix()).append(space()) .append(componentFactory.title(config.getTopStatsTitle(), Target.TOP)).append(space()) .append(componentFactory.titleNumber(statListSize)).append(space()) - .append(getStatNameComponent(statRequest)) //space is provided by statUnitComponent - .append(getStatUnitComponent(statRequest.getStatistic(), statRequest.getTarget())) + .append(getStatNameComponent(requestSettings)) //space is provided by statUnitComponent + .append(getStatUnitComponent(requestSettings.getStatistic(), requestSettings.getTarget())) .build(); } - private TextComponent getTopStatDescription(StatRequest statRequest, int statListSize) { + private TextComponent getTopStatDescription(RequestSettings requestSettings, int statListSize) { return Component.text() .append(componentFactory.title(config.getTopStatsTitle(), Target.TOP)).append(space()) .append(componentFactory.titleNumber(statListSize)).append(space()) - .append(getStatNameComponent(statRequest)) //space is provided by statUnitComponent + .append(getStatNameComponent(requestSettings)) //space is provided by statUnitComponent .build(); } - private TextComponent getTopStatListComponent(LinkedHashMap topStats, StatRequest statRequest) { + private TextComponent getTopStatListComponent(LinkedHashMap topStats, RequestSettings requestSettings) { TextComponent.Builder topList = Component.text(); Set playerNames = topStats.keySet(); boolean useDots = config.useDots(); @@ -331,7 +331,7 @@ public final class MessageBuilder { else { topList.append(componentFactory.playerName(playerName + ":", Target.TOP)); } - topList.append(space()).append(getStatNumberComponent(statRequest, topStats.get(playerName))); + topList.append(space()).append(getStatNumberComponent(requestSettings, topStats.get(playerName))); } return topList.build(); } @@ -350,30 +350,30 @@ public final class MessageBuilder { /** 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 statRequest) { + private TextComponent getStatNameComponent(RequestSettings requestSettings) { if (config.useTranslatableComponents()) { - String statKey = languageKeyHandler.getStatKey(statRequest.getStatistic()); - String subStatKey = statRequest.getSubStatEntryName(); + String statKey = languageKeyHandler.getStatKey(requestSettings.getStatistic()); + String subStatKey = requestSettings.getSubStatEntryName(); if (subStatKey != null) { - switch (statRequest.getStatistic().getType()) { - case BLOCK -> subStatKey = languageKeyHandler.getBlockKey(statRequest.getBlock()); - case ENTITY -> subStatKey = languageKeyHandler.getEntityKey(statRequest.getEntity()); - case ITEM -> subStatKey = languageKeyHandler.getItemKey(statRequest.getItem()); + switch (requestSettings.getStatistic().getType()) { + case BLOCK -> subStatKey = languageKeyHandler.getBlockKey(requestSettings.getBlock()); + case ENTITY -> subStatKey = languageKeyHandler.getEntityKey(requestSettings.getEntity()); + case ITEM -> subStatKey = languageKeyHandler.getItemKey(requestSettings.getItem()); default -> { } } } - return componentFactory.statAndSubStatNameTranslatable(statKey, subStatKey, statRequest.getTarget()); + return componentFactory.statAndSubStatNameTranslatable(statKey, subStatKey, requestSettings.getTarget()); } else { return componentFactory.statAndSubStatName( - StringUtils.prettify(statRequest.getStatistic().toString()), - StringUtils.prettify(statRequest.getSubStatEntryName()), - statRequest.getTarget()); + StringUtils.prettify(requestSettings.getStatistic().toString()), + StringUtils.prettify(requestSettings.getSubStatEntryName()), + requestSettings.getTarget()); } } - private TextComponent getStatNumberComponent(StatRequest request, long statNumber) { + private TextComponent getStatNumberComponent(RequestSettings request, long statNumber) { return getStatNumberComponent(request.getStatistic(), request.getTarget(), statNumber); } 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 161232f..5a6272b 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 @@ -5,7 +5,7 @@ import com.gmail.artemis.the.gr8.playerstats.api.StatFormatter; import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler; import com.gmail.artemis.the.gr8.playerstats.enums.StandardMessage; import com.gmail.artemis.the.gr8.playerstats.msg.components.ComponentFactory; -import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest; +import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestSettings; import com.gmail.artemis.the.gr8.playerstats.msg.components.BukkitConsoleComponentFactory; import com.gmail.artemis.the.gr8.playerstats.msg.components.PrideComponentFactory; import net.kyori.adventure.platform.bukkit.BukkitAudiences; @@ -77,19 +77,19 @@ public final class OutputManager implements StatFormatter { } @Override - public TextComponent formatPlayerStat(@NotNull StatRequest statRequest, int playerStat) { + public TextComponent formatPlayerStat(@NotNull RequestSettings requestSettings, int playerStat) { BiFunction playerStatFunction = - getMessageBuilder(statRequest).formattedPlayerStatFunction(playerStat, statRequest); + getMessageBuilder(requestSettings).formattedPlayerStatFunction(playerStat, requestSettings); - return processFunction(statRequest.getCommandSender(), playerStatFunction); + return processFunction(requestSettings.getCommandSender(), playerStatFunction); } @Override - public TextComponent formatServerStat(@NotNull StatRequest statRequest, long serverStat) { + public TextComponent formatServerStat(@NotNull RequestSettings requestSettings, long serverStat) { BiFunction serverStatFunction = - getMessageBuilder(statRequest).formattedServerStatFunction(serverStat, statRequest); + getMessageBuilder(requestSettings).formattedServerStatFunction(serverStat, requestSettings); - return processFunction(statRequest.getCommandSender(), serverStatFunction); + return processFunction(requestSettings.getCommandSender(), serverStatFunction); } @Override @@ -98,11 +98,11 @@ public final class OutputManager implements StatFormatter { } @Override - public TextComponent formatTopStat(@NotNull StatRequest statRequest, @NotNull LinkedHashMap topStats) { + public TextComponent formatTopStat(@NotNull RequestSettings requestSettings, @NotNull LinkedHashMap topStats) { BiFunction topStatFunction = - getMessageBuilder(statRequest).formattedTopStatFunction(topStats, statRequest); + getMessageBuilder(requestSettings).formattedTopStatFunction(topStats, requestSettings); - return processFunction(statRequest.getCommandSender(), topStatFunction); + return processFunction(requestSettings.getCommandSender(), topStatFunction); } public void sendFeedbackMsg(@NotNull CommandSender sender, StandardMessage message) { @@ -168,8 +168,8 @@ public final class OutputManager implements StatFormatter { return sender instanceof ConsoleCommandSender ? consoleMessageBuilder : messageBuilder; } - private MessageBuilder getMessageBuilder(StatRequest statRequest) { - if (statRequest.isAPIRequest() || !statRequest.isConsoleSender()) { + private MessageBuilder getMessageBuilder(RequestSettings requestSettings) { + if (requestSettings.isAPIRequest() || !requestSettings.isConsoleSender()) { return messageBuilder; } else { return consoleMessageBuilder; 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 7715a13..047661c 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 @@ -7,7 +7,7 @@ import com.gmail.artemis.the.gr8.playerstats.enums.DebugLevel; import com.gmail.artemis.the.gr8.playerstats.enums.StandardMessage; import com.gmail.artemis.the.gr8.playerstats.msg.OutputManager; import com.gmail.artemis.the.gr8.playerstats.statistic.StatThread; -import com.gmail.artemis.the.gr8.playerstats.statistic.StatRetriever; +import com.gmail.artemis.the.gr8.playerstats.statistic.StatCalculator; import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger; import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; import org.bukkit.Bukkit; @@ -47,7 +47,7 @@ public final class ReloadThread extends Thread { /** This method will perform a series of tasks. If a {@link StatThread} is still running, it will join the statThread and wait for it to finish. Then, it will reload the config, update the offlinePlayerList in the {@link OfflinePlayerHandler}, update the {@link DebugLevel}, - update the share-settings in {@link ShareManager} and topListSize-settings in {@link StatRetriever}, + update the share-settings in {@link ShareManager} and topListSize-settings in {@link StatCalculator}, and update the MessageBuilders in the {@link OutputManager}.*/ @Override public void run() { 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 56926df..ac24e78 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 @@ -1,7 +1,7 @@ package com.gmail.artemis.the.gr8.playerstats.statistic; import com.gmail.artemis.the.gr8.playerstats.ThreadManager; -import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest; +import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestSettings; import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger; import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; import com.google.common.collect.ImmutableList; @@ -18,7 +18,7 @@ final class StatAction extends RecursiveTask> private final OfflinePlayerHandler offlinePlayerHandler; private final ImmutableList playerNames; - private final StatRequest statRequest; + private final RequestSettings requestSettings; private final ConcurrentHashMap allStats; /** @@ -26,15 +26,15 @@ final class StatAction extends RecursiveTask> * using the default ForkJoinPool, and returns the ConcurrentHashMap when everything is done * @param offlinePlayerHandler the OfflinePlayerHandler to convert playerNames into Players * @param playerNames ImmutableList of playerNames for players that should be included in stat calculations - * @param statRequest a validated statRequest + * @param requestSettings a validated requestSettings * @param allStats the ConcurrentHashMap to put the results on */ - public StatAction(OfflinePlayerHandler offlinePlayerHandler, ImmutableList playerNames, StatRequest statRequest, ConcurrentHashMap allStats) { + public StatAction(OfflinePlayerHandler offlinePlayerHandler, ImmutableList playerNames, RequestSettings requestSettings, ConcurrentHashMap allStats) { threshold = ThreadManager.getTaskThreshold(); this.offlinePlayerHandler = offlinePlayerHandler; this.playerNames = playerNames; - this.statRequest = statRequest; + this.requestSettings = requestSettings; this.allStats = allStats; MyLogger.subActionCreated(Thread.currentThread().getName()); @@ -46,8 +46,8 @@ final class StatAction extends RecursiveTask> return getStatsDirectly(); } else { - final StatAction subTask1 = new StatAction(offlinePlayerHandler, playerNames.subList(0, playerNames.size()/2), statRequest, allStats); - final StatAction subTask2 = new StatAction(offlinePlayerHandler, playerNames.subList(playerNames.size()/2, playerNames.size()), statRequest, allStats); + final StatAction subTask1 = new StatAction(offlinePlayerHandler, playerNames.subList(0, playerNames.size()/2), requestSettings, allStats); + final StatAction subTask2 = new StatAction(offlinePlayerHandler, playerNames.subList(playerNames.size()/2, playerNames.size()), requestSettings, allStats); //queue and compute all subtasks in the right order subTask1.fork(); @@ -65,11 +65,11 @@ final class StatAction extends RecursiveTask> OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(playerName); if (player != null) { int statistic = 0; - switch (statRequest.getStatistic().getType()) { - case UNTYPED -> statistic = player.getStatistic(statRequest.getStatistic()); - case ENTITY -> statistic = player.getStatistic(statRequest.getStatistic(), statRequest.getEntity()); - case BLOCK -> statistic = player.getStatistic(statRequest.getStatistic(), statRequest.getBlock()); - case ITEM -> statistic = player.getStatistic(statRequest.getStatistic(), statRequest.getItem()); + 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); diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatRetriever.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatCalculator.java similarity index 64% rename from src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatRetriever.java rename to src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatCalculator.java index ed92c1a..62521a4 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatRetriever.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatCalculator.java @@ -2,7 +2,7 @@ package com.gmail.artemis.the.gr8.playerstats.statistic; import com.gmail.artemis.the.gr8.playerstats.ThreadManager; import com.gmail.artemis.the.gr8.playerstats.enums.DebugLevel; -import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest; +import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestSettings; import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger; import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; import com.google.common.collect.ImmutableList; @@ -14,37 +14,37 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ForkJoinPool; import java.util.stream.Collectors; -public final class StatRetriever { +public final class StatCalculator { private final OfflinePlayerHandler offlinePlayerHandler; - public StatRetriever(OfflinePlayerHandler offlinePlayerHandler) { + public StatCalculator(OfflinePlayerHandler offlinePlayerHandler) { this.offlinePlayerHandler = offlinePlayerHandler; } - public int getPlayerStat(StatRequest statRequest) { - OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(statRequest.getPlayerName()); + public int getPlayerStat(RequestSettings requestSettings) { + OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(requestSettings.getPlayerName()); if (player != null) { - return switch (statRequest.getStatistic().getType()) { - case UNTYPED -> player.getStatistic(statRequest.getStatistic()); - case ENTITY -> player.getStatistic(statRequest.getStatistic(), statRequest.getEntity()); - case BLOCK -> player.getStatistic(statRequest.getStatistic(), statRequest.getBlock()); - case ITEM -> player.getStatistic(statRequest.getStatistic(), statRequest.getItem()); + 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; } - public LinkedHashMap getTopStats(StatRequest statRequest) { - return getAllStatsAsync(statRequest).entrySet().stream() + public LinkedHashMap getTopStats(RequestSettings requestSettings) { + return getAllStatsAsync(requestSettings).entrySet().stream() .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) - .limit(statRequest.getTopListSize()) + .limit(requestSettings.getTopListSize()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); } - public long getServerStat(StatRequest statRequest) { - List numbers = getAllStatsAsync(statRequest) + public long getServerStat(RequestSettings requestSettings) { + List numbers = getAllStatsAsync(requestSettings) .values() .parallelStream() .toList(); @@ -53,16 +53,16 @@ public final class StatRetriever { /** Invokes a bunch of worker pool threads to divide and conquer (get the statistics for all players that are stored in the {@link OfflinePlayerHandler}) */ - private @NotNull ConcurrentHashMap getAllStatsAsync(StatRequest statRequest) { + private @NotNull ConcurrentHashMap getAllStatsAsync(RequestSettings requestSettings) { long time = System.currentTimeMillis(); ForkJoinPool commonPool = ForkJoinPool.commonPool(); ConcurrentHashMap allStats; try { - allStats = commonPool.invoke(getStatTask(statRequest)); + allStats = commonPool.invoke(getStatTask(requestSettings)); } catch (ConcurrentModificationException e) { - MyLogger.logMsg("The statRequest could not be executed due to a ConcurrentModificationException. " + + MyLogger.logMsg("The requestSettings could not be executed due to a ConcurrentModificationException. " + "This likely happened because Bukkit hasn't fully initialized all player-data yet. " + "Try again and it should be fine!", true); throw new ConcurrentModificationException(e.toString()); @@ -75,12 +75,12 @@ public final class StatRetriever { return allStats; } - private StatAction getStatTask(StatRequest statRequest) { + private StatAction getStatTask(RequestSettings requestSettings) { int size = offlinePlayerHandler.getOfflinePlayerCount() != 0 ? offlinePlayerHandler.getOfflinePlayerCount() : 16; ConcurrentHashMap allStats = new ConcurrentHashMap<>(size); ImmutableList playerNames = ImmutableList.copyOf(offlinePlayerHandler.getOfflinePlayerNames()); - StatAction task = new StatAction(offlinePlayerHandler, playerNames, statRequest, allStats); + StatAction task = new StatAction(offlinePlayerHandler, playerNames, requestSettings, allStats); MyLogger.actionCreated(playerNames.size()); return task; diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java index 9e61abb..9e70290 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java @@ -3,7 +3,7 @@ package com.gmail.artemis.the.gr8.playerstats.statistic; import com.gmail.artemis.the.gr8.playerstats.enums.StandardMessage; import com.gmail.artemis.the.gr8.playerstats.enums.Target; import com.gmail.artemis.the.gr8.playerstats.msg.components.ComponentUtils; -import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest; +import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestSettings; import com.gmail.artemis.the.gr8.playerstats.msg.OutputManager; import com.gmail.artemis.the.gr8.playerstats.reload.ReloadThread; import com.gmail.artemis.the.gr8.playerstats.ThreadManager; @@ -17,19 +17,19 @@ import java.util.*; public final class StatThread extends Thread { private static OutputManager outputManager; - private static StatRetriever statRetriever; + private static StatCalculator statCalculator; private final ReloadThread reloadThread; - private final StatRequest statRequest; + private final RequestSettings requestSettings; - public StatThread(OutputManager m, StatRetriever t, int ID, StatRequest s, @Nullable ReloadThread r) { + public StatThread(OutputManager m, StatCalculator t, int ID, RequestSettings s, @Nullable ReloadThread r) { outputManager = m; - statRetriever = t; + statCalculator = t; reloadThread = r; - statRequest = s; + requestSettings = s; - this.setName("StatThread-" + statRequest.getCommandSender().getName() + "-" + ID); + this.setName("StatThread-" + requestSettings.getCommandSender().getName() + "-" + ID); MyLogger.threadCreated(this.getName()); } @@ -37,13 +37,13 @@ public final class StatThread extends Thread { public void run() throws IllegalStateException, NullPointerException { MyLogger.threadStart(this.getName()); - if (statRequest == null) { - throw new NullPointerException("No statistic statRequest was found!"); + if (requestSettings == null) { + throw new NullPointerException("No statistic requestSettings was found!"); } if (reloadThread != null && reloadThread.isAlive()) { try { MyLogger.waitingForOtherThread(this.getName(), reloadThread.getName()); - outputManager.sendFeedbackMsg(statRequest.getCommandSender(), StandardMessage.STILL_RELOADING); + outputManager.sendFeedbackMsg(requestSettings.getCommandSender(), StandardMessage.STILL_RELOADING); reloadThread.join(); } catch (InterruptedException e) { @@ -54,28 +54,28 @@ public final class StatThread extends Thread { long lastCalc = ThreadManager.getLastRecordedCalcTime(); if (lastCalc > 2000) { - outputManager.sendFeedbackMsgWaitAMoment(statRequest.getCommandSender(), lastCalc > 20000); + outputManager.sendFeedbackMsgWaitAMoment(requestSettings.getCommandSender(), lastCalc > 20000); } - Target selection = statRequest.getTarget(); + Target selection = requestSettings.getTarget(); try { TextComponent statResult = switch (selection) { - case PLAYER -> outputManager.formatPlayerStat(statRequest, statRetriever.getPlayerStat(statRequest)); - case TOP -> outputManager.formatTopStat(statRequest, statRetriever.getTopStats(statRequest)); - case SERVER -> outputManager.formatServerStat(statRequest, statRetriever.getServerStat(statRequest)); + case PLAYER -> outputManager.formatPlayerStat(requestSettings, statCalculator.getPlayerStat(requestSettings)); + case TOP -> outputManager.formatTopStat(requestSettings, statCalculator.getTopStats(requestSettings)); + case SERVER -> outputManager.formatServerStat(requestSettings, statCalculator.getServerStat(requestSettings)); }; - if (statRequest.isAPIRequest()) { + if (requestSettings.isAPIRequest()) { String msg = ComponentUtils.getTranslatableComponentSerializer() .serialize(statResult); - statRequest.getCommandSender().sendMessage(msg); + requestSettings.getCommandSender().sendMessage(msg); } else { - outputManager.sendToCommandSender(statRequest.getCommandSender(), statResult); + outputManager.sendToCommandSender(requestSettings.getCommandSender(), statResult); } } catch (ConcurrentModificationException e) { - if (!statRequest.isConsoleSender()) { - outputManager.sendFeedbackMsg(statRequest.getCommandSender(), StandardMessage.UNKNOWN_ERROR); + if (!requestSettings.isConsoleSender()) { + outputManager.sendFeedbackMsg(requestSettings.getCommandSender(), StandardMessage.UNKNOWN_ERROR); } } } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/PlayerStatRequest.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/PlayerStatRequest.java index 6ec48dd..d9c46a2 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/PlayerStatRequest.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/PlayerStatRequest.java @@ -1,7 +1,7 @@ package com.gmail.artemis.the.gr8.playerstats.statistic.request; +import com.gmail.artemis.the.gr8.playerstats.Main; import com.gmail.artemis.the.gr8.playerstats.api.RequestGenerator; -import com.gmail.artemis.the.gr8.playerstats.api.RequestExecutor; import com.gmail.artemis.the.gr8.playerstats.statistic.result.PlayerStatResult; import com.gmail.artemis.the.gr8.playerstats.statistic.result.StatResult; import net.kyori.adventure.text.TextComponent; @@ -10,43 +10,42 @@ import org.bukkit.Statistic; import org.bukkit.entity.EntityType; import org.jetbrains.annotations.NotNull; -public final class PlayerStatRequest implements RequestGenerator, RequestExecutor { +public final class PlayerStatRequest extends StatRequest implements RequestGenerator { - private final StatRequest statRequest; - private final StatRequestHandler statRequestHandler; + private final RequestHandler requestHandler; - public PlayerStatRequest(StatRequest request) { - statRequest = request; - statRequestHandler = new StatRequestHandler(request); + public PlayerStatRequest(RequestSettings request) { + super(request); + requestHandler = new RequestHandler(request); } @Override - public RequestExecutor untyped(@NotNull Statistic statistic) { - StatRequest completedRequest = statRequestHandler.untyped(statistic); + public StatRequest untyped(@NotNull Statistic statistic) { + RequestSettings completedRequest = requestHandler.untyped(statistic); return new PlayerStatRequest(completedRequest); } @Override - public RequestExecutor blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) { - StatRequest completedRequest = statRequestHandler.blockOrItemType(statistic, material); + public StatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) { + RequestSettings completedRequest = requestHandler.blockOrItemType(statistic, material); return new PlayerStatRequest(completedRequest); } @Override - public RequestExecutor entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) { - StatRequest completedRequest = statRequestHandler.entityType(statistic, entityType); + public StatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) { + RequestSettings completedRequest = requestHandler.entityType(statistic, entityType); return new PlayerStatRequest(completedRequest); } @Override public StatResult execute() { - return getStatResult(statRequest); + return getStatResult(super.requestSettings); } - private PlayerStatResult getStatResult(StatRequest completedRequest) { - int stat = RequestExecutor.getStatCalculator() + private PlayerStatResult getStatResult(RequestSettings completedRequest) { + int stat = Main.getStatCalculator() .getPlayerStat(completedRequest); - TextComponent prettyStat = RequestExecutor.getStatFormatter() + TextComponent prettyStat = Main.getStatFormatter() .formatPlayerStat(completedRequest, stat); return new PlayerStatResult(stat, prettyStat); 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/RequestHandler.java similarity index 51% rename from src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/StatRequestHandler.java rename to src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/RequestHandler.java index b93f76e..d22f113 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/RequestHandler.java @@ -1,7 +1,6 @@ package com.gmail.artemis.the.gr8.playerstats.statistic.request; import com.gmail.artemis.the.gr8.playerstats.Main; -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; @@ -13,29 +12,29 @@ import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; -public final class StatRequestHandler { +public final class RequestHandler { - private final StatRequest statRequest; + private final RequestSettings requestSettings; - public StatRequestHandler(StatRequest request) { - statRequest = request; + public RequestHandler(RequestSettings request) { + requestSettings = request; } - public static StatRequest getBasicPlayerStatRequest(String playerName) { - StatRequest request = StatRequest.getBasicAPIRequest(); + public static RequestSettings getBasicPlayerStatRequest(String playerName) { + RequestSettings request = RequestSettings.getBasicAPIRequest(); request.setTarget(Target.PLAYER); request.setPlayerName(playerName); return request; } - public static StatRequest getBasicServerStatRequest() { - StatRequest request = StatRequest.getBasicAPIRequest(); + public static RequestSettings getBasicServerStatRequest() { + RequestSettings request = RequestSettings.getBasicAPIRequest(); request.setTarget(Target.SERVER); return request; } - public static StatRequest getBasicTopStatRequest(int topListSize) { - StatRequest request = StatRequest.getBasicAPIRequest(); + public static RequestSettings getBasicTopStatRequest(int topListSize) { + RequestSettings request = RequestSettings.getBasicAPIRequest(); request.setTarget(Target.TOP); request.setTopListSize(topListSize != 0 ? topListSize : Main.getConfigHandler().getTopListMaxSize()); return request; @@ -44,129 +43,129 @@ public final class StatRequestHandler { /** @param sender the CommandSender that requested this specific statistic */ - public static StatRequest getBasicInternalStatRequest(CommandSender sender) { - StatRequest request = StatRequest.getBasicRequest(sender); + public static RequestSettings getBasicInternalStatRequest(CommandSender sender) { + RequestSettings request = RequestSettings.getBasicRequest(sender); request.setTopListSize(Main.getConfigHandler().getTopListMaxSize()); return request; } - public StatRequest untyped(@NotNull Statistic statistic) throws IllegalArgumentException { + public RequestSettings untyped(@NotNull Statistic statistic) throws IllegalArgumentException { if (statistic.getType() == Statistic.Type.UNTYPED) { - statRequest.setStatistic(statistic); - return statRequest; + requestSettings.setStatistic(statistic); + return requestSettings; } throw new IllegalArgumentException("This statistic is not of Type.Untyped"); } - public StatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException { + public RequestSettings blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException { Statistic.Type type = statistic.getType(); if (type == Statistic.Type.BLOCK && material.isBlock()) { - statRequest.setBlock(material); + requestSettings.setBlock(material); } else if (type == Statistic.Type.ITEM && material.isItem()){ - statRequest.setItem(material); + requestSettings.setItem(material); } else { throw new IllegalArgumentException("Either this statistic is not of Type.Block or Type.Item, or no valid block or item has been provided"); } - statRequest.setSubStatEntryName(material.toString()); - return statRequest; + requestSettings.setSubStatEntryName(material.toString()); + return requestSettings; } - public StatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException { + public RequestSettings entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException { if (statistic.getType() == Statistic.Type.ENTITY) { - statRequest.setSubStatEntryName(entityType.toString()); - statRequest.setEntity(entityType); - return statRequest; + requestSettings.setSubStatEntryName(entityType.toString()); + requestSettings.setEntity(entityType); + return requestSettings; } throw new IllegalArgumentException("This statistic is not of Type.Entity"); } /** - This will create a {@link StatRequest} from the provided args, with the requesting Player (or Console) - as CommandSender. This CommandSender will receive feedback messages if the StatRequest could not be created. + This will create a {@link RequestSettings} from the provided args, with the requesting Player (or Console) + as CommandSender. This CommandSender will receive feedback messages if the RequestSettings could not be created. @param args an Array of args such as a CommandSender would put in Minecraft chat:

- a statName (example: "mine_block")

- if applicable, a subStatEntryName (example: diorite)(

- a target for this lookup: can be "top", "server", "player" (or "me" to indicate the current CommandSender)

- if "player" was chosen, include a playerName

- @return the generated StatRequest + @return the generated RequestSettings */ - public StatRequest getRequestFromArgs(String[] args) { + public RequestSettings getRequestFromArgs(String[] args) { EnumHandler enumHandler = Main.getEnumHandler(); OfflinePlayerHandler offlinePlayerHandler = Main.getOfflinePlayerHandler(); - CommandSender sender = statRequest.getCommandSender(); + CommandSender sender = requestSettings.getCommandSender(); for (String arg : args) { //check for statName - if (enumHandler.isStatistic(arg) && statRequest.getStatistic() == null) { - statRequest.setStatistic(EnumHandler.getStatEnum(arg)); + if (enumHandler.isStatistic(arg) && requestSettings.getStatistic() == null) { + requestSettings.setStatistic(EnumHandler.getStatEnum(arg)); } //check for subStatEntry and playerFlag else if (enumHandler.isSubStatEntry(arg)) { - if (arg.equalsIgnoreCase("player") && !statRequest.getPlayerFlag()) { - statRequest.setPlayerFlag(true); + if (arg.equalsIgnoreCase("player") && !requestSettings.getPlayerFlag()) { + requestSettings.setPlayerFlag(true); } else { - if (statRequest.getSubStatEntryName() == null) statRequest.setSubStatEntryName(arg); + if (requestSettings.getSubStatEntryName() == null) requestSettings.setSubStatEntryName(arg); } } //check for selection else if (arg.equalsIgnoreCase("top")) { - statRequest.setTarget(Target.TOP); + requestSettings.setTarget(Target.TOP); } else if (arg.equalsIgnoreCase("server")) { - statRequest.setTarget(Target.SERVER); + requestSettings.setTarget(Target.SERVER); } else if (arg.equalsIgnoreCase("me")) { if (sender instanceof Player) { - statRequest.setPlayerName(sender.getName()); - statRequest.setTarget(Target.PLAYER); + requestSettings.setPlayerName(sender.getName()); + requestSettings.setTarget(Target.PLAYER); } else if (sender instanceof ConsoleCommandSender) { - statRequest.setTarget(Target.SERVER); + requestSettings.setTarget(Target.SERVER); } - } else if (offlinePlayerHandler.isRelevantPlayer(arg) && statRequest.getPlayerName() == null) { - statRequest.setPlayerName(arg); - statRequest.setTarget(Target.PLAYER); + } else if (offlinePlayerHandler.isRelevantPlayer(arg) && requestSettings.getPlayerName() == null) { + requestSettings.setPlayerName(arg); + requestSettings.setTarget(Target.PLAYER); } else if (arg.equalsIgnoreCase("api")) { - statRequest.setAPIRequest(); + requestSettings.setAPIRequest(); } } - patchRequest(statRequest); - return statRequest; + patchRequest(requestSettings); + return requestSettings; } /** - Adjust the StatRequest object if needed: unpack the playerFlag into a subStatEntry, + Adjust the RequestSettings object if needed: unpack the playerFlag into a subStatEntry, try to retrieve the corresponding Enum Constant for any relevant block/entity/item, and remove any unnecessary subStatEntries. */ - private void patchRequest(StatRequest statRequest) { - if (statRequest.getStatistic() != null) { - Statistic.Type type = statRequest.getStatistic().getType(); + private void patchRequest(RequestSettings requestSettings) { + if (requestSettings.getStatistic() != null) { + Statistic.Type type = requestSettings.getStatistic().getType(); - if (statRequest.getPlayerFlag()) { //unpack the playerFlag - if (type == Statistic.Type.ENTITY && statRequest.getSubStatEntryName() == null) { - statRequest.setSubStatEntryName("player"); + if (requestSettings.getPlayerFlag()) { //unpack the playerFlag + if (type == Statistic.Type.ENTITY && requestSettings.getSubStatEntryName() == null) { + requestSettings.setSubStatEntryName("player"); } else { - statRequest.setTarget(Target.PLAYER); + requestSettings.setTarget(Target.PLAYER); } } - String subStatEntry = statRequest.getSubStatEntryName(); + String subStatEntry = requestSettings.getSubStatEntryName(); switch (type) { //attempt to convert relevant subStatEntries into their corresponding Enum Constant case BLOCK -> { Material block = EnumHandler.getBlockEnum(subStatEntry); - if (block != null) statRequest.setBlock(block); + if (block != null) requestSettings.setBlock(block); } case ENTITY -> { EntityType entity = EnumHandler.getEntityEnum(subStatEntry); - if (entity != null) statRequest.setEntity(entity); + if (entity != null) requestSettings.setEntity(entity); } case ITEM -> { Material item = EnumHandler.getItemEnum(subStatEntry); - if (item != null) statRequest.setItem(item); + if (item != null) requestSettings.setItem(item); } case UNTYPED -> { //remove unnecessary subStatEntries - if (subStatEntry != null) statRequest.setSubStatEntryName(null); + if (subStatEntry != null) requestSettings.setSubStatEntryName(null); } } } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/RequestSettings.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/RequestSettings.java new file mode 100644 index 0000000..a8ba2d9 --- /dev/null +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/RequestSettings.java @@ -0,0 +1,186 @@ +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; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.entity.EntityType; +import org.jetbrains.annotations.NotNull; + +/** The object PlayerStats uses to calculate and format the requested statistic. + The settings in this RequestSettings object can be configured from two different sources: +
- Internally: by PlayerStats itself when /stat is called, using the args provided by the CommandSender. +
- Externally: through the API methods provided by the {@link RequestGenerator} interface. +
+
For this RequestSettings object to be valid, the following values need to be set: +
    +
  • a {@link Statistic} statistic
  • +
  • if this Statistic is not of {@link Statistic.Type} Untyped, a subStatEntryName needs to be set, + together with one of the following values: +
    - for Type.Block: a {@link Material} blockMaterial +
    - for Type.Item: a {@link Material} itemMaterial +
    - for Type.Entity: an {@link EntityType} entityType +
  • a {@link Target} target (automatically set for all API-requests) +
  • if the target is Target.Player, a playerName needs to be added +
*/ +public final class RequestSettings { + + private final CommandSender sender; + private boolean isAPIRequest; + private Statistic statistic; + private String playerName; + private Target target; + private int topListSize; + + private String subStatEntryName; + private EntityType entity; + private Material block; + private Material item; + private boolean playerFlag; + + /** Create a new {@link RequestSettings} with default values: +
- CommandSender sender (provided) +
- Target target = {@link Target#TOP} +
- int topListSize = 10 +
- boolean playerFlag = false +
- boolean isAPIRequest + + @param sender the CommandSender who prompted this RequestGenerator + @param isAPIRequest whether this RequestGenerator is coming through the API or the onCommand + */ + private RequestSettings(@NotNull CommandSender sender, boolean isAPIRequest) { + this.sender = sender; + this.isAPIRequest = isAPIRequest; + target = Target.TOP; + playerFlag = false; + } + + public static RequestSettings getBasicRequest(CommandSender sender) { + return new RequestSettings(sender, false); + } + + public static RequestSettings getBasicAPIRequest() { + return new RequestSettings(Bukkit.getConsoleSender(), true); + } + + public void setAPIRequest() { + this.isAPIRequest = true; + } + + public boolean isAPIRequest() { + return isAPIRequest; + } + + public @NotNull CommandSender getCommandSender() { + return sender; + } + + public boolean isConsoleSender() { + return sender instanceof ConsoleCommandSender; + } + + public void setStatistic(Statistic statistic) { + this.statistic = statistic; + } + + public Statistic getStatistic() { + return statistic; + } + + public void setSubStatEntryName(String subStatEntry) { + this.subStatEntryName = subStatEntry; + } + + public String getSubStatEntryName() { + return subStatEntryName; + } + + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + + public String getPlayerName() { + return playerName; + } + + public void setPlayerFlag(boolean playerFlag) { + this.playerFlag = playerFlag; + } + + public boolean getPlayerFlag() { + return playerFlag; + } + + public void setTarget(Target target) { + this.target = target; + } + + public @NotNull Target getTarget() { + return target; + } + + public void setTopListSize(int topListSize) { + this.topListSize = topListSize; + } + + public int getTopListSize() { + return this.topListSize; + } + + public void setEntity(EntityType entity) { + this.entity = entity; + } + + public EntityType getEntity() { + return entity; + } + + public void setBlock(Material block) { + this.block = block; + } + + public Material getBlock() { + return block; + } + + public void setItem(Material item) { + this.item = item; + } + + public Material getItem() { + return item; + } + + public boolean isValid() { + if (statistic == null) { + return false; + } else if (target == Target.PLAYER && playerName == null) { + return false; + } else if (statistic.getType() != Statistic.Type.UNTYPED && + subStatEntryName == null) { + return false; + } else { + return hasMatchingSubStat(); + } + } + + private boolean hasMatchingSubStat() { + switch (statistic.getType()) { + case BLOCK -> { + return block != null; + } + case ENTITY -> { + return entity != null; + } + case ITEM -> { + return item != null; + } + default -> { + return true; + } + } + } +} \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/ServerStatRequest.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/ServerStatRequest.java index 68e1e80..9c33ada 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/ServerStatRequest.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/ServerStatRequest.java @@ -1,6 +1,6 @@ package com.gmail.artemis.the.gr8.playerstats.statistic.request; -import com.gmail.artemis.the.gr8.playerstats.api.RequestExecutor; +import com.gmail.artemis.the.gr8.playerstats.Main; import com.gmail.artemis.the.gr8.playerstats.api.RequestGenerator; import com.gmail.artemis.the.gr8.playerstats.statistic.result.ServerStatResult; import com.gmail.artemis.the.gr8.playerstats.statistic.result.StatResult; @@ -10,43 +10,42 @@ import org.bukkit.Statistic; import org.bukkit.entity.EntityType; import org.jetbrains.annotations.NotNull; -public final class ServerStatRequest implements RequestGenerator, RequestExecutor { +public final class ServerStatRequest extends StatRequest implements RequestGenerator { - private final StatRequest statRequest; - private final StatRequestHandler statRequestHandler; + private final RequestHandler requestHandler; - public ServerStatRequest(StatRequest request) { - statRequest = request; - statRequestHandler = new StatRequestHandler(statRequest); + public ServerStatRequest(RequestSettings request) { + super(request); + requestHandler = new RequestHandler(requestSettings); } @Override - public RequestExecutor untyped(@NotNull Statistic statistic) { - StatRequest completedRequest = statRequestHandler.untyped(statistic); + public StatRequest untyped(@NotNull Statistic statistic) { + RequestSettings completedRequest = requestHandler.untyped(statistic); return new ServerStatRequest(completedRequest); } @Override - public RequestExecutor blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) { - StatRequest completedRequest = statRequestHandler.blockOrItemType(statistic, material); + public StatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) { + RequestSettings completedRequest = requestHandler.blockOrItemType(statistic, material); return new ServerStatRequest(completedRequest); } @Override - public RequestExecutor entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) { - StatRequest completedRequest = statRequestHandler.entityType(statistic, entityType); + public StatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) { + RequestSettings completedRequest = requestHandler.entityType(statistic, entityType); return new ServerStatRequest(completedRequest); } @Override public StatResult execute() { - return getStatResult(statRequest); + return getStatResult(requestSettings); } - private ServerStatResult getStatResult(StatRequest completedRequest) { - long stat = RequestExecutor.getStatCalculator() + private ServerStatResult getStatResult(RequestSettings completedRequest) { + long stat = Main.getStatCalculator() .getServerStat(completedRequest); - TextComponent prettyStat = RequestExecutor.getStatFormatter() + TextComponent prettyStat = Main.getStatFormatter() .formatServerStat(completedRequest, stat); return new ServerStatResult(stat, prettyStat); 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 ff1ac08..ce973aa 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 @@ -1,186 +1,40 @@ 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 com.gmail.artemis.the.gr8.playerstats.statistic.result.StatResult; import org.bukkit.Material; import org.bukkit.Statistic; -import org.bukkit.command.CommandSender; -import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.EntityType; -import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -/** A StatRequest is the object PlayerStats uses to calculate and format the requested statistic. - This object can be generated from two different sources: -
- Internally: by PlayerStats itself when /stat is called, using the args provided by the CommandSender. -
- Externally: through the API methods provided by the {@link RequestGenerator} interface. -
-
For this StatRequest to be valid, it needs the following values: -
    -
  • a {@link Statistic} statistic
  • -
  • if this Statistic is not of {@link Statistic.Type} Untyped, a subStatEntryName needs to be set, - together with one of the following values: -
    - for Type.Block: a {@link Material} blockMaterial -
    - for Type.Item: a {@link Material} itemMaterial -
    - for Type.Entity: an {@link EntityType} entityType -
  • a {@link Target} target (automatically set for all API-requests) -
  • if the target is Target.Player, a playerName needs to be added -
*/ -public final class StatRequest { +public abstract class StatRequest { - private final CommandSender sender; - private boolean isAPIRequest; - private Statistic statistic; - private String playerName; - private Target target; - private int topListSize; + protected final RequestSettings requestSettings; - private String subStatEntryName; - private EntityType entity; - private Material block; - private Material item; - private boolean playerFlag; + protected StatRequest(RequestSettings request) { + requestSettings = request; + } - /** Create a new {@link StatRequest} with default values: -
- CommandSender sender (provided) -
- Target target = {@link Target#TOP} -
- int topListSize = 10 -
- boolean playerFlag = false -
- boolean isAPIRequest + /** Don't call this from the Main Thread!*/ + public abstract StatResult execute(); - @param sender the CommandSender who prompted this RequestGenerator - @param isAPIRequest whether this RequestGenerator is coming through the API or the onCommand - */ - private StatRequest(@NotNull CommandSender sender, boolean isAPIRequest) { - this.sender = sender; - this.isAPIRequest = isAPIRequest; - target = Target.TOP; - playerFlag = false; - } + public Statistic getStatistic() { + return requestSettings.getStatistic(); + } - public static StatRequest getBasicRequest(CommandSender sender) { - return new StatRequest(sender, false); - } + public @Nullable Material getBlock() { + return requestSettings.getBlock(); + } - public static StatRequest getBasicAPIRequest() { - return new StatRequest(Bukkit.getConsoleSender(), true); - } + public @Nullable Material getItem() { + return requestSettings.getItem(); + } - public void setAPIRequest() { - this.isAPIRequest = true; - } + public @Nullable EntityType getEntity() { + return requestSettings.getEntity(); + } - public boolean isAPIRequest() { - return isAPIRequest; - } - - public @NotNull CommandSender getCommandSender() { - return sender; - } - - public boolean isConsoleSender() { - return sender instanceof ConsoleCommandSender; - } - - public void setStatistic(Statistic statistic) { - this.statistic = statistic; - } - - public Statistic getStatistic() { - return statistic; - } - - public void setSubStatEntryName(String subStatEntry) { - this.subStatEntryName = subStatEntry; - } - - public String getSubStatEntryName() { - return subStatEntryName; - } - - public void setPlayerName(String playerName) { - this.playerName = playerName; - } - - public String getPlayerName() { - return playerName; - } - - public void setPlayerFlag(boolean playerFlag) { - this.playerFlag = playerFlag; - } - - public boolean getPlayerFlag() { - return playerFlag; - } - - public void setTarget(Target target) { - this.target = target; - } - - public @NotNull Target getTarget() { - return target; - } - - public void setTopListSize(int topListSize) { - this.topListSize = topListSize; - } - - public int getTopListSize() { - return this.topListSize; - } - - public void setEntity(EntityType entity) { - this.entity = entity; - } - - public EntityType getEntity() { - return entity; - } - - public void setBlock(Material block) { - this.block = block; - } - - public Material getBlock() { - return block; - } - - public void setItem(Material item) { - this.item = item; - } - - public Material getItem() { - return item; - } - - public boolean isValid() { - if (statistic == null) { - return false; - } else if (target == Target.PLAYER && playerName == null) { - return false; - } else if (statistic.getType() != Statistic.Type.UNTYPED && - subStatEntryName == null) { - return false; - } else { - return hasMatchingSubStat(); - } - } - - private boolean hasMatchingSubStat() { - switch (statistic.getType()) { - case BLOCK -> { - return block != null; - } - case ENTITY -> { - return entity != null; - } - case ITEM -> { - return item != null; - } - default -> { - return true; - } - } - } + public Target getTarget() { + return requestSettings.getTarget(); + } } \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/TopStatRequest.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/TopStatRequest.java index 3c5b76f..52b253d 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/TopStatRequest.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/request/TopStatRequest.java @@ -1,6 +1,6 @@ package com.gmail.artemis.the.gr8.playerstats.statistic.request; -import com.gmail.artemis.the.gr8.playerstats.api.RequestExecutor; +import com.gmail.artemis.the.gr8.playerstats.Main; import com.gmail.artemis.the.gr8.playerstats.api.RequestGenerator; import com.gmail.artemis.the.gr8.playerstats.statistic.result.StatResult; import com.gmail.artemis.the.gr8.playerstats.statistic.result.TopStatResult; @@ -12,43 +12,42 @@ import org.jetbrains.annotations.NotNull; import java.util.LinkedHashMap; -public final class TopStatRequest implements RequestGenerator>, RequestExecutor> { +public final class TopStatRequest extends StatRequest> implements RequestGenerator> { - private final StatRequest statRequest; - private final StatRequestHandler statRequestHandler; + private final RequestHandler requestHandler; - public TopStatRequest(StatRequest request) { - statRequest = request; - statRequestHandler = new StatRequestHandler(request); + public TopStatRequest(RequestSettings request) { + super(request); + requestHandler = new RequestHandler(request); } @Override - public TopStatRequest untyped(@NotNull Statistic statistic) { - StatRequest completedRequest = statRequestHandler.untyped(statistic); + public StatRequest> untyped(@NotNull Statistic statistic) { + RequestSettings completedRequest = requestHandler.untyped(statistic); return new TopStatRequest(completedRequest); } @Override public TopStatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) { - StatRequest completedRequest = statRequestHandler.blockOrItemType(statistic, material); + RequestSettings completedRequest = requestHandler.blockOrItemType(statistic, material); return new TopStatRequest(completedRequest); } @Override public TopStatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) { - StatRequest completedRequest = statRequestHandler.entityType(statistic, entityType); + RequestSettings completedRequest = requestHandler.entityType(statistic, entityType); return new TopStatRequest(completedRequest); } @Override public StatResult> execute() { - return getStatResult(statRequest); + return getStatResult(super.requestSettings); } - private TopStatResult getStatResult(StatRequest completedRequest) { - LinkedHashMap stat = RequestExecutor.getStatCalculator() + private TopStatResult getStatResult(RequestSettings completedRequest) { + LinkedHashMap stat = Main.getStatCalculator() .getTopStats(completedRequest); - TextComponent prettyStat = RequestExecutor.getStatFormatter() + TextComponent prettyStat = Main.getStatFormatter() .formatTopStat(completedRequest, stat); return new TopStatResult(stat, prettyStat); diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/InternalStatResult.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/InternalStatResult.java index 37a1fa6..2eb037b 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/InternalStatResult.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/InternalStatResult.java @@ -23,7 +23,7 @@ public record InternalStatResult(String executorName, TextComponent formattedVal } @Override - public String toString() { + public String getFormattedString() { return ComponentUtils.getTranslatableComponentSerializer() .serialize(formattedValue); } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/PlayerStatResult.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/PlayerStatResult.java index fadec88..da747d0 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/PlayerStatResult.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/PlayerStatResult.java @@ -16,7 +16,7 @@ public record PlayerStatResult(int value, TextComponent formattedValue) implemen } @Override - public String toString() { + public String getFormattedString() { return ComponentUtils.getTranslatableComponentSerializer() .serialize(formattedValue); } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/ServerStatResult.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/ServerStatResult.java index 4ed892b..77cd804 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/ServerStatResult.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/ServerStatResult.java @@ -16,7 +16,7 @@ public record ServerStatResult(long value, TextComponent formattedValue) impleme } @Override - public String toString() { + public String getFormattedString() { return ComponentUtils.getTranslatableComponentSerializer() .serialize(formattedValue); } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/StatResult.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/StatResult.java index 0ee9249..6c51f0d 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/StatResult.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/StatResult.java @@ -33,7 +33,7 @@ import net.kyori.adventure.text.TextComponent; Information on how to get and use the BukkitAudiences object can be found on Adventure's website. -

You can also use the provided {@link #toString()} method to get the same information +

You can also use the provided {@link #getFormattedString()} method to get the same information in String-format. Don't use Adventure's toString methods on the Components - those are for debugging purposes. And finally, if you want the results to be formatted differently, you can get an instance of the {@link Formatter}. @@ -59,5 +59,5 @@ public interface StatResult { the same style and color settings that are specified in the PlayerStats config, but it is not translatable (it is always plain English). See class description for more information.*/ - String toString(); + String getFormattedString(); } \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/TopStatResult.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/TopStatResult.java index 8046a4b..d5acf7d 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/TopStatResult.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/result/TopStatResult.java @@ -18,7 +18,7 @@ public record TopStatResult(LinkedHashMap value, TextComponent } @Override - public String toString() { + public String getFormattedString() { return ComponentUtils.getTranslatableComponentSerializer() .serialize(formattedValue); }