mirror of
https://github.com/itHotL/PlayerStats.git
synced 2024-11-22 11:55:17 +01:00
Separated responsibility of OutputManager and ShareManager and made the RequestProcessor handle the StatRequest executing (#114)
This commit is contained in:
parent
31713007f5
commit
d16e6db036
@ -10,6 +10,7 @@ import com.artemis.the.gr8.playerstats.commands.TabCompleter;
|
||||
import com.artemis.the.gr8.playerstats.config.ConfigHandler;
|
||||
import com.artemis.the.gr8.playerstats.listeners.JoinListener;
|
||||
import com.artemis.the.gr8.playerstats.msg.msgutils.LanguageKeyHandler;
|
||||
import com.artemis.the.gr8.playerstats.share.ShareManager;
|
||||
import com.artemis.the.gr8.playerstats.statistic.RequestProcessor;
|
||||
import com.artemis.the.gr8.playerstats.utils.EnumHandler;
|
||||
import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
|
||||
@ -43,7 +44,7 @@ public final class Main extends JavaPlugin {
|
||||
private static ShareManager shareManager;
|
||||
private static RequestProcessor requestProcessor;
|
||||
|
||||
private static PlayerStats playerStatsAPI;
|
||||
private static PlayerStats playerStatsImpl;
|
||||
|
||||
|
||||
@Override
|
||||
@ -103,10 +104,10 @@ public final class Main extends JavaPlugin {
|
||||
}
|
||||
|
||||
public static @NotNull PlayerStats getPlayerStatsAPI() throws IllegalStateException {
|
||||
if (playerStatsAPI == null) {
|
||||
if (playerStatsImpl == null) {
|
||||
throw new IllegalStateException("PlayerStats does not seem to be loaded!");
|
||||
}
|
||||
return playerStatsAPI;
|
||||
return playerStatsImpl;
|
||||
}
|
||||
|
||||
public static @NotNull RequestProcessor getRequestProcessor() throws IllegalStateException {
|
||||
@ -156,16 +157,15 @@ public final class Main extends JavaPlugin {
|
||||
adventure = BukkitAudiences.create(this);
|
||||
enumHandler = new EnumHandler();
|
||||
languageKeyHandler = new LanguageKeyHandler();
|
||||
|
||||
config = new ConfigHandler();
|
||||
|
||||
offlinePlayerHandler = new OfflinePlayerHandler(config);
|
||||
shareManager = new ShareManager(config);
|
||||
outputManager = new OutputManager(adventure, config);
|
||||
requestProcessor = new RequestProcessor(offlinePlayerHandler, outputManager, shareManager);
|
||||
|
||||
outputManager = new OutputManager(adventure, config, shareManager);
|
||||
requestProcessor = new RequestProcessor(offlinePlayerHandler, outputManager);
|
||||
threadManager = new ThreadManager(config, requestProcessor, outputManager);
|
||||
|
||||
playerStatsAPI = new PlayerStatsImpl(outputManager, offlinePlayerHandler);
|
||||
threadManager = new ThreadManager(config, outputManager);
|
||||
playerStatsImpl = new PlayerStatsImpl(offlinePlayerHandler, outputManager);
|
||||
}
|
||||
|
||||
private void setupMetrics() {
|
||||
|
@ -4,9 +4,8 @@ import com.artemis.the.gr8.playerstats.msg.OutputManager;
|
||||
import com.artemis.the.gr8.playerstats.config.ConfigHandler;
|
||||
import com.artemis.the.gr8.playerstats.enums.StandardMessage;
|
||||
import com.artemis.the.gr8.playerstats.reload.ReloadThread;
|
||||
import com.artemis.the.gr8.playerstats.statistic.RequestProcessor;
|
||||
import com.artemis.the.gr8.playerstats.statistic.StatThread;
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.StatRequest;
|
||||
import com.artemis.the.gr8.playerstats.statistic.StatRequest;
|
||||
import com.artemis.the.gr8.playerstats.utils.MyLogger;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -30,17 +29,15 @@ public final class ThreadManager {
|
||||
|
||||
private static ConfigHandler config;
|
||||
private static OutputManager outputManager;
|
||||
private static RequestProcessor requestProcessor;
|
||||
|
||||
private ReloadThread activatedReloadThread;
|
||||
private StatThread activatedStatThread;
|
||||
private final HashMap<String, Thread> statThreads;
|
||||
private static long lastRecordedCalcTime;
|
||||
|
||||
public ThreadManager(ConfigHandler config, RequestProcessor requestProcessor, OutputManager outputManager) {
|
||||
public ThreadManager(ConfigHandler config, OutputManager outputManager) {
|
||||
ThreadManager.config = config;
|
||||
ThreadManager.outputManager = outputManager;
|
||||
ThreadManager.requestProcessor = requestProcessor;
|
||||
|
||||
statThreads = new HashMap<>();
|
||||
statThreadID = 0;
|
||||
@ -98,7 +95,7 @@ public final class ThreadManager {
|
||||
}
|
||||
|
||||
private void startNewStatThread(StatRequest<?> request) {
|
||||
activatedStatThread = new StatThread(outputManager, requestProcessor, statThreadID, request, activatedReloadThread);
|
||||
activatedStatThread = new StatThread(outputManager, statThreadID, request, activatedReloadThread);
|
||||
statThreads.put(request.getSettings().getCommandSender().getName(), activatedStatThread);
|
||||
activatedStatThread.start();
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.artemis.the.gr8.playerstats.api;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.Main;
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.StatRequest;
|
||||
import com.artemis.the.gr8.playerstats.statistic.StatRequest;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
@ -1,7 +1,9 @@
|
||||
package com.artemis.the.gr8.playerstats.api;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.msg.OutputManager;
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.*;
|
||||
import com.artemis.the.gr8.playerstats.statistic.PlayerStatRequest;
|
||||
import com.artemis.the.gr8.playerstats.statistic.ServerStatRequest;
|
||||
import com.artemis.the.gr8.playerstats.statistic.TopStatRequest;
|
||||
import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
@ -11,13 +13,13 @@ import static org.jetbrains.annotations.ApiStatus.Internal;
|
||||
/** The implementation of the API Interface */
|
||||
public final class PlayerStatsImpl implements PlayerStats, StatManager {
|
||||
|
||||
private final OfflinePlayerHandler offlinePlayerHandler;
|
||||
private static OutputManager outputManager;
|
||||
private final OfflinePlayerHandler offlinePlayerHandler;
|
||||
|
||||
@Internal
|
||||
public PlayerStatsImpl(OutputManager outputManager, OfflinePlayerHandler offlinePlayers) {
|
||||
public PlayerStatsImpl(OfflinePlayerHandler offlinePlayerHandler, OutputManager outputManager) {
|
||||
PlayerStatsImpl.outputManager = outputManager;
|
||||
offlinePlayerHandler = offlinePlayers;
|
||||
this.offlinePlayerHandler = offlinePlayerHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.artemis.the.gr8.playerstats.api;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.statistic.RequestProcessor;
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.StatRequest;
|
||||
import com.artemis.the.gr8.playerstats.statistic.StatRequest;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Statistic;
|
||||
import org.bukkit.entity.EntityType;
|
||||
|
@ -3,7 +3,7 @@ package com.artemis.the.gr8.playerstats.api;
|
||||
import com.artemis.the.gr8.playerstats.enums.Unit;
|
||||
import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils;
|
||||
import com.artemis.the.gr8.playerstats.msg.msgutils.NumberFormatter;
|
||||
import com.artemis.the.gr8.playerstats.statistic.result.StatResult;
|
||||
import com.artemis.the.gr8.playerstats.statistic.StatResult;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.Statistic;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.artemis.the.gr8.playerstats.api;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.StatRequest;
|
||||
import com.artemis.the.gr8.playerstats.statistic.StatRequest;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
package com.artemis.the.gr8.playerstats.commands;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.ShareManager;
|
||||
import com.artemis.the.gr8.playerstats.share.ShareManager;
|
||||
import com.artemis.the.gr8.playerstats.enums.StandardMessage;
|
||||
import com.artemis.the.gr8.playerstats.msg.OutputManager;
|
||||
import com.artemis.the.gr8.playerstats.statistic.result.InternalStatResult;
|
||||
import com.artemis.the.gr8.playerstats.share.StoredResult;
|
||||
import com.artemis.the.gr8.playerstats.utils.MyLogger;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
@ -37,7 +37,7 @@ public final class ShareCommand implements CommandExecutor {
|
||||
outputManager.sendFeedbackMsg(sender, StandardMessage.STILL_ON_SHARE_COOLDOWN);
|
||||
}
|
||||
else {
|
||||
InternalStatResult result = shareManager.getStatResult(sender.getName(), shareCode);
|
||||
StoredResult result = shareManager.getStatResult(sender.getName(), shareCode);
|
||||
if (result == null) { //at this point the only possible cause of formattedComponent being null is the request being older than 25 player-requests ago
|
||||
outputManager.sendFeedbackMsg(sender, StandardMessage.STAT_RESULTS_TOO_OLD);
|
||||
} else {
|
||||
|
@ -4,7 +4,8 @@ import com.artemis.the.gr8.playerstats.ThreadManager;
|
||||
import com.artemis.the.gr8.playerstats.enums.StandardMessage;
|
||||
import com.artemis.the.gr8.playerstats.enums.Target;
|
||||
import com.artemis.the.gr8.playerstats.msg.OutputManager;
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.*;
|
||||
import com.artemis.the.gr8.playerstats.statistic.InternalStatRequest;
|
||||
import com.artemis.the.gr8.playerstats.statistic.StatRequest;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.Statistic;
|
||||
import org.bukkit.command.Command;
|
||||
|
@ -1,14 +1,18 @@
|
||||
package com.artemis.the.gr8.playerstats.commands;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.api.PlayerStats;
|
||||
import com.artemis.the.gr8.playerstats.statistic.StatRequest;
|
||||
import com.artemis.the.gr8.playerstats.utils.EnumHandler;
|
||||
import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
|
||||
import com.artemis.the.gr8.playerstats.commands.cmdutils.TabCompleteHelper;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Statistic;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -16,100 +20,137 @@ public final class TabCompleter implements org.bukkit.command.TabCompleter {
|
||||
|
||||
private final EnumHandler enumHandler;
|
||||
private final OfflinePlayerHandler offlinePlayerHandler;
|
||||
private final TabCompleteHelper tabCompleteHelper;
|
||||
|
||||
private final List<String> commandOptions;
|
||||
private List<String> targetSuggestions;
|
||||
private List<String> itemBrokenSuggestions;
|
||||
private List<String> entitySuggestions;
|
||||
|
||||
public TabCompleter(EnumHandler enumHandler, OfflinePlayerHandler offlinePlayerHandler) {
|
||||
this.enumHandler = enumHandler;
|
||||
this.offlinePlayerHandler = offlinePlayerHandler;
|
||||
tabCompleteHelper = new TabCompleteHelper(enumHandler);
|
||||
|
||||
commandOptions = new ArrayList<>();
|
||||
commandOptions.add("top");
|
||||
commandOptions.add("player");
|
||||
commandOptions.add("server");
|
||||
commandOptions.add("me");
|
||||
prepareLists();
|
||||
|
||||
}
|
||||
|
||||
//args[0] = statistic (length = 1)
|
||||
//args[1] = commandOption (top/player/me) OR substatistic (block/item/entitytype) (length = 2)
|
||||
//args[2] = executorName OR commandOption (top/player/me) (length = 3)
|
||||
//args[3] = executorName (length = 4)
|
||||
//args[0] = statistic (length = 1)
|
||||
//args[1] = target (player/server/top) OR sub-stat (block/item/entity) (length = 2)
|
||||
//args[2] = playerName OR target (player/server/top) (length = 3)
|
||||
//args[3] = playerName (length = 4)
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||
List<String> tabSuggestions = new ArrayList<>();
|
||||
|
||||
if (args.length >= 1) {
|
||||
String currentArg = args[args.length -1];
|
||||
String currentArg = args[args.length-1];
|
||||
|
||||
if (args.length == 1) { //after typing "stat", suggest a list of viable statistics
|
||||
tabSuggestions = getFirstArgSuggestions(args[0]);
|
||||
if (args.length == 1) {
|
||||
return getFirstArgSuggestions(args[0]);
|
||||
}
|
||||
|
||||
else { //after checking if args[0] is a viable statistic, suggest substatistic OR commandOptions
|
||||
String previousArg = args[args.length -2];
|
||||
//after checking if args[0] is a viable statistic, suggest sub-stat OR targets
|
||||
String previousArg = args[args.length-2];
|
||||
|
||||
if (enumHandler.isStatistic(previousArg)) {
|
||||
Statistic stat = EnumHandler.getStatEnum(previousArg);
|
||||
if (stat != null) {
|
||||
tabSuggestions = getTabSuggestions(getRelevantList(stat), currentArg);
|
||||
}
|
||||
}
|
||||
|
||||
//if previous arg = "player"
|
||||
else if (previousArg.equalsIgnoreCase("player")) {
|
||||
|
||||
if (args.length >= 3 && enumHandler.isEntityStatistic(args[args.length-3])) {
|
||||
tabSuggestions = commandOptions; //if arg before "player" was entity-stat, suggest commandOptions
|
||||
}
|
||||
else { //otherwise "player" is target-flag: suggest playerNames
|
||||
tabSuggestions = getTabSuggestions(offlinePlayerHandler.getOfflinePlayerNames(), currentArg);
|
||||
}
|
||||
}
|
||||
|
||||
//after a substatistic, suggest commandOptions
|
||||
else if (enumHandler.isSubStatEntry(previousArg)) {
|
||||
tabSuggestions = commandOptions;
|
||||
if (enumHandler.isStatistic(previousArg)) {
|
||||
Statistic stat = EnumHandler.getStatEnum(previousArg);
|
||||
if (stat != null) {
|
||||
return getDynamicTabSuggestions(getAfterStatSuggestions(stat), currentArg);
|
||||
}
|
||||
}
|
||||
else if (previousArg.equalsIgnoreCase("player")) {
|
||||
|
||||
if (args.length >= 3 && enumHandler.isEntityStatistic(args[args.length-3])) {
|
||||
return targetSuggestions; //if arg before "player" was entity-sub-stat, suggest targets
|
||||
}
|
||||
else { //otherwise "player" is the target: suggest playerNames
|
||||
return getDynamicTabSuggestions(offlinePlayerHandler.getOfflinePlayerNames(), currentArg);
|
||||
}
|
||||
}
|
||||
|
||||
//after a substatistic, suggest targets
|
||||
else if (enumHandler.isSubStatEntry(previousArg)) {
|
||||
return targetSuggestions;
|
||||
}
|
||||
|
||||
}
|
||||
return tabSuggestions;
|
||||
return null;
|
||||
}
|
||||
|
||||
private List<String> getFirstArgSuggestions(String currentArg) {
|
||||
List<String> suggestions = enumHandler.getStatNames();
|
||||
suggestions.add("examples");
|
||||
suggestions.add("help");
|
||||
return getTabSuggestions(suggestions, currentArg);
|
||||
return getDynamicTabSuggestions(suggestions, currentArg);
|
||||
}
|
||||
|
||||
private List<String> getTabSuggestions(List<String> completeList, String currentArg) {
|
||||
/**
|
||||
* These tabSuggestions take into account that the commandSender
|
||||
* will have been typing, so they are filtered for the letters
|
||||
* that have already been typed.
|
||||
*/
|
||||
private List<String> getDynamicTabSuggestions(@NotNull List<String> completeList, String currentArg) {
|
||||
return completeList.stream()
|
||||
.filter(item -> item.toLowerCase().contains(currentArg.toLowerCase()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private List<String> getRelevantList(Statistic stat) {
|
||||
private List<String> getAfterStatSuggestions(@NotNull Statistic stat) {
|
||||
switch (stat.getType()) {
|
||||
case BLOCK -> {
|
||||
return tabCompleteHelper.getAllBlockNames();
|
||||
return getAllBlockNames();
|
||||
}
|
||||
case ITEM -> {
|
||||
if (stat == Statistic.BREAK_ITEM) {
|
||||
return tabCompleteHelper.getItemBrokenSuggestions();
|
||||
return getItemBrokenSuggestions();
|
||||
} else {
|
||||
return tabCompleteHelper.getAllItemNames();
|
||||
return getAllItemNames();
|
||||
}
|
||||
}
|
||||
case ENTITY -> {
|
||||
return tabCompleteHelper.getEntitySuggestions();
|
||||
return getEntitySuggestions();
|
||||
}
|
||||
default -> {
|
||||
return commandOptions;
|
||||
return targetSuggestions;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> getAllItemNames() {
|
||||
return enumHandler.getItemNames();
|
||||
}
|
||||
|
||||
private List<String> getItemBrokenSuggestions() {
|
||||
return itemBrokenSuggestions;
|
||||
}
|
||||
|
||||
private List<String> getAllBlockNames() {
|
||||
return enumHandler.getBlockNames();
|
||||
}
|
||||
|
||||
private List<String> getEntitySuggestions() {
|
||||
return entitySuggestions;
|
||||
}
|
||||
|
||||
private void prepareLists() {
|
||||
targetSuggestions = new ArrayList<>();
|
||||
targetSuggestions.add("top");
|
||||
targetSuggestions.add("player");
|
||||
targetSuggestions.add("server");
|
||||
targetSuggestions.add("me");
|
||||
|
||||
//breaking an item means running its durability negative
|
||||
itemBrokenSuggestions = Arrays.stream(Material.values())
|
||||
.parallel()
|
||||
.filter(Material::isItem)
|
||||
.filter(item -> item.getMaxDurability() != 0)
|
||||
.map(Material::toString)
|
||||
.map(String::toLowerCase)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
//the only statistics dealing with entities are killed_entity and entity_killed_by
|
||||
entitySuggestions = Arrays.stream(EntityType.values())
|
||||
.parallel()
|
||||
.filter(EntityType::isAlive)
|
||||
.map(EntityType::toString)
|
||||
.map(String::toLowerCase)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
package com.artemis.the.gr8.playerstats.commands.cmdutils;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.utils.EnumHandler;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.EntityType;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public final class TabCompleteHelper {
|
||||
|
||||
private final EnumHandler enumHandler;
|
||||
private static List<String> itemBrokenSuggestions;
|
||||
private static List<String> entitySuggestions;
|
||||
|
||||
public TabCompleteHelper(EnumHandler enumHandler) {
|
||||
this.enumHandler = enumHandler;
|
||||
prepareLists();
|
||||
}
|
||||
|
||||
public List<String> getAllItemNames() {
|
||||
return enumHandler.getItemNames();
|
||||
}
|
||||
|
||||
public List<String> getItemBrokenSuggestions() {
|
||||
return itemBrokenSuggestions;
|
||||
}
|
||||
|
||||
public List<String> getAllBlockNames() {
|
||||
return enumHandler.getBlockNames();
|
||||
}
|
||||
|
||||
public List<String> getEntitySuggestions() {
|
||||
return entitySuggestions;
|
||||
}
|
||||
|
||||
|
||||
private static void prepareLists() {
|
||||
//breaking an item means running its durability negative
|
||||
itemBrokenSuggestions = Arrays.stream(Material.values())
|
||||
.parallel()
|
||||
.filter(Material::isItem)
|
||||
.filter(item -> item.getMaxDurability() != 0)
|
||||
.map(Material::toString)
|
||||
.map(String::toLowerCase)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
//the only statistics dealing with entities are killed_entity and entity_killed_by
|
||||
entitySuggestions = Arrays.stream(EntityType.values())
|
||||
.parallel()
|
||||
.filter(EntityType::isAlive)
|
||||
.map(EntityType::toString)
|
||||
.map(String::toLowerCase)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.artemis.the.gr8.playerstats.msg;
|
||||
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public final class FormattingFunction {
|
||||
|
||||
private final BiFunction<Integer, CommandSender, TextComponent> formattingFunction;
|
||||
|
||||
public FormattingFunction(BiFunction<Integer, CommandSender, TextComponent> formattingFunction) {
|
||||
this.formattingFunction = formattingFunction;
|
||||
}
|
||||
|
||||
public TextComponent getResultWithShareButton(Integer shareCode) {
|
||||
return this.apply(shareCode, null);
|
||||
}
|
||||
|
||||
public TextComponent getResultWithSharerName(CommandSender sender) {
|
||||
return this.apply(null, sender);
|
||||
}
|
||||
|
||||
public TextComponent getDefaultResult() {
|
||||
return this.apply(null, null);
|
||||
}
|
||||
|
||||
private TextComponent apply(Integer shareCode, CommandSender sender) {
|
||||
return formattingFunction.apply(shareCode, sender);
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ 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.statistic.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;
|
||||
@ -313,7 +313,7 @@ public final class MessageBuilder implements StatFormatter {
|
||||
* <br>- If both parameters are null, the formattedComponent will be returned
|
||||
* as is.
|
||||
*/
|
||||
public @NotNull BiFunction<Integer, CommandSender, TextComponent> formattedPlayerStatFunction(int stat, @NotNull StatRequest.Settings request) {
|
||||
public @NotNull FormattingFunction 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 StatFormatter {
|
||||
* <br>- If both parameters are null, the formattedComponent will be returned
|
||||
* as is.
|
||||
*/
|
||||
public @NotNull BiFunction<Integer, CommandSender, TextComponent> formattedServerStatFunction(long stat, @NotNull StatRequest.Settings request) {
|
||||
public @NotNull FormattingFunction formattedServerStatFunction(long stat, @NotNull StatRequest.Settings request) {
|
||||
TextComponent serverStat = formatServerStat(stat, request.getStatistic(), request.getSubStatEntryName());
|
||||
return getFormattingFunction(serverStat, Target.SERVER);
|
||||
}
|
||||
@ -345,13 +345,13 @@ public final class MessageBuilder implements StatFormatter {
|
||||
* <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 StatRequest.Settings request) {
|
||||
public @NotNull FormattingFunction 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);
|
||||
final boolean useEntersForShared = config.useEnters(Target.TOP, true);
|
||||
|
||||
return (shareCode, sender) -> {
|
||||
BiFunction<Integer, CommandSender, TextComponent> biFunction = (shareCode, sender) -> {
|
||||
TextComponent.Builder topBuilder = text();
|
||||
|
||||
//if we're adding a share-button
|
||||
@ -395,6 +395,7 @@ public final class MessageBuilder implements StatFormatter {
|
||||
}
|
||||
return topBuilder.build();
|
||||
};
|
||||
return new FormattingFunction(biFunction);
|
||||
}
|
||||
|
||||
private @NotNull TextComponent getPlayerStatComponent(String playerName, TextComponent statNumberComponent, Statistic statistic, @Nullable String subStatName, @Nullable Unit unit) {
|
||||
@ -676,11 +677,11 @@ public final class MessageBuilder implements StatFormatter {
|
||||
return componentFactory.sharerName(sender.getName());
|
||||
}
|
||||
|
||||
private @NotNull BiFunction<Integer, CommandSender, TextComponent> getFormattingFunction(@NotNull TextComponent statResult, Target target) {
|
||||
private @NotNull FormattingFunction getFormattingFunction(@NotNull TextComponent statResult, Target target) {
|
||||
boolean useEnters = config.useEnters(target, false);
|
||||
boolean useEntersForShared = config.useEnters(target, true);
|
||||
|
||||
return (shareCode, sender) -> {
|
||||
BiFunction<Integer, CommandSender, TextComponent> biFunction = (shareCode, sender) -> {
|
||||
TextComponent.Builder statBuilder = text();
|
||||
|
||||
//if we're adding a share-button
|
||||
@ -711,6 +712,7 @@ public final class MessageBuilder implements StatFormatter {
|
||||
}
|
||||
return statBuilder.build();
|
||||
};
|
||||
return new FormattingFunction(biFunction);
|
||||
}
|
||||
|
||||
private int getNumberOfDotsToAlign(String displayText) {
|
||||
|
@ -1,11 +1,10 @@
|
||||
package com.artemis.the.gr8.playerstats.msg;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.ShareManager;
|
||||
import com.artemis.the.gr8.playerstats.config.ConfigHandler;
|
||||
import com.artemis.the.gr8.playerstats.enums.StandardMessage;
|
||||
import com.artemis.the.gr8.playerstats.msg.components.BukkitConsoleComponentFactory;
|
||||
import com.artemis.the.gr8.playerstats.msg.components.PrideComponentFactory;
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.StatRequest;
|
||||
import com.artemis.the.gr8.playerstats.statistic.StatRequest;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.Bukkit;
|
||||
@ -19,7 +18,6 @@ import java.time.LocalDate;
|
||||
import java.time.Month;
|
||||
import java.util.EnumMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static com.artemis.the.gr8.playerstats.enums.StandardMessage.*;
|
||||
@ -35,16 +33,13 @@ public final class OutputManager {
|
||||
|
||||
private static BukkitAudiences adventure;
|
||||
private static ConfigHandler config;
|
||||
private static ShareManager shareManager;
|
||||
private static MessageBuilder messageBuilder;
|
||||
private static MessageBuilder consoleMessageBuilder;
|
||||
|
||||
private static EnumMap<StandardMessage, Function<MessageBuilder, TextComponent>> standardMessages;
|
||||
|
||||
public OutputManager(BukkitAudiences adventure, ConfigHandler config, ShareManager shareManager) {
|
||||
public OutputManager(BukkitAudiences adventure, ConfigHandler config) {
|
||||
OutputManager.adventure = adventure;
|
||||
OutputManager.config = config;
|
||||
OutputManager.shareManager = shareManager;
|
||||
|
||||
getMessageBuilders();
|
||||
prepareFunctions();
|
||||
@ -58,39 +53,34 @@ public final class OutputManager {
|
||||
return messageBuilder;
|
||||
}
|
||||
|
||||
//TODO separate formatting from internal saving for sharing
|
||||
|
||||
/** @return a TextComponent with the following parts:
|
||||
/**
|
||||
* @return a TextComponent with the following parts:
|
||||
* <br>[player-name]: [number] [stat-name] {sub-stat-name}
|
||||
*/
|
||||
public TextComponent formatAndSavePlayerStat(@NotNull StatRequest.Settings requestSettings, int playerStat) {
|
||||
BiFunction<Integer, CommandSender, TextComponent> playerStatFunction =
|
||||
getMessageBuilder(requestSettings.getCommandSender()).formattedPlayerStatFunction(playerStat, requestSettings);
|
||||
|
||||
return processFunction(requestSettings.getCommandSender(), playerStatFunction);
|
||||
public @NotNull FormattingFunction formatPlayerStat(@NotNull StatRequest.Settings requestSettings, int playerStat) {
|
||||
return getMessageBuilder(requestSettings.getCommandSender())
|
||||
.formattedPlayerStatFunction(playerStat, requestSettings);
|
||||
}
|
||||
|
||||
/** @return a TextComponent with the following parts:
|
||||
/**
|
||||
* @return a TextComponent with the following parts:
|
||||
* <br>[Total on] [server-name]: [number] [stat-name] [sub-stat-name]
|
||||
*/
|
||||
public TextComponent formatAndSaveServerStat(@NotNull StatRequest.Settings requestSettings, long serverStat) {
|
||||
BiFunction<Integer, CommandSender, TextComponent> serverStatFunction =
|
||||
getMessageBuilder(requestSettings.getCommandSender()).formattedServerStatFunction(serverStat, requestSettings);
|
||||
|
||||
return processFunction(requestSettings.getCommandSender(), serverStatFunction);
|
||||
public @NotNull FormattingFunction formatServerStat(@NotNull StatRequest.Settings requestSettings, long serverStat) {
|
||||
return getMessageBuilder(requestSettings.getCommandSender())
|
||||
.formattedServerStatFunction(serverStat, requestSettings);
|
||||
}
|
||||
|
||||
/** @return a TextComponent with the following parts:
|
||||
/**
|
||||
* @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...
|
||||
*/
|
||||
public TextComponent formatAndSaveTopStat(@NotNull StatRequest.Settings requestSettings, @NotNull LinkedHashMap<String, Integer> topStats) {
|
||||
BiFunction<Integer, CommandSender, TextComponent> topStatFunction =
|
||||
getMessageBuilder(requestSettings.getCommandSender()).formattedTopStatFunction(topStats, requestSettings);
|
||||
|
||||
return processFunction(requestSettings.getCommandSender(), topStatFunction);
|
||||
public @NotNull FormattingFunction formatTopStats(@NotNull StatRequest.Settings requestSettings, @NotNull LinkedHashMap<String, Integer> topStats) {
|
||||
return getMessageBuilder(requestSettings.getCommandSender())
|
||||
.formattedTopStatFunction(topStats, requestSettings);
|
||||
}
|
||||
|
||||
public void sendFeedbackMsg(@NotNull CommandSender sender, StandardMessage message) {
|
||||
@ -137,21 +127,6 @@ public final class OutputManager {
|
||||
adventure.sender(sender).sendMessage(component);
|
||||
}
|
||||
|
||||
private TextComponent processFunction(CommandSender sender, @NotNull BiFunction<Integer, CommandSender, TextComponent> statResultFunction) {
|
||||
boolean saveOutput = !(sender instanceof ConsoleCommandSender) &&
|
||||
ShareManager.isEnabled() &&
|
||||
shareManager.senderHasPermission(sender);
|
||||
|
||||
if (saveOutput) {
|
||||
int shareCode =
|
||||
shareManager.saveStatResult(sender.getName(), statResultFunction.apply(null, sender));
|
||||
return statResultFunction.apply(shareCode, null);
|
||||
}
|
||||
else {
|
||||
return statResultFunction.apply(null, null);
|
||||
}
|
||||
}
|
||||
|
||||
private MessageBuilder getMessageBuilder(CommandSender sender) {
|
||||
return sender instanceof ConsoleCommandSender ? consoleMessageBuilder : messageBuilder;
|
||||
}
|
||||
@ -168,7 +143,7 @@ public final class OutputManager {
|
||||
return MessageBuilder.defaultBuilder(config);
|
||||
}
|
||||
|
||||
private static MessageBuilder getConsoleMessageBuilder() {
|
||||
private static @NotNull MessageBuilder getConsoleMessageBuilder() {
|
||||
MessageBuilder consoleBuilder;
|
||||
if (isBukkit()) {
|
||||
consoleBuilder = MessageBuilder.fromComponentFactory(config, new BukkitConsoleComponentFactory(config));
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.artemis.the.gr8.playerstats.reload;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.Main;
|
||||
import com.artemis.the.gr8.playerstats.ShareManager;
|
||||
import com.artemis.the.gr8.playerstats.share.ShareManager;
|
||||
import com.artemis.the.gr8.playerstats.enums.StandardMessage;
|
||||
import com.artemis.the.gr8.playerstats.msg.OutputManager;
|
||||
import com.artemis.the.gr8.playerstats.msg.msgutils.LanguageKeyHandler;
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.artemis.the.gr8.playerstats;
|
||||
package com.artemis.the.gr8.playerstats.share;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.statistic.result.InternalStatResult;
|
||||
import com.artemis.the.gr8.playerstats.config.ConfigHandler;
|
||||
import com.artemis.the.gr8.playerstats.utils.MyLogger;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
@ -30,7 +29,7 @@ public final class ShareManager {
|
||||
private static int waitingTime;
|
||||
|
||||
private static volatile AtomicInteger resultID;
|
||||
private static ConcurrentHashMap<Integer, InternalStatResult> statResultQueue;
|
||||
private static ConcurrentHashMap<Integer, StoredResult> statResultQueue;
|
||||
private static ConcurrentHashMap<String, Instant> shareTimeStamp;
|
||||
private static ArrayBlockingQueue<Integer> sharedResults;
|
||||
|
||||
@ -75,8 +74,7 @@ public final class ShareManager {
|
||||
removeExcessResults(playerName);
|
||||
|
||||
int ID = getNextIDNumber();
|
||||
//UUID shareCode = UUID.randomUUID();
|
||||
InternalStatResult result = new InternalStatResult(playerName, statResult, ID);
|
||||
StoredResult result = new StoredResult(playerName, statResult, ID);
|
||||
int shareCode = result.hashCode();
|
||||
statResultQueue.put(shareCode, result);
|
||||
MyLogger.logMediumLevelMsg("Saving statResults with no. " + ID);
|
||||
@ -103,7 +101,7 @@ public final class ShareManager {
|
||||
* and returns the formattedComponent. If no formattedComponent was found,
|
||||
* returns null.
|
||||
*/
|
||||
public @Nullable InternalStatResult getStatResult(String playerName, int shareCode) {
|
||||
public @Nullable StoredResult getStatResult(String playerName, int shareCode) {
|
||||
if (statResultQueue.containsKey(shareCode)) {
|
||||
shareTimeStamp.put(playerName, Instant.now());
|
||||
|
||||
@ -134,7 +132,7 @@ public final class ShareManager {
|
||||
* StatResults saved, remove the oldest one.
|
||||
*/
|
||||
private void removeExcessResults(String playerName) {
|
||||
List<InternalStatResult> alreadySavedResults = statResultQueue.values()
|
||||
List<StoredResult> alreadySavedResults = statResultQueue.values()
|
||||
.parallelStream()
|
||||
.filter(result -> result.executorName().equalsIgnoreCase(playerName))
|
||||
.toList();
|
||||
@ -142,7 +140,7 @@ public final class ShareManager {
|
||||
if (alreadySavedResults.size() > 25) {
|
||||
int hashCode = alreadySavedResults
|
||||
.parallelStream()
|
||||
.min(Comparator.comparing(InternalStatResult::ID))
|
||||
.min(Comparator.comparing(StoredResult::ID))
|
||||
.orElseThrow().hashCode();
|
||||
MyLogger.logMediumLevelMsg("Removing old stat no. " + statResultQueue.get(hashCode).ID() + " for player " + playerName);
|
||||
statResultQueue.remove(hashCode);
|
@ -0,0 +1,10 @@
|
||||
package com.artemis.the.gr8.playerstats.share;
|
||||
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
|
||||
/**
|
||||
* This Record is used to store stat-results internally,
|
||||
* so Players can share them by clicking a share-button.
|
||||
*/
|
||||
public record StoredResult(String executorName, TextComponent formattedValue, int ID) {
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package com.artemis.the.gr8.playerstats.statistic.request;
|
||||
package com.artemis.the.gr8.playerstats.statistic;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.Main;
|
||||
import com.artemis.the.gr8.playerstats.statistic.result.StatResult;
|
||||
import com.artemis.the.gr8.playerstats.config.ConfigHandler;
|
||||
import com.artemis.the.gr8.playerstats.utils.EnumHandler;
|
||||
import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
@ -21,12 +21,14 @@ import java.util.regex.Pattern;
|
||||
|
||||
public final class InternalStatRequest extends StatRequest<TextComponent> {
|
||||
|
||||
private final ConfigHandler config;
|
||||
private final OfflinePlayerHandler offlinePlayerHandler;
|
||||
private final EnumHandler enumHandler;
|
||||
private final Pattern targetPattern;
|
||||
|
||||
public InternalStatRequest(CommandSender sender, String[] args) {
|
||||
super(sender);
|
||||
config = Main.getConfigHandler();
|
||||
offlinePlayerHandler = Main.getOfflinePlayerHandler();
|
||||
enumHandler = Main.getEnumHandler();
|
||||
targetPattern = Pattern.compile("top|server|me|player");
|
||||
@ -36,7 +38,7 @@ public final class InternalStatRequest extends StatRequest<TextComponent> {
|
||||
|
||||
@Override
|
||||
public @NotNull StatResult<TextComponent> execute() {
|
||||
return Main.getRequestProcessor().getInternalResult(settings);
|
||||
return Main.getRequestProcessor().getInternalResult(super.getSettings());
|
||||
}
|
||||
|
||||
private void processArgs(CommandSender sender, String[] args) {
|
||||
@ -56,31 +58,31 @@ public final class InternalStatRequest extends StatRequest<TextComponent> {
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
super.settings.configureForPlayer(playerName);
|
||||
super.getSettings().configureForPlayer(playerName);
|
||||
String[] extractedPlayerName = removeArg(leftoverArgs, playerName);
|
||||
return removeArg(extractedPlayerName, arg);
|
||||
}
|
||||
}
|
||||
case "me" -> {
|
||||
if (sender instanceof Player) {
|
||||
super.settings.configureForPlayer(sender.getName());
|
||||
super.getSettings().configureForPlayer(sender.getName());
|
||||
} else {
|
||||
super.settings.configureForServer();
|
||||
super.getSettings().configureForServer();
|
||||
}
|
||||
}
|
||||
case "server" -> super.settings.configureForServer();
|
||||
case "top" -> super.settings.configureForTop();
|
||||
case "server" -> super.getSettings().configureForServer();
|
||||
case "top" -> super.getSettings().configureForTop(config.getTopListMaxSize());
|
||||
}
|
||||
return removeArg(leftoverArgs, arg);
|
||||
}
|
||||
}
|
||||
//if no target is found, but there is a playerName, assume target = Target.PLAYER
|
||||
if (playerName != null) {
|
||||
super.settings.configureForPlayer(playerName);
|
||||
super.getSettings().configureForPlayer(playerName);
|
||||
return removeArg(leftoverArgs, playerName);
|
||||
}
|
||||
//otherwise, assume target = Target.TOP
|
||||
super.settings.configureForTop();
|
||||
super.getSettings().configureForTop(config.getTopListMaxSize());
|
||||
return leftoverArgs;
|
||||
}
|
||||
|
||||
@ -102,23 +104,23 @@ public final class InternalStatRequest extends StatRequest<TextComponent> {
|
||||
for (String arg : leftoverArgs) {
|
||||
if (enumHandler.isSubStatEntry(arg)) {
|
||||
switch (statistic.getType()) {
|
||||
case UNTYPED -> super.configureUntyped(statistic);
|
||||
case UNTYPED -> super.getSettings().configureUntyped(statistic);
|
||||
case ITEM -> {
|
||||
Material item = EnumHandler.getItemEnum(arg);
|
||||
if (item != null) {
|
||||
super.configureBlockOrItemType(statistic, item);
|
||||
super.getSettings().configureBlockOrItemType(statistic, item);
|
||||
}
|
||||
}
|
||||
case BLOCK -> {
|
||||
Material block = EnumHandler.getBlockEnum(arg);
|
||||
if (block != null) {
|
||||
super.configureBlockOrItemType(statistic, block);
|
||||
super.getSettings().configureBlockOrItemType(statistic, block);
|
||||
}
|
||||
}
|
||||
case ENTITY -> {
|
||||
EntityType entityType = EnumHandler.getEntityEnum(arg);
|
||||
if (entityType != null) {
|
||||
super.configureEntityType(statistic, entityType);
|
||||
super.getSettings().configureEntityType(statistic, entityType);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -142,5 +144,4 @@ public final class InternalStatRequest extends StatRequest<TextComponent> {
|
||||
currentArgs.remove(argToRemove);
|
||||
return currentArgs.toArray(String[]::new);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,43 +1,40 @@
|
||||
package com.artemis.the.gr8.playerstats.statistic.request;
|
||||
package com.artemis.the.gr8.playerstats.statistic;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.Main;
|
||||
import com.artemis.the.gr8.playerstats.api.RequestGenerator;
|
||||
import com.artemis.the.gr8.playerstats.statistic.result.StatResult;
|
||||
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> {
|
||||
|
||||
public PlayerStatRequest(String playerName) {
|
||||
this(Bukkit.getConsoleSender(), playerName);
|
||||
}
|
||||
|
||||
public PlayerStatRequest(CommandSender requester, String playerName) {
|
||||
super(requester);
|
||||
super.settings.configureForPlayer(playerName);
|
||||
super(Bukkit.getConsoleSender());
|
||||
super.getSettings().configureForPlayer(playerName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StatRequest<Integer> untyped(@NotNull Statistic statistic) {
|
||||
return super.configureUntyped(statistic);
|
||||
super.getSettings().configureUntyped(statistic);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StatRequest<Integer> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
|
||||
return super.configureBlockOrItemType(statistic, material);
|
||||
super.getSettings().configureBlockOrItemType(statistic, material);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StatRequest<Integer> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
|
||||
return super.configureEntityType(statistic, entityType);
|
||||
super.getSettings().configureEntityType(statistic, entityType);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull StatResult<Integer> execute() {
|
||||
return Main.getRequestProcessor().getPlayerResult(settings);
|
||||
return Main.getRequestProcessor().processPlayerRequest(super.getSettings());
|
||||
}
|
||||
}
|
@ -1,15 +1,17 @@
|
||||
package com.artemis.the.gr8.playerstats.statistic;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.ThreadManager;
|
||||
import com.artemis.the.gr8.playerstats.msg.FormattingFunction;
|
||||
import com.artemis.the.gr8.playerstats.msg.OutputManager;
|
||||
import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils;
|
||||
import com.artemis.the.gr8.playerstats.statistic.request.StatRequest;
|
||||
import com.artemis.the.gr8.playerstats.statistic.result.StatResult;
|
||||
import com.artemis.the.gr8.playerstats.share.ShareManager;
|
||||
import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
|
||||
import com.artemis.the.gr8.playerstats.utils.MyLogger;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.*;
|
||||
@ -22,44 +24,49 @@ public final class RequestProcessor {
|
||||
|
||||
private final OfflinePlayerHandler offlinePlayerHandler;
|
||||
private static OutputManager outputManager;
|
||||
private static ShareManager shareManager;
|
||||
|
||||
public RequestProcessor(OfflinePlayerHandler offlinePlayerHandler, OutputManager outputManager) {
|
||||
public RequestProcessor(OfflinePlayerHandler offlinePlayerHandler, OutputManager outputManager, ShareManager shareManager) {
|
||||
this.offlinePlayerHandler = offlinePlayerHandler;
|
||||
RequestProcessor.outputManager = outputManager;
|
||||
RequestProcessor.shareManager = shareManager;
|
||||
}
|
||||
|
||||
public @NotNull StatResult<TextComponent> getInternalResult(@NotNull StatRequest.Settings requestSettings) {
|
||||
StatResult<?> result = switch (requestSettings.getTarget()) {
|
||||
case PLAYER -> getPlayerResult(requestSettings);
|
||||
case SERVER -> getServerResult(requestSettings);
|
||||
case TOP -> getTopResult(requestSettings);
|
||||
case PLAYER -> processPlayerRequest(requestSettings);
|
||||
case SERVER -> processServerRequest(requestSettings);
|
||||
case TOP -> processTopRequest(requestSettings);
|
||||
};
|
||||
|
||||
return new StatResult<>(result.formattedComponent(), result.formattedComponent(), result.formattedString());
|
||||
}
|
||||
|
||||
public @NotNull StatResult<Integer> getPlayerResult(StatRequest.Settings requestSettings) {
|
||||
public @NotNull StatResult<Integer> processPlayerRequest(StatRequest.Settings requestSettings) {
|
||||
int stat = getPlayerStat(requestSettings);
|
||||
TextComponent result = outputManager.formatAndSavePlayerStat(requestSettings, stat);
|
||||
String serializedResult = ComponentUtils.getTranslatableComponentSerializer().serialize(result);
|
||||
FormattingFunction formattingFunction = outputManager.formatPlayerStat(requestSettings, stat);
|
||||
TextComponent formattedResult = processFunction(requestSettings.getCommandSender(), formattingFunction);
|
||||
String resultAsString = ComponentUtils.getTranslatableComponentSerializer().serialize(formattedResult);
|
||||
|
||||
return new StatResult<>(stat, result, serializedResult);
|
||||
return new StatResult<>(stat, formattedResult, resultAsString);
|
||||
}
|
||||
|
||||
public @NotNull StatResult<Long> getServerResult(StatRequest.Settings requestSettings) {
|
||||
public @NotNull StatResult<Long> processServerRequest(StatRequest.Settings requestSettings) {
|
||||
long stat = getServerStat(requestSettings);
|
||||
TextComponent result = outputManager.formatAndSaveServerStat(requestSettings, stat);
|
||||
String serializedResult = ComponentUtils.getTranslatableComponentSerializer().serialize(result);
|
||||
FormattingFunction formattingFunction = outputManager.formatServerStat(requestSettings, stat);
|
||||
TextComponent formattedResult = processFunction(requestSettings.getCommandSender(), formattingFunction);
|
||||
String resultAsString = ComponentUtils.getTranslatableComponentSerializer().serialize(formattedResult);
|
||||
|
||||
return new StatResult<>(stat, result, serializedResult);
|
||||
return new StatResult<>(stat, formattedResult, resultAsString);
|
||||
}
|
||||
|
||||
public @NotNull StatResult<LinkedHashMap<String, Integer>> getTopResult(StatRequest.Settings requestSettings) {
|
||||
public @NotNull StatResult<LinkedHashMap<String, Integer>> processTopRequest(StatRequest.Settings requestSettings) {
|
||||
LinkedHashMap<String, Integer> stats = getTopStats(requestSettings);
|
||||
TextComponent result = outputManager.formatAndSaveTopStat(requestSettings, stats);
|
||||
String serializedResult = ComponentUtils.getTranslatableComponentSerializer().serialize(result);
|
||||
FormattingFunction formattingFunction = outputManager.formatTopStats(requestSettings, stats);
|
||||
TextComponent formattedResult = processFunction(requestSettings.getCommandSender(), formattingFunction);
|
||||
String resultAsString = ComponentUtils.getTranslatableComponentSerializer().serialize(formattedResult);
|
||||
|
||||
return new StatResult<>(stats, result, serializedResult);
|
||||
return new StatResult<>(stats, formattedResult, resultAsString);
|
||||
}
|
||||
|
||||
private int getPlayerStat(@NotNull StatRequest.Settings requestSettings) {
|
||||
@ -87,6 +94,20 @@ public final class RequestProcessor {
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
|
||||
}
|
||||
|
||||
private TextComponent processFunction(CommandSender sender, FormattingFunction function) {
|
||||
if (outputShouldBeStored(sender)) {
|
||||
int shareCode = shareManager.saveStatResult(sender.getName(), function.getResultWithSharerName(sender));
|
||||
return function.getResultWithShareButton(shareCode);
|
||||
}
|
||||
return function.getDefaultResult();
|
||||
}
|
||||
|
||||
private boolean outputShouldBeStored(CommandSender sender) {
|
||||
return !(sender instanceof ConsoleCommandSender) &&
|
||||
ShareManager.isEnabled() &&
|
||||
shareManager.senderHasPermission(sender);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes a bunch of worker pool threads to get the statistics for
|
||||
* all players that are stored in the {@link OfflinePlayerHandler}).
|
||||
|
@ -1,8 +1,7 @@
|
||||
package com.artemis.the.gr8.playerstats.statistic.request;
|
||||
package com.artemis.the.gr8.playerstats.statistic;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.Main;
|
||||
import com.artemis.the.gr8.playerstats.api.RequestGenerator;
|
||||
import com.artemis.the.gr8.playerstats.statistic.result.StatResult;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Statistic;
|
||||
@ -12,32 +11,32 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public final class ServerStatRequest extends StatRequest<Long> implements RequestGenerator<Long> {
|
||||
|
||||
public ServerStatRequest() {
|
||||
this(Bukkit.getConsoleSender());
|
||||
}
|
||||
|
||||
public ServerStatRequest(CommandSender requester) {
|
||||
super(requester);
|
||||
super.settings.configureForServer();
|
||||
public ServerStatRequest() {
|
||||
super(Bukkit.getConsoleSender());
|
||||
super.getSettings().configureForServer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public StatRequest<Long> untyped(@NotNull Statistic statistic) {
|
||||
return super.configureUntyped(statistic);
|
||||
super.getSettings().configureUntyped(statistic);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StatRequest<Long> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
|
||||
return super.configureBlockOrItemType(statistic, material);
|
||||
super.getSettings().configureBlockOrItemType(statistic, material);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StatRequest<Long> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
|
||||
return super.configureEntityType(statistic, entityType);
|
||||
super.getSettings().configureEntityType(statistic, entityType);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull StatResult<Long> execute() {
|
||||
return Main.getRequestProcessor().getServerResult(settings);
|
||||
return Main.getRequestProcessor().processServerRequest(super.getSettings());
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
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.utils.MyLogger;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
@ -1,8 +1,6 @@
|
||||
package com.artemis.the.gr8.playerstats.statistic.request;
|
||||
package com.artemis.the.gr8.playerstats.statistic;
|
||||
|
||||
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;
|
||||
@ -22,7 +20,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
*/
|
||||
public abstract class StatRequest<T> {
|
||||
|
||||
protected final Settings settings;
|
||||
private final Settings settings;
|
||||
|
||||
protected StatRequest(CommandSender requester) {
|
||||
settings = new Settings(requester);
|
||||
@ -77,40 +75,6 @@ public abstract class StatRequest<T> {
|
||||
}
|
||||
}
|
||||
|
||||
protected StatRequest<T> configureUntyped(@NotNull Statistic statistic) {
|
||||
if (statistic.getType() == Statistic.Type.UNTYPED) {
|
||||
settings.statistic = 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.block = material;
|
||||
}
|
||||
else if (type == Statistic.Type.ITEM && material.isItem()){
|
||||
settings.item = 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.statistic = statistic;
|
||||
settings.subStatEntryName = material.toString();
|
||||
return this;
|
||||
}
|
||||
|
||||
protected StatRequest<T> configureEntityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException {
|
||||
if (statistic.getType() == Statistic.Type.ENTITY) {
|
||||
settings.statistic = statistic;
|
||||
settings.entity = entityType;
|
||||
settings.subStatEntryName = entityType.toString();
|
||||
return this;
|
||||
}
|
||||
throw new IllegalArgumentException("This statistic is not of Type.Entity");
|
||||
}
|
||||
|
||||
public static final class Settings {
|
||||
private final CommandSender sender;
|
||||
private Statistic statistic;
|
||||
@ -139,15 +103,40 @@ public abstract class StatRequest<T> {
|
||||
this.target = Target.SERVER;
|
||||
}
|
||||
|
||||
void configureForTop() {
|
||||
configureForTop(Main.getConfigHandler().getTopListMaxSize());
|
||||
}
|
||||
|
||||
void configureForTop(int topListSize) {
|
||||
this.target = Target.TOP;
|
||||
this.topListSize = topListSize != 0 ?
|
||||
topListSize :
|
||||
Main.getConfigHandler().getTopListMaxSize();
|
||||
this.topListSize = topListSize;
|
||||
}
|
||||
|
||||
void configureUntyped(@NotNull Statistic statistic) {
|
||||
if (statistic.getType() == Statistic.Type.UNTYPED) {
|
||||
this.statistic = statistic;
|
||||
}
|
||||
throw new IllegalArgumentException("This statistic is not of Type.Untyped");
|
||||
}
|
||||
|
||||
void configureBlockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException {
|
||||
Statistic.Type type = statistic.getType();
|
||||
if (type == Statistic.Type.BLOCK && material.isBlock()) {
|
||||
this.block = material;
|
||||
}
|
||||
else if (type == Statistic.Type.ITEM && material.isItem()){
|
||||
this.item = 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");
|
||||
}
|
||||
this.statistic = statistic;
|
||||
this.subStatEntryName = material.toString();
|
||||
}
|
||||
|
||||
void configureEntityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException {
|
||||
if (statistic.getType() == Statistic.Type.ENTITY) {
|
||||
this.statistic = statistic;
|
||||
this.entity = entityType;
|
||||
this.subStatEntryName = entityType.toString();
|
||||
}
|
||||
throw new IllegalArgumentException("This statistic is not of Type.Entity");
|
||||
}
|
||||
|
||||
public @NotNull CommandSender getCommandSender() {
|
@ -1,4 +1,4 @@
|
||||
package com.artemis.the.gr8.playerstats.statistic.result;
|
||||
package com.artemis.the.gr8.playerstats.statistic;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.api.StatFormatter;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
@ -2,8 +2,6 @@ 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.statistic.result.StatResult;
|
||||
import com.artemis.the.gr8.playerstats.utils.MyLogger;
|
||||
import com.artemis.the.gr8.playerstats.enums.StandardMessage;
|
||||
import com.artemis.the.gr8.playerstats.reload.ReloadThread;
|
||||
@ -18,15 +16,12 @@ import java.util.*;
|
||||
public final class StatThread extends Thread {
|
||||
|
||||
private static OutputManager outputManager;
|
||||
private static RequestProcessor requestProcessor;
|
||||
|
||||
private final ReloadThread reloadThread;
|
||||
private final StatRequest<?> statRequest;
|
||||
|
||||
public StatThread(OutputManager m, RequestProcessor t, int ID, StatRequest<?> s, @Nullable ReloadThread r) {
|
||||
public StatThread(OutputManager m, int ID, StatRequest<?> s, @Nullable ReloadThread r) {
|
||||
outputManager = m;
|
||||
requestProcessor = t;
|
||||
|
||||
reloadThread = r;
|
||||
statRequest = s;
|
||||
|
||||
|
@ -1,12 +1,10 @@
|
||||
package com.artemis.the.gr8.playerstats.statistic.request;
|
||||
package com.artemis.the.gr8.playerstats.statistic;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.Main;
|
||||
import com.artemis.the.gr8.playerstats.statistic.result.StatResult;
|
||||
import com.artemis.the.gr8.playerstats.api.RequestGenerator;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Statistic;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@ -15,31 +13,30 @@ import java.util.LinkedHashMap;
|
||||
public final class TopStatRequest extends StatRequest<LinkedHashMap<String, Integer>> implements RequestGenerator<LinkedHashMap<String, Integer>> {
|
||||
|
||||
public TopStatRequest(int topListSize) {
|
||||
this(Bukkit.getConsoleSender(), topListSize);
|
||||
}
|
||||
|
||||
public TopStatRequest(CommandSender requester, int topListSize) {
|
||||
super(requester);
|
||||
super.settings.configureForTop(topListSize);
|
||||
super(Bukkit.getConsoleSender());
|
||||
super.getSettings().configureForTop(topListSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StatRequest<LinkedHashMap<String, Integer>> untyped(@NotNull Statistic statistic) {
|
||||
return super.configureUntyped(statistic);
|
||||
super.getSettings().configureUntyped(statistic);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StatRequest<LinkedHashMap<String, Integer>> blockOrItemType(@NotNull Statistic statistic, @NotNull Material material) {
|
||||
return super.configureBlockOrItemType(statistic, material);
|
||||
super.getSettings().configureBlockOrItemType(statistic, material);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StatRequest<LinkedHashMap<String, Integer>> entityType(@NotNull Statistic statistic, @NotNull EntityType entityType) {
|
||||
return super.configureEntityType(statistic, entityType);
|
||||
super.getSettings().configureEntityType(statistic, entityType);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull StatResult<LinkedHashMap<String, Integer>> execute() {
|
||||
return Main.getRequestProcessor().getTopResult(settings);
|
||||
return Main.getRequestProcessor().processTopRequest(super.getSettings());
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
package com.artemis.the.gr8.playerstats.statistic.result;
|
||||
|
||||
import com.artemis.the.gr8.playerstats.msg.components.ComponentUtils;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
|
||||
/**
|
||||
* This Record is used to store stat-results internally,
|
||||
* so Players can share them by clicking a share-button.
|
||||
*/
|
||||
public record InternalStatResult(String executorName, TextComponent formattedValue, int ID) {
|
||||
|
||||
/**
|
||||
* Gets the ID number for this StatResult. Unlike for the
|
||||
* other {@link StatResult} implementations, this one does
|
||||
* not return the actual statistic data, because this
|
||||
* implementation is meant for internal saving-and-sharing only.
|
||||
* This method is only for Interface-consistency,
|
||||
* InternalStatResult#ID is better.
|
||||
*
|
||||
@return Integer that represents this StatResult's ID number
|
||||
*/
|
||||
public Integer getNumericalValue() {
|
||||
return ID;
|
||||
}
|
||||
|
||||
public TextComponent getFormattedTextComponent() {
|
||||
return formattedValue;
|
||||
}
|
||||
|
||||
public String getFormattedString() {
|
||||
return ComponentUtils.getTranslatableComponentSerializer()
|
||||
.serialize(formattedValue);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user