From b1c015e156259f7e9fc14f36e1f05c6a4da22198 Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Fri, 14 Oct 2022 16:40:39 +0200 Subject: [PATCH] Started to move arg-processing to StatCommand after testing (feedback is no longer working with the increased StatRequest conditions of what settings are accepted) --- .../gr8/playerstats/commands/StatCommand.java | 94 +++++++++++++++++++ .../statistic/InternalStatRequest.java | 15 ++- .../playerstats/statistic/StatRequest.java | 16 ++-- 3 files changed, 116 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/artemis/the/gr8/playerstats/commands/StatCommand.java b/src/main/java/com/artemis/the/gr8/playerstats/commands/StatCommand.java index f3b62b1..fea7533 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/commands/StatCommand.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/commands/StatCommand.java @@ -5,18 +5,30 @@ 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.InternalStatRequest; +import com.artemis.the.gr8.playerstats.statistic.PlayerStatRequest; 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 net.kyori.adventure.text.TextComponent; import org.bukkit.Statistic; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class StatCommand implements CommandExecutor { private static ThreadManager threadManager; private static OutputManager outputManager; + private OfflinePlayerHandler offlinePlayerHandler; + private EnumHandler enumHandler; public StatCommand(OutputManager m, ThreadManager t) { threadManager = t; @@ -44,6 +56,88 @@ public class StatCommand implements CommandExecutor { return true; } + private final class ArgProcessor { + + private String[] argsToProcess; + private Statistic statistic; + private String subStatistic; + + private ArgProcessor(CommandSender sender, String[] args) { + argsToProcess = args; + process(sender); + } + + private StatRequest process(CommandSender sender) { + Pattern pattern = Pattern.compile("top|server|me|player"); + extractStatistic(); + + String playerName = tryToFindPlayerName(argsToProcess); + + for (String arg : argsToProcess) { + Matcher matcher = pattern.matcher(arg); + if (matcher.find()) { + switch (matcher.group()) { + case "player" -> { + if (playerName != null || containsPlayerTwice(argsToProcess)) { + new PlayerStatRequest(playerName); + } + } + } + } + } + } + + private void extractStatistic() { + String statName = null; + for (String arg : argsToProcess) { + if (enumHandler.isStatistic(arg)) { + statName = arg; + break; + } + } + if (statName != null) { + statistic = EnumHandler.getStatEnum(statName); + argsToProcess = removeArg(argsToProcess, statName); + } + } + + private void extractSubStatistic() { + if (statistic == null || + statistic.getType() == Statistic.Type.UNTYPED || + argsToProcess.length == 0) { + return; + } + + for (String arg : argsToProcess) { + + } + } + + @Contract(pure = true) + private @Nullable String tryToFindPlayerName(@NotNull String[] args) { + for (String arg : args) { + if (offlinePlayerHandler.isRelevantPlayer(arg)) { + return arg; + } + } + return null; + } + + private boolean containsPlayerTwice(String[] args) { + return Arrays.stream(args) + .filter(arg -> arg.equalsIgnoreCase("player")) + .toList() + .size() >= 2; + } + + private String[] removeArg(@NotNull String[] args, String argToRemove) { + ArrayList currentArgs = new ArrayList<>(Arrays.asList(args)); + currentArgs.remove(argToRemove); + return currentArgs.toArray(String[]::new); + } + + } + /** * If a given {@link StatRequest} object does not result in a valid * statistic look-up, this will send a feedback message to the CommandSender diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/InternalStatRequest.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/InternalStatRequest.java index bc7485e..9dfeb09 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/InternalStatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/InternalStatRequest.java @@ -3,6 +3,7 @@ package com.artemis.the.gr8.playerstats.statistic; import com.artemis.the.gr8.playerstats.Main; import com.artemis.the.gr8.playerstats.config.ConfigHandler; import com.artemis.the.gr8.playerstats.utils.EnumHandler; +import com.artemis.the.gr8.playerstats.utils.MyLogger; import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; import net.kyori.adventure.text.TextComponent; import org.bukkit.Material; @@ -42,7 +43,11 @@ public final class InternalStatRequest extends StatRequest { } private void processArgs(CommandSender sender, String[] args) { + MyLogger.logWarning("processArgs: " + Arrays.toString(args)); + String[] argsMinusTarget = extractAndStoreTarget(sender, args); + MyLogger.logWarning("processArgs minus target: " + Arrays.toString(argsMinusTarget)); + findStatAndSubStat(argsMinusTarget); } @@ -59,6 +64,7 @@ public final class InternalStatRequest extends StatRequest { } else { super.getSettings().configureForPlayer(playerName); + String[] extractedPlayerName = removeArg(leftoverArgs, playerName); return removeArg(extractedPlayerName, arg); } @@ -87,8 +93,10 @@ public final class InternalStatRequest extends StatRequest { } private void findStatAndSubStat(@NotNull String[] leftoverArgs) { + MyLogger.logWarning("findStatAndSubStat: " + Arrays.toString(leftoverArgs)); for (String arg : leftoverArgs) { if (enumHandler.isStatistic(arg)) { + MyLogger.logWarning("statistic found: " + arg); Statistic stat = EnumHandler.getStatEnum(arg); String[] argsWithoutStat = removeArg(leftoverArgs, arg); findAndStoreSubStat(argsWithoutStat, stat); @@ -97,7 +105,12 @@ public final class InternalStatRequest extends StatRequest { } private void findAndStoreSubStat(String[] leftoverArgs, Statistic statistic) { - if (statistic == null || leftoverArgs.length == 0) { + MyLogger.logWarning("findAndStoreSubStat: " + Arrays.toString(leftoverArgs)); + if (statistic == null) { + return; + } + else if (leftoverArgs.length == 0) { + super.getSettings().configureUntyped(statistic); return; } diff --git a/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatRequest.java b/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatRequest.java index 7af52d2..f7fe80e 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatRequest.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/statistic/StatRequest.java @@ -109,10 +109,10 @@ public abstract class StatRequest { } void configureUntyped(@NotNull Statistic statistic) { - if (statistic.getType() == Statistic.Type.UNTYPED) { - this.statistic = statistic; + if (statistic.getType() != Statistic.Type.UNTYPED) { + throw new IllegalArgumentException("This statistic is not of Type.Untyped"); } - throw new IllegalArgumentException("This statistic is not of Type.Untyped"); + this.statistic = statistic; } void configureBlockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException { @@ -131,12 +131,12 @@ public abstract class StatRequest { } 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(); + if (statistic.getType() != Statistic.Type.ENTITY) { + throw new IllegalArgumentException("This statistic is not of Type.Entity"); } - throw new IllegalArgumentException("This statistic is not of Type.Entity"); + this.statistic = statistic; + this.entity = entityType; + this.subStatEntryName = entityType.toString(); } public @NotNull CommandSender getCommandSender() {