From 56dc30830aa648e6669807c23a753973eec3f756 Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Wed, 5 Oct 2022 17:24:51 +0200 Subject: [PATCH] Started a rewrite to make the request-business more clear and better organized --- .../the/gr8/playerstats/ThreadManager.java | 5 +- .../gr8/playerstats/api/PlayerStatsAPI.java | 9 +- .../playerstats/msg/InternalFormatter.java | 7 +- .../gr8/playerstats/msg/MessageBuilder.java | 8 +- .../gr8/playerstats/msg/OutputManager.java | 21 +- .../gr8/playerstats/statistic/StatAction.java | 6 +- .../playerstats/statistic/StatCalculator.java | 12 +- .../gr8/playerstats/statistic/StatThread.java | 5 +- .../statistic/request/PlayerStatRequest.java | 35 +-- .../statistic/request/ServerStatRequest.java | 35 +-- .../statistic/request/StatRequest.java | 238 ++++++++++++++---- .../statistic/request/TopStatRequest.java | 35 +-- 12 files changed, 282 insertions(+), 134 deletions(-) diff --git a/src/main/java/com/artemis/the/gr8/playerstats/ThreadManager.java b/src/main/java/com/artemis/the/gr8/playerstats/ThreadManager.java index 8561be0..6dbd29d 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/ThreadManager.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/ThreadManager.java @@ -7,6 +7,7 @@ import com.artemis.the.gr8.playerstats.statistic.request.RequestSettings; import com.artemis.the.gr8.playerstats.reload.ReloadThread; import com.artemis.the.gr8.playerstats.statistic.StatCalculator; import com.artemis.the.gr8.playerstats.statistic.StatThread; +import com.artemis.the.gr8.playerstats.statistic.request.StatRequest; import com.artemis.the.gr8.playerstats.utils.MyLogger; import org.bukkit.command.CommandSender; @@ -63,7 +64,7 @@ public final class ThreadManager { } } - public void startStatThread(RequestSettings requestSettings) { + public void startStatThread(StatRequest.Settings requestSettings) { statThreadID += 1; String cmdSender = requestSettings.getCommandSender().getName(); @@ -95,7 +96,7 @@ public final class ThreadManager { return lastRecordedCalcTime; } - private void startNewStatThread(RequestSettings requestSettings) { + private void startNewStatThread(StatRequest.Settings requestSettings) { lastActiveStatThread = new StatThread(outputManager, statCalculator, statThreadID, requestSettings, lastActiveReloadThread); statThreads.put(requestSettings.getCommandSender().getName(), lastActiveStatThread); lastActiveStatThread.start(); diff --git a/src/main/java/com/artemis/the/gr8/playerstats/api/PlayerStatsAPI.java b/src/main/java/com/artemis/the/gr8/playerstats/api/PlayerStatsAPI.java index 9fb62a4..b6a7636 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/api/PlayerStatsAPI.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/api/PlayerStatsAPI.java @@ -29,20 +29,17 @@ public final class PlayerStatsAPI implements PlayerStats, StatManager { @Override public PlayerStatRequest playerStatRequest(String playerName) { - RequestSettings request = RequestHandler.getBasicPlayerStatRequest(playerName); - return new PlayerStatRequest(request); + return new PlayerStatRequest(playerName); } @Override public ServerStatRequest serverStatRequest() { - RequestSettings request = RequestHandler.getBasicServerStatRequest(); - return new ServerStatRequest(request); + return new ServerStatRequest(); } @Override public TopStatRequest topStatRequest(int topListSize) { - RequestSettings request = RequestHandler.getBasicTopStatRequest(topListSize); - return new TopStatRequest(request); + return new TopStatRequest(topListSize); } @Override diff --git a/src/main/java/com/artemis/the/gr8/playerstats/msg/InternalFormatter.java b/src/main/java/com/artemis/the/gr8/playerstats/msg/InternalFormatter.java index 4041c7c..d63c8aa 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/msg/InternalFormatter.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/msg/InternalFormatter.java @@ -2,6 +2,7 @@ package com.artemis.the.gr8.playerstats.msg; import com.artemis.the.gr8.playerstats.statistic.request.RequestSettings; import com.artemis.the.gr8.playerstats.statistic.StatCalculator; +import com.artemis.the.gr8.playerstats.statistic.request.StatRequest; import net.kyori.adventure.text.*; import org.jetbrains.annotations.ApiStatus.Internal; @@ -19,12 +20,12 @@ public interface InternalFormatter { /** @return a TextComponent with the following parts: *
[player-name]: [number] [stat-name] {sub-stat-name} */ - TextComponent formatAndSavePlayerStat(RequestSettings requestSettings, int playerStat); + TextComponent formatAndSavePlayerStat(StatRequest.Settings requestSettings, int playerStat); /** @return a TextComponent with the following parts: *
[Total on] [server-name]: [number] [stat-name] [sub-stat-name] */ - TextComponent formatAndSaveServerStat(RequestSettings requestSettings, long serverStat); + TextComponent formatAndSaveServerStat(StatRequest.Settings requestSettings, long serverStat); /** @return a TextComponent with the following parts: *
[PlayerStats] [Top 10] [stat-name] [sub-stat-name] @@ -32,5 +33,5 @@ public interface InternalFormatter { *
[2.] [player-name] [number] *
[3.] etc... */ - TextComponent formatAndSaveTopStat(RequestSettings requestSettings, LinkedHashMap topStats); + TextComponent formatAndSaveTopStat(StatRequest.Settings requestSettings, LinkedHashMap topStats); } \ No newline at end of file diff --git a/src/main/java/com/artemis/the/gr8/playerstats/msg/MessageBuilder.java b/src/main/java/com/artemis/the/gr8/playerstats/msg/MessageBuilder.java index 8d31819..5923ff1 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/msg/MessageBuilder.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/msg/MessageBuilder.java @@ -8,13 +8,13 @@ import com.artemis.the.gr8.playerstats.msg.components.HelpMessage; import com.artemis.the.gr8.playerstats.msg.components.BukkitConsoleComponentFactory; import com.artemis.the.gr8.playerstats.msg.components.PrideComponentFactory; import com.artemis.the.gr8.playerstats.msg.msgutils.*; +import com.artemis.the.gr8.playerstats.statistic.request.StatRequest; import com.artemis.the.gr8.playerstats.utils.EnumHandler; import com.artemis.the.gr8.playerstats.utils.MyLogger; import com.artemis.the.gr8.playerstats.enums.Target; import com.artemis.the.gr8.playerstats.config.ConfigHandler; import com.artemis.the.gr8.playerstats.enums.Unit; -import com.artemis.the.gr8.playerstats.statistic.request.RequestSettings; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; import org.bukkit.Statistic; @@ -313,7 +313,7 @@ public final class MessageBuilder implements ApiFormatter { *
- If both parameters are null, the formattedComponent will be returned * as is. */ - public @NotNull BiFunction formattedPlayerStatFunction(int stat, @NotNull RequestSettings request) { + public @NotNull BiFunction formattedPlayerStatFunction(int stat, @NotNull StatRequest.Settings request) { TextComponent playerStat = formatPlayerStat(request.getPlayerName(), stat, request.getStatistic(), request.getSubStatEntryName()); return getFormattingFunction(playerStat, Target.PLAYER); } @@ -329,7 +329,7 @@ public final class MessageBuilder implements ApiFormatter { *
- If both parameters are null, the formattedComponent will be returned * as is. */ - public @NotNull BiFunction formattedServerStatFunction(long stat, @NotNull RequestSettings request) { + public @NotNull BiFunction formattedServerStatFunction(long stat, @NotNull StatRequest.Settings request) { TextComponent serverStat = formatServerStat(stat, request.getStatistic(), request.getSubStatEntryName()); return getFormattingFunction(serverStat, Target.SERVER); } @@ -345,7 +345,7 @@ public final class MessageBuilder implements ApiFormatter { *
- If both parameters are null, the formattedComponent will be returned * as is. */ - public @NotNull BiFunction formattedTopStatFunction(@NotNull LinkedHashMap topStats, @NotNull RequestSettings request) { + public @NotNull BiFunction formattedTopStatFunction(@NotNull LinkedHashMap topStats, @NotNull StatRequest.Settings request) { final TextComponent title = getTopStatTitle(topStats.size(), request.getStatistic(), request.getSubStatEntryName()); final TextComponent list = getTopStatListComponent(topStats, request.getStatistic()); final boolean useEnters = config.useEnters(Target.TOP, false); diff --git a/src/main/java/com/artemis/the/gr8/playerstats/msg/OutputManager.java b/src/main/java/com/artemis/the/gr8/playerstats/msg/OutputManager.java index 10f54fd..22bbc95 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/msg/OutputManager.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/msg/OutputManager.java @@ -6,6 +6,7 @@ import com.artemis.the.gr8.playerstats.enums.StandardMessage; import com.artemis.the.gr8.playerstats.statistic.request.RequestSettings; import com.artemis.the.gr8.playerstats.msg.components.BukkitConsoleComponentFactory; import com.artemis.the.gr8.playerstats.msg.components.PrideComponentFactory; +import com.artemis.the.gr8.playerstats.statistic.request.StatRequest; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.text.TextComponent; import org.bukkit.Bukkit; @@ -55,25 +56,25 @@ public final class OutputManager implements InternalFormatter { } @Override - public TextComponent formatAndSavePlayerStat(@NotNull RequestSettings requestSettings, int playerStat) { + public TextComponent formatAndSavePlayerStat(@NotNull StatRequest.Settings requestSettings, int playerStat) { BiFunction playerStatFunction = - getMessageBuilder(requestSettings).formattedPlayerStatFunction(playerStat, requestSettings); + getMessageBuilder(requestSettings.getCommandSender()).formattedPlayerStatFunction(playerStat, requestSettings); return processFunction(requestSettings.getCommandSender(), playerStatFunction); } @Override - public TextComponent formatAndSaveServerStat(@NotNull RequestSettings requestSettings, long serverStat) { + public TextComponent formatAndSaveServerStat(@NotNull StatRequest.Settings requestSettings, long serverStat) { BiFunction serverStatFunction = - getMessageBuilder(requestSettings).formattedServerStatFunction(serverStat, requestSettings); + getMessageBuilder(requestSettings.getCommandSender()).formattedServerStatFunction(serverStat, requestSettings); return processFunction(requestSettings.getCommandSender(), serverStatFunction); } @Override - public TextComponent formatAndSaveTopStat(@NotNull RequestSettings requestSettings, @NotNull LinkedHashMap topStats) { + public TextComponent formatAndSaveTopStat(@NotNull StatRequest.Settings requestSettings, @NotNull LinkedHashMap topStats) { BiFunction topStatFunction = - getMessageBuilder(requestSettings).formattedTopStatFunction(topStats, requestSettings); + getMessageBuilder(requestSettings.getCommandSender()).formattedTopStatFunction(topStats, requestSettings); return processFunction(requestSettings.getCommandSender(), topStatFunction); } @@ -141,14 +142,6 @@ public final class OutputManager implements InternalFormatter { return sender instanceof ConsoleCommandSender ? consoleMessageBuilder : messageBuilder; } - private MessageBuilder getMessageBuilder(RequestSettings requestSettings) { - if (!requestSettings.isConsoleSender()) { - return messageBuilder; - } else { - return consoleMessageBuilder; - } - } - private static void getMessageBuilders() { messageBuilder = getClientMessageBuilder(); consoleMessageBuilder = getConsoleMessageBuilder(); diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatAction.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatAction.java index b11ce4f..7f1d598 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatAction.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatAction.java @@ -1,8 +1,8 @@ package com.artemis.the.gr8.playerstats.statistic; import com.artemis.the.gr8.playerstats.ThreadManager; +import com.artemis.the.gr8.playerstats.statistic.request.StatRequest; import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; -import com.artemis.the.gr8.playerstats.statistic.request.RequestSettings; import com.artemis.the.gr8.playerstats.utils.MyLogger; import com.google.common.collect.ImmutableList; import org.bukkit.OfflinePlayer; @@ -20,7 +20,7 @@ final class StatAction extends RecursiveTask> private final OfflinePlayerHandler offlinePlayerHandler; private final ImmutableList playerNames; - private final RequestSettings requestSettings; + private final StatRequest.Settings requestSettings; private final ConcurrentHashMap allStats; /** @@ -34,7 +34,7 @@ final class StatAction extends RecursiveTask> * @param requestSettings a validated requestSettings object * @param allStats the ConcurrentHashMap to put the results on */ - public StatAction(OfflinePlayerHandler offlinePlayerHandler, ImmutableList playerNames, RequestSettings requestSettings, ConcurrentHashMap allStats) { + public StatAction(OfflinePlayerHandler offlinePlayerHandler, ImmutableList playerNames, StatRequest.Settings requestSettings, ConcurrentHashMap allStats) { threshold = ThreadManager.getTaskThreshold(); this.offlinePlayerHandler = offlinePlayerHandler; diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatCalculator.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatCalculator.java index 78b4573..573df7d 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatCalculator.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatCalculator.java @@ -1,8 +1,8 @@ package com.artemis.the.gr8.playerstats.statistic; import com.artemis.the.gr8.playerstats.ThreadManager; +import com.artemis.the.gr8.playerstats.statistic.request.StatRequest; import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; -import com.artemis.the.gr8.playerstats.statistic.request.RequestSettings; import com.artemis.the.gr8.playerstats.utils.MyLogger; import com.google.common.collect.ImmutableList; import org.bukkit.OfflinePlayer; @@ -21,7 +21,7 @@ public final class StatCalculator { this.offlinePlayerHandler = offlinePlayerHandler; } - public int getPlayerStat(RequestSettings requestSettings) { + public int getPlayerStat(StatRequest.Settings requestSettings) { OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(requestSettings.getPlayerName()); return switch (requestSettings.getStatistic().getType()) { case UNTYPED -> player.getStatistic(requestSettings.getStatistic()); @@ -31,14 +31,14 @@ public final class StatCalculator { }; } - public LinkedHashMap getTopStats(RequestSettings requestSettings) { + public LinkedHashMap getTopStats(StatRequest.Settings requestSettings) { return getAllStatsAsync(requestSettings).entrySet().stream() .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) .limit(requestSettings.getTopListSize()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); } - public long getServerStat(RequestSettings requestSettings) { + public long getServerStat(StatRequest.Settings requestSettings) { List numbers = getAllStatsAsync(requestSettings) .values() .parallelStream() @@ -50,7 +50,7 @@ public final class StatCalculator { * Invokes a bunch of worker pool threads to get the statistics for * all players that are stored in the {@link OfflinePlayerHandler}). */ - private @NotNull ConcurrentHashMap getAllStatsAsync(RequestSettings requestSettings) { + private @NotNull ConcurrentHashMap getAllStatsAsync(StatRequest.Settings requestSettings) { long time = System.currentTimeMillis(); ForkJoinPool commonPool = ForkJoinPool.commonPool(); @@ -72,7 +72,7 @@ public final class StatCalculator { return allStats; } - private StatAction getStatTask(RequestSettings requestSettings) { + private StatAction getStatTask(StatRequest.Settings requestSettings) { int size = offlinePlayerHandler.getOfflinePlayerCount() != 0 ? offlinePlayerHandler.getOfflinePlayerCount() : 16; ConcurrentHashMap allStats = new ConcurrentHashMap<>(size); ImmutableList playerNames = ImmutableList.copyOf(offlinePlayerHandler.getOfflinePlayerNames()); diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatThread.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatThread.java index bb6f999..aad139b 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatThread.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatThread.java @@ -2,6 +2,7 @@ package com.artemis.the.gr8.playerstats.statistic; import com.artemis.the.gr8.playerstats.ThreadManager; import com.artemis.the.gr8.playerstats.msg.OutputManager; +import com.artemis.the.gr8.playerstats.statistic.request.StatRequest; import com.artemis.the.gr8.playerstats.utils.MyLogger; import com.artemis.the.gr8.playerstats.enums.StandardMessage; import com.artemis.the.gr8.playerstats.enums.Target; @@ -21,9 +22,9 @@ public final class StatThread extends Thread { private static StatCalculator statCalculator; private final ReloadThread reloadThread; - private final RequestSettings requestSettings; + private final StatRequest.Settings requestSettings; - public StatThread(OutputManager m, StatCalculator t, int ID, RequestSettings s, @Nullable ReloadThread r) { + public StatThread(OutputManager m, StatCalculator t, int ID, StatRequest.Settings s, @Nullable ReloadThread r) { outputManager = m; statCalculator = t; diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/PlayerStatRequest.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/PlayerStatRequest.java index 45739da..d84ffd8 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/PlayerStatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/PlayerStatRequest.java @@ -5,51 +5,52 @@ import com.artemis.the.gr8.playerstats.statistic.result.PlayerStatResult; import com.artemis.the.gr8.playerstats.api.RequestGenerator; import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils; import net.kyori.adventure.text.TextComponent; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Statistic; +import org.bukkit.command.CommandSender; import org.bukkit.entity.EntityType; import org.jetbrains.annotations.NotNull; public final class PlayerStatRequest extends StatRequest implements RequestGenerator { - private final RequestHandler requestHandler; + public PlayerStatRequest(String playerName) { + this(Bukkit.getConsoleSender(), playerName); + } - public PlayerStatRequest(RequestSettings request) { - super(request); - requestHandler = new RequestHandler(request); + public PlayerStatRequest(CommandSender requester, String playerName) { + super(requester); + super.settings.configureForPlayer(playerName); } @Override - public PlayerStatRequest untyped(@NotNull Statistic statistic) { - RequestSettings completedRequest = requestHandler.untyped(statistic); - return new PlayerStatRequest(completedRequest); + public StatRequest untyped(@NotNull Statistic statistic) { + return super.configureUntyped(statistic); } @Override - public PlayerStatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) { - RequestSettings completedRequest = requestHandler.blockOrItemType(statistic, material); - return new PlayerStatRequest(completedRequest); + public StatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) { + return super.configureBlockOrItemType(statistic, material); } @Override - public PlayerStatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) { - RequestSettings completedRequest = requestHandler.entityType(statistic, entityType); - return new PlayerStatRequest(completedRequest); + public StatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) { + return super.configureEntityType(statistic, entityType); } @Override public PlayerStatResult execute() { - return getStatResult(super.requestSettings); + return getStatResult(); } - private PlayerStatResult getStatResult(RequestSettings completedRequest) { + private PlayerStatResult getStatResult() { int stat = Main .getStatCalculator() - .getPlayerStat(completedRequest); + .getPlayerStat(settings); TextComponent prettyComponent = Main .getOutputManager() - .formatAndSavePlayerStat(completedRequest, stat); + .formatAndSavePlayerStat(settings, stat); String prettyString = ComponentUtils .getTranslatableComponentSerializer() diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/ServerStatRequest.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/ServerStatRequest.java index 15521a3..f4b19fb 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/ServerStatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/ServerStatRequest.java @@ -5,51 +5,52 @@ import com.artemis.the.gr8.playerstats.statistic.result.ServerStatResult; import com.artemis.the.gr8.playerstats.api.RequestGenerator; import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils; import net.kyori.adventure.text.TextComponent; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Statistic; +import org.bukkit.command.CommandSender; import org.bukkit.entity.EntityType; import org.jetbrains.annotations.NotNull; public final class ServerStatRequest extends StatRequest implements RequestGenerator { - private final RequestHandler requestHandler; + public ServerStatRequest() { + this(Bukkit.getConsoleSender()); + } - public ServerStatRequest(RequestSettings request) { - super(request); - requestHandler = new RequestHandler(requestSettings); + public ServerStatRequest(CommandSender requester) { + super(requester); + super.settings.configureForServer(); } @Override - public ServerStatRequest untyped(@NotNull Statistic statistic) { - RequestSettings completedRequest = requestHandler.untyped(statistic); - return new ServerStatRequest(completedRequest); + public StatRequest untyped(@NotNull Statistic statistic) { + return super.configureUntyped(statistic); } @Override - public ServerStatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) { - RequestSettings completedRequest = requestHandler.blockOrItemType(statistic, material); - return new ServerStatRequest(completedRequest); + public StatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) { + return super.configureBlockOrItemType(statistic, material); } @Override - public ServerStatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) { - RequestSettings completedRequest = requestHandler.entityType(statistic, entityType); - return new ServerStatRequest(completedRequest); + public StatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) { + return super.configureEntityType(statistic, entityType); } @Override public ServerStatResult execute() { - return getStatResult(requestSettings); + return getStatResult(); } - private ServerStatResult getStatResult(RequestSettings completedRequest) { + private ServerStatResult getStatResult() { long stat = Main .getStatCalculator() - .getServerStat(completedRequest); + .getServerStat(settings); TextComponent prettyComponent = Main .getOutputManager() - .formatAndSaveServerStat(completedRequest, stat); + .formatAndSaveServerStat(settings, stat); String prettyString = ComponentUtils .getTranslatableComponentSerializer() diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/StatRequest.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/StatRequest.java index c5567e2..dfd656a 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/StatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/StatRequest.java @@ -1,11 +1,15 @@ package com.artemis.the.gr8.playerstats.statistic.request; +import com.artemis.the.gr8.playerstats.Main; import com.artemis.the.gr8.playerstats.api.PlayerStats; import com.artemis.the.gr8.playerstats.statistic.result.StatResult; import com.artemis.the.gr8.playerstats.enums.Target; 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; /** @@ -18,10 +22,44 @@ import org.jetbrains.annotations.Nullable; */ public abstract class StatRequest { - protected final RequestSettings requestSettings; + protected final Settings settings; - protected StatRequest(RequestSettings request) { - requestSettings = request; + protected StatRequest(CommandSender requester) { + settings = new Settings(requester); + } + + protected StatRequest configureUntyped(@NotNull Statistic statistic) { + if (statistic.getType() == Statistic.Type.UNTYPED) { + settings.setStatistic(statistic); + return this; + } + throw new IllegalArgumentException("This statistic is not of Type.Untyped"); + } + + protected StatRequest configureBlockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException { + Statistic.Type type = statistic.getType(); + if (type == Statistic.Type.BLOCK && material.isBlock()) { + settings.setBlock(material); + } + else if (type == Statistic.Type.ITEM && material.isItem()){ + settings.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"); + } + settings.setStatistic(statistic); + settings.setSubStatEntryName(material.toString()); + return this; + } + + protected StatRequest configureEntityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException { + if (statistic.getType() == Statistic.Type.ENTITY) { + settings.setStatistic(statistic); + settings.setSubStatEntryName(entityType.toString()); + settings.setEntity(entityType); + return this; + } + throw new IllegalArgumentException("This statistic is not of Type.Entity"); } /** @@ -35,53 +73,167 @@ public abstract class StatRequest { */ public abstract StatResult execute(); - /** - * Gets the Statistic that calling {@link #execute()} will calculate - * the data for. - * @return the Statistic - */ - public Statistic getStatisticSetting() { - return requestSettings.getStatistic(); + public boolean isValid() { + if (settings.statistic == null) { + return false; + } else if (settings.target == Target.PLAYER && settings.playerName == null) { + return false; + } else if (settings.statistic.getType() != Statistic.Type.UNTYPED && + settings.subStatEntryName == null) { + return false; + } else { + return hasMatchingSubStat(); + } + } + + private boolean hasMatchingSubStat() { + switch (settings.statistic.getType()) { + case BLOCK -> { + return settings.block != null; + } + case ENTITY -> { + return settings.entity != null; + } + case ITEM -> { + return settings.item != null; + } + default -> { + return true; + } + } } /** - * If the Statistic setting for this StatRequest is of Type.Block, - * this will get the Material that was set. - * - * @return a Material for which #isBlock is true, or null if no - * Material was set + * Use this method to view the settings that have + * been configured for this StatRequest. */ - public @Nullable Material getBlockSetting() { - return requestSettings.getBlock(); + public Settings getSettings() { + return settings; } - /** - * If the Statistic setting for this StatRequest is of Type.Item, - * this will get the Material that was set. - * - * @return a Material for which #isItem is true, or null if no - * Material was set - */ - public @Nullable Material getItemSetting() { - return requestSettings.getItem(); - } + public static final class Settings { + private final CommandSender sender; + private Statistic statistic; + private String playerName; + private Target target; + private int topListSize; - /** - * If the Statistic setting for this StatRequest is of Type.Entity, - * this will get the EntityType that was set. - * - * @return an EntityType, or null if no EntityType was set - */ - public @Nullable EntityType getEntitySetting() { - return requestSettings.getEntity(); - } + private String subStatEntryName; + private EntityType entity; + private Material block; + private Material item; + private boolean playerFlag; - /** - * Gets the Target that will be used when calling {@link #execute()}. - * - * @return the Target for this lookup (either Player, Server or Top) - */ - public Target getTargetSetting() { - return requestSettings.getTarget(); + /** + * Create a new {@link Settings} with default values: + *
- CommandSender sender (provided) + *
- Target target = {@link Target#TOP} + *
- int topListSize = 10 + *
- boolean playerFlag = false + * + * @param sender the CommandSender who prompted this RequestGenerator + */ + private Settings(@NotNull CommandSender sender) { + this.sender = sender; + target = Target.TOP; + playerFlag = false; + } + + void configureForPlayer(String playerName) { + setTarget(Target.PLAYER); + setPlayerName(playerName); + } + + void configureForServer() { + setTarget(Target.SERVER); + } + + void configureForTop(int topListSize) { + setTarget(Target.TOP); + + this.topListSize = topListSize != 0 ? + topListSize : + Main.getConfigHandler().getTopListMaxSize(); + } + + public @NotNull CommandSender getCommandSender() { + return sender; + } + + public boolean isConsoleSender() { + return sender instanceof ConsoleCommandSender; + } + + void setStatistic(Statistic statistic) { + this.statistic = statistic; + } + + public Statistic getStatistic() { + return statistic; + } + + private void setSubStatEntryName(String subStatEntry) { + this.subStatEntryName = subStatEntry; + } + + public @Nullable String getSubStatEntryName() { + return subStatEntryName; + } + + void setPlayerName(String playerName) { + this.playerName = playerName; + } + + public String getPlayerName() { + return playerName; + } + + void setPlayerFlag(boolean playerFlag) { + this.playerFlag = playerFlag; + } + + public boolean getPlayerFlag() { + return playerFlag; + } + + void setTarget(@NotNull Target target) { + this.target = target; + } + + public @NotNull Target getTarget() { + return target; + } + + void setTopListSize(int topListSize) { + this.topListSize = topListSize; + } + + public int getTopListSize() { + return this.topListSize; + } + + void setEntity(EntityType entity) { + this.entity = entity; + } + + public EntityType getEntity() { + return entity; + } + + void setBlock(Material block) { + this.block = block; + } + + public Material getBlock() { + return block; + } + + void setItem(Material item) { + this.item = item; + } + + public Material getItem() { + return item; + } } } \ No newline at end of file diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/TopStatRequest.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/TopStatRequest.java index 04c8f25..64bf073 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/TopStatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/TopStatRequest.java @@ -5,8 +5,10 @@ import com.artemis.the.gr8.playerstats.statistic.result.TopStatResult; import com.artemis.the.gr8.playerstats.api.RequestGenerator; import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils; import net.kyori.adventure.text.TextComponent; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Statistic; +import org.bukkit.command.CommandSender; import org.bukkit.entity.EntityType; import org.jetbrains.annotations.NotNull; @@ -14,44 +16,43 @@ import java.util.LinkedHashMap; public final class TopStatRequest extends StatRequest> implements RequestGenerator> { - private final RequestHandler requestHandler; + public TopStatRequest(int topListSize) { + this(Bukkit.getConsoleSender(), topListSize); + } - public TopStatRequest(RequestSettings request) { - super(request); - requestHandler = new RequestHandler(request); + public TopStatRequest(CommandSender requester, int topListSize) { + super(requester); + super.settings.configureForTop(topListSize); } @Override - public TopStatRequest untyped(@NotNull Statistic statistic) { - RequestSettings completedRequest = requestHandler.untyped(statistic); - return new TopStatRequest(completedRequest); + public StatRequest> untyped(@NotNull Statistic statistic) { + return super.configureUntyped(statistic); } @Override - public TopStatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) { - RequestSettings completedRequest = requestHandler.blockOrItemType(statistic, material); - return new TopStatRequest(completedRequest); + public StatRequest> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) { + return super.configureBlockOrItemType(statistic, material); } @Override - public TopStatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) { - RequestSettings completedRequest = requestHandler.entityType(statistic, entityType); - return new TopStatRequest(completedRequest); + public StatRequest> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) { + return super.configureEntityType(statistic, entityType); } @Override public TopStatResult execute() { - return getStatResult(super.requestSettings); + return getStatResult(); } - private TopStatResult getStatResult(RequestSettings completedRequest) { + private TopStatResult getStatResult() { LinkedHashMap stat = Main .getStatCalculator() - .getTopStats(completedRequest); + .getTopStats(settings); TextComponent prettyComponent = Main .getOutputManager() - .formatAndSaveTopStat(completedRequest, stat); + .formatAndSaveTopStat(settings, stat); String prettyString = ComponentUtils .getTranslatableComponentSerializer()