Separated request-generating from request-executing to keep all execute-logic within the statistic package (#114)

This commit is contained in:
Artemis-the-gr8 2022-10-16 15:23:13 +02:00
parent 03efe136b0
commit abf85b3948
18 changed files with 239 additions and 219 deletions

View File

@ -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() {

View File

@ -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<String, Thread> 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();
}

View File

@ -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!
*
* <p> 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.
*
* <p> 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
* <code>ClassDefNotFoundExceptions</code>.
*
* @return the version of PlayerStatsImpl present on the server
* @return the version of PlayerStatsAPI present on the server
*/
default String getVersion() {
return "1.8";

View File

@ -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;
}
}

View File

@ -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<Integer> playerStatRequest(String playerName) {
return new PlayerStatRequest(playerName);
}
@Override
public RequestGenerator<Long> serverStatRequest() {
return new ServerStatRequest();
}
@Override
public RequestGenerator<LinkedHashMap<String, Integer>> topStatRequest(int topListSize) {
return new TopStatRequest(topListSize);
}
@Override
public RequestGenerator<LinkedHashMap<String, Integer>> totalTopStatRequest() {
int playerCount = offlinePlayerHandler.getOfflinePlayerCount();
return topStatRequest(playerCount);
}
}

View File

@ -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 <code>NumberFormatter</code>
*/
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 <code>NumberFormatter</code>
*/
default NumberFormatter getNumberFormatter() {
return new NumberFormatter();
}
String textComponentToString(TextComponent component);
/**
* Gets the default prefix PlayerStats uses.

View File

@ -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<Integer> playerStatRequest(String playerName);
RequestGenerator<Integer> 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<Integer> executePlayerStatRequest(StatRequest<Integer> 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<Long> serverStatRequest();
RequestGenerator<Long> 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<Long> executeServerStatRequest(StatRequest<Long> 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<LinkedHashMap<String, Integer>> topStatRequest(int topListSize);
RequestGenerator<LinkedHashMap<String, Integer>> 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<LinkedHashMap<String, Integer>> totalTopStatRequest();
}
RequestGenerator<LinkedHashMap<String, Integer>> 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<LinkedHashMap<String, Integer>> executeTopRequest(StatRequest<LinkedHashMap<String, Integer>> request);
}

View File

@ -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();

View File

@ -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<StandardMessage, Function<MessageBuilder, TextComponent>> 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:
* <br>[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");
}

View File

@ -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()

View File

@ -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);
}
}

View File

@ -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<Integer> implements Req
super.getSettings().configureEntityType(statistic, entityType);
return this;
}
@Override
public @NotNull StatResult<Integer> execute() {
return Main.getRequestProcessor().processPlayerRequest(super.getSettings());
}
}

View File

@ -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<Integer> createPlayerStatRequest(String playerName) {
return new PlayerStatRequest(playerName);
}
@Override
public StatResult<Integer> executePlayerStatRequest(StatRequest<Integer> request) {
return processor.processPlayerRequest(request.getSettings());
}
@Override
public RequestGenerator<Long> createServerStatRequest() {
return new ServerStatRequest();
}
@Override
public StatResult<Long> executeServerStatRequest(StatRequest<Long> request) {
return processor.processServerRequest(request.getSettings());
}
@Override
public RequestGenerator<LinkedHashMap<String, Integer>> createTopStatRequest(int topListSize) {
return new TopStatRequest(topListSize);
}
@Override
public RequestGenerator<LinkedHashMap<String, Integer>> createTotalTopStatRequest() {
int playerCount = offlinePlayerHandler.getOfflinePlayerCount();
return createTopStatRequest(playerCount);
}
@Override
public StatResult<LinkedHashMap<String, Integer>> executeTopRequest(StatRequest<LinkedHashMap<String, Integer>> request) {
return processor.processTopRequest(request.getSettings());
}
}

View File

@ -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<TextComponent> 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<Integer> 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<String, Integer> 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);
}

View File

@ -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<Long> implements Reques
super.getSettings().configureEntityType(statistic, entityType);
return this;
}
@Override
public @NotNull StatResult<Long> execute() {
return Main.getRequestProcessor().processServerRequest(super.getSettings());
}
}

View File

@ -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<T> {
@ -26,17 +23,6 @@ public abstract class StatRequest<T> {
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<T> execute();
/**
* Use this method to view the settings that have
* been configured for this StatRequest.

View File

@ -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) {

View File

@ -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<LinkedHashMap<String, Inte
super.getSettings().configureEntityType(statistic, entityType);
return this;
}
@Override
public @NotNull StatResult<LinkedHashMap<String, Integer>> execute() {
return Main.getRequestProcessor().processTopRequest(super.getSettings());
}
}