Improved Request-business inside API

This commit is contained in:
Artemis-the-gr8 2022-08-08 17:24:22 +02:00
parent 3a9eb86c1e
commit eeb164e194
26 changed files with 518 additions and 495 deletions

View File

@ -2,6 +2,7 @@ package com.gmail.artemis.the.gr8.playerstats;
import com.gmail.artemis.the.gr8.playerstats.api.PlayerStats;
import com.gmail.artemis.the.gr8.playerstats.api.PlayerStatsAPI;
import com.gmail.artemis.the.gr8.playerstats.api.StatFormatter;
import com.gmail.artemis.the.gr8.playerstats.commands.ReloadCommand;
import com.gmail.artemis.the.gr8.playerstats.commands.ShareCommand;
import com.gmail.artemis.the.gr8.playerstats.commands.StatCommand;
@ -9,7 +10,7 @@ import com.gmail.artemis.the.gr8.playerstats.commands.TabCompleter;
import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler;
import com.gmail.artemis.the.gr8.playerstats.listeners.JoinListener;
import com.gmail.artemis.the.gr8.playerstats.msg.OutputManager;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatRetriever;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatCalculator;
import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler;
import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
@ -29,6 +30,7 @@ public final class Main extends JavaPlugin {
private static OutputManager outputManager;
private static ShareManager shareManager;
private static StatCalculator statCalculator;
private static ThreadManager threadManager;
private static PlayerStats playerStatsAPI;
@ -82,6 +84,13 @@ public final class Main extends JavaPlugin {
return config;
}
public static @NotNull OfflinePlayerHandler getOfflinePlayerHandler() throws IllegalStateException {
if (offlinePlayerHandler == null) {
throw new IllegalStateException("PlayerStats does not seem to be fully loaded!");
}
return offlinePlayerHandler;
}
public static @NotNull EnumHandler getEnumHandler() {
if (enumHandler == null) {
enumHandler = new EnumHandler();
@ -89,11 +98,18 @@ public final class Main extends JavaPlugin {
return enumHandler;
}
public static @NotNull OfflinePlayerHandler getOfflinePlayerHandler() throws IllegalStateException {
if (offlinePlayerHandler == null) {
throw new IllegalStateException("PlayerStats does not seem to be fully loaded!");
public static @NotNull StatCalculator getStatCalculator() throws IllegalStateException {
if (statCalculator == null) {
throw new IllegalStateException("PlayerStats does not seem to be loaded!");
}
return offlinePlayerHandler;
return statCalculator;
}
public static @NotNull StatFormatter getStatFormatter() throws IllegalStateException {
if (outputManager == null) {
throw new IllegalStateException("PlayerStats does not seem to be loaded!");
}
return outputManager;
}
public static @NotNull PlayerStats getPlayerStatsAPI() throws IllegalStateException {
@ -112,9 +128,9 @@ public final class Main extends JavaPlugin {
shareManager = new ShareManager(config);
outputManager = new OutputManager(getAdventure(), config, shareManager);
StatRetriever statRetriever = new StatRetriever(offlinePlayerHandler);
threadManager = new ThreadManager(config, statRetriever, outputManager);
statCalculator = new StatCalculator(offlinePlayerHandler);
threadManager = new ThreadManager(config, statCalculator, outputManager);
playerStatsAPI = new PlayerStatsAPI(statRetriever, outputManager, offlinePlayerHandler);
playerStatsAPI = new PlayerStatsAPI(outputManager, offlinePlayerHandler);
}
}

View File

@ -2,10 +2,10 @@ package com.gmail.artemis.the.gr8.playerstats;
import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler;
import com.gmail.artemis.the.gr8.playerstats.enums.StandardMessage;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestSettings;
import com.gmail.artemis.the.gr8.playerstats.msg.OutputManager;
import com.gmail.artemis.the.gr8.playerstats.reload.ReloadThread;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatRetriever;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatCalculator;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatThread;
import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger;
import org.bukkit.command.CommandSender;
@ -25,17 +25,17 @@ public final class ThreadManager {
private static ConfigHandler config;
private static OutputManager outputManager;
private static StatRetriever statRetriever;
private static StatCalculator statCalculator;
private ReloadThread lastActiveReloadThread;
private StatThread lastActiveStatThread;
private final HashMap<String, Thread> statThreads;
private static long lastRecordedCalcTime;
public ThreadManager(ConfigHandler config, StatRetriever statRetriever, OutputManager outputManager) {
public ThreadManager(ConfigHandler config, StatCalculator statCalculator, OutputManager outputManager) {
ThreadManager.config = config;
ThreadManager.outputManager = outputManager;
ThreadManager.statRetriever = statRetriever;
ThreadManager.statCalculator = statCalculator;
statThreads = new HashMap<>();
statThreadID = 0;
@ -61,19 +61,19 @@ public final class ThreadManager {
}
}
public void startStatThread(StatRequest statRequest) {
public void startStatThread(RequestSettings requestSettings) {
statThreadID += 1;
String cmdSender = statRequest.getCommandSender().getName();
String cmdSender = requestSettings.getCommandSender().getName();
if (config.limitStatRequests() && statThreads.containsKey(cmdSender)) {
Thread runningThread = statThreads.get(cmdSender);
if (runningThread.isAlive()) {
outputManager.sendFeedbackMsg(statRequest.getCommandSender(), StandardMessage.REQUEST_ALREADY_RUNNING);
outputManager.sendFeedbackMsg(requestSettings.getCommandSender(), StandardMessage.REQUEST_ALREADY_RUNNING);
} else {
startNewStatThread(statRequest);
startNewStatThread(requestSettings);
}
} else {
startNewStatThread(statRequest);
startNewStatThread(requestSettings);
}
}
@ -89,9 +89,9 @@ public final class ThreadManager {
return lastRecordedCalcTime;
}
private void startNewStatThread(StatRequest statRequest) {
lastActiveStatThread = new StatThread(outputManager, statRetriever, statThreadID, statRequest, lastActiveReloadThread);
statThreads.put(statRequest.getCommandSender().getName(), lastActiveStatThread);
private void startNewStatThread(RequestSettings requestSettings) {
lastActiveStatThread = new StatThread(outputManager, statCalculator, statThreadID, requestSettings, lastActiveReloadThread);
statThreads.put(requestSettings.getCommandSender().getName(), lastActiveStatThread);
lastActiveStatThread.start();
}
}

View File

@ -1,6 +1,7 @@
package com.gmail.artemis.the.gr8.playerstats.api;
import com.gmail.artemis.the.gr8.playerstats.Main;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
@ -9,7 +10,7 @@ import org.jetbrains.annotations.NotNull;
{@link PlayerStatsAPI}. You can then use this object to access any of the further methods.
<br>
<br>Since calculating a top or server statistics can take some time, I strongly
encourage you to call all the serverStat() and topStat() methods asynchronously.
encourage you to call {@link StatRequest#execute()} asynchronously.
Otherwise, the main Thread will have to wait until all calculations are done,
and this can severely impact server performance.
*/

View File

@ -1,7 +1,5 @@
package com.gmail.artemis.the.gr8.playerstats.api;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatRetriever;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.*;
import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
@ -11,24 +9,14 @@ import static org.jetbrains.annotations.ApiStatus.Internal;
public final class PlayerStatsAPI implements PlayerStats, StatManager {
private final OfflinePlayerHandler offlinePlayerHandler;
private static StatRetriever statRetriever;
private static StatFormatter statFormatter;
@Internal
public PlayerStatsAPI(StatRetriever stat, StatFormatter format, OfflinePlayerHandler offlinePlayers) {
statRetriever = stat;
public PlayerStatsAPI(StatFormatter format, OfflinePlayerHandler offlinePlayers) {
statFormatter = format;
offlinePlayerHandler = offlinePlayers;
}
static StatRetriever statCalculator() {
return statRetriever;
}
static StatFormatter statFormatter() {
return statFormatter;
}
@Override
public Formatter getFormatter() {
return statFormatter;
@ -40,26 +28,26 @@ public final class PlayerStatsAPI implements PlayerStats, StatManager {
}
@Override
public RequestGenerator<Integer> getPlayerStat(String playerName) {
StatRequest request = StatRequestHandler.getBasicPlayerStatRequest(playerName);
public RequestGenerator<Integer> playerStatRequest(String playerName) {
RequestSettings request = RequestHandler.getBasicPlayerStatRequest(playerName);
return new PlayerStatRequest(request);
}
@Override
public ServerStatRequest calculateServerStat() {
StatRequest request = StatRequestHandler.getBasicServerStatRequest();
public ServerStatRequest serverStatRequest() {
RequestSettings request = RequestHandler.getBasicServerStatRequest();
return new ServerStatRequest(request);
}
@Override
public TopStatRequest calculateTopStat(int topListSize) {
StatRequest request = StatRequestHandler.getBasicTopStatRequest(topListSize);
public TopStatRequest topStatRequest(int topListSize) {
RequestSettings request = RequestHandler.getBasicTopStatRequest(topListSize);
return new TopStatRequest(request);
}
@Override
public TopStatRequest calculateTotalTopStatList() {
public TopStatRequest totalTopStatListRequest() {
int playerCount = offlinePlayerHandler.getOfflinePlayerCount();
return calculateTopStat(playerCount);
return topStatRequest(playerCount);
}
}

View File

@ -1,20 +0,0 @@
package com.gmail.artemis.the.gr8.playerstats.api;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatRetriever;
import com.gmail.artemis.the.gr8.playerstats.statistic.result.StatResult;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest;
/** Completes a basic {@link StatRequest} provided by the {@link PlayerStatsAPI}
and performs a statistic lookup with the information that is stored inside this StatRequest.*/
public interface RequestExecutor<T> {
static StatRetriever getStatCalculator() {
return PlayerStatsAPI.statCalculator();
}
static StatFormatter getStatFormatter() {
return PlayerStatsAPI.statFormatter();
}
StatResult<T> execute();
}

View File

@ -1,25 +1,25 @@
package com.gmail.artemis.the.gr8.playerstats.api;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatRetriever;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatCalculator;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest;
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;
/** Turns user input into a completed {@link StatRequest}. This StatRequest should hold all
the information PlayerStats needs to work with, and is used by the {@link StatRetriever}
/** Is responsible for creating an executable {@link StatRequest}. This Request holds all
the information PlayerStats needs to work with, and is used by the {@link StatCalculator}
to get the desired statistic data.*/
public interface RequestGenerator<T> {
/** Gets a StatRequest for a Statistic of Statistic.Type {@code Untyped}.
/** Gets an executable Request object for a Statistic of Statistic.Type {@code Untyped}.
@param statistic a Statistic of Type.Untyped
@return a {@link StatRequest}
@throws IllegalArgumentException if <code>statistic</code> is not of Type.Untyped*/
RequestExecutor<T> untyped(@NotNull Statistic statistic) throws IllegalArgumentException;
StatRequest<T> untyped(@NotNull Statistic statistic) throws IllegalArgumentException;
/** Gets a StatRequest for a Statistic of Statistic.Type Block or Item.
/** Gets an executable Request object for a Statistic of Statistic.Type Block or Item.
@param statistic a Statistic of Type.Block or Type.Item
@param material a block if the <code>statistic</code> is of Type.Block,
@ -28,13 +28,13 @@ public interface RequestGenerator<T> {
@throws IllegalArgumentException if <code>statistic</code> is not of Type.Block
(with a block as <code>material</code>), or <code>statistic</code> is not of Type.Item
(with an item as <code>material</code>) */
RequestExecutor<T> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException;
StatRequest<T> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException;
/** Gets a StatRequest for a Statistic of Statistic.Type Entity.
/** Gets an executable Request object for a Statistic of Statistic.Type Entity.
@param statistic a Statistic of Type.Entity
@param entityType an EntityType
@return a {@link StatRequest}
@throws IllegalArgumentException if <code>statistic</code> is not of Type.Entity*/
RequestExecutor<T> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException;
StatRequest<T> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException;
}

View File

@ -1,6 +1,7 @@
package com.gmail.artemis.the.gr8.playerstats.api;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestSettings;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatCalculator;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.*;
import org.jetbrains.annotations.ApiStatus.Internal;
@ -8,7 +9,7 @@ import org.jetbrains.annotations.ApiStatus.Internal;
import java.util.LinkedHashMap;
/** The {@link StatFormatter} formats raw numbers into pretty messages.
This Formatter takes a {@link StatRequest} and combines it with the raw number(s)
This Formatter takes a {@link RequestSettings} object and combines it with the raw number(s)
returned by the {@link StatCalculator}, and transforms those into a pretty message
(by default a TextComponent) with all the relevant information in it.
<br>
@ -28,16 +29,16 @@ interface StatFormatter extends Formatter {
/** @return a TextComponent with the following parts:
<br>[player-name]: [number] [stat-name] {sub-stat-name}*/
TextComponent formatPlayerStat(StatRequest statRequest, int playerStat);
TextComponent formatPlayerStat(RequestSettings requestSettings, int playerStat);
/** @return a TextComponent with the following parts:
<br>[Total on] [server-name]: [number] [stat-name] [sub-stat-name]*/
TextComponent formatServerStat(StatRequest statRequest, long serverStat);
TextComponent formatServerStat(RequestSettings requestSettings, long serverStat);
/** @return a TextComponent with the following parts:
<br>[PlayerStats] [Top 10] [stat-name] [sub-stat-name]
<br> [1.] [player-name] [number]
<br> [2.] [player-name] [number]
<br> [3.] etc...*/
TextComponent formatTopStat(StatRequest statRequest, LinkedHashMap<String, Integer> topStats);
TextComponent formatTopStat(RequestSettings requestSettings, LinkedHashMap<String, Integer> topStats);
}

View File

@ -4,32 +4,33 @@ import java.util.LinkedHashMap;
public interface StatManager {
/** Gets a StatRequest object that can be used to look up a player-statistic.
This StatRequest will have all default settings already configured,
and will be processed as soon as you call one of its methods.
/** Gets a RequestGenerator that can be used to create a PlayerStatRequest.
This RequestGenerator will make sure all default settings
for a player-statistic-lookup are configured.
@return a PlayerStatRequest that can be used to look up a statistic for the
Player whose name is provided*/
RequestGenerator<Integer> getPlayerStat(String playerName);
@param playerName the player whose statistic is being requested
@return the RequestGenerator */
RequestGenerator<Integer> playerStatRequest(String playerName);
/** Gets a StatRequest object that can be used to look up a server-statistic.
This StatRequest will have all default settings already configured,
and will be processed as soon as you call one of its methods.
<br>
<br> Don't call this from the main Thread! (see class description)
/** Gets a RequestGenerator that can be used to create a ServerStatRequest.
This RequestGenerator will make sure all default settings
for a server-statistic-lookup are configured.
@return a ServerStatRequest that can be used to look up a server total*/
RequestGenerator<Long> calculateServerStat();
@return the RequestGenerator*/
RequestGenerator<Long> serverStatRequest();
/** Gets a StatRequest object that can be used to look up a top-x-statistic.
This StatRequest will have all default settings already configured, and will be
processed as soon as you call one of its methods.
<br>
<br> Don't call this from the main Thread! (see class description)
/** Gets a RequestGenerator that can be used to create a TopStatRequest
for a top-list of the specified size. This RequestGenerator will
make sure all default settings for a top-statistic-lookup are configured.
@param topListSize how big the top-x should be (10 by default)
@return a TopStatRequest that can be used to look up a top statistic*/
RequestGenerator<LinkedHashMap<String, Integer>> calculateTopStat(int topListSize);
@return the RequestGenerator*/
RequestGenerator<LinkedHashMap<String, Integer>> topStatRequest(int topListSize);
RequestGenerator<LinkedHashMap<String, Integer>> calculateTotalTopStatList();
/** Gets a RequestGenerator that can be used to create a TopStatRequest
for all offline players on the server. This RequestGenerator will make sure
all default settings for a top-statistic-lookup are configured.
@return the RequestGenerator*/
RequestGenerator<LinkedHashMap<String, Integer>> totalTopStatListRequest();
}

View File

@ -3,8 +3,8 @@ package com.gmail.artemis.the.gr8.playerstats.commands;
import com.gmail.artemis.the.gr8.playerstats.ThreadManager;
import com.gmail.artemis.the.gr8.playerstats.enums.StandardMessage;
import com.gmail.artemis.the.gr8.playerstats.enums.Target;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequestHandler;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestSettings;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestHandler;
import com.gmail.artemis.the.gr8.playerstats.msg.OutputManager;
import org.bukkit.Statistic;
import org.bukkit.command.Command;
@ -32,10 +32,10 @@ public final class StatCommand implements CommandExecutor {
outputManager.sendExamples(sender);
}
else {
StatRequest baseRequest = StatRequestHandler.getBasicInternalStatRequest(sender);
StatRequestHandler statRequestHandler = new StatRequestHandler(baseRequest);
RequestSettings baseRequest = RequestHandler.getBasicInternalStatRequest(sender);
RequestHandler requestHandler = new RequestHandler(baseRequest);
StatRequest completedRequest = statRequestHandler.getRequestFromArgs(args);
RequestSettings completedRequest = requestHandler.getRequestFromArgs(args);
if (completedRequest.isValid()) {
threadManager.startStatThread(completedRequest);
} else {
@ -46,7 +46,7 @@ public final class StatCommand implements CommandExecutor {
return true;
}
/** If a given {@link StatRequest} does not result in a valid statistic look-up,
/** If a given {@link RequestSettings} does not result in a valid statistic look-up,
this will send a feedback message to the CommandSender that made the request.
<br> The following is checked:
<ul>
@ -54,23 +54,23 @@ public final class StatCommand implements CommandExecutor {
<li>Is a <code>subStatEntry</code> needed, and if so, is a corresponding Material/EntityType present?
<li>If the <code>target</code> is Player, is a valid <code>playerName</code> provided?
</ul>
@param statRequest the StatRequest to give feedback on
@param requestSettings the RequestSettings to give feedback on
*/
private void sendFeedback(StatRequest statRequest) {
CommandSender sender = statRequest.getCommandSender();
private void sendFeedback(RequestSettings requestSettings) {
CommandSender sender = requestSettings.getCommandSender();
if (statRequest.getStatistic() == null) {
if (requestSettings.getStatistic() == null) {
outputManager.sendFeedbackMsg(sender, StandardMessage.MISSING_STAT_NAME);
}
else if (statRequest.getTarget() == Target.PLAYER && statRequest.getPlayerName() == null) {
else if (requestSettings.getTarget() == Target.PLAYER && requestSettings.getPlayerName() == null) {
outputManager.sendFeedbackMsg(sender, StandardMessage.MISSING_PLAYER_NAME);
}
else {
Statistic.Type type = statRequest.getStatistic().getType();
if (type != Statistic.Type.UNTYPED && statRequest.getSubStatEntryName() == null) {
Statistic.Type type = requestSettings.getStatistic().getType();
if (type != Statistic.Type.UNTYPED && requestSettings.getSubStatEntryName() == null) {
outputManager.sendFeedbackMsgMissingSubStat(sender, type);
} else {
outputManager.sendFeedbackMsgWrongSubStat(sender, type, statRequest.getSubStatEntryName());
outputManager.sendFeedbackMsgWrongSubStat(sender, type, requestSettings.getSubStatEntryName());
}
}
}

View File

@ -5,7 +5,7 @@ import com.gmail.artemis.the.gr8.playerstats.enums.Target;
import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler;
import com.gmail.artemis.the.gr8.playerstats.enums.Unit;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestSettings;
import com.gmail.artemis.the.gr8.playerstats.msg.components.ComponentFactory;
import com.gmail.artemis.the.gr8.playerstats.msg.components.ExampleMessage;
import com.gmail.artemis.the.gr8.playerstats.msg.components.HelpMessage;
@ -182,15 +182,15 @@ public final class MessageBuilder {
<p>- Integer shareCode: if a shareCode is provided, a clickable "share" button will be added.
<br>- CommandSender sender: if a sender is provided, a signature with "shared by sender-name" will be added.</br>
<br>- If both parameters are null, the formattedValue will be returned as is.</br>*/
public BiFunction<Integer, CommandSender, TextComponent> formattedPlayerStatFunction(int stat, @NotNull StatRequest statRequest) {
public BiFunction<Integer, CommandSender, TextComponent> formattedPlayerStatFunction(int stat, @NotNull RequestSettings requestSettings) {
TextComponent playerStat = Component.text()
.append(componentFactory.playerName(statRequest.getPlayerName(), Target.PLAYER)
.append(componentFactory.playerName(requestSettings.getPlayerName(), Target.PLAYER)
.append(text(":"))
.append(space()))
.append(getStatNumberComponent(statRequest, stat))
.append(getStatNumberComponent(requestSettings, stat))
.append(space())
.append(getStatNameComponent(statRequest))
.append(getStatUnitComponent(statRequest.getStatistic(), statRequest.getTarget())) //space is provided by statUnitComponent
.append(getStatNameComponent(requestSettings))
.append(getStatUnitComponent(requestSettings.getStatistic(), requestSettings.getTarget())) //space is provided by statUnitComponent
.build();
return getFormattingFunction(playerStat, Target.PLAYER);
@ -201,16 +201,16 @@ public final class MessageBuilder {
<p>- Integer shareCode: if a shareCode is provided, a clickable "share" button will be added.
<br>- CommandSender sender: if a sender is provided, a signature with "shared by sender-name" will be added.</br>
<br>- If both parameters are null, the formattedValue will be returned as is.</br>*/
public BiFunction<Integer, CommandSender, TextComponent> formattedServerStatFunction(long stat, @NotNull StatRequest statRequest) {
public BiFunction<Integer, CommandSender, TextComponent> formattedServerStatFunction(long stat, @NotNull RequestSettings requestSettings) {
TextComponent serverStat = text()
.append(componentFactory.title(config.getServerTitle(), Target.SERVER))
.append(space())
.append(componentFactory.serverName(config.getServerName()))
.append(space())
.append(getStatNumberComponent(statRequest, stat))
.append(getStatNumberComponent(requestSettings, stat))
.append(space())
.append(getStatNameComponent(statRequest))
.append(getStatUnitComponent(statRequest.getStatistic(), statRequest.getTarget())) //space is provided by statUnit
.append(getStatNameComponent(requestSettings))
.append(getStatUnitComponent(requestSettings.getStatistic(), requestSettings.getTarget())) //space is provided by statUnit
.build();
return getFormattingFunction(serverStat, Target.SERVER);
@ -221,10 +221,10 @@ public final class MessageBuilder {
<p>- Integer shareCode: if a shareCode is provided, a clickable "share" button will be added.
<br>- CommandSender sender: if a sender is provided, a signature with "shared by sender-name" will be added.</br>
<br>- If both parameters are null, the formattedValue will be returned as is.</br>*/
public BiFunction<Integer, CommandSender, TextComponent> formattedTopStatFunction(@NotNull LinkedHashMap<String, Integer> topStats, @NotNull StatRequest statRequest) {
final TextComponent title = getTopStatsTitleComponent(statRequest, topStats.size());
final TextComponent shortTitle = getTopStatDescription(statRequest, topStats.size());
final TextComponent list = getTopStatListComponent(topStats, statRequest);
public BiFunction<Integer, CommandSender, TextComponent> formattedTopStatFunction(@NotNull LinkedHashMap<String, Integer> topStats, @NotNull RequestSettings requestSettings) {
final TextComponent title = getTopStatsTitleComponent(requestSettings, topStats.size());
final TextComponent shortTitle = getTopStatDescription(requestSettings, topStats.size());
final TextComponent list = getTopStatListComponent(topStats, requestSettings);
final boolean useEnters = config.useEnters(Target.TOP, false);
final boolean useEntersForShared = config.useEnters(Target.TOP, true);
@ -296,25 +296,25 @@ public final class MessageBuilder {
return componentFactory.sharerName(sender.getName());
}
private TextComponent getTopStatsTitleComponent(StatRequest statRequest, int statListSize) {
private TextComponent getTopStatsTitleComponent(RequestSettings requestSettings, int statListSize) {
return Component.text()
.append(componentFactory.pluginPrefix()).append(space())
.append(componentFactory.title(config.getTopStatsTitle(), Target.TOP)).append(space())
.append(componentFactory.titleNumber(statListSize)).append(space())
.append(getStatNameComponent(statRequest)) //space is provided by statUnitComponent
.append(getStatUnitComponent(statRequest.getStatistic(), statRequest.getTarget()))
.append(getStatNameComponent(requestSettings)) //space is provided by statUnitComponent
.append(getStatUnitComponent(requestSettings.getStatistic(), requestSettings.getTarget()))
.build();
}
private TextComponent getTopStatDescription(StatRequest statRequest, int statListSize) {
private TextComponent getTopStatDescription(RequestSettings requestSettings, int statListSize) {
return Component.text()
.append(componentFactory.title(config.getTopStatsTitle(), Target.TOP)).append(space())
.append(componentFactory.titleNumber(statListSize)).append(space())
.append(getStatNameComponent(statRequest)) //space is provided by statUnitComponent
.append(getStatNameComponent(requestSettings)) //space is provided by statUnitComponent
.build();
}
private TextComponent getTopStatListComponent(LinkedHashMap<String, Integer> topStats, StatRequest statRequest) {
private TextComponent getTopStatListComponent(LinkedHashMap<String, Integer> topStats, RequestSettings requestSettings) {
TextComponent.Builder topList = Component.text();
Set<String> playerNames = topStats.keySet();
boolean useDots = config.useDots();
@ -331,7 +331,7 @@ public final class MessageBuilder {
else {
topList.append(componentFactory.playerName(playerName + ":", Target.TOP));
}
topList.append(space()).append(getStatNumberComponent(statRequest, topStats.get(playerName)));
topList.append(space()).append(getStatNumberComponent(requestSettings, topStats.get(playerName)));
}
return topList.build();
}
@ -350,30 +350,30 @@ public final class MessageBuilder {
/** Depending on the config settings, return either a TranslatableComponent representing
the statName (and potential subStatName), or a TextComponent with capitalized English names.*/
private TextComponent getStatNameComponent(StatRequest statRequest) {
private TextComponent getStatNameComponent(RequestSettings requestSettings) {
if (config.useTranslatableComponents()) {
String statKey = languageKeyHandler.getStatKey(statRequest.getStatistic());
String subStatKey = statRequest.getSubStatEntryName();
String statKey = languageKeyHandler.getStatKey(requestSettings.getStatistic());
String subStatKey = requestSettings.getSubStatEntryName();
if (subStatKey != null) {
switch (statRequest.getStatistic().getType()) {
case BLOCK -> subStatKey = languageKeyHandler.getBlockKey(statRequest.getBlock());
case ENTITY -> subStatKey = languageKeyHandler.getEntityKey(statRequest.getEntity());
case ITEM -> subStatKey = languageKeyHandler.getItemKey(statRequest.getItem());
switch (requestSettings.getStatistic().getType()) {
case BLOCK -> subStatKey = languageKeyHandler.getBlockKey(requestSettings.getBlock());
case ENTITY -> subStatKey = languageKeyHandler.getEntityKey(requestSettings.getEntity());
case ITEM -> subStatKey = languageKeyHandler.getItemKey(requestSettings.getItem());
default -> {
}
}
}
return componentFactory.statAndSubStatNameTranslatable(statKey, subStatKey, statRequest.getTarget());
return componentFactory.statAndSubStatNameTranslatable(statKey, subStatKey, requestSettings.getTarget());
}
else {
return componentFactory.statAndSubStatName(
StringUtils.prettify(statRequest.getStatistic().toString()),
StringUtils.prettify(statRequest.getSubStatEntryName()),
statRequest.getTarget());
StringUtils.prettify(requestSettings.getStatistic().toString()),
StringUtils.prettify(requestSettings.getSubStatEntryName()),
requestSettings.getTarget());
}
}
private TextComponent getStatNumberComponent(StatRequest request, long statNumber) {
private TextComponent getStatNumberComponent(RequestSettings request, long statNumber) {
return getStatNumberComponent(request.getStatistic(), request.getTarget(), statNumber);
}

View File

@ -5,7 +5,7 @@ import com.gmail.artemis.the.gr8.playerstats.api.StatFormatter;
import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler;
import com.gmail.artemis.the.gr8.playerstats.enums.StandardMessage;
import com.gmail.artemis.the.gr8.playerstats.msg.components.ComponentFactory;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestSettings;
import com.gmail.artemis.the.gr8.playerstats.msg.components.BukkitConsoleComponentFactory;
import com.gmail.artemis.the.gr8.playerstats.msg.components.PrideComponentFactory;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
@ -77,19 +77,19 @@ public final class OutputManager implements StatFormatter {
}
@Override
public TextComponent formatPlayerStat(@NotNull StatRequest statRequest, int playerStat) {
public TextComponent formatPlayerStat(@NotNull RequestSettings requestSettings, int playerStat) {
BiFunction<Integer, CommandSender, TextComponent> playerStatFunction =
getMessageBuilder(statRequest).formattedPlayerStatFunction(playerStat, statRequest);
getMessageBuilder(requestSettings).formattedPlayerStatFunction(playerStat, requestSettings);
return processFunction(statRequest.getCommandSender(), playerStatFunction);
return processFunction(requestSettings.getCommandSender(), playerStatFunction);
}
@Override
public TextComponent formatServerStat(@NotNull StatRequest statRequest, long serverStat) {
public TextComponent formatServerStat(@NotNull RequestSettings requestSettings, long serverStat) {
BiFunction<Integer, CommandSender, TextComponent> serverStatFunction =
getMessageBuilder(statRequest).formattedServerStatFunction(serverStat, statRequest);
getMessageBuilder(requestSettings).formattedServerStatFunction(serverStat, requestSettings);
return processFunction(statRequest.getCommandSender(), serverStatFunction);
return processFunction(requestSettings.getCommandSender(), serverStatFunction);
}
@Override
@ -98,11 +98,11 @@ public final class OutputManager implements StatFormatter {
}
@Override
public TextComponent formatTopStat(@NotNull StatRequest statRequest, @NotNull LinkedHashMap<String, Integer> topStats) {
public TextComponent formatTopStat(@NotNull RequestSettings requestSettings, @NotNull LinkedHashMap<String, Integer> topStats) {
BiFunction<Integer, CommandSender, TextComponent> topStatFunction =
getMessageBuilder(statRequest).formattedTopStatFunction(topStats, statRequest);
getMessageBuilder(requestSettings).formattedTopStatFunction(topStats, requestSettings);
return processFunction(statRequest.getCommandSender(), topStatFunction);
return processFunction(requestSettings.getCommandSender(), topStatFunction);
}
public void sendFeedbackMsg(@NotNull CommandSender sender, StandardMessage message) {
@ -168,8 +168,8 @@ public final class OutputManager implements StatFormatter {
return sender instanceof ConsoleCommandSender ? consoleMessageBuilder : messageBuilder;
}
private MessageBuilder getMessageBuilder(StatRequest statRequest) {
if (statRequest.isAPIRequest() || !statRequest.isConsoleSender()) {
private MessageBuilder getMessageBuilder(RequestSettings requestSettings) {
if (requestSettings.isAPIRequest() || !requestSettings.isConsoleSender()) {
return messageBuilder;
} else {
return consoleMessageBuilder;

View File

@ -7,7 +7,7 @@ import com.gmail.artemis.the.gr8.playerstats.enums.DebugLevel;
import com.gmail.artemis.the.gr8.playerstats.enums.StandardMessage;
import com.gmail.artemis.the.gr8.playerstats.msg.OutputManager;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatThread;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatRetriever;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatCalculator;
import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger;
import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
import org.bukkit.Bukkit;
@ -47,7 +47,7 @@ public final class ReloadThread extends Thread {
/** This method will perform a series of tasks. If a {@link StatThread} is still running,
it will join the statThread and wait for it to finish. Then, it will reload the config,
update the offlinePlayerList in the {@link OfflinePlayerHandler}, update the {@link DebugLevel},
update the share-settings in {@link ShareManager} and topListSize-settings in {@link StatRetriever},
update the share-settings in {@link ShareManager} and topListSize-settings in {@link StatCalculator},
and update the MessageBuilders in the {@link OutputManager}.*/
@Override
public void run() {

View File

@ -1,7 +1,7 @@
package com.gmail.artemis.the.gr8.playerstats.statistic;
import com.gmail.artemis.the.gr8.playerstats.ThreadManager;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestSettings;
import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger;
import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
import com.google.common.collect.ImmutableList;
@ -18,7 +18,7 @@ final class StatAction extends RecursiveTask<ConcurrentHashMap<String, Integer>>
private final OfflinePlayerHandler offlinePlayerHandler;
private final ImmutableList<String> playerNames;
private final StatRequest statRequest;
private final RequestSettings requestSettings;
private final ConcurrentHashMap<String, Integer> allStats;
/**
@ -26,15 +26,15 @@ final class StatAction extends RecursiveTask<ConcurrentHashMap<String, Integer>>
* using the default ForkJoinPool, and returns the ConcurrentHashMap when everything is done
* @param offlinePlayerHandler the OfflinePlayerHandler to convert playerNames into Players
* @param playerNames ImmutableList of playerNames for players that should be included in stat calculations
* @param statRequest a validated statRequest
* @param requestSettings a validated requestSettings
* @param allStats the ConcurrentHashMap to put the results on
*/
public StatAction(OfflinePlayerHandler offlinePlayerHandler, ImmutableList<String> playerNames, StatRequest statRequest, ConcurrentHashMap<String, Integer> allStats) {
public StatAction(OfflinePlayerHandler offlinePlayerHandler, ImmutableList<String> playerNames, RequestSettings requestSettings, ConcurrentHashMap<String, Integer> allStats) {
threshold = ThreadManager.getTaskThreshold();
this.offlinePlayerHandler = offlinePlayerHandler;
this.playerNames = playerNames;
this.statRequest = statRequest;
this.requestSettings = requestSettings;
this.allStats = allStats;
MyLogger.subActionCreated(Thread.currentThread().getName());
@ -46,8 +46,8 @@ final class StatAction extends RecursiveTask<ConcurrentHashMap<String, Integer>>
return getStatsDirectly();
}
else {
final StatAction subTask1 = new StatAction(offlinePlayerHandler, playerNames.subList(0, playerNames.size()/2), statRequest, allStats);
final StatAction subTask2 = new StatAction(offlinePlayerHandler, playerNames.subList(playerNames.size()/2, playerNames.size()), statRequest, allStats);
final StatAction subTask1 = new StatAction(offlinePlayerHandler, playerNames.subList(0, playerNames.size()/2), requestSettings, allStats);
final StatAction subTask2 = new StatAction(offlinePlayerHandler, playerNames.subList(playerNames.size()/2, playerNames.size()), requestSettings, allStats);
//queue and compute all subtasks in the right order
subTask1.fork();
@ -65,11 +65,11 @@ final class StatAction extends RecursiveTask<ConcurrentHashMap<String, Integer>>
OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(playerName);
if (player != null) {
int statistic = 0;
switch (statRequest.getStatistic().getType()) {
case UNTYPED -> statistic = player.getStatistic(statRequest.getStatistic());
case ENTITY -> statistic = player.getStatistic(statRequest.getStatistic(), statRequest.getEntity());
case BLOCK -> statistic = player.getStatistic(statRequest.getStatistic(), statRequest.getBlock());
case ITEM -> statistic = player.getStatistic(statRequest.getStatistic(), statRequest.getItem());
switch (requestSettings.getStatistic().getType()) {
case UNTYPED -> statistic = player.getStatistic(requestSettings.getStatistic());
case ENTITY -> statistic = player.getStatistic(requestSettings.getStatistic(), requestSettings.getEntity());
case BLOCK -> statistic = player.getStatistic(requestSettings.getStatistic(), requestSettings.getBlock());
case ITEM -> statistic = player.getStatistic(requestSettings.getStatistic(), requestSettings.getItem());
}
if (statistic > 0) {
allStats.put(playerName, statistic);

View File

@ -2,7 +2,7 @@ package com.gmail.artemis.the.gr8.playerstats.statistic;
import com.gmail.artemis.the.gr8.playerstats.ThreadManager;
import com.gmail.artemis.the.gr8.playerstats.enums.DebugLevel;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestSettings;
import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger;
import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
import com.google.common.collect.ImmutableList;
@ -14,37 +14,37 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;
public final class StatRetriever {
public final class StatCalculator {
private final OfflinePlayerHandler offlinePlayerHandler;
public StatRetriever(OfflinePlayerHandler offlinePlayerHandler) {
public StatCalculator(OfflinePlayerHandler offlinePlayerHandler) {
this.offlinePlayerHandler = offlinePlayerHandler;
}
public int getPlayerStat(StatRequest statRequest) {
OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(statRequest.getPlayerName());
public int getPlayerStat(RequestSettings requestSettings) {
OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(requestSettings.getPlayerName());
if (player != null) {
return switch (statRequest.getStatistic().getType()) {
case UNTYPED -> player.getStatistic(statRequest.getStatistic());
case ENTITY -> player.getStatistic(statRequest.getStatistic(), statRequest.getEntity());
case BLOCK -> player.getStatistic(statRequest.getStatistic(), statRequest.getBlock());
case ITEM -> player.getStatistic(statRequest.getStatistic(), statRequest.getItem());
return switch (requestSettings.getStatistic().getType()) {
case UNTYPED -> player.getStatistic(requestSettings.getStatistic());
case ENTITY -> player.getStatistic(requestSettings.getStatistic(), requestSettings.getEntity());
case BLOCK -> player.getStatistic(requestSettings.getStatistic(), requestSettings.getBlock());
case ITEM -> player.getStatistic(requestSettings.getStatistic(), requestSettings.getItem());
};
}
return 0;
}
public LinkedHashMap<String, Integer> getTopStats(StatRequest statRequest) {
return getAllStatsAsync(statRequest).entrySet().stream()
public LinkedHashMap<String, Integer> getTopStats(RequestSettings requestSettings) {
return getAllStatsAsync(requestSettings).entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.limit(statRequest.getTopListSize())
.limit(requestSettings.getTopListSize())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
public long getServerStat(StatRequest statRequest) {
List<Integer> numbers = getAllStatsAsync(statRequest)
public long getServerStat(RequestSettings requestSettings) {
List<Integer> numbers = getAllStatsAsync(requestSettings)
.values()
.parallelStream()
.toList();
@ -53,16 +53,16 @@ public final class StatRetriever {
/** Invokes a bunch of worker pool threads to divide and conquer (get the statistics for all players
that are stored in the {@link OfflinePlayerHandler}) */
private @NotNull ConcurrentHashMap<String, Integer> getAllStatsAsync(StatRequest statRequest) {
private @NotNull ConcurrentHashMap<String, Integer> getAllStatsAsync(RequestSettings requestSettings) {
long time = System.currentTimeMillis();
ForkJoinPool commonPool = ForkJoinPool.commonPool();
ConcurrentHashMap<String, Integer> allStats;
try {
allStats = commonPool.invoke(getStatTask(statRequest));
allStats = commonPool.invoke(getStatTask(requestSettings));
} catch (ConcurrentModificationException e) {
MyLogger.logMsg("The statRequest could not be executed due to a ConcurrentModificationException. " +
MyLogger.logMsg("The requestSettings could not be executed due to a ConcurrentModificationException. " +
"This likely happened because Bukkit hasn't fully initialized all player-data yet. " +
"Try again and it should be fine!", true);
throw new ConcurrentModificationException(e.toString());
@ -75,12 +75,12 @@ public final class StatRetriever {
return allStats;
}
private StatAction getStatTask(StatRequest statRequest) {
private StatAction getStatTask(RequestSettings requestSettings) {
int size = offlinePlayerHandler.getOfflinePlayerCount() != 0 ? offlinePlayerHandler.getOfflinePlayerCount() : 16;
ConcurrentHashMap<String, Integer> allStats = new ConcurrentHashMap<>(size);
ImmutableList<String> playerNames = ImmutableList.copyOf(offlinePlayerHandler.getOfflinePlayerNames());
StatAction task = new StatAction(offlinePlayerHandler, playerNames, statRequest, allStats);
StatAction task = new StatAction(offlinePlayerHandler, playerNames, requestSettings, allStats);
MyLogger.actionCreated(playerNames.size());
return task;

View File

@ -3,7 +3,7 @@ package com.gmail.artemis.the.gr8.playerstats.statistic;
import com.gmail.artemis.the.gr8.playerstats.enums.StandardMessage;
import com.gmail.artemis.the.gr8.playerstats.enums.Target;
import com.gmail.artemis.the.gr8.playerstats.msg.components.ComponentUtils;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.RequestSettings;
import com.gmail.artemis.the.gr8.playerstats.msg.OutputManager;
import com.gmail.artemis.the.gr8.playerstats.reload.ReloadThread;
import com.gmail.artemis.the.gr8.playerstats.ThreadManager;
@ -17,19 +17,19 @@ import java.util.*;
public final class StatThread extends Thread {
private static OutputManager outputManager;
private static StatRetriever statRetriever;
private static StatCalculator statCalculator;
private final ReloadThread reloadThread;
private final StatRequest statRequest;
private final RequestSettings requestSettings;
public StatThread(OutputManager m, StatRetriever t, int ID, StatRequest s, @Nullable ReloadThread r) {
public StatThread(OutputManager m, StatCalculator t, int ID, RequestSettings s, @Nullable ReloadThread r) {
outputManager = m;
statRetriever = t;
statCalculator = t;
reloadThread = r;
statRequest = s;
requestSettings = s;
this.setName("StatThread-" + statRequest.getCommandSender().getName() + "-" + ID);
this.setName("StatThread-" + requestSettings.getCommandSender().getName() + "-" + ID);
MyLogger.threadCreated(this.getName());
}
@ -37,13 +37,13 @@ public final class StatThread extends Thread {
public void run() throws IllegalStateException, NullPointerException {
MyLogger.threadStart(this.getName());
if (statRequest == null) {
throw new NullPointerException("No statistic statRequest was found!");
if (requestSettings == null) {
throw new NullPointerException("No statistic requestSettings was found!");
}
if (reloadThread != null && reloadThread.isAlive()) {
try {
MyLogger.waitingForOtherThread(this.getName(), reloadThread.getName());
outputManager.sendFeedbackMsg(statRequest.getCommandSender(), StandardMessage.STILL_RELOADING);
outputManager.sendFeedbackMsg(requestSettings.getCommandSender(), StandardMessage.STILL_RELOADING);
reloadThread.join();
} catch (InterruptedException e) {
@ -54,28 +54,28 @@ public final class StatThread extends Thread {
long lastCalc = ThreadManager.getLastRecordedCalcTime();
if (lastCalc > 2000) {
outputManager.sendFeedbackMsgWaitAMoment(statRequest.getCommandSender(), lastCalc > 20000);
outputManager.sendFeedbackMsgWaitAMoment(requestSettings.getCommandSender(), lastCalc > 20000);
}
Target selection = statRequest.getTarget();
Target selection = requestSettings.getTarget();
try {
TextComponent statResult = switch (selection) {
case PLAYER -> outputManager.formatPlayerStat(statRequest, statRetriever.getPlayerStat(statRequest));
case TOP -> outputManager.formatTopStat(statRequest, statRetriever.getTopStats(statRequest));
case SERVER -> outputManager.formatServerStat(statRequest, statRetriever.getServerStat(statRequest));
case PLAYER -> outputManager.formatPlayerStat(requestSettings, statCalculator.getPlayerStat(requestSettings));
case TOP -> outputManager.formatTopStat(requestSettings, statCalculator.getTopStats(requestSettings));
case SERVER -> outputManager.formatServerStat(requestSettings, statCalculator.getServerStat(requestSettings));
};
if (statRequest.isAPIRequest()) {
if (requestSettings.isAPIRequest()) {
String msg = ComponentUtils.getTranslatableComponentSerializer()
.serialize(statResult);
statRequest.getCommandSender().sendMessage(msg);
requestSettings.getCommandSender().sendMessage(msg);
}
else {
outputManager.sendToCommandSender(statRequest.getCommandSender(), statResult);
outputManager.sendToCommandSender(requestSettings.getCommandSender(), statResult);
}
}
catch (ConcurrentModificationException e) {
if (!statRequest.isConsoleSender()) {
outputManager.sendFeedbackMsg(statRequest.getCommandSender(), StandardMessage.UNKNOWN_ERROR);
if (!requestSettings.isConsoleSender()) {
outputManager.sendFeedbackMsg(requestSettings.getCommandSender(), StandardMessage.UNKNOWN_ERROR);
}
}
}

View File

@ -1,7 +1,7 @@
package com.gmail.artemis.the.gr8.playerstats.statistic.request;
import com.gmail.artemis.the.gr8.playerstats.Main;
import com.gmail.artemis.the.gr8.playerstats.api.RequestGenerator;
import com.gmail.artemis.the.gr8.playerstats.api.RequestExecutor;
import com.gmail.artemis.the.gr8.playerstats.statistic.result.PlayerStatResult;
import com.gmail.artemis.the.gr8.playerstats.statistic.result.StatResult;
import net.kyori.adventure.text.TextComponent;
@ -10,43 +10,42 @@ import org.bukkit.Statistic;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;
public final class PlayerStatRequest implements RequestGenerator<Integer>, RequestExecutor<Integer> {
public final class PlayerStatRequest extends StatRequest<Integer> implements RequestGenerator<Integer> {
private final StatRequest statRequest;
private final StatRequestHandler statRequestHandler;
private final RequestHandler requestHandler;
public PlayerStatRequest(StatRequest request) {
statRequest = request;
statRequestHandler = new StatRequestHandler(request);
public PlayerStatRequest(RequestSettings request) {
super(request);
requestHandler = new RequestHandler(request);
}
@Override
public RequestExecutor<Integer> untyped(@NotNull Statistic statistic) {
StatRequest completedRequest = statRequestHandler.untyped(statistic);
public StatRequest<Integer> untyped(@NotNull Statistic statistic) {
RequestSettings completedRequest = requestHandler.untyped(statistic);
return new PlayerStatRequest(completedRequest);
}
@Override
public RequestExecutor<Integer> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
StatRequest completedRequest = statRequestHandler.blockOrItemType(statistic, material);
public StatRequest<Integer> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
RequestSettings completedRequest = requestHandler.blockOrItemType(statistic, material);
return new PlayerStatRequest(completedRequest);
}
@Override
public RequestExecutor<Integer> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
StatRequest completedRequest = statRequestHandler.entityType(statistic, entityType);
public StatRequest<Integer> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
RequestSettings completedRequest = requestHandler.entityType(statistic, entityType);
return new PlayerStatRequest(completedRequest);
}
@Override
public StatResult<Integer> execute() {
return getStatResult(statRequest);
return getStatResult(super.requestSettings);
}
private PlayerStatResult getStatResult(StatRequest completedRequest) {
int stat = RequestExecutor.getStatCalculator()
private PlayerStatResult getStatResult(RequestSettings completedRequest) {
int stat = Main.getStatCalculator()
.getPlayerStat(completedRequest);
TextComponent prettyStat = RequestExecutor.getStatFormatter()
TextComponent prettyStat = Main.getStatFormatter()
.formatPlayerStat(completedRequest, stat);
return new PlayerStatResult(stat, prettyStat);

View File

@ -1,7 +1,6 @@
package com.gmail.artemis.the.gr8.playerstats.statistic.request;
import com.gmail.artemis.the.gr8.playerstats.Main;
import com.gmail.artemis.the.gr8.playerstats.api.RequestGenerator;
import com.gmail.artemis.the.gr8.playerstats.enums.Target;
import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler;
import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
@ -13,29 +12,29 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public final class StatRequestHandler {
public final class RequestHandler {
private final StatRequest statRequest;
private final RequestSettings requestSettings;
public StatRequestHandler(StatRequest request) {
statRequest = request;
public RequestHandler(RequestSettings request) {
requestSettings = request;
}
public static StatRequest getBasicPlayerStatRequest(String playerName) {
StatRequest request = StatRequest.getBasicAPIRequest();
public static RequestSettings getBasicPlayerStatRequest(String playerName) {
RequestSettings request = RequestSettings.getBasicAPIRequest();
request.setTarget(Target.PLAYER);
request.setPlayerName(playerName);
return request;
}
public static StatRequest getBasicServerStatRequest() {
StatRequest request = StatRequest.getBasicAPIRequest();
public static RequestSettings getBasicServerStatRequest() {
RequestSettings request = RequestSettings.getBasicAPIRequest();
request.setTarget(Target.SERVER);
return request;
}
public static StatRequest getBasicTopStatRequest(int topListSize) {
StatRequest request = StatRequest.getBasicAPIRequest();
public static RequestSettings getBasicTopStatRequest(int topListSize) {
RequestSettings request = RequestSettings.getBasicAPIRequest();
request.setTarget(Target.TOP);
request.setTopListSize(topListSize != 0 ? topListSize : Main.getConfigHandler().getTopListMaxSize());
return request;
@ -44,129 +43,129 @@ public final class StatRequestHandler {
/**
@param sender the CommandSender that requested this specific statistic
*/
public static StatRequest getBasicInternalStatRequest(CommandSender sender) {
StatRequest request = StatRequest.getBasicRequest(sender);
public static RequestSettings getBasicInternalStatRequest(CommandSender sender) {
RequestSettings request = RequestSettings.getBasicRequest(sender);
request.setTopListSize(Main.getConfigHandler().getTopListMaxSize());
return request;
}
public StatRequest untyped(@NotNull Statistic statistic) throws IllegalArgumentException {
public RequestSettings untyped(@NotNull Statistic statistic) throws IllegalArgumentException {
if (statistic.getType() == Statistic.Type.UNTYPED) {
statRequest.setStatistic(statistic);
return statRequest;
requestSettings.setStatistic(statistic);
return requestSettings;
}
throw new IllegalArgumentException("This statistic is not of Type.Untyped");
}
public StatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException {
public RequestSettings blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException {
Statistic.Type type = statistic.getType();
if (type == Statistic.Type.BLOCK && material.isBlock()) {
statRequest.setBlock(material);
requestSettings.setBlock(material);
}
else if (type == Statistic.Type.ITEM && material.isItem()){
statRequest.setItem(material);
requestSettings.setItem(material);
}
else {
throw new IllegalArgumentException("Either this statistic is not of Type.Block or Type.Item, or no valid block or item has been provided");
}
statRequest.setSubStatEntryName(material.toString());
return statRequest;
requestSettings.setSubStatEntryName(material.toString());
return requestSettings;
}
public StatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException {
public RequestSettings entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException {
if (statistic.getType() == Statistic.Type.ENTITY) {
statRequest.setSubStatEntryName(entityType.toString());
statRequest.setEntity(entityType);
return statRequest;
requestSettings.setSubStatEntryName(entityType.toString());
requestSettings.setEntity(entityType);
return requestSettings;
}
throw new IllegalArgumentException("This statistic is not of Type.Entity");
}
/**
This will create a {@link StatRequest} from the provided args, with the requesting Player (or Console)
as CommandSender. This CommandSender will receive feedback messages if the StatRequest could not be created.
This will create a {@link RequestSettings} from the provided args, with the requesting Player (or Console)
as CommandSender. This CommandSender will receive feedback messages if the RequestSettings could not be created.
@param args an Array of args such as a CommandSender would put in Minecraft chat:
<p>- a <code>statName</code> (example: "mine_block")</p>
<p>- if applicable, a <code>subStatEntryName</code> (example: diorite)(</p>
<p>- a <code>target</code> for this lookup: can be "top", "server", "player" (or "me" to indicate the current CommandSender)</p>
<p>- if "player" was chosen, include a <code>playerName</code></p>
@return the generated StatRequest
@return the generated RequestSettings
*/
public StatRequest getRequestFromArgs(String[] args) {
public RequestSettings getRequestFromArgs(String[] args) {
EnumHandler enumHandler = Main.getEnumHandler();
OfflinePlayerHandler offlinePlayerHandler = Main.getOfflinePlayerHandler();
CommandSender sender = statRequest.getCommandSender();
CommandSender sender = requestSettings.getCommandSender();
for (String arg : args) {
//check for statName
if (enumHandler.isStatistic(arg) && statRequest.getStatistic() == null) {
statRequest.setStatistic(EnumHandler.getStatEnum(arg));
if (enumHandler.isStatistic(arg) && requestSettings.getStatistic() == null) {
requestSettings.setStatistic(EnumHandler.getStatEnum(arg));
}
//check for subStatEntry and playerFlag
else if (enumHandler.isSubStatEntry(arg)) {
if (arg.equalsIgnoreCase("player") && !statRequest.getPlayerFlag()) {
statRequest.setPlayerFlag(true);
if (arg.equalsIgnoreCase("player") && !requestSettings.getPlayerFlag()) {
requestSettings.setPlayerFlag(true);
} else {
if (statRequest.getSubStatEntryName() == null) statRequest.setSubStatEntryName(arg);
if (requestSettings.getSubStatEntryName() == null) requestSettings.setSubStatEntryName(arg);
}
}
//check for selection
else if (arg.equalsIgnoreCase("top")) {
statRequest.setTarget(Target.TOP);
requestSettings.setTarget(Target.TOP);
} else if (arg.equalsIgnoreCase("server")) {
statRequest.setTarget(Target.SERVER);
requestSettings.setTarget(Target.SERVER);
} else if (arg.equalsIgnoreCase("me")) {
if (sender instanceof Player) {
statRequest.setPlayerName(sender.getName());
statRequest.setTarget(Target.PLAYER);
requestSettings.setPlayerName(sender.getName());
requestSettings.setTarget(Target.PLAYER);
} else if (sender instanceof ConsoleCommandSender) {
statRequest.setTarget(Target.SERVER);
requestSettings.setTarget(Target.SERVER);
}
} else if (offlinePlayerHandler.isRelevantPlayer(arg) && statRequest.getPlayerName() == null) {
statRequest.setPlayerName(arg);
statRequest.setTarget(Target.PLAYER);
} else if (offlinePlayerHandler.isRelevantPlayer(arg) && requestSettings.getPlayerName() == null) {
requestSettings.setPlayerName(arg);
requestSettings.setTarget(Target.PLAYER);
} else if (arg.equalsIgnoreCase("api")) {
statRequest.setAPIRequest();
requestSettings.setAPIRequest();
}
}
patchRequest(statRequest);
return statRequest;
patchRequest(requestSettings);
return requestSettings;
}
/**
Adjust the StatRequest object if needed: unpack the playerFlag into a subStatEntry,
Adjust the RequestSettings object if needed: unpack the playerFlag into a subStatEntry,
try to retrieve the corresponding Enum Constant for any relevant block/entity/item,
and remove any unnecessary subStatEntries.
*/
private void patchRequest(StatRequest statRequest) {
if (statRequest.getStatistic() != null) {
Statistic.Type type = statRequest.getStatistic().getType();
private void patchRequest(RequestSettings requestSettings) {
if (requestSettings.getStatistic() != null) {
Statistic.Type type = requestSettings.getStatistic().getType();
if (statRequest.getPlayerFlag()) { //unpack the playerFlag
if (type == Statistic.Type.ENTITY && statRequest.getSubStatEntryName() == null) {
statRequest.setSubStatEntryName("player");
if (requestSettings.getPlayerFlag()) { //unpack the playerFlag
if (type == Statistic.Type.ENTITY && requestSettings.getSubStatEntryName() == null) {
requestSettings.setSubStatEntryName("player");
} else {
statRequest.setTarget(Target.PLAYER);
requestSettings.setTarget(Target.PLAYER);
}
}
String subStatEntry = statRequest.getSubStatEntryName();
String subStatEntry = requestSettings.getSubStatEntryName();
switch (type) { //attempt to convert relevant subStatEntries into their corresponding Enum Constant
case BLOCK -> {
Material block = EnumHandler.getBlockEnum(subStatEntry);
if (block != null) statRequest.setBlock(block);
if (block != null) requestSettings.setBlock(block);
}
case ENTITY -> {
EntityType entity = EnumHandler.getEntityEnum(subStatEntry);
if (entity != null) statRequest.setEntity(entity);
if (entity != null) requestSettings.setEntity(entity);
}
case ITEM -> {
Material item = EnumHandler.getItemEnum(subStatEntry);
if (item != null) statRequest.setItem(item);
if (item != null) requestSettings.setItem(item);
}
case UNTYPED -> { //remove unnecessary subStatEntries
if (subStatEntry != null) statRequest.setSubStatEntryName(null);
if (subStatEntry != null) requestSettings.setSubStatEntryName(null);
}
}
}

View File

@ -0,0 +1,186 @@
package com.gmail.artemis.the.gr8.playerstats.statistic.request;
import com.gmail.artemis.the.gr8.playerstats.api.RequestGenerator;
import com.gmail.artemis.the.gr8.playerstats.enums.Target;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;
/** The object PlayerStats uses to calculate and format the requested statistic.
The settings in this RequestSettings object can be configured from two different sources:
<br>- Internally: by PlayerStats itself when /stat is called, using the args provided by the CommandSender.
<br>- Externally: through the API methods provided by the {@link RequestGenerator} interface.
<br>
<br>For this RequestSettings object to be valid, the following values need to be set:
<ul>
<li> a {@link Statistic} <code>statistic</code> </li>
<li> if this Statistic is not of {@link Statistic.Type} Untyped, a <code>subStatEntryName</code> needs to be set,
together with one of the following values:
<br>- for Type.Block: a {@link Material} <code>blockMaterial</code>
<br>- for Type.Item: a {@link Material} <code>itemMaterial</code>
<br>- for Type.Entity: an {@link EntityType} <code>entityType</code>
<li> a {@link Target} <code>target</code> (automatically set for all API-requests)
<li> if the <code>target</code> is Target.Player, a <code>playerName</code> needs to be added
</ul>*/
public final class RequestSettings {
private final CommandSender sender;
private boolean isAPIRequest;
private Statistic statistic;
private String playerName;
private Target target;
private int topListSize;
private String subStatEntryName;
private EntityType entity;
private Material block;
private Material item;
private boolean playerFlag;
/** Create a new {@link RequestSettings} with default values:
<br>- CommandSender sender (provided)
<br>- Target target = {@link Target#TOP}
<br>- int topListSize = 10
<br>- boolean playerFlag = false
<br>- boolean isAPIRequest
@param sender the CommandSender who prompted this RequestGenerator
@param isAPIRequest whether this RequestGenerator is coming through the API or the onCommand
*/
private RequestSettings(@NotNull CommandSender sender, boolean isAPIRequest) {
this.sender = sender;
this.isAPIRequest = isAPIRequest;
target = Target.TOP;
playerFlag = false;
}
public static RequestSettings getBasicRequest(CommandSender sender) {
return new RequestSettings(sender, false);
}
public static RequestSettings getBasicAPIRequest() {
return new RequestSettings(Bukkit.getConsoleSender(), true);
}
public void setAPIRequest() {
this.isAPIRequest = true;
}
public boolean isAPIRequest() {
return isAPIRequest;
}
public @NotNull CommandSender getCommandSender() {
return sender;
}
public boolean isConsoleSender() {
return sender instanceof ConsoleCommandSender;
}
public void setStatistic(Statistic statistic) {
this.statistic = statistic;
}
public Statistic getStatistic() {
return statistic;
}
public void setSubStatEntryName(String subStatEntry) {
this.subStatEntryName = subStatEntry;
}
public String getSubStatEntryName() {
return subStatEntryName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public String getPlayerName() {
return playerName;
}
public void setPlayerFlag(boolean playerFlag) {
this.playerFlag = playerFlag;
}
public boolean getPlayerFlag() {
return playerFlag;
}
public void setTarget(Target target) {
this.target = target;
}
public @NotNull Target getTarget() {
return target;
}
public void setTopListSize(int topListSize) {
this.topListSize = topListSize;
}
public int getTopListSize() {
return this.topListSize;
}
public void setEntity(EntityType entity) {
this.entity = entity;
}
public EntityType getEntity() {
return entity;
}
public void setBlock(Material block) {
this.block = block;
}
public Material getBlock() {
return block;
}
public void setItem(Material item) {
this.item = item;
}
public Material getItem() {
return item;
}
public boolean isValid() {
if (statistic == null) {
return false;
} else if (target == Target.PLAYER && playerName == null) {
return false;
} else if (statistic.getType() != Statistic.Type.UNTYPED &&
subStatEntryName == null) {
return false;
} else {
return hasMatchingSubStat();
}
}
private boolean hasMatchingSubStat() {
switch (statistic.getType()) {
case BLOCK -> {
return block != null;
}
case ENTITY -> {
return entity != null;
}
case ITEM -> {
return item != null;
}
default -> {
return true;
}
}
}
}

View File

@ -1,6 +1,6 @@
package com.gmail.artemis.the.gr8.playerstats.statistic.request;
import com.gmail.artemis.the.gr8.playerstats.api.RequestExecutor;
import com.gmail.artemis.the.gr8.playerstats.Main;
import com.gmail.artemis.the.gr8.playerstats.api.RequestGenerator;
import com.gmail.artemis.the.gr8.playerstats.statistic.result.ServerStatResult;
import com.gmail.artemis.the.gr8.playerstats.statistic.result.StatResult;
@ -10,43 +10,42 @@ import org.bukkit.Statistic;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;
public final class ServerStatRequest implements RequestGenerator<Long>, RequestExecutor<Long> {
public final class ServerStatRequest extends StatRequest<Long> implements RequestGenerator<Long> {
private final StatRequest statRequest;
private final StatRequestHandler statRequestHandler;
private final RequestHandler requestHandler;
public ServerStatRequest(StatRequest request) {
statRequest = request;
statRequestHandler = new StatRequestHandler(statRequest);
public ServerStatRequest(RequestSettings request) {
super(request);
requestHandler = new RequestHandler(requestSettings);
}
@Override
public RequestExecutor<Long> untyped(@NotNull Statistic statistic) {
StatRequest completedRequest = statRequestHandler.untyped(statistic);
public StatRequest<Long> untyped(@NotNull Statistic statistic) {
RequestSettings completedRequest = requestHandler.untyped(statistic);
return new ServerStatRequest(completedRequest);
}
@Override
public RequestExecutor<Long> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
StatRequest completedRequest = statRequestHandler.blockOrItemType(statistic, material);
public StatRequest<Long> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
RequestSettings completedRequest = requestHandler.blockOrItemType(statistic, material);
return new ServerStatRequest(completedRequest);
}
@Override
public RequestExecutor<Long> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
StatRequest completedRequest = statRequestHandler.entityType(statistic, entityType);
public StatRequest<Long> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
RequestSettings completedRequest = requestHandler.entityType(statistic, entityType);
return new ServerStatRequest(completedRequest);
}
@Override
public StatResult<Long> execute() {
return getStatResult(statRequest);
return getStatResult(requestSettings);
}
private ServerStatResult getStatResult(StatRequest completedRequest) {
long stat = RequestExecutor.getStatCalculator()
private ServerStatResult getStatResult(RequestSettings completedRequest) {
long stat = Main.getStatCalculator()
.getServerStat(completedRequest);
TextComponent prettyStat = RequestExecutor.getStatFormatter()
TextComponent prettyStat = Main.getStatFormatter()
.formatServerStat(completedRequest, stat);
return new ServerStatResult(stat, prettyStat);

View File

@ -1,186 +1,40 @@
package com.gmail.artemis.the.gr8.playerstats.statistic.request;
import com.gmail.artemis.the.gr8.playerstats.api.RequestGenerator;
import com.gmail.artemis.the.gr8.playerstats.enums.Target;
import org.bukkit.Bukkit;
import com.gmail.artemis.the.gr8.playerstats.statistic.result.StatResult;
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/** A StatRequest is the object PlayerStats uses to calculate and format the requested statistic.
This object can be generated from two different sources:
<br>- Internally: by PlayerStats itself when /stat is called, using the args provided by the CommandSender.
<br>- Externally: through the API methods provided by the {@link RequestGenerator} interface.
<br>
<br>For this StatRequest to be valid, it needs the following values:
<ul>
<li> a {@link Statistic} <code>statistic</code> </li>
<li> if this Statistic is not of {@link Statistic.Type} Untyped, a <code>subStatEntryName</code> needs to be set,
together with one of the following values:
<br>- for Type.Block: a {@link Material} <code>blockMaterial</code>
<br>- for Type.Item: a {@link Material} <code>itemMaterial</code>
<br>- for Type.Entity: an {@link EntityType} <code>entityType</code>
<li> a {@link Target} <code>target</code> (automatically set for all API-requests)
<li> if the <code>target</code> is Target.Player, a <code>playerName</code> needs to be added
</ul>*/
public final class StatRequest {
public abstract class StatRequest<T> {
private final CommandSender sender;
private boolean isAPIRequest;
private Statistic statistic;
private String playerName;
private Target target;
private int topListSize;
protected final RequestSettings requestSettings;
private String subStatEntryName;
private EntityType entity;
private Material block;
private Material item;
private boolean playerFlag;
protected StatRequest(RequestSettings request) {
requestSettings = request;
}
/** Create a new {@link StatRequest} with default values:
<br>- CommandSender sender (provided)
<br>- Target target = {@link Target#TOP}
<br>- int topListSize = 10
<br>- boolean playerFlag = false
<br>- boolean isAPIRequest
/** Don't call this from the Main Thread!*/
public abstract StatResult<T> execute();
@param sender the CommandSender who prompted this RequestGenerator
@param isAPIRequest whether this RequestGenerator is coming through the API or the onCommand
*/
private StatRequest(@NotNull CommandSender sender, boolean isAPIRequest) {
this.sender = sender;
this.isAPIRequest = isAPIRequest;
target = Target.TOP;
playerFlag = false;
}
public Statistic getStatistic() {
return requestSettings.getStatistic();
}
public static StatRequest getBasicRequest(CommandSender sender) {
return new StatRequest(sender, false);
}
public @Nullable Material getBlock() {
return requestSettings.getBlock();
}
public static StatRequest getBasicAPIRequest() {
return new StatRequest(Bukkit.getConsoleSender(), true);
}
public @Nullable Material getItem() {
return requestSettings.getItem();
}
public void setAPIRequest() {
this.isAPIRequest = true;
}
public @Nullable EntityType getEntity() {
return requestSettings.getEntity();
}
public boolean isAPIRequest() {
return isAPIRequest;
}
public @NotNull CommandSender getCommandSender() {
return sender;
}
public boolean isConsoleSender() {
return sender instanceof ConsoleCommandSender;
}
public void setStatistic(Statistic statistic) {
this.statistic = statistic;
}
public Statistic getStatistic() {
return statistic;
}
public void setSubStatEntryName(String subStatEntry) {
this.subStatEntryName = subStatEntry;
}
public String getSubStatEntryName() {
return subStatEntryName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public String getPlayerName() {
return playerName;
}
public void setPlayerFlag(boolean playerFlag) {
this.playerFlag = playerFlag;
}
public boolean getPlayerFlag() {
return playerFlag;
}
public void setTarget(Target target) {
this.target = target;
}
public @NotNull Target getTarget() {
return target;
}
public void setTopListSize(int topListSize) {
this.topListSize = topListSize;
}
public int getTopListSize() {
return this.topListSize;
}
public void setEntity(EntityType entity) {
this.entity = entity;
}
public EntityType getEntity() {
return entity;
}
public void setBlock(Material block) {
this.block = block;
}
public Material getBlock() {
return block;
}
public void setItem(Material item) {
this.item = item;
}
public Material getItem() {
return item;
}
public boolean isValid() {
if (statistic == null) {
return false;
} else if (target == Target.PLAYER && playerName == null) {
return false;
} else if (statistic.getType() != Statistic.Type.UNTYPED &&
subStatEntryName == null) {
return false;
} else {
return hasMatchingSubStat();
}
}
private boolean hasMatchingSubStat() {
switch (statistic.getType()) {
case BLOCK -> {
return block != null;
}
case ENTITY -> {
return entity != null;
}
case ITEM -> {
return item != null;
}
default -> {
return true;
}
}
}
public Target getTarget() {
return requestSettings.getTarget();
}
}

View File

@ -1,6 +1,6 @@
package com.gmail.artemis.the.gr8.playerstats.statistic.request;
import com.gmail.artemis.the.gr8.playerstats.api.RequestExecutor;
import com.gmail.artemis.the.gr8.playerstats.Main;
import com.gmail.artemis.the.gr8.playerstats.api.RequestGenerator;
import com.gmail.artemis.the.gr8.playerstats.statistic.result.StatResult;
import com.gmail.artemis.the.gr8.playerstats.statistic.result.TopStatResult;
@ -12,43 +12,42 @@ import org.jetbrains.annotations.NotNull;
import java.util.LinkedHashMap;
public final class TopStatRequest implements RequestGenerator<LinkedHashMap<String, Integer>>, RequestExecutor<LinkedHashMap<String, Integer>> {
public final class TopStatRequest extends StatRequest<LinkedHashMap<String, Integer>> implements RequestGenerator<LinkedHashMap<String, Integer>> {
private final StatRequest statRequest;
private final StatRequestHandler statRequestHandler;
private final RequestHandler requestHandler;
public TopStatRequest(StatRequest request) {
statRequest = request;
statRequestHandler = new StatRequestHandler(request);
public TopStatRequest(RequestSettings request) {
super(request);
requestHandler = new RequestHandler(request);
}
@Override
public TopStatRequest untyped(@NotNull Statistic statistic) {
StatRequest completedRequest = statRequestHandler.untyped(statistic);
public StatRequest<LinkedHashMap<String, Integer>> untyped(@NotNull Statistic statistic) {
RequestSettings completedRequest = requestHandler.untyped(statistic);
return new TopStatRequest(completedRequest);
}
@Override
public TopStatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
StatRequest completedRequest = statRequestHandler.blockOrItemType(statistic, material);
RequestSettings completedRequest = requestHandler.blockOrItemType(statistic, material);
return new TopStatRequest(completedRequest);
}
@Override
public TopStatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
StatRequest completedRequest = statRequestHandler.entityType(statistic, entityType);
RequestSettings completedRequest = requestHandler.entityType(statistic, entityType);
return new TopStatRequest(completedRequest);
}
@Override
public StatResult<LinkedHashMap<String, Integer>> execute() {
return getStatResult(statRequest);
return getStatResult(super.requestSettings);
}
private TopStatResult getStatResult(StatRequest completedRequest) {
LinkedHashMap<String, Integer> stat = RequestExecutor.getStatCalculator()
private TopStatResult getStatResult(RequestSettings completedRequest) {
LinkedHashMap<String, Integer> stat = Main.getStatCalculator()
.getTopStats(completedRequest);
TextComponent prettyStat = RequestExecutor.getStatFormatter()
TextComponent prettyStat = Main.getStatFormatter()
.formatTopStat(completedRequest, stat);
return new TopStatResult(stat, prettyStat);

View File

@ -23,7 +23,7 @@ public record InternalStatResult(String executorName, TextComponent formattedVal
}
@Override
public String toString() {
public String getFormattedString() {
return ComponentUtils.getTranslatableComponentSerializer()
.serialize(formattedValue);
}

View File

@ -16,7 +16,7 @@ public record PlayerStatResult(int value, TextComponent formattedValue) implemen
}
@Override
public String toString() {
public String getFormattedString() {
return ComponentUtils.getTranslatableComponentSerializer()
.serialize(formattedValue);
}

View File

@ -16,7 +16,7 @@ public record ServerStatResult(long value, TextComponent formattedValue) impleme
}
@Override
public String toString() {
public String getFormattedString() {
return ComponentUtils.getTranslatableComponentSerializer()
.serialize(formattedValue);
}

View File

@ -33,7 +33,7 @@ import net.kyori.adventure.text.TextComponent;
Information on how to get and use the BukkitAudiences object can be
found on <a href="https://docs.adventure.kyori.net/platform/bukkit.html">Adventure's website</a>.
<p>You can also use the provided {@link #toString()} method to get the same information
<p>You can also use the provided {@link #getFormattedString()} method to get the same information
in String-format. Don't use Adventure's toString methods on the Components
- those are for debugging purposes. And finally, if you want the results to be
formatted differently, you can get an instance of the {@link Formatter}.
@ -59,5 +59,5 @@ public interface StatResult<T> {
the same style and color settings that are specified in the PlayerStats config,
but it is not translatable (it is always plain English). See class description
for more information.*/
String toString();
String getFormattedString();
}

View File

@ -18,7 +18,7 @@ public record TopStatResult(LinkedHashMap<String, Integer> value, TextComponent
}
@Override
public String toString() {
public String getFormattedString() {
return ComponentUtils.getTranslatableComponentSerializer()
.serialize(formattedValue);
}