SteamWar/SpigotCore
Archiviert
13
0

Optimize SWCommandUtils.numberCompleter

Dieser Commit ist enthalten in:
yoyosource 2021-05-05 10:25:44 +02:00
Ursprung 218641b21d
Commit a1125ae48f

Datei anzeigen

@ -50,10 +50,7 @@ public class SWCommandUtils {
static final BiFunction<Class<Enum<?>>, String, Enum<?>> ENUM_MAPPER = (enumClass, s) -> { static final BiFunction<Class<Enum<?>>, String, Enum<?>> ENUM_MAPPER = (enumClass, s) -> {
Enum<?>[] enums = enumClass.getEnumConstants(); Enum<?>[] enums = enumClass.getEnumConstants();
for (Enum<?> e : enums) { return Arrays.stream(enums).filter(e -> e.name().equalsIgnoreCase(s)).findFirst().orElse(null);
if (e.name().equalsIgnoreCase(s)) return e;
}
return null;
}; };
static { static {
@ -105,9 +102,7 @@ public class SWCommandUtils {
Object[] arguments = new Object[parameters.length + 1]; Object[] arguments = new Object[parameters.length + 1];
int index = 0; int index = 0;
while (index < subCommand.length) { while (index < subCommand.length) {
if (!args[index].equalsIgnoreCase(subCommand[index])) { if (!args[index].equalsIgnoreCase(subCommand[index])) throw new CommandParseException();
throw new CommandParseException();
}
index++; index++;
} }
@ -116,17 +111,13 @@ public class SWCommandUtils {
length = args.length - parameters.length - subCommand.length + 1; length = args.length - parameters.length - subCommand.length + 1;
arguments[arguments.length - 1] = Array.newInstance(varArgType, length); arguments[arguments.length - 1] = Array.newInstance(varArgType, length);
if (index > args.length - 1) { if (index > args.length - 1) return arguments;
return arguments;
}
} }
for (int i = 0; i < parameters.length - (varArgType != null ? 1 : 0); i++) { for (int i = 0; i < parameters.length - (varArgType != null ? 1 : 0); i++) {
arguments[i + 1] = parameters[i].map(commandSender, Arrays.copyOf(args, index), args[index]); arguments[i + 1] = parameters[i].map(commandSender, Arrays.copyOf(args, index), args[index]);
index++; index++;
if (arguments[i + 1] == null) { if (arguments[i + 1] == null) throw new CommandParseException();
throw new CommandParseException();
}
} }
if (varArgType != null) { if (varArgType != null) {
@ -134,9 +125,7 @@ public class SWCommandUtils {
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
Object value = parameters[parameters.length - 1].map(commandSender, Arrays.copyOf(args, index), args[index]); Object value = parameters[parameters.length - 1].map(commandSender, Arrays.copyOf(args, index), args[index]);
if (value == null) { if (value == null) throw new CommandParseException();
throw new CommandParseException();
}
Array.set(varArgument, i, value); Array.set(varArgument, i, value);
index++; index++;
} }
@ -149,8 +138,7 @@ public class SWCommandUtils {
} }
public static void addMapper(String name, TypeMapper<?> mapper) { public static void addMapper(String name, TypeMapper<?> mapper) {
if (MAPPER_FUNCTIONS.containsKey(name)) return; MAPPER_FUNCTIONS.putIfAbsent(name, mapper);
MAPPER_FUNCTIONS.put(name, mapper);
} }
public static <T> TypeMapper<T> createMapper(Function<String, T> mapper, Function<String, List<String>> tabCompleter) { public static <T> TypeMapper<T> createMapper(Function<String, T> mapper, Function<String, List<String>> tabCompleter) {