Made an "execute" step in the API

This commit is contained in:
Artemis-the-gr8 2022-08-08 00:54:18 +02:00
parent a34bf32c5a
commit 3a9eb86c1e
16 changed files with 178 additions and 207 deletions

View File

@ -9,7 +9,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.StatManager;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatRetriever;
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;
@ -112,9 +112,9 @@ public final class Main extends JavaPlugin {
shareManager = new ShareManager(config);
outputManager = new OutputManager(getAdventure(), config, shareManager);
StatManager statManager = new StatManager(offlinePlayerHandler);
threadManager = new ThreadManager(config, statManager, outputManager);
StatRetriever statRetriever = new StatRetriever(offlinePlayerHandler);
threadManager = new ThreadManager(config, statRetriever, outputManager);
playerStatsAPI = new PlayerStatsAPI(statManager, outputManager, offlinePlayerHandler);
playerStatsAPI = new PlayerStatsAPI(statRetriever, outputManager, offlinePlayerHandler);
}
}

View File

@ -5,7 +5,7 @@ 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.msg.OutputManager;
import com.gmail.artemis.the.gr8.playerstats.reload.ReloadThread;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatManager;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatRetriever;
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 StatManager statManager;
private static StatRetriever statRetriever;
private ReloadThread lastActiveReloadThread;
private StatThread lastActiveStatThread;
private final HashMap<String, Thread> statThreads;
private static long lastRecordedCalcTime;
public ThreadManager(ConfigHandler config, StatManager statManager, OutputManager outputManager) {
public ThreadManager(ConfigHandler config, StatRetriever statRetriever, OutputManager outputManager) {
ThreadManager.config = config;
ThreadManager.outputManager = outputManager;
ThreadManager.statManager = statManager;
ThreadManager.statRetriever = statRetriever;
statThreads = new HashMap<>();
statThreadID = 0;
@ -90,7 +90,7 @@ public final class ThreadManager {
}
private void startNewStatThread(StatRequest statRequest) {
lastActiveStatThread = new StatThread(outputManager, statManager, statThreadID, statRequest, lastActiveReloadThread);
lastActiveStatThread = new StatThread(outputManager, statRetriever, statThreadID, statRequest, lastActiveReloadThread);
statThreads.put(statRequest.getCommandSender().getName(), lastActiveStatThread);
lastActiveStatThread.start();
}

View File

@ -1,9 +1,6 @@
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.PlayerStatRequest;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.ServerStatRequest;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.TopStatRequest;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
@ -27,34 +24,7 @@ public interface PlayerStats {
return Main.getPlayerStatsAPI();
}
/** 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.
@return a PlayerStatRequest that can be used to look up a statistic for the
Player whose name is provided*/
PlayerStatRequest playerStat(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)
@return a ServerStatRequest that can be used to look up a server total*/
ServerStatRequest serverStat();
/** 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)
@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*/
TopStatRequest topStat(int topListSize);
TopStatRequest totalTopStatList();
StatManager getStatManager();
Formatter getFormatter();
}

View File

@ -1,48 +1,32 @@
package com.gmail.artemis.the.gr8.playerstats.api;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatManager;
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;
import static org.jetbrains.annotations.ApiStatus.Internal;
/** The implementation of the API Interface */
public final class PlayerStatsAPI implements PlayerStats {
public final class PlayerStatsAPI implements PlayerStats, StatManager {
private final OfflinePlayerHandler offlinePlayerHandler;
private static StatCalculator statCalculator;
private static StatRetriever statRetriever;
private static StatFormatter statFormatter;
@Internal
public PlayerStatsAPI(StatManager stat, StatFormatter format, OfflinePlayerHandler offlinePlayers) {
statCalculator = stat;
public PlayerStatsAPI(StatRetriever stat, StatFormatter format, OfflinePlayerHandler offlinePlayers) {
statRetriever = stat;
statFormatter = format;
offlinePlayerHandler = offlinePlayers;
}
@Override
public PlayerStatRequest playerStat(String playerName) {
StatRequestHandler statRequestHandler = StatRequestHandler.playerRequestHandler(playerName);
return new PlayerStatRequest(statRequestHandler);
static StatRetriever statCalculator() {
return statRetriever;
}
@Override
public ServerStatRequest serverStat() {
StatRequestHandler statRequestHandler = StatRequestHandler.serverRequestHandler();
return new ServerStatRequest(statRequestHandler);
}
@Override
public TopStatRequest topStat(int topListSize) {
StatRequestHandler statRequestHandler = StatRequestHandler.topRequestHandler(topListSize);
return new TopStatRequest(statRequestHandler);
}
@Override
public TopStatRequest totalTopStatList() {
int playerCount = offlinePlayerHandler.getOfflinePlayerCount();
return topStat(playerCount);
static StatFormatter statFormatter() {
return statFormatter;
}
@Override
@ -50,11 +34,32 @@ public final class PlayerStatsAPI implements PlayerStats {
return statFormatter;
}
static StatCalculator statCalculator() {
return statCalculator;
@Override
public StatManager getStatManager() {
return this;
}
static StatFormatter statFormatter() {
return statFormatter;
@Override
public RequestGenerator<Integer> getPlayerStat(String playerName) {
StatRequest request = StatRequestHandler.getBasicPlayerStatRequest(playerName);
return new PlayerStatRequest(request);
}
@Override
public ServerStatRequest calculateServerStat() {
StatRequest request = StatRequestHandler.getBasicServerStatRequest();
return new ServerStatRequest(request);
}
@Override
public TopStatRequest calculateTopStat(int topListSize) {
StatRequest request = StatRequestHandler.getBasicTopStatRequest(topListSize);
return new TopStatRequest(request);
}
@Override
public TopStatRequest calculateTotalTopStatList() {
int playerCount = offlinePlayerHandler.getOfflinePlayerCount();
return calculateTopStat(playerCount);
}
}

View File

@ -1,51 +1,20 @@
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;
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.ApiStatus.Internal;
import org.jetbrains.annotations.NotNull;
/** 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> {
@Internal
default StatCalculator getStatCalculator() {
static StatRetriever getStatCalculator() {
return PlayerStatsAPI.statCalculator();
}
@Internal
default StatFormatter getStatFormatter() {
static StatFormatter getStatFormatter() {
return PlayerStatsAPI.statFormatter();
}
/** Gets a StatResult for a Statistic of Statistic.Type {@code Untyped}.
@param statistic a Statistic of Type.Untyped
@return a {@link StatResult}
@throws IllegalArgumentException if <code>statistic</code> is not of Type.Untyped*/
StatResult<T> untyped(@NotNull Statistic statistic) throws IllegalArgumentException;
/** Gets a StatResult 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,
and an item if the <code>statistic</code> is of Type.Item
@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>)
@return a {@link StatResult} */
StatResult<T> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException;
/** Gets a StatResult for a Statistic of Statistic.Type Entity.
@param statistic a Statistic of Type.Entity
@param entityType an EntityType
@throws IllegalArgumentException if <code>statistic</code> is not of Type.Entity,
or <code>entityType</code> is not a valid EntityType
@return a {@link StatResult} */
StatResult<T> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException;
StatResult<T> execute();
}

View File

@ -1,5 +1,6 @@
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.StatRequest;
import org.bukkit.Material;
import org.bukkit.Statistic;
@ -7,16 +8,16 @@ 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 StatCalculator}
the information PlayerStats needs to work with, and is used by the {@link StatRetriever}
to get the desired statistic data.*/
public interface RequestGenerator {
public interface RequestGenerator<T> {
/** Gets a StatRequest 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*/
StatRequest untyped(@NotNull Statistic statistic) throws IllegalArgumentException;
RequestExecutor<T> untyped(@NotNull Statistic statistic) throws IllegalArgumentException;
/** Gets a StatRequest for a Statistic of Statistic.Type Block or Item.
@ -27,7 +28,7 @@ public interface RequestGenerator {
@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>) */
StatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException;
RequestExecutor<T> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException;
/** Gets a StatRequest for a Statistic of Statistic.Type Entity.
@ -35,5 +36,5 @@ public interface RequestGenerator {
@param entityType an EntityType
@return a {@link StatRequest}
@throws IllegalArgumentException if <code>statistic</code> is not of Type.Entity*/
StatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException;
RequestExecutor<T> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException;
}

View File

@ -1,32 +0,0 @@
package com.gmail.artemis.the.gr8.playerstats.api;
import com.gmail.artemis.the.gr8.playerstats.enums.Target;
import com.gmail.artemis.the.gr8.playerstats.statistic.request.StatRequest;
import org.jetbrains.annotations.ApiStatus.Internal;
import java.util.LinkedHashMap;
/** The {@link StatCalculator} is responsible for getting, calculating and/or ordering raw numbers.
It represents the actual statistic-getting magic that happens once a valid
{@link StatRequest} is passed to it.
<br>
<br>The StatCalculator gets its data from the vanilla statistic files (stored by the server). It can return three kinds of data,
depending on the chosen {@link Target}:
<br>- int (for {@link Target#PLAYER})
<br>- long (for {@link Target#SERVER})
<br>- LinkedHashMap[String player-name, Integer number] (for {@link Target#TOP})
<br>
<br>For more information on how to create a valid StatRequest,
see the class description for {@link StatRequest}.*/
@Internal
public interface StatCalculator {
/** Returns the requested Statistic*/
int getPlayerStat(StatRequest statRequest);
/** Don't call from main Thread!*/
long getServerStat(StatRequest statRequest);
/** Don't call from main Thread!*/
LinkedHashMap<String, Integer> getTopStats(StatRequest statRequest);
}

View File

@ -0,0 +1,35 @@
package com.gmail.artemis.the.gr8.playerstats.api;
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.
@return a PlayerStatRequest that can be used to look up a statistic for the
Player whose name is provided*/
RequestGenerator<Integer> getPlayerStat(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)
@return a ServerStatRequest that can be used to look up a server total*/
RequestGenerator<Long> calculateServerStat();
/** 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)
@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);
RequestGenerator<LinkedHashMap<String, Integer>> calculateTotalTopStatList();
}

View File

@ -32,13 +32,14 @@ public final class StatCommand implements CommandExecutor {
outputManager.sendExamples(sender);
}
else {
StatRequestHandler statRequestHandler = StatRequestHandler.internalRequestHandler(sender);
StatRequest statRequest = statRequestHandler.getRequestFromArgs(args);
StatRequest baseRequest = StatRequestHandler.getBasicInternalStatRequest(sender);
StatRequestHandler statRequestHandler = new StatRequestHandler(baseRequest);
if (statRequest.isValid()) {
threadManager.startStatThread(statRequest);
StatRequest completedRequest = statRequestHandler.getRequestFromArgs(args);
if (completedRequest.isValid()) {
threadManager.startStatThread(completedRequest);
} else {
sendFeedback(statRequest);
sendFeedback(completedRequest);
return false;
}
}

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.StatManager;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatRetriever;
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 StatManager},
update the share-settings in {@link ShareManager} and topListSize-settings in {@link StatRetriever},
and update the MessageBuilders in the {@link OutputManager}.*/
@Override
public void run() {

View File

@ -1,7 +1,6 @@
package com.gmail.artemis.the.gr8.playerstats.statistic;
import com.gmail.artemis.the.gr8.playerstats.ThreadManager;
import com.gmail.artemis.the.gr8.playerstats.api.StatCalculator;
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.utils.MyLogger;
@ -15,17 +14,15 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;
public final class StatManager implements StatCalculator {
public final class StatRetriever {
private final OfflinePlayerHandler offlinePlayerHandler;
public StatManager(OfflinePlayerHandler offlinePlayerHandler) {
public StatRetriever(OfflinePlayerHandler offlinePlayerHandler) {
this.offlinePlayerHandler = offlinePlayerHandler;
}
/** Gets the statistic data for an individual player. If somehow the player
cannot be found, this returns 0.*/
@Override
public int getPlayerStat(StatRequest statRequest) {
OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(statRequest.getPlayerName());
if (player != null) {
@ -39,7 +36,6 @@ public final class StatManager implements StatCalculator {
return 0;
}
@Override
public LinkedHashMap<String, Integer> getTopStats(StatRequest statRequest) {
return getAllStatsAsync(statRequest).entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
@ -47,7 +43,6 @@ public final class StatManager implements StatCalculator {
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
@Override
public long getServerStat(StatRequest statRequest) {
List<Integer> numbers = getAllStatsAsync(statRequest)
.values()

View File

@ -17,14 +17,14 @@ import java.util.*;
public final class StatThread extends Thread {
private static OutputManager outputManager;
private static StatManager statManager;
private static StatRetriever statRetriever;
private final ReloadThread reloadThread;
private final StatRequest statRequest;
public StatThread(OutputManager m, StatManager t, int ID, StatRequest s, @Nullable ReloadThread r) {
public StatThread(OutputManager m, StatRetriever t, int ID, StatRequest s, @Nullable ReloadThread r) {
outputManager = m;
statManager = t;
statRetriever = t;
reloadThread = r;
statRequest = s;
@ -60,9 +60,9 @@ public final class StatThread extends Thread {
Target selection = statRequest.getTarget();
try {
TextComponent statResult = switch (selection) {
case PLAYER -> outputManager.formatPlayerStat(statRequest, statManager.getPlayerStat(statRequest));
case TOP -> outputManager.formatTopStat(statRequest, statManager.getTopStats(statRequest));
case SERVER -> outputManager.formatServerStat(statRequest, statManager.getServerStat(statRequest));
case PLAYER -> outputManager.formatPlayerStat(statRequest, statRetriever.getPlayerStat(statRequest));
case TOP -> outputManager.formatTopStat(statRequest, statRetriever.getTopStats(statRequest));
case SERVER -> outputManager.formatServerStat(statRequest, statRetriever.getServerStat(statRequest));
};
if (statRequest.isAPIRequest()) {
String msg = ComponentUtils.getTranslatableComponentSerializer()

View File

@ -1,5 +1,6 @@
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.api.RequestExecutor;
import com.gmail.artemis.the.gr8.playerstats.statistic.result.PlayerStatResult;
import com.gmail.artemis.the.gr8.playerstats.statistic.result.StatResult;
@ -9,36 +10,43 @@ import org.bukkit.Statistic;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;
public final class PlayerStatRequest implements RequestExecutor<Integer> {
public final class PlayerStatRequest implements RequestGenerator<Integer>, RequestExecutor<Integer> {
private final StatRequest statRequest;
private final StatRequestHandler statRequestHandler;
public PlayerStatRequest(StatRequestHandler statRequestHandler) {
this.statRequestHandler = statRequestHandler;
public PlayerStatRequest(StatRequest request) {
statRequest = request;
statRequestHandler = new StatRequestHandler(request);
}
@Override
public StatResult<Integer> untyped(@NotNull Statistic statistic) {
public RequestExecutor<Integer> untyped(@NotNull Statistic statistic) {
StatRequest completedRequest = statRequestHandler.untyped(statistic);
return getStatResult(completedRequest);
return new PlayerStatRequest(completedRequest);
}
@Override
public StatResult<Integer> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
public RequestExecutor<Integer> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
StatRequest completedRequest = statRequestHandler.blockOrItemType(statistic, material);
return getStatResult(completedRequest);
return new PlayerStatRequest(completedRequest);
}
@Override
public StatResult<Integer> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
public RequestExecutor<Integer> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
StatRequest completedRequest = statRequestHandler.entityType(statistic, entityType);
return getStatResult(completedRequest);
return new PlayerStatRequest(completedRequest);
}
@Override
public StatResult<Integer> execute() {
return getStatResult(statRequest);
}
private PlayerStatResult getStatResult(StatRequest completedRequest) {
int stat = RequestExecutor.super.getStatCalculator()
int stat = RequestExecutor.getStatCalculator()
.getPlayerStat(completedRequest);
TextComponent prettyStat = RequestExecutor.super.getStatFormatter()
TextComponent prettyStat = RequestExecutor.getStatFormatter()
.formatPlayerStat(completedRequest, stat);
return new PlayerStatResult(stat, prettyStat);

View File

@ -1,6 +1,7 @@
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.api.RequestGenerator;
import com.gmail.artemis.the.gr8.playerstats.statistic.result.ServerStatResult;
import com.gmail.artemis.the.gr8.playerstats.statistic.result.StatResult;
import net.kyori.adventure.text.TextComponent;
@ -9,36 +10,43 @@ import org.bukkit.Statistic;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;
public final class ServerStatRequest implements RequestExecutor<Long> {
public final class ServerStatRequest implements RequestGenerator<Long>, RequestExecutor<Long> {
private final StatRequest statRequest;
private final StatRequestHandler statRequestHandler;
public ServerStatRequest(StatRequestHandler statRequestHandler) {
this.statRequestHandler = statRequestHandler;
public ServerStatRequest(StatRequest request) {
statRequest = request;
statRequestHandler = new StatRequestHandler(statRequest);
}
@Override
public StatResult<Long> untyped(@NotNull Statistic statistic) {
public RequestExecutor<Long> untyped(@NotNull Statistic statistic) {
StatRequest completedRequest = statRequestHandler.untyped(statistic);
return getStatResult(completedRequest);
return new ServerStatRequest(completedRequest);
}
@Override
public StatResult<Long> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
public RequestExecutor<Long> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
StatRequest completedRequest = statRequestHandler.blockOrItemType(statistic, material);
return getStatResult(completedRequest);
return new ServerStatRequest(completedRequest);
}
@Override
public StatResult<Long> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
public RequestExecutor<Long> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
StatRequest completedRequest = statRequestHandler.entityType(statistic, entityType);
return getStatResult(completedRequest);
return new ServerStatRequest(completedRequest);
}
@Override
public StatResult<Long> execute() {
return getStatResult(statRequest);
}
private ServerStatResult getStatResult(StatRequest completedRequest) {
long stat = RequestExecutor.super.getStatCalculator()
long stat = RequestExecutor.getStatCalculator()
.getServerStat(completedRequest);
TextComponent prettyStat = RequestExecutor.super.getStatFormatter()
TextComponent prettyStat = RequestExecutor.getStatFormatter()
.formatServerStat(completedRequest, stat);
return new ServerStatResult(stat, prettyStat);

View File

@ -13,38 +13,43 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public record StatRequestHandler(StatRequest statRequest) implements RequestGenerator {
public final class StatRequestHandler {
public static StatRequestHandler playerRequestHandler(String playerName) {
private final StatRequest statRequest;
public StatRequestHandler(StatRequest request) {
statRequest = request;
}
public static StatRequest getBasicPlayerStatRequest(String playerName) {
StatRequest request = StatRequest.getBasicAPIRequest();
request.setTarget(Target.PLAYER);
request.setPlayerName(playerName);
return new StatRequestHandler(request);
return request;
}
public static StatRequestHandler serverRequestHandler() {
public static StatRequest getBasicServerStatRequest() {
StatRequest request = StatRequest.getBasicAPIRequest();
request.setTarget(Target.SERVER);
return new StatRequestHandler(request);
return request;
}
public static StatRequestHandler topRequestHandler(int topListSize) {
public static StatRequest getBasicTopStatRequest(int topListSize) {
StatRequest request = StatRequest.getBasicAPIRequest();
request.setTarget(Target.TOP);
request.setTopListSize(topListSize != 0 ? topListSize : Main.getConfigHandler().getTopListMaxSize());
return new StatRequestHandler(request);
return request;
}
/**
@param sender the CommandSender that requested this specific statistic
*/
public static StatRequestHandler internalRequestHandler(CommandSender sender) {
public static StatRequest getBasicInternalStatRequest(CommandSender sender) {
StatRequest request = StatRequest.getBasicRequest(sender);
request.setTopListSize(Main.getConfigHandler().getTopListMaxSize());
return new StatRequestHandler(request);
return request;
}
@Override
public StatRequest untyped(@NotNull Statistic statistic) throws IllegalArgumentException {
if (statistic.getType() == Statistic.Type.UNTYPED) {
statRequest.setStatistic(statistic);
@ -53,7 +58,6 @@ public record StatRequestHandler(StatRequest statRequest) implements RequestGene
throw new IllegalArgumentException("This statistic is not of Type.Untyped");
}
@Override
public StatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException {
Statistic.Type type = statistic.getType();
if (type == Statistic.Type.BLOCK && material.isBlock()) {
@ -69,7 +73,6 @@ public record StatRequestHandler(StatRequest statRequest) implements RequestGene
return statRequest;
}
@Override
public StatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException {
if (statistic.getType() == Statistic.Type.ENTITY) {
statRequest.setSubStatEntryName(entityType.toString());

View File

@ -1,6 +1,7 @@
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.api.RequestGenerator;
import com.gmail.artemis.the.gr8.playerstats.statistic.result.StatResult;
import com.gmail.artemis.the.gr8.playerstats.statistic.result.TopStatResult;
import net.kyori.adventure.text.TextComponent;
@ -11,36 +12,43 @@ import org.jetbrains.annotations.NotNull;
import java.util.LinkedHashMap;
public final class TopStatRequest implements RequestExecutor<LinkedHashMap<String, Integer>> {
public final class TopStatRequest implements RequestGenerator<LinkedHashMap<String, Integer>>, RequestExecutor<LinkedHashMap<String, Integer>> {
private final StatRequest statRequest;
private final StatRequestHandler statRequestHandler;
public TopStatRequest(StatRequestHandler statRequestHandler) {
this.statRequestHandler = statRequestHandler;
public TopStatRequest(StatRequest request) {
statRequest = request;
statRequestHandler = new StatRequestHandler(request);
}
@Override
public StatResult<LinkedHashMap<String, Integer>> untyped(@NotNull Statistic statistic) {
public TopStatRequest untyped(@NotNull Statistic statistic) {
StatRequest completedRequest = statRequestHandler.untyped(statistic);
return getStatResult(completedRequest);
return new TopStatRequest(completedRequest);
}
@Override
public StatResult<LinkedHashMap<String, Integer>> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
public TopStatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
StatRequest completedRequest = statRequestHandler.blockOrItemType(statistic, material);
return getStatResult(completedRequest);
return new TopStatRequest(completedRequest);
}
@Override
public StatResult<LinkedHashMap<String, Integer>> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
public TopStatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
StatRequest completedRequest = statRequestHandler.entityType(statistic, entityType);
return getStatResult(completedRequest);
return new TopStatRequest(completedRequest);
}
@Override
public StatResult<LinkedHashMap<String, Integer>> execute() {
return getStatResult(statRequest);
}
private TopStatResult getStatResult(StatRequest completedRequest) {
LinkedHashMap<String, Integer> stat = RequestExecutor.super.getStatCalculator()
LinkedHashMap<String, Integer> stat = RequestExecutor.getStatCalculator()
.getTopStats(completedRequest);
TextComponent prettyStat = RequestExecutor.super.getStatFormatter()
TextComponent prettyStat = RequestExecutor.getStatFormatter()
.formatTopStat(completedRequest, stat);
return new TopStatResult(stat, prettyStat);