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 511c484..d1cd611 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/Main.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/Main.java @@ -1,7 +1,8 @@ package com.artemis.the.gr8.playerstats; import com.artemis.the.gr8.playerstats.api.PlayerStats; -import com.artemis.the.gr8.playerstats.api.PlayerStatsImpl; +import com.artemis.the.gr8.playerstats.api.PlayerStatsAPI; +import com.artemis.the.gr8.playerstats.statistic.RequestManager; import com.artemis.the.gr8.playerstats.msg.OutputManager; import com.artemis.the.gr8.playerstats.commands.ReloadCommand; import com.artemis.the.gr8.playerstats.commands.ShareCommand; @@ -13,6 +14,7 @@ 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.MyLogger; import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.expansion.PlaceholderExpansion; @@ -42,7 +44,6 @@ public final class Main extends JavaPlugin { private static OutputManager outputManager; private static ShareManager shareManager; - private static RequestProcessor requestProcessor; private static PlayerStats playerStatsImpl; @@ -80,6 +81,15 @@ public final class Main extends JavaPlugin { this.getLogger().info("Disabled PlayerStats!"); } + public void reloadPlugin() { + config.reload(); + MyLogger.setDebugLevel(config.getDebugLevel()); + languageKeyHandler.reload(); + offlinePlayerHandler.reload(); + outputManager.update(); + ShareManager.updateSettings(config); + } + /** * * @return the JavaPlugin instance associated with PlayerStats @@ -99,27 +109,6 @@ public final class Main extends JavaPlugin { return playerStatsImpl; } - public static @NotNull RequestProcessor getRequestProcessor() throws IllegalStateException { - if (requestProcessor == null) { - throw new IllegalStateException("PlayerStats does not seem to be loaded!"); - } - return requestProcessor; - } - - public static @NotNull OfflinePlayerHandler getOfflinePlayerHandler() throws IllegalStateException { - if (offlinePlayerHandler == null) { - throw new IllegalStateException("PlayerStats does not seem to be loaded!"); - } - return offlinePlayerHandler; - } - - public static @NotNull LanguageKeyHandler getLanguageKeyHandler() { - if (languageKeyHandler == null) { - languageKeyHandler = new LanguageKeyHandler(); - } - return languageKeyHandler; - } - private void initializeMainClasses() { pluginInstance = this; adventure = BukkitAudiences.create(this); @@ -129,11 +118,12 @@ public final class Main extends JavaPlugin { offlinePlayerHandler = new OfflinePlayerHandler(config); shareManager = new ShareManager(config); - outputManager = new OutputManager(adventure, config); - requestProcessor = new RequestProcessor(offlinePlayerHandler, outputManager, shareManager); + outputManager = new OutputManager(adventure, config, languageKeyHandler); - threadManager = new ThreadManager(config, outputManager); - playerStatsImpl = new PlayerStatsImpl(offlinePlayerHandler, outputManager); + RequestProcessor requestProcessor = new RequestProcessor(offlinePlayerHandler, outputManager, shareManager); + RequestManager statManager = new RequestManager(offlinePlayerHandler, requestProcessor); + threadManager = new ThreadManager(this, config, outputManager, statManager); + playerStatsImpl = new PlayerStatsAPI(statManager, 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 72ab480..511ee63 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/ThreadManager.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/ThreadManager.java @@ -6,6 +6,7 @@ import com.artemis.the.gr8.playerstats.enums.StandardMessage; import com.artemis.the.gr8.playerstats.reload.ReloadThread; import com.artemis.the.gr8.playerstats.statistic.StatThread; import com.artemis.the.gr8.playerstats.statistic.StatRequest; +import com.artemis.the.gr8.playerstats.statistic.RequestManager; import com.artemis.the.gr8.playerstats.utils.MyLogger; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; @@ -27,17 +28,21 @@ public final class ThreadManager { private int statThreadID; private int reloadThreadID; + private final Main main; private static ConfigHandler config; private static OutputManager outputManager; + private final RequestManager statManager; private ReloadThread activatedReloadThread; private StatThread activatedStatThread; private final HashMap statThreads; private static long lastRecordedCalcTime; - public ThreadManager(ConfigHandler config, OutputManager outputManager) { + public ThreadManager(Main main, ConfigHandler config, OutputManager outputManager, RequestManager statManager) { + this.main = main; ThreadManager.config = config; ThreadManager.outputManager = outputManager; + this.statManager = statManager; statThreads = new HashMap<>(); statThreadID = 0; @@ -53,7 +58,7 @@ public final class ThreadManager { if (activatedReloadThread == null || !activatedReloadThread.isAlive()) { reloadThreadID += 1; - activatedReloadThread = new ReloadThread(config, outputManager, reloadThreadID, activatedStatThread, sender); + activatedReloadThread = new ReloadThread(main, outputManager, reloadThreadID, activatedStatThread, sender); activatedReloadThread.start(); } else { @@ -95,7 +100,7 @@ public final class ThreadManager { } private void startNewStatThread(StatRequest request) { - activatedStatThread = new StatThread(outputManager, statThreadID, request, activatedReloadThread); + activatedStatThread = new StatThread(outputManager, statManager, 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 bf6e8e0..af0fc74 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.StatRequest; +import com.artemis.the.gr8.playerstats.statistic.RequestManager; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @@ -9,19 +9,14 @@ import org.jetbrains.annotations.NotNull; * The outgoing API that represents the core functionality of PlayerStats! * *

To work with it, you'll need to call PlayerStats.{@link #getAPI()} and get an instance of - * {@link PlayerStatsImpl}. You can then use this object to access any of the further methods. + * {@link PlayerStatsAPI}. You can then use this object to access any of the further methods. * - *

Since calculating a top or server statistics can take some time, I strongly - * encourage you to call {@link StatRequest#execute()} asynchronously. - * Otherwise, the main Thread will have to wait until all calculations are done, - * and this can severely impact server performance. - * - * @see StatManager + * @see RequestManager * @see StatFormatter */ public interface PlayerStats { - /** Gets an instance of the {@link PlayerStatsImpl}. + /** Gets an instance of the {@link PlayerStatsAPI}. * @return the PlayerStats API * @throws IllegalStateException if PlayerStats is not loaded on the server when this method is called*/ @@ -31,13 +26,13 @@ public interface PlayerStats { } /** - * Gets the current version of PlayerStatsImpl. + * Gets the current version of PlayerStatsAPI. * Use this method to ensure the correct version of * PlayerStats is running on the server before * accessing further API methods, to prevent * ClassDefNotFoundExceptions. * - * @return the version of PlayerStatsImpl present on the server + * @return the version of PlayerStatsAPI present on the server */ default String getVersion() { return "1.8"; 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 new file mode 100644 index 0000000..a65d1e6 --- /dev/null +++ b/src/main/java/com/artemis/the/gr8/playerstats/api/PlayerStatsAPI.java @@ -0,0 +1,28 @@ +package com.artemis.the.gr8.playerstats.api; + +import com.artemis.the.gr8.playerstats.msg.OutputManager; + +import static org.jetbrains.annotations.ApiStatus.Internal; + +/** The implementation of the API Interface */ +public final class PlayerStatsAPI implements PlayerStats { + + private static OutputManager outputManager; + private final StatManager statManager; + + @Internal + public PlayerStatsAPI(StatManager statManager, OutputManager outputManager) { + PlayerStatsAPI.outputManager = outputManager; + this.statManager = statManager; + } + + @Override + public StatFormatter getFormatter() { + return outputManager.getMainMessageBuilder(); + } + + @Override + public StatManager getStatManager() { + return statManager; + } +} \ No newline at end of file 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 deleted file mode 100644 index 80a0ad2..0000000 --- a/src/main/java/com/artemis/the/gr8/playerstats/api/PlayerStatsImpl.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.artemis.the.gr8.playerstats.api; - -import com.artemis.the.gr8.playerstats.msg.OutputManager; -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; - -import static org.jetbrains.annotations.ApiStatus.Internal; - -/** The implementation of the API Interface */ -public final class PlayerStatsImpl implements PlayerStats, StatManager { - - private static OutputManager outputManager; - private final OfflinePlayerHandler offlinePlayerHandler; - - @Internal - public PlayerStatsImpl(OfflinePlayerHandler offlinePlayerHandler, OutputManager outputManager) { - PlayerStatsImpl.outputManager = outputManager; - this.offlinePlayerHandler = offlinePlayerHandler; - } - - @Override - public StatFormatter getFormatter() { - return outputManager.getCurrentMainMessageBuilder(); - } - - @Override - public StatManager getStatManager() { - return this; - } - - @Override - public RequestGenerator playerStatRequest(String playerName) { - return new PlayerStatRequest(playerName); - } - - @Override - public RequestGenerator serverStatRequest() { - return new ServerStatRequest(); - } - - @Override - public RequestGenerator> topStatRequest(int topListSize) { - return new TopStatRequest(topListSize); - } - - @Override - public RequestGenerator> totalTopStatRequest() { - int playerCount = offlinePlayerHandler.getOfflinePlayerCount(); - return topStatRequest(playerCount); - } -} \ No newline at end of file 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 a99a6a4..afeb6e8 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 @@ -1,7 +1,6 @@ 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.StatResult; import net.kyori.adventure.text.TextComponent; @@ -18,6 +17,15 @@ import org.jetbrains.annotations.Nullable; */ public interface StatFormatter { + /** + * Gets a {@link NumberFormatter} to format raw numbers into something more readable. + * + * @return the NumberFormatter + */ + default NumberFormatter getNumberFormatter() { + return new NumberFormatter(); + } + /** * Turns a TextComponent into its String representation. This method is equipped * to turn all PlayerStats' formatted statResults into String, using a custom @@ -28,19 +36,7 @@ public interface StatFormatter { * but with color, style and formatting. TranslatableComponents will be turned into * plain English. */ - default String TextComponentToString(TextComponent component) { - return ComponentUtils.getTranslatableComponentSerializer() - .serialize(component); - } - - /** - * Gets a {@link NumberFormatter} to format raw numbers into something more readable. - * - * @return the NumberFormatter - */ - default NumberFormatter getNumberFormatter() { - return new NumberFormatter(); - } + String textComponentToString(TextComponent component); /** * Gets the default prefix PlayerStats uses. 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 52f64b8..96b7e97 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,13 +1,10 @@ package com.artemis.the.gr8.playerstats.api; import com.artemis.the.gr8.playerstats.statistic.StatRequest; +import com.artemis.the.gr8.playerstats.statistic.StatResult; import java.util.LinkedHashMap; -/** - * Turns user input into a {@link StatRequest} that can be - * used to get statistic data. - */ public interface StatManager { /** Gets a RequestGenerator that can be used to create a PlayerStatRequest. @@ -16,14 +13,36 @@ public interface StatManager { * * @param playerName the player whose statistic is being requested * @return the RequestGenerator */ - RequestGenerator playerStatRequest(String playerName); + RequestGenerator createPlayerStatRequest(String playerName); + + /** + * Executes this StatRequest. This calculation can take some time, + * so don't call this from the main Thread if you can help it! + * + * @return a StatResult containing the value of this lookup, both as + * numerical value and as formatted message + * @see PlayerStats + * @see StatResult + */ + StatResult executePlayerStatRequest(StatRequest request); /** Gets a RequestGenerator that can be used to create a ServerStatRequest. * This RequestGenerator will make sure all default settings * for a server-statistic-lookup are configured. * * @return the RequestGenerator*/ - RequestGenerator serverStatRequest(); + RequestGenerator createServerStatRequest(); + + /** + * Executes this StatRequest. This calculation can take some time, + * so don't call this from the main Thread if you can help it! + * + * @return a StatResult containing the value of this lookup, both as + * numerical value and as formatted message + * @see PlayerStats + * @see StatResult + */ + StatResult executeServerStatRequest(StatRequest request); /** Gets a RequestGenerator that can be used to create a TopStatRequest * for a top-list of the specified size. This RequestGenerator will @@ -31,7 +50,7 @@ public interface StatManager { * * @param topListSize how big the top-x should be (10 by default) * @return the RequestGenerator*/ - RequestGenerator> topStatRequest(int topListSize); + RequestGenerator> createTopStatRequest(int topListSize); /** Gets a RequestGenerator that can be used to create a TopStatRequest * for all offline players on the server (those that are included by @@ -39,5 +58,16 @@ public interface StatManager { * all default settings for a top-statistic-lookup are configured. * * @return the RequestGenerator*/ - RequestGenerator> totalTopStatRequest(); -} \ No newline at end of file + RequestGenerator> createTotalTopStatRequest(); + + /** + * Executes this StatRequest. This calculation can take some time, + * so don't call this from the main Thread if you can help it! + * + * @return a StatResult containing the value of this lookup, both as + * numerical value and as formatted message + * @see PlayerStats + * @see StatResult + */ + StatResult> executeTopRequest(StatRequest> request); +} 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 20b0fba..d713b1c 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 @@ -1,6 +1,5 @@ package com.artemis.the.gr8.playerstats.msg; -import com.artemis.the.gr8.playerstats.Main; import com.artemis.the.gr8.playerstats.api.StatFormatter; import com.artemis.the.gr8.playerstats.msg.components.ComponentFactory; import com.artemis.the.gr8.playerstats.msg.components.ExampleMessage; @@ -48,28 +47,30 @@ public final class MessageBuilder implements StatFormatter { private final ComponentFactory componentFactory; private final LanguageKeyHandler languageKeyHandler; private final NumberFormatter formatter; + private final ComponentSerializer serializer; - private MessageBuilder(ConfigHandler config) { - this (config, new ComponentFactory(config)); + private MessageBuilder(ConfigHandler config, LanguageKeyHandler language) { + this (config, language, new ComponentFactory(config)); } - private MessageBuilder(ConfigHandler configHandler, ComponentFactory factory) { + private MessageBuilder(ConfigHandler configHandler, LanguageKeyHandler language, ComponentFactory factory) { config = configHandler; useHoverText = config.useHoverText(); componentFactory = factory; + languageKeyHandler = language; formatter = new NumberFormatter(); - languageKeyHandler = Main.getLanguageKeyHandler(); - } - - @Contract("_ -> new") - public static @NotNull MessageBuilder defaultBuilder(ConfigHandler config) { - return new MessageBuilder(config); + serializer = new ComponentSerializer(languageKeyHandler); } @Contract("_, _ -> new") - public static @NotNull MessageBuilder fromComponentFactory(ConfigHandler config, ComponentFactory factory) { - return new MessageBuilder(config, factory); + public static @NotNull MessageBuilder defaultBuilder(ConfigHandler config, LanguageKeyHandler language) { + return new MessageBuilder(config, language); + } + + @Contract("_, _, _ -> new") + public static @NotNull MessageBuilder fromComponentFactory(ConfigHandler config, LanguageKeyHandler language, ComponentFactory factory) { + return new MessageBuilder(config, language, factory); } /** @@ -84,6 +85,11 @@ public final class MessageBuilder implements StatFormatter { this.isConsoleBuilder = isConsoleBuilder; } + @Override + public @NotNull String textComponentToString(TextComponent component) { + return serializer.getTranslatableComponentSerializer().serialize(component); + } + @Override public TextComponent getPluginPrefix() { return componentFactory.pluginPrefix(); 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 40b3bd8..57aec04 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,9 +1,11 @@ package com.artemis.the.gr8.playerstats.msg; +import com.artemis.the.gr8.playerstats.api.StatFormatter; 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.msg.msgutils.LanguageKeyHandler; import com.artemis.the.gr8.playerstats.statistic.StatRequest; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.text.TextComponent; @@ -33,26 +35,32 @@ public final class OutputManager { private static BukkitAudiences adventure; private static ConfigHandler config; - private static MessageBuilder messageBuilder; - private static MessageBuilder consoleMessageBuilder; private static EnumMap> standardMessages; - public OutputManager(BukkitAudiences adventure, ConfigHandler config) { + private final LanguageKeyHandler languageKeyHandler; + private MessageBuilder messageBuilder; + private MessageBuilder consoleMessageBuilder; + + public OutputManager(BukkitAudiences adventure, ConfigHandler config, LanguageKeyHandler language) { OutputManager.adventure = adventure; OutputManager.config = config; - + languageKeyHandler = language; getMessageBuilders(); prepareFunctions(); } - public static void updateMessageBuilders() { + public void update() { getMessageBuilders(); } - public MessageBuilder getCurrentMainMessageBuilder() { + public StatFormatter getMainMessageBuilder() { return messageBuilder; } + public @NotNull String textComponentToString(TextComponent component) { + return messageBuilder.textComponentToString(component); + } + /** * @return a TextComponent with the following parts: *
[player-name]: [number] [stat-name] {sub-stat-name} @@ -131,22 +139,22 @@ public final class OutputManager { return sender instanceof ConsoleCommandSender ? consoleMessageBuilder : messageBuilder; } - private static void getMessageBuilders() { + private void getMessageBuilders() { messageBuilder = getClientMessageBuilder(); consoleMessageBuilder = getConsoleMessageBuilder(); } - private static MessageBuilder getClientMessageBuilder() { + private MessageBuilder getClientMessageBuilder() { if (useRainbowStyle()) { - return MessageBuilder.fromComponentFactory(config, new PrideComponentFactory(config)); + return MessageBuilder.fromComponentFactory(config, languageKeyHandler, new PrideComponentFactory(config)); } - return MessageBuilder.defaultBuilder(config); + return MessageBuilder.defaultBuilder(config, languageKeyHandler); } - private static @NotNull MessageBuilder getConsoleMessageBuilder() { + private @NotNull MessageBuilder getConsoleMessageBuilder() { MessageBuilder consoleBuilder; if (isBukkit()) { - consoleBuilder = MessageBuilder.fromComponentFactory(config, new BukkitConsoleComponentFactory(config)); + consoleBuilder = MessageBuilder.fromComponentFactory(config,languageKeyHandler, new BukkitConsoleComponentFactory(config)); } else { consoleBuilder = getClientMessageBuilder(); } @@ -155,11 +163,11 @@ public final class OutputManager { return consoleBuilder; } - private static boolean useRainbowStyle() { + private boolean useRainbowStyle() { return config.useRainbowMode() || (config.useFestiveFormatting() && LocalDate.now().getMonth().equals(Month.JUNE)); } - private static boolean isBukkit() { + private boolean isBukkit() { return Bukkit.getName().equalsIgnoreCase("CraftBukkit"); } diff --git a/src/main/java/com/artemis/the/gr8/playerstats/msg/components/ComponentUtils.java b/src/main/java/com/artemis/the/gr8/playerstats/msg/msgutils/ComponentSerializer.java similarity index 92% rename from src/main/java/com/artemis/the/gr8/playerstats/msg/components/ComponentUtils.java rename to src/main/java/com/artemis/the/gr8/playerstats/msg/msgutils/ComponentSerializer.java index efdb20c..b34f37e 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/msg/components/ComponentUtils.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/msg/msgutils/ComponentSerializer.java @@ -1,7 +1,5 @@ -package com.artemis.the.gr8.playerstats.msg.components; +package com.artemis.the.gr8.playerstats.msg.msgutils; -import com.artemis.the.gr8.playerstats.Main; -import com.artemis.the.gr8.playerstats.msg.msgutils.LanguageKeyHandler; import net.kyori.adventure.text.*; import net.kyori.adventure.text.flattener.ComponentFlattener; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; @@ -11,7 +9,13 @@ import org.jetbrains.annotations.NotNull; /** * A small utility class for turning PlayerStats' custom Components into String. */ -public final class ComponentUtils { +public final class ComponentSerializer { + + private final LanguageKeyHandler languageKeyHandler; + + public ComponentSerializer(LanguageKeyHandler languageKeyHandler) { + this.languageKeyHandler = languageKeyHandler; + } /** * Returns a LegacyComponentSerializer that is capable of serializing @@ -23,8 +27,7 @@ public final class ComponentUtils { * @return the Serializer * @see LanguageKeyHandler */ - public static @NotNull LegacyComponentSerializer getTranslatableComponentSerializer() { - LanguageKeyHandler languageKeyHandler = Main.getLanguageKeyHandler(); + public @NotNull LegacyComponentSerializer getTranslatableComponentSerializer() { LegacyComponentSerializer serializer = getTextComponentSerializer(); ComponentFlattener flattener = ComponentFlattener.basic().toBuilder() 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 b6c8d63..a579803 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 @@ -9,7 +9,6 @@ import com.artemis.the.gr8.playerstats.statistic.RequestProcessor; import com.artemis.the.gr8.playerstats.statistic.StatThread; import com.artemis.the.gr8.playerstats.utils.MyLogger; import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; -import com.artemis.the.gr8.playerstats.config.ConfigHandler; import com.artemis.the.gr8.playerstats.enums.DebugLevel; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.Nullable; @@ -17,14 +16,14 @@ import org.jetbrains.annotations.Nullable; /** The Thread that is in charge of reloading PlayerStats. */ public final class ReloadThread extends Thread { - private static ConfigHandler config; + private final Main main; private static OutputManager outputManager; private final StatThread statThread; private final CommandSender sender; - public ReloadThread(ConfigHandler c, OutputManager m, int ID, @Nullable StatThread s, @Nullable CommandSender se) { - config = c; + public ReloadThread(Main main, OutputManager m, int ID, @Nullable StatThread s, @Nullable CommandSender se) { + this.main = main; outputManager = m; statThread = s; @@ -58,20 +57,10 @@ public final class ReloadThread extends Thread { } MyLogger.logLowLevelMsg("Reloading!"); - reloadEverything(); + main.reloadPlugin(); if (sender != null) { outputManager.sendFeedbackMsg(sender, StandardMessage.RELOADED_CONFIG); } } - - private void reloadEverything() { - config.reload(); - MyLogger.setDebugLevel(config.getDebugLevel()); - Main.getLanguageKeyHandler().reload(); - Main.getOfflinePlayerHandler().reload(); - - OutputManager.updateMessageBuilders(); - ShareManager.updateSettings(config); - } } \ No newline at end of file diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/PlayerStatRequest.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/PlayerStatRequest.java index 11e4e36..52855a5 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/PlayerStatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/PlayerStatRequest.java @@ -1,6 +1,5 @@ package com.artemis.the.gr8.playerstats.statistic; -import com.artemis.the.gr8.playerstats.Main; import com.artemis.the.gr8.playerstats.api.RequestGenerator; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -32,9 +31,4 @@ public final class PlayerStatRequest extends StatRequest implements Req super.getSettings().configureEntityType(statistic, entityType); return this; } - - @Override - public @NotNull StatResult execute() { - return Main.getRequestProcessor().processPlayerRequest(super.getSettings()); - } } \ No newline at end of file diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/RequestManager.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/RequestManager.java new file mode 100644 index 0000000..2ef1511 --- /dev/null +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/RequestManager.java @@ -0,0 +1,67 @@ +package com.artemis.the.gr8.playerstats.statistic; + +import com.artemis.the.gr8.playerstats.api.RequestGenerator; +import com.artemis.the.gr8.playerstats.api.StatManager; +import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; +import org.jetbrains.annotations.NotNull; + +import java.util.LinkedHashMap; + +/** + * Turns user input into a {@link StatRequest} that can be + * used to get statistic data. + */ +public final class RequestManager implements StatManager { + + private static OfflinePlayerHandler offlinePlayerHandler; + private final RequestProcessor processor; + + public RequestManager(OfflinePlayerHandler offlinePlayerHandler, RequestProcessor processor) { + RequestManager.offlinePlayerHandler = offlinePlayerHandler; + this.processor = processor; + } + + public StatResult execute(@NotNull StatRequest request) { + return switch (request.getSettings().getTarget()) { + case PLAYER -> processor.processPlayerRequest(request.getSettings()); + case SERVER -> processor.processServerRequest(request.getSettings()); + case TOP -> processor.processTopRequest(request.getSettings()); + }; + } + + @Override + public RequestGenerator createPlayerStatRequest(String playerName) { + return new PlayerStatRequest(playerName); + } + + @Override + public StatResult executePlayerStatRequest(StatRequest request) { + return processor.processPlayerRequest(request.getSettings()); + } + + @Override + public RequestGenerator createServerStatRequest() { + return new ServerStatRequest(); + } + + @Override + public StatResult executeServerStatRequest(StatRequest request) { + return processor.processServerRequest(request.getSettings()); + } + + @Override + public RequestGenerator> createTopStatRequest(int topListSize) { + return new TopStatRequest(topListSize); + } + + @Override + public RequestGenerator> createTotalTopStatRequest() { + int playerCount = offlinePlayerHandler.getOfflinePlayerCount(); + return createTopStatRequest(playerCount); + } + + @Override + public StatResult> executeTopRequest(StatRequest> request) { + return processor.processTopRequest(request.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 2efe9d2..a36c988 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 @@ -3,7 +3,6 @@ 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.share.ShareManager; import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; import com.artemis.the.gr8.playerstats.utils.MyLogger; @@ -20,7 +19,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ForkJoinPool; import java.util.stream.Collectors; -public final class RequestProcessor { +public class RequestProcessor { private final OfflinePlayerHandler offlinePlayerHandler; private static OutputManager outputManager; @@ -32,21 +31,11 @@ public final class RequestProcessor { RequestProcessor.shareManager = shareManager; } - public @NotNull StatResult getInternalResult(@NotNull StatRequest.Settings requestSettings) { - StatResult result = switch (requestSettings.getTarget()) { - case PLAYER -> processPlayerRequest(requestSettings); - case SERVER -> processServerRequest(requestSettings); - case TOP -> processTopRequest(requestSettings); - }; - - return new StatResult<>(result.formattedComponent(), result.formattedComponent(), result.formattedString()); - } - public @NotNull StatResult processPlayerRequest(StatRequest.Settings requestSettings) { int stat = getPlayerStat(requestSettings); FormattingFunction formattingFunction = outputManager.formatPlayerStat(requestSettings, stat); TextComponent formattedResult = processFunction(requestSettings.getCommandSender(), formattingFunction); - String resultAsString = ComponentUtils.getTranslatableComponentSerializer().serialize(formattedResult); + String resultAsString = outputManager.textComponentToString(formattedResult); return new StatResult<>(stat, formattedResult, resultAsString); } @@ -55,7 +44,7 @@ public final class RequestProcessor { long stat = getServerStat(requestSettings); FormattingFunction formattingFunction = outputManager.formatServerStat(requestSettings, stat); TextComponent formattedResult = processFunction(requestSettings.getCommandSender(), formattingFunction); - String resultAsString = ComponentUtils.getTranslatableComponentSerializer().serialize(formattedResult); + String resultAsString = outputManager.textComponentToString(formattedResult); return new StatResult<>(stat, formattedResult, resultAsString); } @@ -64,7 +53,7 @@ public final class RequestProcessor { LinkedHashMap stats = getTopStats(requestSettings); FormattingFunction formattingFunction = outputManager.formatTopStats(requestSettings, stats); TextComponent formattedResult = processFunction(requestSettings.getCommandSender(), formattingFunction); - String resultAsString = ComponentUtils.getTranslatableComponentSerializer().serialize(formattedResult); + String resultAsString = outputManager.textComponentToString(formattedResult); return new StatResult<>(stats, formattedResult, resultAsString); } diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/ServerStatRequest.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/ServerStatRequest.java index 762ce4d..8170a61 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/ServerStatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/ServerStatRequest.java @@ -1,11 +1,9 @@ package com.artemis.the.gr8.playerstats.statistic; -import com.artemis.the.gr8.playerstats.Main; 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; @@ -34,9 +32,4 @@ public final class ServerStatRequest extends StatRequest implements Reques super.getSettings().configureEntityType(statistic, entityType); return this; } - - @Override - public @NotNull StatResult execute() { - return Main.getRequestProcessor().processServerRequest(super.getSettings()); - } } \ No newline at end of file diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatRequest.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatRequest.java index f7fe80e..e060146 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatRequest.java @@ -1,6 +1,6 @@ package com.artemis.the.gr8.playerstats.statistic; -import com.artemis.the.gr8.playerstats.api.PlayerStats; +import com.artemis.the.gr8.playerstats.api.StatManager; import com.artemis.the.gr8.playerstats.enums.Target; import org.bukkit.Material; import org.bukkit.Statistic; @@ -12,11 +12,8 @@ import org.jetbrains.annotations.Nullable; /** * Holds all the information PlayerStats needs to perform - * a lookup, and can be executed to get the results. Calling - * {@link #execute()} on a Top- or ServerRequest can take some - * time (especially if there is a substantial amount of - * OfflinePlayers on this particular server), so I strongly - * advice you to call this asynchronously! + * a lookup, and can be executed by the {@link StatManager} + * to get the results. */ public abstract class StatRequest { @@ -26,17 +23,6 @@ public abstract class StatRequest { settings = new Settings(requester); } - /** - * Executes this StatRequest. This calculation can take some time, - * so don't call this from the main Thread if you can help it! - * - * @return a StatResult containing the value of this lookup, both as - * numerical value and as formatted message - * @see PlayerStats - * @see StatResult - */ - public abstract StatResult execute(); - /** * Use this method to view the settings that have * been configured for this StatRequest. 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 3ac5868..ff03a55 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 @@ -16,12 +16,14 @@ import java.util.*; public final class StatThread extends Thread { private static OutputManager outputManager; + private final RequestManager statManager; private final ReloadThread reloadThread; private final StatRequest statRequest; - public StatThread(OutputManager m, int ID, StatRequest s, @Nullable ReloadThread r) { + public StatThread(OutputManager m, RequestManager stat, int ID, StatRequest s, @Nullable ReloadThread r) { outputManager = m; + statManager = stat; reloadThread = r; statRequest = s; @@ -52,7 +54,7 @@ public final class StatThread extends Thread { } try { - StatResult result = statRequest.execute(); + StatResult result = statManager.execute(statRequest); outputManager.sendToCommandSender(statRequester, result.formattedComponent()); } catch (ConcurrentModificationException e) { diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/TopStatRequest.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/TopStatRequest.java index f475ec9..d911fb2 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/TopStatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/TopStatRequest.java @@ -1,6 +1,5 @@ package com.artemis.the.gr8.playerstats.statistic; -import com.artemis.the.gr8.playerstats.Main; import com.artemis.the.gr8.playerstats.api.RequestGenerator; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -34,9 +33,4 @@ public final class TopStatRequest extends StatRequest> execute() { - return Main.getRequestProcessor().processTopRequest(super.getSettings()); - } } \ No newline at end of file