Started to move arg-processing to StatCommand after testing (feedback is no longer working with the increased StatRequest conditions of what settings are accepted)

This commit is contained in:
Artemis-the-gr8 2022-10-14 16:40:39 +02:00
parent d16e6db036
commit b1c015e156
3 changed files with 116 additions and 9 deletions

View File

@ -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<String> 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

View File

@ -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<TextComponent> {
}
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<TextComponent> {
}
else {
super.getSettings().configureForPlayer(playerName);
String[] extractedPlayerName = removeArg(leftoverArgs, playerName);
return removeArg(extractedPlayerName, arg);
}
@ -87,8 +93,10 @@ public final class InternalStatRequest extends StatRequest<TextComponent> {
}
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<TextComponent> {
}
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;
}

View File

@ -109,10 +109,10 @@ public abstract class StatRequest<T> {
}
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<T> {
}
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() {