mirror of
https://github.com/itHotL/PlayerStats.git
synced 2025-01-08 19:37:58 +01:00
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:
parent
d16e6db036
commit
b1c015e156
@ -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.enums.Target;
|
||||||
import com.artemis.the.gr8.playerstats.msg.OutputManager;
|
import com.artemis.the.gr8.playerstats.msg.OutputManager;
|
||||||
import com.artemis.the.gr8.playerstats.statistic.InternalStatRequest;
|
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.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 net.kyori.adventure.text.TextComponent;
|
||||||
import org.bukkit.Statistic;
|
import org.bukkit.Statistic;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.jetbrains.annotations.Contract;
|
||||||
import org.jetbrains.annotations.NotNull;
|
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 {
|
public class StatCommand implements CommandExecutor {
|
||||||
|
|
||||||
private static ThreadManager threadManager;
|
private static ThreadManager threadManager;
|
||||||
private static OutputManager outputManager;
|
private static OutputManager outputManager;
|
||||||
|
private OfflinePlayerHandler offlinePlayerHandler;
|
||||||
|
private EnumHandler enumHandler;
|
||||||
|
|
||||||
public StatCommand(OutputManager m, ThreadManager t) {
|
public StatCommand(OutputManager m, ThreadManager t) {
|
||||||
threadManager = t;
|
threadManager = t;
|
||||||
@ -44,6 +56,88 @@ public class StatCommand implements CommandExecutor {
|
|||||||
return true;
|
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
|
* If a given {@link StatRequest} object does not result in a valid
|
||||||
* statistic look-up, this will send a feedback message to the CommandSender
|
* statistic look-up, this will send a feedback message to the CommandSender
|
||||||
|
@ -3,6 +3,7 @@ package com.artemis.the.gr8.playerstats.statistic;
|
|||||||
import com.artemis.the.gr8.playerstats.Main;
|
import com.artemis.the.gr8.playerstats.Main;
|
||||||
import com.artemis.the.gr8.playerstats.config.ConfigHandler;
|
import com.artemis.the.gr8.playerstats.config.ConfigHandler;
|
||||||
import com.artemis.the.gr8.playerstats.utils.EnumHandler;
|
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 com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
|
||||||
import net.kyori.adventure.text.TextComponent;
|
import net.kyori.adventure.text.TextComponent;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
@ -42,7 +43,11 @@ public final class InternalStatRequest extends StatRequest<TextComponent> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void processArgs(CommandSender sender, String[] args) {
|
private void processArgs(CommandSender sender, String[] args) {
|
||||||
|
MyLogger.logWarning("processArgs: " + Arrays.toString(args));
|
||||||
|
|
||||||
String[] argsMinusTarget = extractAndStoreTarget(sender, args);
|
String[] argsMinusTarget = extractAndStoreTarget(sender, args);
|
||||||
|
MyLogger.logWarning("processArgs minus target: " + Arrays.toString(argsMinusTarget));
|
||||||
|
|
||||||
findStatAndSubStat(argsMinusTarget);
|
findStatAndSubStat(argsMinusTarget);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,6 +64,7 @@ public final class InternalStatRequest extends StatRequest<TextComponent> {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
super.getSettings().configureForPlayer(playerName);
|
super.getSettings().configureForPlayer(playerName);
|
||||||
|
|
||||||
String[] extractedPlayerName = removeArg(leftoverArgs, playerName);
|
String[] extractedPlayerName = removeArg(leftoverArgs, playerName);
|
||||||
return removeArg(extractedPlayerName, arg);
|
return removeArg(extractedPlayerName, arg);
|
||||||
}
|
}
|
||||||
@ -87,8 +93,10 @@ public final class InternalStatRequest extends StatRequest<TextComponent> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void findStatAndSubStat(@NotNull String[] leftoverArgs) {
|
private void findStatAndSubStat(@NotNull String[] leftoverArgs) {
|
||||||
|
MyLogger.logWarning("findStatAndSubStat: " + Arrays.toString(leftoverArgs));
|
||||||
for (String arg : leftoverArgs) {
|
for (String arg : leftoverArgs) {
|
||||||
if (enumHandler.isStatistic(arg)) {
|
if (enumHandler.isStatistic(arg)) {
|
||||||
|
MyLogger.logWarning("statistic found: " + arg);
|
||||||
Statistic stat = EnumHandler.getStatEnum(arg);
|
Statistic stat = EnumHandler.getStatEnum(arg);
|
||||||
String[] argsWithoutStat = removeArg(leftoverArgs, arg);
|
String[] argsWithoutStat = removeArg(leftoverArgs, arg);
|
||||||
findAndStoreSubStat(argsWithoutStat, stat);
|
findAndStoreSubStat(argsWithoutStat, stat);
|
||||||
@ -97,7 +105,12 @@ public final class InternalStatRequest extends StatRequest<TextComponent> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void findAndStoreSubStat(String[] leftoverArgs, Statistic statistic) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,11 +109,11 @@ public abstract class StatRequest<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void configureUntyped(@NotNull Statistic statistic) {
|
void configureUntyped(@NotNull Statistic statistic) {
|
||||||
if (statistic.getType() == Statistic.Type.UNTYPED) {
|
if (statistic.getType() != Statistic.Type.UNTYPED) {
|
||||||
this.statistic = statistic;
|
|
||||||
}
|
|
||||||
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 {
|
void configureBlockOrItemType(@NotNull Statistic statistic, @NotNull Material material) throws IllegalArgumentException {
|
||||||
Statistic.Type type = statistic.getType();
|
Statistic.Type type = statistic.getType();
|
||||||
@ -131,13 +131,13 @@ public abstract class StatRequest<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void configureEntityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException {
|
void configureEntityType(@NotNull Statistic statistic, @NotNull EntityType entityType) throws IllegalArgumentException {
|
||||||
if (statistic.getType() == Statistic.Type.ENTITY) {
|
if (statistic.getType() != Statistic.Type.ENTITY) {
|
||||||
|
throw new IllegalArgumentException("This statistic is not of Type.Entity");
|
||||||
|
}
|
||||||
this.statistic = statistic;
|
this.statistic = statistic;
|
||||||
this.entity = entityType;
|
this.entity = entityType;
|
||||||
this.subStatEntryName = entityType.toString();
|
this.subStatEntryName = entityType.toString();
|
||||||
}
|
}
|
||||||
throw new IllegalArgumentException("This statistic is not of Type.Entity");
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NotNull CommandSender getCommandSender() {
|
public @NotNull CommandSender getCommandSender() {
|
||||||
return sender;
|
return sender;
|
||||||
|
Loading…
Reference in New Issue
Block a user