mirror of
https://github.com/itHotL/PlayerStats.git
synced 2024-11-22 11:55:17 +01:00
Started a rewrite to make the request-business more clear and better organized
This commit is contained in:
parent
e30bc5efd2
commit
56dc30830a
@ -7,6 +7,7 @@ import com.artemis.the.gr8.playerstats.statistic.request.RequestSettings;
|
||||
import com.artemis.the.gr8.playerstats.reload.ReloadThread;
|
||||
import com.artemis.the.gr8.playerstats.statistic.StatCalculator;
|
||||
import com.artemis.the.gr8.playerstats.statistic.StatThread;
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.StatRequest;
|
||||
import com.artemis.the.gr8.playerstats.utils.MyLogger;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
@ -63,7 +64,7 @@ public final class ThreadManager {
|
||||
}
|
||||
}
|
||||
|
||||
public void startStatThread(RequestSettings requestSettings) {
|
||||
public void startStatThread(StatRequest.Settings requestSettings) {
|
||||
statThreadID += 1;
|
||||
String cmdSender = requestSettings.getCommandSender().getName();
|
||||
|
||||
@ -95,7 +96,7 @@ public final class ThreadManager {
|
||||
return lastRecordedCalcTime;
|
||||
}
|
||||
|
||||
private void startNewStatThread(RequestSettings requestSettings) {
|
||||
private void startNewStatThread(StatRequest.Settings requestSettings) {
|
||||
lastActiveStatThread = new StatThread(outputManager, statCalculator, statThreadID, requestSettings, lastActiveReloadThread);
|
||||
statThreads.put(requestSettings.getCommandSender().getName(), lastActiveStatThread);
|
||||
lastActiveStatThread.start();
|
||||
|
@ -29,20 +29,17 @@ public final class PlayerStatsAPI implements PlayerStats, StatManager {
|
||||
|
||||
@Override
|
||||
public PlayerStatRequest playerStatRequest(String playerName) {
|
||||
RequestSettings request = RequestHandler.getBasicPlayerStatRequest(playerName);
|
||||
return new PlayerStatRequest(request);
|
||||
return new PlayerStatRequest(playerName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerStatRequest serverStatRequest() {
|
||||
RequestSettings request = RequestHandler.getBasicServerStatRequest();
|
||||
return new ServerStatRequest(request);
|
||||
return new ServerStatRequest();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TopStatRequest topStatRequest(int topListSize) {
|
||||
RequestSettings request = RequestHandler.getBasicTopStatRequest(topListSize);
|
||||
return new TopStatRequest(request);
|
||||
return new TopStatRequest(topListSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -2,6 +2,7 @@ package com.artemis.the.gr8.playerstats.msg;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.RequestSettings;
|
||||
import com.artemis.the.gr8.playerstats.statistic.StatCalculator;
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.StatRequest;
|
||||
import net.kyori.adventure.text.*;
|
||||
import org.jetbrains.annotations.ApiStatus.Internal;
|
||||
|
||||
@ -19,12 +20,12 @@ public interface InternalFormatter {
|
||||
/** @return a TextComponent with the following parts:
|
||||
* <br>[player-name]: [number] [stat-name] {sub-stat-name}
|
||||
*/
|
||||
TextComponent formatAndSavePlayerStat(RequestSettings requestSettings, int playerStat);
|
||||
TextComponent formatAndSavePlayerStat(StatRequest.Settings requestSettings, int playerStat);
|
||||
|
||||
/** @return a TextComponent with the following parts:
|
||||
* <br>[Total on] [server-name]: [number] [stat-name] [sub-stat-name]
|
||||
*/
|
||||
TextComponent formatAndSaveServerStat(RequestSettings requestSettings, long serverStat);
|
||||
TextComponent formatAndSaveServerStat(StatRequest.Settings requestSettings, long serverStat);
|
||||
|
||||
/** @return a TextComponent with the following parts:
|
||||
* <br>[PlayerStats] [Top 10] [stat-name] [sub-stat-name]
|
||||
@ -32,5 +33,5 @@ public interface InternalFormatter {
|
||||
* <br> [2.] [player-name] [number]
|
||||
* <br> [3.] etc...
|
||||
*/
|
||||
TextComponent formatAndSaveTopStat(RequestSettings requestSettings, LinkedHashMap<String, Integer> topStats);
|
||||
TextComponent formatAndSaveTopStat(StatRequest.Settings requestSettings, LinkedHashMap<String, Integer> topStats);
|
||||
}
|
@ -8,13 +8,13 @@ import com.artemis.the.gr8.playerstats.msg.components.HelpMessage;
|
||||
import com.artemis.the.gr8.playerstats.msg.components.BukkitConsoleComponentFactory;
|
||||
import com.artemis.the.gr8.playerstats.msg.components.PrideComponentFactory;
|
||||
import com.artemis.the.gr8.playerstats.msg.msgutils.*;
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.StatRequest;
|
||||
import com.artemis.the.gr8.playerstats.utils.EnumHandler;
|
||||
import com.artemis.the.gr8.playerstats.utils.MyLogger;
|
||||
import com.artemis.the.gr8.playerstats.enums.Target;
|
||||
import com.artemis.the.gr8.playerstats.config.ConfigHandler;
|
||||
import com.artemis.the.gr8.playerstats.enums.Unit;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.RequestSettings;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.Statistic;
|
||||
@ -313,7 +313,7 @@ public final class MessageBuilder implements ApiFormatter {
|
||||
* <br>- If both parameters are null, the formattedComponent will be returned
|
||||
* as is.
|
||||
*/
|
||||
public @NotNull BiFunction<Integer, CommandSender, TextComponent> formattedPlayerStatFunction(int stat, @NotNull RequestSettings request) {
|
||||
public @NotNull BiFunction<Integer, CommandSender, TextComponent> formattedPlayerStatFunction(int stat, @NotNull StatRequest.Settings request) {
|
||||
TextComponent playerStat = formatPlayerStat(request.getPlayerName(), stat, request.getStatistic(), request.getSubStatEntryName());
|
||||
return getFormattingFunction(playerStat, Target.PLAYER);
|
||||
}
|
||||
@ -329,7 +329,7 @@ public final class MessageBuilder implements ApiFormatter {
|
||||
* <br>- If both parameters are null, the formattedComponent will be returned
|
||||
* as is.
|
||||
*/
|
||||
public @NotNull BiFunction<Integer, CommandSender, TextComponent> formattedServerStatFunction(long stat, @NotNull RequestSettings request) {
|
||||
public @NotNull BiFunction<Integer, CommandSender, TextComponent> formattedServerStatFunction(long stat, @NotNull StatRequest.Settings request) {
|
||||
TextComponent serverStat = formatServerStat(stat, request.getStatistic(), request.getSubStatEntryName());
|
||||
return getFormattingFunction(serverStat, Target.SERVER);
|
||||
}
|
||||
@ -345,7 +345,7 @@ public final class MessageBuilder implements ApiFormatter {
|
||||
* <br>- If both parameters are null, the formattedComponent will be returned
|
||||
* as is.
|
||||
*/
|
||||
public @NotNull BiFunction<Integer, CommandSender, TextComponent> formattedTopStatFunction(@NotNull LinkedHashMap<String, Integer> topStats, @NotNull RequestSettings request) {
|
||||
public @NotNull BiFunction<Integer, CommandSender, TextComponent> formattedTopStatFunction(@NotNull LinkedHashMap<String, Integer> topStats, @NotNull StatRequest.Settings request) {
|
||||
final TextComponent title = getTopStatTitle(topStats.size(), request.getStatistic(), request.getSubStatEntryName());
|
||||
final TextComponent list = getTopStatListComponent(topStats, request.getStatistic());
|
||||
final boolean useEnters = config.useEnters(Target.TOP, false);
|
||||
|
@ -6,6 +6,7 @@ import com.artemis.the.gr8.playerstats.enums.StandardMessage;
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.RequestSettings;
|
||||
import com.artemis.the.gr8.playerstats.msg.components.BukkitConsoleComponentFactory;
|
||||
import com.artemis.the.gr8.playerstats.msg.components.PrideComponentFactory;
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.StatRequest;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -55,25 +56,25 @@ public final class OutputManager implements InternalFormatter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextComponent formatAndSavePlayerStat(@NotNull RequestSettings requestSettings, int playerStat) {
|
||||
public TextComponent formatAndSavePlayerStat(@NotNull StatRequest.Settings requestSettings, int playerStat) {
|
||||
BiFunction<Integer, CommandSender, TextComponent> playerStatFunction =
|
||||
getMessageBuilder(requestSettings).formattedPlayerStatFunction(playerStat, requestSettings);
|
||||
getMessageBuilder(requestSettings.getCommandSender()).formattedPlayerStatFunction(playerStat, requestSettings);
|
||||
|
||||
return processFunction(requestSettings.getCommandSender(), playerStatFunction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextComponent formatAndSaveServerStat(@NotNull RequestSettings requestSettings, long serverStat) {
|
||||
public TextComponent formatAndSaveServerStat(@NotNull StatRequest.Settings requestSettings, long serverStat) {
|
||||
BiFunction<Integer, CommandSender, TextComponent> serverStatFunction =
|
||||
getMessageBuilder(requestSettings).formattedServerStatFunction(serverStat, requestSettings);
|
||||
getMessageBuilder(requestSettings.getCommandSender()).formattedServerStatFunction(serverStat, requestSettings);
|
||||
|
||||
return processFunction(requestSettings.getCommandSender(), serverStatFunction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextComponent formatAndSaveTopStat(@NotNull RequestSettings requestSettings, @NotNull LinkedHashMap<String, Integer> topStats) {
|
||||
public TextComponent formatAndSaveTopStat(@NotNull StatRequest.Settings requestSettings, @NotNull LinkedHashMap<String, Integer> topStats) {
|
||||
BiFunction<Integer, CommandSender, TextComponent> topStatFunction =
|
||||
getMessageBuilder(requestSettings).formattedTopStatFunction(topStats, requestSettings);
|
||||
getMessageBuilder(requestSettings.getCommandSender()).formattedTopStatFunction(topStats, requestSettings);
|
||||
|
||||
return processFunction(requestSettings.getCommandSender(), topStatFunction);
|
||||
}
|
||||
@ -141,14 +142,6 @@ public final class OutputManager implements InternalFormatter {
|
||||
return sender instanceof ConsoleCommandSender ? consoleMessageBuilder : messageBuilder;
|
||||
}
|
||||
|
||||
private MessageBuilder getMessageBuilder(RequestSettings requestSettings) {
|
||||
if (!requestSettings.isConsoleSender()) {
|
||||
return messageBuilder;
|
||||
} else {
|
||||
return consoleMessageBuilder;
|
||||
}
|
||||
}
|
||||
|
||||
private static void getMessageBuilders() {
|
||||
messageBuilder = getClientMessageBuilder();
|
||||
consoleMessageBuilder = getConsoleMessageBuilder();
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.artemis.the.gr8.playerstats.statistic;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.ThreadManager;
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.StatRequest;
|
||||
import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.RequestSettings;
|
||||
import com.artemis.the.gr8.playerstats.utils.MyLogger;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
@ -20,7 +20,7 @@ final class StatAction extends RecursiveTask<ConcurrentHashMap<String, Integer>>
|
||||
|
||||
private final OfflinePlayerHandler offlinePlayerHandler;
|
||||
private final ImmutableList<String> playerNames;
|
||||
private final RequestSettings requestSettings;
|
||||
private final StatRequest.Settings requestSettings;
|
||||
private final ConcurrentHashMap<String, Integer> allStats;
|
||||
|
||||
/**
|
||||
@ -34,7 +34,7 @@ final class StatAction extends RecursiveTask<ConcurrentHashMap<String, Integer>>
|
||||
* @param requestSettings a validated requestSettings object
|
||||
* @param allStats the ConcurrentHashMap to put the results on
|
||||
*/
|
||||
public StatAction(OfflinePlayerHandler offlinePlayerHandler, ImmutableList<String> playerNames, RequestSettings requestSettings, ConcurrentHashMap<String, Integer> allStats) {
|
||||
public StatAction(OfflinePlayerHandler offlinePlayerHandler, ImmutableList<String> playerNames, StatRequest.Settings requestSettings, ConcurrentHashMap<String, Integer> allStats) {
|
||||
threshold = ThreadManager.getTaskThreshold();
|
||||
|
||||
this.offlinePlayerHandler = offlinePlayerHandler;
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.artemis.the.gr8.playerstats.statistic;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.ThreadManager;
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.StatRequest;
|
||||
import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.RequestSettings;
|
||||
import com.artemis.the.gr8.playerstats.utils.MyLogger;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
@ -21,7 +21,7 @@ public final class StatCalculator {
|
||||
this.offlinePlayerHandler = offlinePlayerHandler;
|
||||
}
|
||||
|
||||
public int getPlayerStat(RequestSettings requestSettings) {
|
||||
public int getPlayerStat(StatRequest.Settings requestSettings) {
|
||||
OfflinePlayer player = offlinePlayerHandler.getOfflinePlayer(requestSettings.getPlayerName());
|
||||
return switch (requestSettings.getStatistic().getType()) {
|
||||
case UNTYPED -> player.getStatistic(requestSettings.getStatistic());
|
||||
@ -31,14 +31,14 @@ public final class StatCalculator {
|
||||
};
|
||||
}
|
||||
|
||||
public LinkedHashMap<String, Integer> getTopStats(RequestSettings requestSettings) {
|
||||
public LinkedHashMap<String, Integer> getTopStats(StatRequest.Settings requestSettings) {
|
||||
return getAllStatsAsync(requestSettings).entrySet().stream()
|
||||
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
|
||||
.limit(requestSettings.getTopListSize())
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
|
||||
}
|
||||
|
||||
public long getServerStat(RequestSettings requestSettings) {
|
||||
public long getServerStat(StatRequest.Settings requestSettings) {
|
||||
List<Integer> numbers = getAllStatsAsync(requestSettings)
|
||||
.values()
|
||||
.parallelStream()
|
||||
@ -50,7 +50,7 @@ public final class StatCalculator {
|
||||
* Invokes a bunch of worker pool threads to get the statistics for
|
||||
* all players that are stored in the {@link OfflinePlayerHandler}).
|
||||
*/
|
||||
private @NotNull ConcurrentHashMap<String, Integer> getAllStatsAsync(RequestSettings requestSettings) {
|
||||
private @NotNull ConcurrentHashMap<String, Integer> getAllStatsAsync(StatRequest.Settings requestSettings) {
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
ForkJoinPool commonPool = ForkJoinPool.commonPool();
|
||||
@ -72,7 +72,7 @@ public final class StatCalculator {
|
||||
return allStats;
|
||||
}
|
||||
|
||||
private StatAction getStatTask(RequestSettings requestSettings) {
|
||||
private StatAction getStatTask(StatRequest.Settings requestSettings) {
|
||||
int size = offlinePlayerHandler.getOfflinePlayerCount() != 0 ? offlinePlayerHandler.getOfflinePlayerCount() : 16;
|
||||
ConcurrentHashMap<String, Integer> allStats = new ConcurrentHashMap<>(size);
|
||||
ImmutableList<String> playerNames = ImmutableList.copyOf(offlinePlayerHandler.getOfflinePlayerNames());
|
||||
|
@ -2,6 +2,7 @@ package com.artemis.the.gr8.playerstats.statistic;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.ThreadManager;
|
||||
import com.artemis.the.gr8.playerstats.msg.OutputManager;
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.StatRequest;
|
||||
import com.artemis.the.gr8.playerstats.utils.MyLogger;
|
||||
import com.artemis.the.gr8.playerstats.enums.StandardMessage;
|
||||
import com.artemis.the.gr8.playerstats.enums.Target;
|
||||
@ -21,9 +22,9 @@ public final class StatThread extends Thread {
|
||||
private static StatCalculator statCalculator;
|
||||
|
||||
private final ReloadThread reloadThread;
|
||||
private final RequestSettings requestSettings;
|
||||
private final StatRequest.Settings requestSettings;
|
||||
|
||||
public StatThread(OutputManager m, StatCalculator t, int ID, RequestSettings s, @Nullable ReloadThread r) {
|
||||
public StatThread(OutputManager m, StatCalculator t, int ID, StatRequest.Settings s, @Nullable ReloadThread r) {
|
||||
outputManager = m;
|
||||
statCalculator = t;
|
||||
|
||||
|
@ -5,51 +5,52 @@ import com.artemis.the.gr8.playerstats.statistic.result.PlayerStatResult;
|
||||
import com.artemis.the.gr8.playerstats.api.RequestGenerator;
|
||||
import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Statistic;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public final class PlayerStatRequest extends StatRequest<Integer> implements RequestGenerator<Integer> {
|
||||
|
||||
private final RequestHandler requestHandler;
|
||||
public PlayerStatRequest(String playerName) {
|
||||
this(Bukkit.getConsoleSender(), playerName);
|
||||
}
|
||||
|
||||
public PlayerStatRequest(RequestSettings request) {
|
||||
super(request);
|
||||
requestHandler = new RequestHandler(request);
|
||||
public PlayerStatRequest(CommandSender requester, String playerName) {
|
||||
super(requester);
|
||||
super.settings.configureForPlayer(playerName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayerStatRequest untyped(@NotNull Statistic statistic) {
|
||||
RequestSettings completedRequest = requestHandler.untyped(statistic);
|
||||
return new PlayerStatRequest(completedRequest);
|
||||
public StatRequest<Integer> untyped(@NotNull Statistic statistic) {
|
||||
return super.configureUntyped(statistic);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayerStatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
|
||||
RequestSettings completedRequest = requestHandler.blockOrItemType(statistic, material);
|
||||
return new PlayerStatRequest(completedRequest);
|
||||
public StatRequest<Integer> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
|
||||
return super.configureBlockOrItemType(statistic, material);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayerStatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
|
||||
RequestSettings completedRequest = requestHandler.entityType(statistic, entityType);
|
||||
return new PlayerStatRequest(completedRequest);
|
||||
public StatRequest<Integer> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
|
||||
return super.configureEntityType(statistic, entityType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayerStatResult execute() {
|
||||
return getStatResult(super.requestSettings);
|
||||
return getStatResult();
|
||||
}
|
||||
|
||||
private PlayerStatResult getStatResult(RequestSettings completedRequest) {
|
||||
private PlayerStatResult getStatResult() {
|
||||
int stat = Main
|
||||
.getStatCalculator()
|
||||
.getPlayerStat(completedRequest);
|
||||
.getPlayerStat(settings);
|
||||
|
||||
TextComponent prettyComponent = Main
|
||||
.getOutputManager()
|
||||
.formatAndSavePlayerStat(completedRequest, stat);
|
||||
.formatAndSavePlayerStat(settings, stat);
|
||||
|
||||
String prettyString = ComponentUtils
|
||||
.getTranslatableComponentSerializer()
|
||||
|
@ -5,51 +5,52 @@ import com.artemis.the.gr8.playerstats.statistic.result.ServerStatResult;
|
||||
import com.artemis.the.gr8.playerstats.api.RequestGenerator;
|
||||
import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Statistic;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public final class ServerStatRequest extends StatRequest<Long> implements RequestGenerator<Long> {
|
||||
|
||||
private final RequestHandler requestHandler;
|
||||
public ServerStatRequest() {
|
||||
this(Bukkit.getConsoleSender());
|
||||
}
|
||||
|
||||
public ServerStatRequest(RequestSettings request) {
|
||||
super(request);
|
||||
requestHandler = new RequestHandler(requestSettings);
|
||||
public ServerStatRequest(CommandSender requester) {
|
||||
super(requester);
|
||||
super.settings.configureForServer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerStatRequest untyped(@NotNull Statistic statistic) {
|
||||
RequestSettings completedRequest = requestHandler.untyped(statistic);
|
||||
return new ServerStatRequest(completedRequest);
|
||||
public StatRequest<Long> untyped(@NotNull Statistic statistic) {
|
||||
return super.configureUntyped(statistic);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerStatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
|
||||
RequestSettings completedRequest = requestHandler.blockOrItemType(statistic, material);
|
||||
return new ServerStatRequest(completedRequest);
|
||||
public StatRequest<Long> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
|
||||
return super.configureBlockOrItemType(statistic, material);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerStatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
|
||||
RequestSettings completedRequest = requestHandler.entityType(statistic, entityType);
|
||||
return new ServerStatRequest(completedRequest);
|
||||
public StatRequest<Long> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
|
||||
return super.configureEntityType(statistic, entityType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerStatResult execute() {
|
||||
return getStatResult(requestSettings);
|
||||
return getStatResult();
|
||||
}
|
||||
|
||||
private ServerStatResult getStatResult(RequestSettings completedRequest) {
|
||||
private ServerStatResult getStatResult() {
|
||||
long stat = Main
|
||||
.getStatCalculator()
|
||||
.getServerStat(completedRequest);
|
||||
.getServerStat(settings);
|
||||
|
||||
TextComponent prettyComponent = Main
|
||||
.getOutputManager()
|
||||
.formatAndSaveServerStat(completedRequest, stat);
|
||||
.formatAndSaveServerStat(settings, stat);
|
||||
|
||||
String prettyString = ComponentUtils
|
||||
.getTranslatableComponentSerializer()
|
||||
|
@ -1,11 +1,15 @@
|
||||
package com.artemis.the.gr8.playerstats.statistic.request;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.Main;
|
||||
import com.artemis.the.gr8.playerstats.api.PlayerStats;
|
||||
import com.artemis.the.gr8.playerstats.statistic.result.StatResult;
|
||||
import com.artemis.the.gr8.playerstats.enums.Target;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Statistic;
|
||||
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;
|
||||
|
||||
/**
|
||||
@ -18,10 +22,44 @@ import org.jetbrains.annotations.Nullable;
|
||||
*/
|
||||
public abstract class StatRequest<T> {
|
||||
|
||||
protected final RequestSettings requestSettings;
|
||||
protected final Settings settings;
|
||||
|
||||
protected StatRequest(RequestSettings request) {
|
||||
requestSettings = request;
|
||||
protected StatRequest(CommandSender requester) {
|
||||
settings = new Settings(requester);
|
||||
}
|
||||
|
||||
protected StatRequest<T> configureUntyped(@NotNull Statistic statistic) {
|
||||
if (statistic.getType() == Statistic.Type.UNTYPED) {
|
||||
settings.setStatistic(statistic);
|
||||
return this;
|
||||
}
|
||||
throw new IllegalArgumentException("This statistic is not of Type.Untyped");
|
||||
}
|
||||
|
||||
protected StatRequest<T> configureBlockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException {
|
||||
Statistic.Type type = statistic.getType();
|
||||
if (type == Statistic.Type.BLOCK && material.isBlock()) {
|
||||
settings.setBlock(material);
|
||||
}
|
||||
else if (type == Statistic.Type.ITEM && material.isItem()){
|
||||
settings.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");
|
||||
}
|
||||
settings.setStatistic(statistic);
|
||||
settings.setSubStatEntryName(material.toString());
|
||||
return this;
|
||||
}
|
||||
|
||||
protected StatRequest<T> configureEntityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException {
|
||||
if (statistic.getType() == Statistic.Type.ENTITY) {
|
||||
settings.setStatistic(statistic);
|
||||
settings.setSubStatEntryName(entityType.toString());
|
||||
settings.setEntity(entityType);
|
||||
return this;
|
||||
}
|
||||
throw new IllegalArgumentException("This statistic is not of Type.Entity");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -35,53 +73,167 @@ public abstract class StatRequest<T> {
|
||||
*/
|
||||
public abstract StatResult<T> execute();
|
||||
|
||||
/**
|
||||
* Gets the Statistic that calling {@link #execute()} will calculate
|
||||
* the data for.
|
||||
* @return the Statistic
|
||||
*/
|
||||
public Statistic getStatisticSetting() {
|
||||
return requestSettings.getStatistic();
|
||||
public boolean isValid() {
|
||||
if (settings.statistic == null) {
|
||||
return false;
|
||||
} else if (settings.target == Target.PLAYER && settings.playerName == null) {
|
||||
return false;
|
||||
} else if (settings.statistic.getType() != Statistic.Type.UNTYPED &&
|
||||
settings.subStatEntryName == null) {
|
||||
return false;
|
||||
} else {
|
||||
return hasMatchingSubStat();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasMatchingSubStat() {
|
||||
switch (settings.statistic.getType()) {
|
||||
case BLOCK -> {
|
||||
return settings.block != null;
|
||||
}
|
||||
case ENTITY -> {
|
||||
return settings.entity != null;
|
||||
}
|
||||
case ITEM -> {
|
||||
return settings.item != null;
|
||||
}
|
||||
default -> {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If the Statistic setting for this StatRequest is of Type.Block,
|
||||
* this will get the Material that was set.
|
||||
*
|
||||
* @return a Material for which #isBlock is true, or null if no
|
||||
* Material was set
|
||||
* Use this method to view the settings that have
|
||||
* been configured for this StatRequest.
|
||||
*/
|
||||
public @Nullable Material getBlockSetting() {
|
||||
return requestSettings.getBlock();
|
||||
public Settings getSettings() {
|
||||
return settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the Statistic setting for this StatRequest is of Type.Item,
|
||||
* this will get the Material that was set.
|
||||
*
|
||||
* @return a Material for which #isItem is true, or null if no
|
||||
* Material was set
|
||||
*/
|
||||
public @Nullable Material getItemSetting() {
|
||||
return requestSettings.getItem();
|
||||
}
|
||||
public static final class Settings {
|
||||
private final CommandSender sender;
|
||||
private Statistic statistic;
|
||||
private String playerName;
|
||||
private Target target;
|
||||
private int topListSize;
|
||||
|
||||
/**
|
||||
* If the Statistic setting for this StatRequest is of Type.Entity,
|
||||
* this will get the EntityType that was set.
|
||||
*
|
||||
* @return an EntityType, or null if no EntityType was set
|
||||
*/
|
||||
public @Nullable EntityType getEntitySetting() {
|
||||
return requestSettings.getEntity();
|
||||
}
|
||||
private String subStatEntryName;
|
||||
private EntityType entity;
|
||||
private Material block;
|
||||
private Material item;
|
||||
private boolean playerFlag;
|
||||
|
||||
/**
|
||||
* Gets the Target that will be used when calling {@link #execute()}.
|
||||
*
|
||||
* @return the Target for this lookup (either Player, Server or Top)
|
||||
*/
|
||||
public Target getTargetSetting() {
|
||||
return requestSettings.getTarget();
|
||||
/**
|
||||
* Create a new {@link Settings} with default values:
|
||||
* <br>- CommandSender sender (provided)
|
||||
* <br>- Target target = {@link Target#TOP}
|
||||
* <br>- int topListSize = 10
|
||||
* <br>- boolean playerFlag = false
|
||||
*
|
||||
* @param sender the CommandSender who prompted this RequestGenerator
|
||||
*/
|
||||
private Settings(@NotNull CommandSender sender) {
|
||||
this.sender = sender;
|
||||
target = Target.TOP;
|
||||
playerFlag = false;
|
||||
}
|
||||
|
||||
void configureForPlayer(String playerName) {
|
||||
setTarget(Target.PLAYER);
|
||||
setPlayerName(playerName);
|
||||
}
|
||||
|
||||
void configureForServer() {
|
||||
setTarget(Target.SERVER);
|
||||
}
|
||||
|
||||
void configureForTop(int topListSize) {
|
||||
setTarget(Target.TOP);
|
||||
|
||||
this.topListSize = topListSize != 0 ?
|
||||
topListSize :
|
||||
Main.getConfigHandler().getTopListMaxSize();
|
||||
}
|
||||
|
||||
public @NotNull CommandSender getCommandSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public boolean isConsoleSender() {
|
||||
return sender instanceof ConsoleCommandSender;
|
||||
}
|
||||
|
||||
void setStatistic(Statistic statistic) {
|
||||
this.statistic = statistic;
|
||||
}
|
||||
|
||||
public Statistic getStatistic() {
|
||||
return statistic;
|
||||
}
|
||||
|
||||
private void setSubStatEntryName(String subStatEntry) {
|
||||
this.subStatEntryName = subStatEntry;
|
||||
}
|
||||
|
||||
public @Nullable String getSubStatEntryName() {
|
||||
return subStatEntryName;
|
||||
}
|
||||
|
||||
void setPlayerName(String playerName) {
|
||||
this.playerName = playerName;
|
||||
}
|
||||
|
||||
public String getPlayerName() {
|
||||
return playerName;
|
||||
}
|
||||
|
||||
void setPlayerFlag(boolean playerFlag) {
|
||||
this.playerFlag = playerFlag;
|
||||
}
|
||||
|
||||
public boolean getPlayerFlag() {
|
||||
return playerFlag;
|
||||
}
|
||||
|
||||
void setTarget(@NotNull Target target) {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public @NotNull Target getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
void setTopListSize(int topListSize) {
|
||||
this.topListSize = topListSize;
|
||||
}
|
||||
|
||||
public int getTopListSize() {
|
||||
return this.topListSize;
|
||||
}
|
||||
|
||||
void setEntity(EntityType entity) {
|
||||
this.entity = entity;
|
||||
}
|
||||
|
||||
public EntityType getEntity() {
|
||||
return entity;
|
||||
}
|
||||
|
||||
void setBlock(Material block) {
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
public Material getBlock() {
|
||||
return block;
|
||||
}
|
||||
|
||||
void setItem(Material item) {
|
||||
this.item = item;
|
||||
}
|
||||
|
||||
public Material getItem() {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
}
|
@ -5,8 +5,10 @@ import com.artemis.the.gr8.playerstats.statistic.result.TopStatResult;
|
||||
import com.artemis.the.gr8.playerstats.api.RequestGenerator;
|
||||
import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Statistic;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@ -14,44 +16,43 @@ import java.util.LinkedHashMap;
|
||||
|
||||
public final class TopStatRequest extends StatRequest<LinkedHashMap<String, Integer>> implements RequestGenerator<LinkedHashMap<String, Integer>> {
|
||||
|
||||
private final RequestHandler requestHandler;
|
||||
public TopStatRequest(int topListSize) {
|
||||
this(Bukkit.getConsoleSender(), topListSize);
|
||||
}
|
||||
|
||||
public TopStatRequest(RequestSettings request) {
|
||||
super(request);
|
||||
requestHandler = new RequestHandler(request);
|
||||
public TopStatRequest(CommandSender requester, int topListSize) {
|
||||
super(requester);
|
||||
super.settings.configureForTop(topListSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TopStatRequest untyped(@NotNull Statistic statistic) {
|
||||
RequestSettings completedRequest = requestHandler.untyped(statistic);
|
||||
return new TopStatRequest(completedRequest);
|
||||
public StatRequest<LinkedHashMap<String, Integer>> untyped(@NotNull Statistic statistic) {
|
||||
return super.configureUntyped(statistic);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TopStatRequest blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
|
||||
RequestSettings completedRequest = requestHandler.blockOrItemType(statistic, material);
|
||||
return new TopStatRequest(completedRequest);
|
||||
public StatRequest<LinkedHashMap<String, Integer>> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
|
||||
return super.configureBlockOrItemType(statistic, material);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TopStatRequest entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
|
||||
RequestSettings completedRequest = requestHandler.entityType(statistic, entityType);
|
||||
return new TopStatRequest(completedRequest);
|
||||
public StatRequest<LinkedHashMap<String, Integer>> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
|
||||
return super.configureEntityType(statistic, entityType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TopStatResult execute() {
|
||||
return getStatResult(super.requestSettings);
|
||||
return getStatResult();
|
||||
}
|
||||
|
||||
private TopStatResult getStatResult(RequestSettings completedRequest) {
|
||||
private TopStatResult getStatResult() {
|
||||
LinkedHashMap<String, Integer> stat = Main
|
||||
.getStatCalculator()
|
||||
.getTopStats(completedRequest);
|
||||
.getTopStats(settings);
|
||||
|
||||
TextComponent prettyComponent = Main
|
||||
.getOutputManager()
|
||||
.formatAndSaveTopStat(completedRequest, stat);
|
||||
.formatAndSaveTopStat(settings, stat);
|
||||
|
||||
String prettyString = ComponentUtils
|
||||
.getTranslatableComponentSerializer()
|
||||
|
Loading…
Reference in New Issue
Block a user