diff --git a/src/main/java/com/artemis/the/gr8/playerstats/Main.java b/src/main/java/com/artemis/the/gr8/playerstats/Main.java index 922a756..e1d6391 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/Main.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/Main.java @@ -10,6 +10,7 @@ import com.artemis.the.gr8.playerstats.commands.TabCompleter; import com.artemis.the.gr8.playerstats.config.ConfigHandler; import com.artemis.the.gr8.playerstats.listeners.JoinListener; import com.artemis.the.gr8.playerstats.msg.msgutils.LanguageKeyHandler; +import com.artemis.the.gr8.playerstats.share.ShareManager; import com.artemis.the.gr8.playerstats.statistic.RequestProcessor; import com.artemis.the.gr8.playerstats.utils.EnumHandler; import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; @@ -43,7 +44,7 @@ public final class Main extends JavaPlugin { private static ShareManager shareManager; private static RequestProcessor requestProcessor; - private static PlayerStats playerStatsAPI; + private static PlayerStats playerStatsImpl; @Override @@ -103,10 +104,10 @@ public final class Main extends JavaPlugin { } public static @NotNull PlayerStats getPlayerStatsAPI() throws IllegalStateException { - if (playerStatsAPI == null) { + if (playerStatsImpl == null) { throw new IllegalStateException("PlayerStats does not seem to be loaded!"); } - return playerStatsAPI; + return playerStatsImpl; } public static @NotNull RequestProcessor getRequestProcessor() throws IllegalStateException { @@ -156,16 +157,15 @@ public final class Main extends JavaPlugin { adventure = BukkitAudiences.create(this); enumHandler = new EnumHandler(); languageKeyHandler = new LanguageKeyHandler(); - config = new ConfigHandler(); + offlinePlayerHandler = new OfflinePlayerHandler(config); shareManager = new ShareManager(config); + outputManager = new OutputManager(adventure, config); + requestProcessor = new RequestProcessor(offlinePlayerHandler, outputManager, shareManager); - outputManager = new OutputManager(adventure, config, shareManager); - requestProcessor = new RequestProcessor(offlinePlayerHandler, outputManager); - threadManager = new ThreadManager(config, requestProcessor, outputManager); - - playerStatsAPI = new PlayerStatsImpl(outputManager, offlinePlayerHandler); + threadManager = new ThreadManager(config, outputManager); + playerStatsImpl = new PlayerStatsImpl(offlinePlayerHandler, outputManager); } private void setupMetrics() { 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 81e6bf4..72ab480 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/ThreadManager.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/ThreadManager.java @@ -4,9 +4,8 @@ import com.artemis.the.gr8.playerstats.msg.OutputManager; import com.artemis.the.gr8.playerstats.config.ConfigHandler; import com.artemis.the.gr8.playerstats.enums.StandardMessage; import com.artemis.the.gr8.playerstats.reload.ReloadThread; -import com.artemis.the.gr8.playerstats.statistic.RequestProcessor; import com.artemis.the.gr8.playerstats.statistic.StatThread; -import com.artemis.the.gr8.playerstats.statistic.request.StatRequest; +import com.artemis.the.gr8.playerstats.statistic.StatRequest; import com.artemis.the.gr8.playerstats.utils.MyLogger; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; @@ -30,17 +29,15 @@ public final class ThreadManager { private static ConfigHandler config; private static OutputManager outputManager; - private static RequestProcessor requestProcessor; private ReloadThread activatedReloadThread; private StatThread activatedStatThread; private final HashMap statThreads; private static long lastRecordedCalcTime; - public ThreadManager(ConfigHandler config, RequestProcessor requestProcessor, OutputManager outputManager) { + public ThreadManager(ConfigHandler config, OutputManager outputManager) { ThreadManager.config = config; ThreadManager.outputManager = outputManager; - ThreadManager.requestProcessor = requestProcessor; statThreads = new HashMap<>(); statThreadID = 0; @@ -98,7 +95,7 @@ public final class ThreadManager { } private void startNewStatThread(StatRequest request) { - activatedStatThread = new StatThread(outputManager, requestProcessor, statThreadID, request, activatedReloadThread); + activatedStatThread = new StatThread(outputManager, statThreadID, request, activatedReloadThread); statThreads.put(request.getSettings().getCommandSender().getName(), activatedStatThread); activatedStatThread.start(); } diff --git a/src/main/java/com/artemis/the/gr8/playerstats/api/PlayerStats.java b/src/main/java/com/artemis/the/gr8/playerstats/api/PlayerStats.java index 813e7d1..bf6e8e0 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/api/PlayerStats.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/api/PlayerStats.java @@ -1,7 +1,7 @@ package com.artemis.the.gr8.playerstats.api; import com.artemis.the.gr8.playerstats.Main; -import com.artemis.the.gr8.playerstats.statistic.request.StatRequest; +import com.artemis.the.gr8.playerstats.statistic.StatRequest; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; diff --git a/src/main/java/com/artemis/the/gr8/playerstats/api/PlayerStatsImpl.java b/src/main/java/com/artemis/the/gr8/playerstats/api/PlayerStatsImpl.java index ed09ad8..80a0ad2 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/api/PlayerStatsImpl.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/api/PlayerStatsImpl.java @@ -1,7 +1,9 @@ package com.artemis.the.gr8.playerstats.api; import com.artemis.the.gr8.playerstats.msg.OutputManager; -import com.artemis.the.gr8.playerstats.statistic.request.*; +import com.artemis.the.gr8.playerstats.statistic.PlayerStatRequest; +import com.artemis.the.gr8.playerstats.statistic.ServerStatRequest; +import com.artemis.the.gr8.playerstats.statistic.TopStatRequest; import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; import java.util.LinkedHashMap; @@ -11,13 +13,13 @@ import static org.jetbrains.annotations.ApiStatus.Internal; /** The implementation of the API Interface */ public final class PlayerStatsImpl implements PlayerStats, StatManager { - private final OfflinePlayerHandler offlinePlayerHandler; private static OutputManager outputManager; + private final OfflinePlayerHandler offlinePlayerHandler; @Internal - public PlayerStatsImpl(OutputManager outputManager, OfflinePlayerHandler offlinePlayers) { + public PlayerStatsImpl(OfflinePlayerHandler offlinePlayerHandler, OutputManager outputManager) { PlayerStatsImpl.outputManager = outputManager; - offlinePlayerHandler = offlinePlayers; + this.offlinePlayerHandler = offlinePlayerHandler; } @Override diff --git a/src/main/java/com/artemis/the/gr8/playerstats/api/RequestGenerator.java b/src/main/java/com/artemis/the/gr8/playerstats/api/RequestGenerator.java index 7072e12..a0eb419 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/api/RequestGenerator.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/api/RequestGenerator.java @@ -1,7 +1,7 @@ package com.artemis.the.gr8.playerstats.api; import com.artemis.the.gr8.playerstats.statistic.RequestProcessor; -import com.artemis.the.gr8.playerstats.statistic.request.StatRequest; +import com.artemis.the.gr8.playerstats.statistic.StatRequest; import org.bukkit.Material; import org.bukkit.Statistic; import org.bukkit.entity.EntityType; diff --git a/src/main/java/com/artemis/the/gr8/playerstats/api/StatFormatter.java b/src/main/java/com/artemis/the/gr8/playerstats/api/StatFormatter.java index f8f5e5a..a99a6a4 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/api/StatFormatter.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/api/StatFormatter.java @@ -3,7 +3,7 @@ package com.artemis.the.gr8.playerstats.api; import com.artemis.the.gr8.playerstats.enums.Unit; import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils; import com.artemis.the.gr8.playerstats.msg.msgutils.NumberFormatter; -import com.artemis.the.gr8.playerstats.statistic.result.StatResult; +import com.artemis.the.gr8.playerstats.statistic.StatResult; import net.kyori.adventure.text.TextComponent; import org.bukkit.Statistic; import org.jetbrains.annotations.Nullable; diff --git a/src/main/java/com/artemis/the/gr8/playerstats/api/StatManager.java b/src/main/java/com/artemis/the/gr8/playerstats/api/StatManager.java index cc1f7cf..52f64b8 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/api/StatManager.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/api/StatManager.java @@ -1,6 +1,6 @@ package com.artemis.the.gr8.playerstats.api; -import com.artemis.the.gr8.playerstats.statistic.request.StatRequest; +import com.artemis.the.gr8.playerstats.statistic.StatRequest; import java.util.LinkedHashMap; diff --git a/src/main/java/com/artemis/the/gr8/playerstats/commands/ShareCommand.java b/src/main/java/com/artemis/the/gr8/playerstats/commands/ShareCommand.java index f3961c4..4285f4f 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/commands/ShareCommand.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/commands/ShareCommand.java @@ -1,9 +1,9 @@ package com.artemis.the.gr8.playerstats.commands; -import com.artemis.the.gr8.playerstats.ShareManager; +import com.artemis.the.gr8.playerstats.share.ShareManager; import com.artemis.the.gr8.playerstats.enums.StandardMessage; import com.artemis.the.gr8.playerstats.msg.OutputManager; -import com.artemis.the.gr8.playerstats.statistic.result.InternalStatResult; +import com.artemis.the.gr8.playerstats.share.StoredResult; import com.artemis.the.gr8.playerstats.utils.MyLogger; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -37,7 +37,7 @@ public final class ShareCommand implements CommandExecutor { outputManager.sendFeedbackMsg(sender, StandardMessage.STILL_ON_SHARE_COOLDOWN); } else { - InternalStatResult result = shareManager.getStatResult(sender.getName(), shareCode); + StoredResult result = shareManager.getStatResult(sender.getName(), shareCode); if (result == null) { //at this point the only possible cause of formattedComponent being null is the request being older than 25 player-requests ago outputManager.sendFeedbackMsg(sender, StandardMessage.STAT_RESULTS_TOO_OLD); } else { diff --git a/src/main/java/com/artemis/the/gr8/playerstats/commands/StatCommand.java b/src/main/java/com/artemis/the/gr8/playerstats/commands/StatCommand.java index 877de24..f3b62b1 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/commands/StatCommand.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/commands/StatCommand.java @@ -4,7 +4,8 @@ import com.artemis.the.gr8.playerstats.ThreadManager; import com.artemis.the.gr8.playerstats.enums.StandardMessage; import com.artemis.the.gr8.playerstats.enums.Target; import com.artemis.the.gr8.playerstats.msg.OutputManager; -import com.artemis.the.gr8.playerstats.statistic.request.*; +import com.artemis.the.gr8.playerstats.statistic.InternalStatRequest; +import com.artemis.the.gr8.playerstats.statistic.StatRequest; import net.kyori.adventure.text.TextComponent; import org.bukkit.Statistic; import org.bukkit.command.Command; diff --git a/src/main/java/com/artemis/the/gr8/playerstats/commands/TabCompleter.java b/src/main/java/com/artemis/the/gr8/playerstats/commands/TabCompleter.java index 6a8238b..68819f7 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/commands/TabCompleter.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/commands/TabCompleter.java @@ -1,14 +1,18 @@ package com.artemis.the.gr8.playerstats.commands; +import com.artemis.the.gr8.playerstats.api.PlayerStats; +import com.artemis.the.gr8.playerstats.statistic.StatRequest; import com.artemis.the.gr8.playerstats.utils.EnumHandler; import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; -import com.artemis.the.gr8.playerstats.commands.cmdutils.TabCompleteHelper; +import org.bukkit.Material; import org.bukkit.Statistic; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import org.bukkit.entity.EntityType; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -16,100 +20,137 @@ public final class TabCompleter implements org.bukkit.command.TabCompleter { private final EnumHandler enumHandler; private final OfflinePlayerHandler offlinePlayerHandler; - private final TabCompleteHelper tabCompleteHelper; - private final List commandOptions; + private List targetSuggestions; + private List itemBrokenSuggestions; + private List entitySuggestions; public TabCompleter(EnumHandler enumHandler, OfflinePlayerHandler offlinePlayerHandler) { this.enumHandler = enumHandler; this.offlinePlayerHandler = offlinePlayerHandler; - tabCompleteHelper = new TabCompleteHelper(enumHandler); - - commandOptions = new ArrayList<>(); - commandOptions.add("top"); - commandOptions.add("player"); - commandOptions.add("server"); - commandOptions.add("me"); + prepareLists(); } - //args[0] = statistic (length = 1) - //args[1] = commandOption (top/player/me) OR substatistic (block/item/entitytype) (length = 2) - //args[2] = executorName OR commandOption (top/player/me) (length = 3) - //args[3] = executorName (length = 4) + //args[0] = statistic (length = 1) + //args[1] = target (player/server/top) OR sub-stat (block/item/entity) (length = 2) + //args[2] = playerName OR target (player/server/top) (length = 3) + //args[3] = playerName (length = 4) @Override public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { - List tabSuggestions = new ArrayList<>(); - if (args.length >= 1) { - String currentArg = args[args.length -1]; + String currentArg = args[args.length-1]; - if (args.length == 1) { //after typing "stat", suggest a list of viable statistics - tabSuggestions = getFirstArgSuggestions(args[0]); + if (args.length == 1) { + return getFirstArgSuggestions(args[0]); } - else { //after checking if args[0] is a viable statistic, suggest substatistic OR commandOptions - String previousArg = args[args.length -2]; + //after checking if args[0] is a viable statistic, suggest sub-stat OR targets + String previousArg = args[args.length-2]; - if (enumHandler.isStatistic(previousArg)) { - Statistic stat = EnumHandler.getStatEnum(previousArg); - if (stat != null) { - tabSuggestions = getTabSuggestions(getRelevantList(stat), currentArg); - } - } - - //if previous arg = "player" - else if (previousArg.equalsIgnoreCase("player")) { - - if (args.length >= 3 && enumHandler.isEntityStatistic(args[args.length-3])) { - tabSuggestions = commandOptions; //if arg before "player" was entity-stat, suggest commandOptions - } - else { //otherwise "player" is target-flag: suggest playerNames - tabSuggestions = getTabSuggestions(offlinePlayerHandler.getOfflinePlayerNames(), currentArg); - } - } - - //after a substatistic, suggest commandOptions - else if (enumHandler.isSubStatEntry(previousArg)) { - tabSuggestions = commandOptions; + if (enumHandler.isStatistic(previousArg)) { + Statistic stat = EnumHandler.getStatEnum(previousArg); + if (stat != null) { + return getDynamicTabSuggestions(getAfterStatSuggestions(stat), currentArg); } } + else if (previousArg.equalsIgnoreCase("player")) { + + if (args.length >= 3 && enumHandler.isEntityStatistic(args[args.length-3])) { + return targetSuggestions; //if arg before "player" was entity-sub-stat, suggest targets + } + else { //otherwise "player" is the target: suggest playerNames + return getDynamicTabSuggestions(offlinePlayerHandler.getOfflinePlayerNames(), currentArg); + } + } + + //after a substatistic, suggest targets + else if (enumHandler.isSubStatEntry(previousArg)) { + return targetSuggestions; + } + } - return tabSuggestions; + return null; } private List getFirstArgSuggestions(String currentArg) { List suggestions = enumHandler.getStatNames(); suggestions.add("examples"); suggestions.add("help"); - return getTabSuggestions(suggestions, currentArg); + return getDynamicTabSuggestions(suggestions, currentArg); } - private List getTabSuggestions(List completeList, String currentArg) { + /** + * These tabSuggestions take into account that the commandSender + * will have been typing, so they are filtered for the letters + * that have already been typed. + */ + private List getDynamicTabSuggestions(@NotNull List completeList, String currentArg) { return completeList.stream() .filter(item -> item.toLowerCase().contains(currentArg.toLowerCase())) .collect(Collectors.toList()); } - private List getRelevantList(Statistic stat) { + private List getAfterStatSuggestions(@NotNull Statistic stat) { switch (stat.getType()) { case BLOCK -> { - return tabCompleteHelper.getAllBlockNames(); + return getAllBlockNames(); } case ITEM -> { if (stat == Statistic.BREAK_ITEM) { - return tabCompleteHelper.getItemBrokenSuggestions(); + return getItemBrokenSuggestions(); } else { - return tabCompleteHelper.getAllItemNames(); + return getAllItemNames(); } } case ENTITY -> { - return tabCompleteHelper.getEntitySuggestions(); + return getEntitySuggestions(); } default -> { - return commandOptions; + return targetSuggestions; } } } + + private List getAllItemNames() { + return enumHandler.getItemNames(); + } + + private List getItemBrokenSuggestions() { + return itemBrokenSuggestions; + } + + private List getAllBlockNames() { + return enumHandler.getBlockNames(); + } + + private List getEntitySuggestions() { + return entitySuggestions; + } + + private void prepareLists() { + targetSuggestions = new ArrayList<>(); + targetSuggestions.add("top"); + targetSuggestions.add("player"); + targetSuggestions.add("server"); + targetSuggestions.add("me"); + + //breaking an item means running its durability negative + itemBrokenSuggestions = Arrays.stream(Material.values()) + .parallel() + .filter(Material::isItem) + .filter(item -> item.getMaxDurability() != 0) + .map(Material::toString) + .map(String::toLowerCase) + .collect(Collectors.toList()); + + //the only statistics dealing with entities are killed_entity and entity_killed_by + entitySuggestions = Arrays.stream(EntityType.values()) + .parallel() + .filter(EntityType::isAlive) + .map(EntityType::toString) + .map(String::toLowerCase) + .collect(Collectors.toList()); + } } \ No newline at end of file diff --git a/src/main/java/com/artemis/the/gr8/playerstats/commands/cmdutils/TabCompleteHelper.java b/src/main/java/com/artemis/the/gr8/playerstats/commands/cmdutils/TabCompleteHelper.java deleted file mode 100644 index d9839cc..0000000 --- a/src/main/java/com/artemis/the/gr8/playerstats/commands/cmdutils/TabCompleteHelper.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.artemis.the.gr8.playerstats.commands.cmdutils; - -import com.artemis.the.gr8.playerstats.utils.EnumHandler; -import org.bukkit.Material; -import org.bukkit.entity.EntityType; - -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -public final class TabCompleteHelper { - - private final EnumHandler enumHandler; - private static List itemBrokenSuggestions; - private static List entitySuggestions; - - public TabCompleteHelper(EnumHandler enumHandler) { - this.enumHandler = enumHandler; - prepareLists(); - } - - public List getAllItemNames() { - return enumHandler.getItemNames(); - } - - public List getItemBrokenSuggestions() { - return itemBrokenSuggestions; - } - - public List getAllBlockNames() { - return enumHandler.getBlockNames(); - } - - public List getEntitySuggestions() { - return entitySuggestions; - } - - - private static void prepareLists() { - //breaking an item means running its durability negative - itemBrokenSuggestions = Arrays.stream(Material.values()) - .parallel() - .filter(Material::isItem) - .filter(item -> item.getMaxDurability() != 0) - .map(Material::toString) - .map(String::toLowerCase) - .collect(Collectors.toList()); - - //the only statistics dealing with entities are killed_entity and entity_killed_by - entitySuggestions = Arrays.stream(EntityType.values()) - .parallel() - .filter(EntityType::isAlive) - .map(EntityType::toString) - .map(String::toLowerCase) - .collect(Collectors.toList()); - } -} \ No newline at end of file diff --git a/src/main/java/com/artemis/the/gr8/playerstats/msg/FormattingFunction.java b/src/main/java/com/artemis/the/gr8/playerstats/msg/FormattingFunction.java new file mode 100644 index 0000000..5f8a0e4 --- /dev/null +++ b/src/main/java/com/artemis/the/gr8/playerstats/msg/FormattingFunction.java @@ -0,0 +1,31 @@ +package com.artemis.the.gr8.playerstats.msg; + +import net.kyori.adventure.text.TextComponent; +import org.bukkit.command.CommandSender; + +import java.util.function.BiFunction; + +public final class FormattingFunction { + + private final BiFunction formattingFunction; + + public FormattingFunction(BiFunction formattingFunction) { + this.formattingFunction = formattingFunction; + } + + public TextComponent getResultWithShareButton(Integer shareCode) { + return this.apply(shareCode, null); + } + + public TextComponent getResultWithSharerName(CommandSender sender) { + return this.apply(null, sender); + } + + public TextComponent getDefaultResult() { + return this.apply(null, null); + } + + private TextComponent apply(Integer shareCode, CommandSender sender) { + return formattingFunction.apply(shareCode, sender); + } +} 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 c61b385..20b0fba 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,7 +8,7 @@ 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.statistic.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; @@ -313,7 +313,7 @@ public final class MessageBuilder implements StatFormatter { *
- If both parameters are null, the formattedComponent will be returned * as is. */ - public @NotNull BiFunction formattedPlayerStatFunction(int stat, @NotNull StatRequest.Settings request) { + public @NotNull FormattingFunction 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 StatFormatter { *
- If both parameters are null, the formattedComponent will be returned * as is. */ - public @NotNull BiFunction formattedServerStatFunction(long stat, @NotNull StatRequest.Settings request) { + public @NotNull FormattingFunction formattedServerStatFunction(long stat, @NotNull StatRequest.Settings request) { TextComponent serverStat = formatServerStat(stat, request.getStatistic(), request.getSubStatEntryName()); return getFormattingFunction(serverStat, Target.SERVER); } @@ -345,13 +345,13 @@ public final class MessageBuilder implements StatFormatter { *
- If both parameters are null, the formattedComponent will be returned * as is. */ - public @NotNull BiFunction formattedTopStatFunction(@NotNull LinkedHashMap topStats, @NotNull StatRequest.Settings request) { + public @NotNull FormattingFunction 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); final boolean useEntersForShared = config.useEnters(Target.TOP, true); - return (shareCode, sender) -> { + BiFunction biFunction = (shareCode, sender) -> { TextComponent.Builder topBuilder = text(); //if we're adding a share-button @@ -395,6 +395,7 @@ public final class MessageBuilder implements StatFormatter { } return topBuilder.build(); }; + return new FormattingFunction(biFunction); } private @NotNull TextComponent getPlayerStatComponent(String playerName, TextComponent statNumberComponent, Statistic statistic, @Nullable String subStatName, @Nullable Unit unit) { @@ -676,11 +677,11 @@ public final class MessageBuilder implements StatFormatter { return componentFactory.sharerName(sender.getName()); } - private @NotNull BiFunction getFormattingFunction(@NotNull TextComponent statResult, Target target) { + private @NotNull FormattingFunction getFormattingFunction(@NotNull TextComponent statResult, Target target) { boolean useEnters = config.useEnters(target, false); boolean useEntersForShared = config.useEnters(target, true); - return (shareCode, sender) -> { + BiFunction biFunction = (shareCode, sender) -> { TextComponent.Builder statBuilder = text(); //if we're adding a share-button @@ -711,6 +712,7 @@ public final class MessageBuilder implements StatFormatter { } return statBuilder.build(); }; + return new FormattingFunction(biFunction); } private int getNumberOfDotsToAlign(String displayText) { 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 dea0a3a..40b3bd8 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 @@ -1,11 +1,10 @@ package com.artemis.the.gr8.playerstats.msg; -import com.artemis.the.gr8.playerstats.ShareManager; import com.artemis.the.gr8.playerstats.config.ConfigHandler; import com.artemis.the.gr8.playerstats.enums.StandardMessage; 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 com.artemis.the.gr8.playerstats.statistic.StatRequest; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.text.TextComponent; import org.bukkit.Bukkit; @@ -19,7 +18,6 @@ import java.time.LocalDate; import java.time.Month; import java.util.EnumMap; import java.util.LinkedHashMap; -import java.util.function.BiFunction; import java.util.function.Function; import static com.artemis.the.gr8.playerstats.enums.StandardMessage.*; @@ -35,16 +33,13 @@ public final class OutputManager { private static BukkitAudiences adventure; private static ConfigHandler config; - private static ShareManager shareManager; private static MessageBuilder messageBuilder; private static MessageBuilder consoleMessageBuilder; - private static EnumMap> standardMessages; - public OutputManager(BukkitAudiences adventure, ConfigHandler config, ShareManager shareManager) { + public OutputManager(BukkitAudiences adventure, ConfigHandler config) { OutputManager.adventure = adventure; OutputManager.config = config; - OutputManager.shareManager = shareManager; getMessageBuilders(); prepareFunctions(); @@ -58,39 +53,34 @@ public final class OutputManager { return messageBuilder; } - //TODO separate formatting from internal saving for sharing - - /** @return a TextComponent with the following parts: + /** + * @return a TextComponent with the following parts: *
[player-name]: [number] [stat-name] {sub-stat-name} */ - public TextComponent formatAndSavePlayerStat(@NotNull StatRequest.Settings requestSettings, int playerStat) { - BiFunction playerStatFunction = - getMessageBuilder(requestSettings.getCommandSender()).formattedPlayerStatFunction(playerStat, requestSettings); - - return processFunction(requestSettings.getCommandSender(), playerStatFunction); + public @NotNull FormattingFunction formatPlayerStat(@NotNull StatRequest.Settings requestSettings, int playerStat) { + return getMessageBuilder(requestSettings.getCommandSender()) + .formattedPlayerStatFunction(playerStat, requestSettings); } - /** @return a TextComponent with the following parts: + /** + * @return a TextComponent with the following parts: *
[Total on] [server-name]: [number] [stat-name] [sub-stat-name] */ - public TextComponent formatAndSaveServerStat(@NotNull StatRequest.Settings requestSettings, long serverStat) { - BiFunction serverStatFunction = - getMessageBuilder(requestSettings.getCommandSender()).formattedServerStatFunction(serverStat, requestSettings); - - return processFunction(requestSettings.getCommandSender(), serverStatFunction); + public @NotNull FormattingFunction formatServerStat(@NotNull StatRequest.Settings requestSettings, long serverStat) { + return getMessageBuilder(requestSettings.getCommandSender()) + .formattedServerStatFunction(serverStat, requestSettings); } - /** @return a TextComponent with the following parts: + /** + * @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... */ - public TextComponent formatAndSaveTopStat(@NotNull StatRequest.Settings requestSettings, @NotNull LinkedHashMap topStats) { - BiFunction topStatFunction = - getMessageBuilder(requestSettings.getCommandSender()).formattedTopStatFunction(topStats, requestSettings); - - return processFunction(requestSettings.getCommandSender(), topStatFunction); + public @NotNull FormattingFunction formatTopStats(@NotNull StatRequest.Settings requestSettings, @NotNull LinkedHashMap topStats) { + return getMessageBuilder(requestSettings.getCommandSender()) + .formattedTopStatFunction(topStats, requestSettings); } public void sendFeedbackMsg(@NotNull CommandSender sender, StandardMessage message) { @@ -137,21 +127,6 @@ public final class OutputManager { adventure.sender(sender).sendMessage(component); } - private TextComponent processFunction(CommandSender sender, @NotNull BiFunction statResultFunction) { - boolean saveOutput = !(sender instanceof ConsoleCommandSender) && - ShareManager.isEnabled() && - shareManager.senderHasPermission(sender); - - if (saveOutput) { - int shareCode = - shareManager.saveStatResult(sender.getName(), statResultFunction.apply(null, sender)); - return statResultFunction.apply(shareCode, null); - } - else { - return statResultFunction.apply(null, null); - } - } - private MessageBuilder getMessageBuilder(CommandSender sender) { return sender instanceof ConsoleCommandSender ? consoleMessageBuilder : messageBuilder; } @@ -168,7 +143,7 @@ public final class OutputManager { return MessageBuilder.defaultBuilder(config); } - private static MessageBuilder getConsoleMessageBuilder() { + private static @NotNull MessageBuilder getConsoleMessageBuilder() { MessageBuilder consoleBuilder; if (isBukkit()) { consoleBuilder = MessageBuilder.fromComponentFactory(config, new BukkitConsoleComponentFactory(config)); diff --git a/src/main/java/com/artemis/the/gr8/playerstats/reload/ReloadThread.java b/src/main/java/com/artemis/the/gr8/playerstats/reload/ReloadThread.java index d8bcaf7..b6c8d63 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/reload/ReloadThread.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/reload/ReloadThread.java @@ -1,7 +1,7 @@ package com.artemis.the.gr8.playerstats.reload; import com.artemis.the.gr8.playerstats.Main; -import com.artemis.the.gr8.playerstats.ShareManager; +import com.artemis.the.gr8.playerstats.share.ShareManager; import com.artemis.the.gr8.playerstats.enums.StandardMessage; import com.artemis.the.gr8.playerstats.msg.OutputManager; import com.artemis.the.gr8.playerstats.msg.msgutils.LanguageKeyHandler; diff --git a/src/main/java/com/artemis/the/gr8/playerstats/ShareManager.java b/src/main/java/com/artemis/the/gr8/playerstats/share/ShareManager.java similarity index 90% rename from src/main/java/com/artemis/the/gr8/playerstats/ShareManager.java rename to src/main/java/com/artemis/the/gr8/playerstats/share/ShareManager.java index 13990e4..9b91bb9 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/ShareManager.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/share/ShareManager.java @@ -1,6 +1,5 @@ -package com.artemis.the.gr8.playerstats; +package com.artemis.the.gr8.playerstats.share; -import com.artemis.the.gr8.playerstats.statistic.result.InternalStatResult; import com.artemis.the.gr8.playerstats.config.ConfigHandler; import com.artemis.the.gr8.playerstats.utils.MyLogger; import net.kyori.adventure.text.TextComponent; @@ -30,7 +29,7 @@ public final class ShareManager { private static int waitingTime; private static volatile AtomicInteger resultID; - private static ConcurrentHashMap statResultQueue; + private static ConcurrentHashMap statResultQueue; private static ConcurrentHashMap shareTimeStamp; private static ArrayBlockingQueue sharedResults; @@ -75,8 +74,7 @@ public final class ShareManager { removeExcessResults(playerName); int ID = getNextIDNumber(); - //UUID shareCode = UUID.randomUUID(); - InternalStatResult result = new InternalStatResult(playerName, statResult, ID); + StoredResult result = new StoredResult(playerName, statResult, ID); int shareCode = result.hashCode(); statResultQueue.put(shareCode, result); MyLogger.logMediumLevelMsg("Saving statResults with no. " + ID); @@ -103,7 +101,7 @@ public final class ShareManager { * and returns the formattedComponent. If no formattedComponent was found, * returns null. */ - public @Nullable InternalStatResult getStatResult(String playerName, int shareCode) { + public @Nullable StoredResult getStatResult(String playerName, int shareCode) { if (statResultQueue.containsKey(shareCode)) { shareTimeStamp.put(playerName, Instant.now()); @@ -134,7 +132,7 @@ public final class ShareManager { * StatResults saved, remove the oldest one. */ private void removeExcessResults(String playerName) { - List alreadySavedResults = statResultQueue.values() + List alreadySavedResults = statResultQueue.values() .parallelStream() .filter(result -> result.executorName().equalsIgnoreCase(playerName)) .toList(); @@ -142,7 +140,7 @@ public final class ShareManager { if (alreadySavedResults.size() > 25) { int hashCode = alreadySavedResults .parallelStream() - .min(Comparator.comparing(InternalStatResult::ID)) + .min(Comparator.comparing(StoredResult::ID)) .orElseThrow().hashCode(); MyLogger.logMediumLevelMsg("Removing old stat no. " + statResultQueue.get(hashCode).ID() + " for player " + playerName); statResultQueue.remove(hashCode); diff --git a/src/main/java/com/artemis/the/gr8/playerstats/share/StoredResult.java b/src/main/java/com/artemis/the/gr8/playerstats/share/StoredResult.java new file mode 100644 index 0000000..afbb940 --- /dev/null +++ b/src/main/java/com/artemis/the/gr8/playerstats/share/StoredResult.java @@ -0,0 +1,10 @@ +package com.artemis.the.gr8.playerstats.share; + +import net.kyori.adventure.text.TextComponent; + +/** + * This Record is used to store stat-results internally, + * so Players can share them by clicking a share-button. + */ +public record StoredResult(String executorName, TextComponent formattedValue, int ID) { +} \ No newline at end of file diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/InternalStatRequest.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/InternalStatRequest.java similarity index 79% rename from src/main/java/com/artemis/the/gr8/playerstats/statistic/request/InternalStatRequest.java rename to src/main/java/com/artemis/the/gr8/playerstats/statistic/InternalStatRequest.java index bdd0a09..bc7485e 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/InternalStatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/InternalStatRequest.java @@ -1,7 +1,7 @@ -package com.artemis.the.gr8.playerstats.statistic.request; +package com.artemis.the.gr8.playerstats.statistic; import com.artemis.the.gr8.playerstats.Main; -import com.artemis.the.gr8.playerstats.statistic.result.StatResult; +import com.artemis.the.gr8.playerstats.config.ConfigHandler; import com.artemis.the.gr8.playerstats.utils.EnumHandler; import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; import net.kyori.adventure.text.TextComponent; @@ -21,12 +21,14 @@ import java.util.regex.Pattern; public final class InternalStatRequest extends StatRequest { + private final ConfigHandler config; private final OfflinePlayerHandler offlinePlayerHandler; private final EnumHandler enumHandler; private final Pattern targetPattern; public InternalStatRequest(CommandSender sender, String[] args) { super(sender); + config = Main.getConfigHandler(); offlinePlayerHandler = Main.getOfflinePlayerHandler(); enumHandler = Main.getEnumHandler(); targetPattern = Pattern.compile("top|server|me|player"); @@ -36,7 +38,7 @@ public final class InternalStatRequest extends StatRequest { @Override public @NotNull StatResult execute() { - return Main.getRequestProcessor().getInternalResult(settings); + return Main.getRequestProcessor().getInternalResult(super.getSettings()); } private void processArgs(CommandSender sender, String[] args) { @@ -56,31 +58,31 @@ public final class InternalStatRequest extends StatRequest { continue; } else { - super.settings.configureForPlayer(playerName); + super.getSettings().configureForPlayer(playerName); String[] extractedPlayerName = removeArg(leftoverArgs, playerName); return removeArg(extractedPlayerName, arg); } } case "me" -> { if (sender instanceof Player) { - super.settings.configureForPlayer(sender.getName()); + super.getSettings().configureForPlayer(sender.getName()); } else { - super.settings.configureForServer(); + super.getSettings().configureForServer(); } } - case "server" -> super.settings.configureForServer(); - case "top" -> super.settings.configureForTop(); + case "server" -> super.getSettings().configureForServer(); + case "top" -> super.getSettings().configureForTop(config.getTopListMaxSize()); } return removeArg(leftoverArgs, arg); } } //if no target is found, but there is a playerName, assume target = Target.PLAYER if (playerName != null) { - super.settings.configureForPlayer(playerName); + super.getSettings().configureForPlayer(playerName); return removeArg(leftoverArgs, playerName); } //otherwise, assume target = Target.TOP - super.settings.configureForTop(); + super.getSettings().configureForTop(config.getTopListMaxSize()); return leftoverArgs; } @@ -102,23 +104,23 @@ public final class InternalStatRequest extends StatRequest { for (String arg : leftoverArgs) { if (enumHandler.isSubStatEntry(arg)) { switch (statistic.getType()) { - case UNTYPED -> super.configureUntyped(statistic); + case UNTYPED -> super.getSettings().configureUntyped(statistic); case ITEM -> { Material item = EnumHandler.getItemEnum(arg); if (item != null) { - super.configureBlockOrItemType(statistic, item); + super.getSettings().configureBlockOrItemType(statistic, item); } } case BLOCK -> { Material block = EnumHandler.getBlockEnum(arg); if (block != null) { - super.configureBlockOrItemType(statistic, block); + super.getSettings().configureBlockOrItemType(statistic, block); } } case ENTITY -> { EntityType entityType = EnumHandler.getEntityEnum(arg); if (entityType != null) { - super.configureEntityType(statistic, entityType); + super.getSettings().configureEntityType(statistic, entityType); } } } @@ -142,5 +144,4 @@ public final class InternalStatRequest extends StatRequest { currentArgs.remove(argToRemove); return currentArgs.toArray(String[]::new); } - -} +} \ No newline at end of file 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/PlayerStatRequest.java similarity index 57% rename from src/main/java/com/artemis/the/gr8/playerstats/statistic/request/PlayerStatRequest.java rename to src/main/java/com/artemis/the/gr8/playerstats/statistic/PlayerStatRequest.java index dce804f..11e4e36 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/PlayerStatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/PlayerStatRequest.java @@ -1,43 +1,40 @@ -package com.artemis.the.gr8.playerstats.statistic.request; +package com.artemis.the.gr8.playerstats.statistic; import com.artemis.the.gr8.playerstats.Main; import com.artemis.the.gr8.playerstats.api.RequestGenerator; -import com.artemis.the.gr8.playerstats.statistic.result.StatResult; 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 { public PlayerStatRequest(String playerName) { - this(Bukkit.getConsoleSender(), playerName); - } - - public PlayerStatRequest(CommandSender requester, String playerName) { - super(requester); - super.settings.configureForPlayer(playerName); + super(Bukkit.getConsoleSender()); + super.getSettings().configureForPlayer(playerName); } @Override public StatRequest untyped(@NotNull Statistic statistic) { - return super.configureUntyped(statistic); + super.getSettings().configureUntyped(statistic); + return this; } @Override public StatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) { - return super.configureBlockOrItemType(statistic, material); + super.getSettings().configureBlockOrItemType(statistic, material); + return this; } @Override public StatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) { - return super.configureEntityType(statistic, entityType); + super.getSettings().configureEntityType(statistic, entityType); + return this; } @Override public @NotNull StatResult execute() { - return Main.getRequestProcessor().getPlayerResult(settings); + return Main.getRequestProcessor().processPlayerRequest(super.getSettings()); } } \ No newline at end of file diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/RequestProcessor.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/RequestProcessor.java index 588c6f0..2efe9d2 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/RequestProcessor.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/RequestProcessor.java @@ -1,15 +1,17 @@ package com.artemis.the.gr8.playerstats.statistic; import com.artemis.the.gr8.playerstats.ThreadManager; +import com.artemis.the.gr8.playerstats.msg.FormattingFunction; import com.artemis.the.gr8.playerstats.msg.OutputManager; import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils; -import com.artemis.the.gr8.playerstats.statistic.request.StatRequest; -import com.artemis.the.gr8.playerstats.statistic.result.StatResult; +import com.artemis.the.gr8.playerstats.share.ShareManager; import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; import com.artemis.the.gr8.playerstats.utils.MyLogger; import com.google.common.collect.ImmutableList; import net.kyori.adventure.text.TextComponent; import org.bukkit.OfflinePlayer; +import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; import org.jetbrains.annotations.NotNull; import java.util.*; @@ -22,44 +24,49 @@ public final class RequestProcessor { private final OfflinePlayerHandler offlinePlayerHandler; private static OutputManager outputManager; + private static ShareManager shareManager; - public RequestProcessor(OfflinePlayerHandler offlinePlayerHandler, OutputManager outputManager) { + public RequestProcessor(OfflinePlayerHandler offlinePlayerHandler, OutputManager outputManager, ShareManager shareManager) { this.offlinePlayerHandler = offlinePlayerHandler; RequestProcessor.outputManager = outputManager; + RequestProcessor.shareManager = shareManager; } public @NotNull StatResult getInternalResult(@NotNull StatRequest.Settings requestSettings) { StatResult result = switch (requestSettings.getTarget()) { - case PLAYER -> getPlayerResult(requestSettings); - case SERVER -> getServerResult(requestSettings); - case TOP -> getTopResult(requestSettings); + case PLAYER -> processPlayerRequest(requestSettings); + case SERVER -> processServerRequest(requestSettings); + case TOP -> processTopRequest(requestSettings); }; return new StatResult<>(result.formattedComponent(), result.formattedComponent(), result.formattedString()); } - public @NotNull StatResult getPlayerResult(StatRequest.Settings requestSettings) { + public @NotNull StatResult processPlayerRequest(StatRequest.Settings requestSettings) { int stat = getPlayerStat(requestSettings); - TextComponent result = outputManager.formatAndSavePlayerStat(requestSettings, stat); - String serializedResult = ComponentUtils.getTranslatableComponentSerializer().serialize(result); + FormattingFunction formattingFunction = outputManager.formatPlayerStat(requestSettings, stat); + TextComponent formattedResult = processFunction(requestSettings.getCommandSender(), formattingFunction); + String resultAsString = ComponentUtils.getTranslatableComponentSerializer().serialize(formattedResult); - return new StatResult<>(stat, result, serializedResult); + return new StatResult<>(stat, formattedResult, resultAsString); } - public @NotNull StatResult getServerResult(StatRequest.Settings requestSettings) { + public @NotNull StatResult processServerRequest(StatRequest.Settings requestSettings) { long stat = getServerStat(requestSettings); - TextComponent result = outputManager.formatAndSaveServerStat(requestSettings, stat); - String serializedResult = ComponentUtils.getTranslatableComponentSerializer().serialize(result); + FormattingFunction formattingFunction = outputManager.formatServerStat(requestSettings, stat); + TextComponent formattedResult = processFunction(requestSettings.getCommandSender(), formattingFunction); + String resultAsString = ComponentUtils.getTranslatableComponentSerializer().serialize(formattedResult); - return new StatResult<>(stat, result, serializedResult); + return new StatResult<>(stat, formattedResult, resultAsString); } - public @NotNull StatResult> getTopResult(StatRequest.Settings requestSettings) { + public @NotNull StatResult> processTopRequest(StatRequest.Settings requestSettings) { LinkedHashMap stats = getTopStats(requestSettings); - TextComponent result = outputManager.formatAndSaveTopStat(requestSettings, stats); - String serializedResult = ComponentUtils.getTranslatableComponentSerializer().serialize(result); + FormattingFunction formattingFunction = outputManager.formatTopStats(requestSettings, stats); + TextComponent formattedResult = processFunction(requestSettings.getCommandSender(), formattingFunction); + String resultAsString = ComponentUtils.getTranslatableComponentSerializer().serialize(formattedResult); - return new StatResult<>(stats, result, serializedResult); + return new StatResult<>(stats, formattedResult, resultAsString); } private int getPlayerStat(@NotNull StatRequest.Settings requestSettings) { @@ -87,6 +94,20 @@ public final class RequestProcessor { .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); } + private TextComponent processFunction(CommandSender sender, FormattingFunction function) { + if (outputShouldBeStored(sender)) { + int shareCode = shareManager.saveStatResult(sender.getName(), function.getResultWithSharerName(sender)); + return function.getResultWithShareButton(shareCode); + } + return function.getDefaultResult(); + } + + private boolean outputShouldBeStored(CommandSender sender) { + return !(sender instanceof ConsoleCommandSender) && + ShareManager.isEnabled() && + shareManager.senderHasPermission(sender); + } + /** * Invokes a bunch of worker pool threads to get the statistics for * all players that are stored in the {@link OfflinePlayerHandler}). 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/ServerStatRequest.java similarity index 60% rename from src/main/java/com/artemis/the/gr8/playerstats/statistic/request/ServerStatRequest.java rename to src/main/java/com/artemis/the/gr8/playerstats/statistic/ServerStatRequest.java index 7298a1d..762ce4d 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/ServerStatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/ServerStatRequest.java @@ -1,8 +1,7 @@ -package com.artemis.the.gr8.playerstats.statistic.request; +package com.artemis.the.gr8.playerstats.statistic; import com.artemis.the.gr8.playerstats.Main; import com.artemis.the.gr8.playerstats.api.RequestGenerator; -import com.artemis.the.gr8.playerstats.statistic.result.StatResult; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Statistic; @@ -12,32 +11,32 @@ import org.jetbrains.annotations.NotNull; public final class ServerStatRequest extends StatRequest implements RequestGenerator { - public ServerStatRequest() { - this(Bukkit.getConsoleSender()); - } - public ServerStatRequest(CommandSender requester) { - super(requester); - super.settings.configureForServer(); + public ServerStatRequest() { + super(Bukkit.getConsoleSender()); + super.getSettings().configureForServer(); } @Override public StatRequest untyped(@NotNull Statistic statistic) { - return super.configureUntyped(statistic); + super.getSettings().configureUntyped(statistic); + return this; } @Override public StatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) { - return super.configureBlockOrItemType(statistic, material); + super.getSettings().configureBlockOrItemType(statistic, material); + return this; } @Override public StatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) { - return super.configureEntityType(statistic, entityType); + super.getSettings().configureEntityType(statistic, entityType); + return this; } @Override public @NotNull StatResult execute() { - return Main.getRequestProcessor().getServerResult(settings); + return Main.getRequestProcessor().processServerRequest(super.getSettings()); } } \ No newline at end of file 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 7f1d598..f1bfcd3 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,7 +1,6 @@ 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.utils.MyLogger; import com.google.common.collect.ImmutableList; 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/StatRequest.java similarity index 66% rename from src/main/java/com/artemis/the/gr8/playerstats/statistic/request/StatRequest.java rename to src/main/java/com/artemis/the/gr8/playerstats/statistic/StatRequest.java index 5402819..7af52d2 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/StatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatRequest.java @@ -1,8 +1,6 @@ -package com.artemis.the.gr8.playerstats.statistic.request; +package com.artemis.the.gr8.playerstats.statistic; -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; @@ -22,7 +20,7 @@ import org.jetbrains.annotations.Nullable; */ public abstract class StatRequest { - protected final Settings settings; + private final Settings settings; protected StatRequest(CommandSender requester) { settings = new Settings(requester); @@ -77,40 +75,6 @@ public abstract class StatRequest { } } - protected StatRequest configureUntyped(@NotNull Statistic statistic) { - if (statistic.getType() == Statistic.Type.UNTYPED) { - settings.statistic = 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.block = material; - } - else if (type == Statistic.Type.ITEM && material.isItem()){ - settings.item = 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.statistic = statistic; - settings.subStatEntryName = material.toString(); - return this; - } - - protected StatRequest configureEntityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException { - if (statistic.getType() == Statistic.Type.ENTITY) { - settings.statistic = statistic; - settings.entity = entityType; - settings.subStatEntryName = entityType.toString(); - return this; - } - throw new IllegalArgumentException("This statistic is not of Type.Entity"); - } - public static final class Settings { private final CommandSender sender; private Statistic statistic; @@ -139,15 +103,40 @@ public abstract class StatRequest { this.target = Target.SERVER; } - void configureForTop() { - configureForTop(Main.getConfigHandler().getTopListMaxSize()); - } - void configureForTop(int topListSize) { this.target = Target.TOP; - this.topListSize = topListSize != 0 ? - topListSize : - Main.getConfigHandler().getTopListMaxSize(); + this.topListSize = topListSize; + } + + void configureUntyped(@NotNull Statistic statistic) { + if (statistic.getType() == Statistic.Type.UNTYPED) { + this.statistic = statistic; + } + throw new IllegalArgumentException("This statistic is not of Type.Untyped"); + } + + void configureBlockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException { + Statistic.Type type = statistic.getType(); + if (type == Statistic.Type.BLOCK && material.isBlock()) { + this.block = material; + } + else if (type == Statistic.Type.ITEM && material.isItem()){ + this.item = 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"); + } + this.statistic = statistic; + this.subStatEntryName = material.toString(); + } + + void configureEntityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException { + if (statistic.getType() == Statistic.Type.ENTITY) { + this.statistic = statistic; + this.entity = entityType; + this.subStatEntryName = entityType.toString(); + } + throw new IllegalArgumentException("This statistic is not of Type.Entity"); } public @NotNull CommandSender getCommandSender() { diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/result/StatResult.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatResult.java similarity index 98% rename from src/main/java/com/artemis/the/gr8/playerstats/statistic/result/StatResult.java rename to src/main/java/com/artemis/the/gr8/playerstats/statistic/StatResult.java index 46c5ea1..97fd1c5 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/result/StatResult.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatResult.java @@ -1,4 +1,4 @@ -package com.artemis.the.gr8.playerstats.statistic.result; +package com.artemis.the.gr8.playerstats.statistic; import com.artemis.the.gr8.playerstats.api.StatFormatter; import net.kyori.adventure.platform.bukkit.BukkitAudiences; 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 faa84e0..3ac5868 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,8 +2,6 @@ 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.statistic.result.StatResult; import com.artemis.the.gr8.playerstats.utils.MyLogger; import com.artemis.the.gr8.playerstats.enums.StandardMessage; import com.artemis.the.gr8.playerstats.reload.ReloadThread; @@ -18,15 +16,12 @@ import java.util.*; public final class StatThread extends Thread { private static OutputManager outputManager; - private static RequestProcessor requestProcessor; private final ReloadThread reloadThread; private final StatRequest statRequest; - public StatThread(OutputManager m, RequestProcessor t, int ID, StatRequest s, @Nullable ReloadThread r) { + public StatThread(OutputManager m, int ID, StatRequest s, @Nullable ReloadThread r) { outputManager = m; - requestProcessor = t; - reloadThread = r; statRequest = s; 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/TopStatRequest.java similarity index 61% rename from src/main/java/com/artemis/the/gr8/playerstats/statistic/request/TopStatRequest.java rename to src/main/java/com/artemis/the/gr8/playerstats/statistic/TopStatRequest.java index f9ad3d2..f475ec9 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/request/TopStatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/TopStatRequest.java @@ -1,12 +1,10 @@ -package com.artemis.the.gr8.playerstats.statistic.request; +package com.artemis.the.gr8.playerstats.statistic; import com.artemis.the.gr8.playerstats.Main; -import com.artemis.the.gr8.playerstats.statistic.result.StatResult; import com.artemis.the.gr8.playerstats.api.RequestGenerator; 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; @@ -15,31 +13,30 @@ import java.util.LinkedHashMap; public final class TopStatRequest extends StatRequest> implements RequestGenerator> { public TopStatRequest(int topListSize) { - this(Bukkit.getConsoleSender(), topListSize); - } - - public TopStatRequest(CommandSender requester, int topListSize) { - super(requester); - super.settings.configureForTop(topListSize); + super(Bukkit.getConsoleSender()); + super.getSettings().configureForTop(topListSize); } @Override public StatRequest> untyped(@NotNull Statistic statistic) { - return super.configureUntyped(statistic); + super.getSettings().configureUntyped(statistic); + return this; } @Override public StatRequest> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) { - return super.configureBlockOrItemType(statistic, material); + super.getSettings().configureBlockOrItemType(statistic, material); + return this; } @Override public StatRequest> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) { - return super.configureEntityType(statistic, entityType); + super.getSettings().configureEntityType(statistic, entityType); + return this; } @Override public @NotNull StatResult> execute() { - return Main.getRequestProcessor().getTopResult(settings); + return Main.getRequestProcessor().processTopRequest(super.getSettings()); } } \ No newline at end of file diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/result/InternalStatResult.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/result/InternalStatResult.java deleted file mode 100644 index 3a63401..0000000 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/result/InternalStatResult.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.artemis.the.gr8.playerstats.statistic.result; - -import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils; -import net.kyori.adventure.text.TextComponent; - -/** - * This Record is used to store stat-results internally, - * so Players can share them by clicking a share-button. - */ -public record InternalStatResult(String executorName, TextComponent formattedValue, int ID) { - - /** - * Gets the ID number for this StatResult. Unlike for the - * other {@link StatResult} implementations, this one does - * not return the actual statistic data, because this - * implementation is meant for internal saving-and-sharing only. - * This method is only for Interface-consistency, - * InternalStatResult#ID is better. - * - @return Integer that represents this StatResult's ID number - */ - public Integer getNumericalValue() { - return ID; - } - - public TextComponent getFormattedTextComponent() { - return formattedValue; - } - - public String getFormattedString() { - return ComponentUtils.getTranslatableComponentSerializer() - .serialize(formattedValue); - } -} \ No newline at end of file