Extend spawn command (#1777)

* add missing EntityTypes

* small command refactorings and improvements
* move common command patterns and methods to CommandHelpers
* let the spawn command detect the entityType instead of spawning every entity as EntityVehicle
* add extra options for spawning gadgets for better debuging and testing

* More spawn command additions and cleanups+EntityVehicle changes
* Moved remaining patterns from GiveCommand and ClearCommand to CommandHelpers
* Added patterns for hp, maxhp, atk, def and (monster)ai for the spawn command
* Moved intParam parsing via regex to the CommandHelpers
* Read most of EntityVehicle stats from the ConfigGadget instead of hardcoding them

Co-authored-by: hartie95 <mail@hartie95.de>
This commit is contained in:
Alexander Hartmann
2022-09-16 19:04:20 +02:00
committed by GitHub
parent 9671a76af2
commit 08f361954a
9 changed files with 298 additions and 169 deletions

View File

@@ -0,0 +1,52 @@
package emu.grasscutter.command;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CommandHelpers {
public static final Pattern lvlRegex = Pattern.compile("l(?:vl?)?(\\d+)"); // Java doesn't have raw string literals :(
public static final Pattern amountRegex = Pattern.compile("((?<=x)\\d+|\\d+(?=x)(?!x\\d))");
public static final Pattern refineRegex = Pattern.compile("r(\\d+)");
public static final Pattern rankRegex = Pattern.compile("(\\d+)\\*");
public static final Pattern constellationRegex = Pattern.compile("c(\\d+)");
public static final Pattern stateRegex = Pattern.compile("state(\\d+)");
public static final Pattern blockRegex = Pattern.compile("blk(\\d+)");
public static final Pattern groupRegex = Pattern.compile("grp(\\d+)");
public static final Pattern configRegex = Pattern.compile("cfg(\\d+)");
public static final Pattern hpRegex = Pattern.compile("hp(\\d+)");
public static final Pattern maxHPRegex = Pattern.compile("maxhp(\\d+)");
public static final Pattern atkRegex = Pattern.compile("atk(\\d+)");
public static final Pattern defRegex = Pattern.compile("def(\\d+)");
public static final Pattern aiRegex = Pattern.compile("ai(\\d+)");
public static int matchIntOrNeg(Pattern pattern, String arg) {
Matcher match = pattern.matcher(arg);
if (match.find()) {
return Integer.parseInt(match.group(1)); // This should be exception-safe as only \d+ can be passed to it (i.e. non-empty string of pure digits)
}
return -1;
}
public static <T> List<String> parseIntParameters(List<String> args, @Nonnull T params, Map<Pattern, BiConsumer<T, Integer>> map) {
for (int i = args.size() - 1; i >= 0; i--) {
String arg = args.get(i).toLowerCase();
boolean deleteArg = false;
int argNum;
for (var entry : map.entrySet()) {
if ((argNum = matchIntOrNeg(entry.getKey(), arg)) != -1) {
entry.getValue().accept(params, argNum);
deleteArg = true;
break;
}
}
if (deleteArg) {
args.remove(i);
}
}
return args;
}
}