SteamWar/SpigotCore
Archiviert
13
0

Optimize SWCommand

Optimize SubCommand
Dieser Commit ist enthalten in:
yoyosource 2021-03-26 09:06:21 +01:00
Ursprung 5bd160b0c9
Commit ccb63e1613
2 geänderte Dateien mit 12 neuen und 27 gelöschten Zeilen

Datei anzeigen

@ -76,7 +76,6 @@ public abstract class SWCommand {
}
});
Set<Method> commandMethods = new HashSet<>();
for (Method method : getClass().getDeclaredMethods()) {
add(Mapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, parameters) -> {
try {
@ -87,6 +86,11 @@ public abstract class SWCommand {
throw new SecurityException(e.getMessage(), e);
}
});
add(RegisterHelp.class, method, i -> i != 1, true, null, (anno, parameters) -> {
commandHelpSet.add(new SubCommand(this, method, anno.value()));
});
}
for (Method method : getClass().getDeclaredMethods()) {
add(Register.class, method, i -> i == 0, true, null, (anno, parameters) -> {
for (int i = 1; i < parameters.length; i++) {
Parameter parameter = parameters[i];
@ -104,13 +108,9 @@ public abstract class SWCommand {
}
if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) return;
}
commandMethods.add(method);
});
add(RegisterHelp.class, method, i -> i != 1, true, null, (anno, parameters) -> {
commandHelpSet.add(new SubCommand(this, method));
commandSet.add(new SubCommand(this, method, anno.value()));
});
}
commandMethods.forEach(method -> commandSet.add(new SubCommand(this, method)));
}
private <T extends Annotation> void add(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, Parameter[]> consumer) {

Datei anzeigen

@ -39,21 +39,13 @@ class SubCommand {
private boolean varArgs = false;
private Function<CommandSender, ?> commandSenderFunction;
public SubCommand(SWCommand swCommand, Method method) {
public SubCommand(SWCommand swCommand, Method method, String[] subCommand) {
this.swCommand = swCommand;
this.method = method;
Parameter[] parameters = method.getParameters();
commandSenderFunction = sender -> parameters[0].getType().cast(sender);
SWCommand.Register register = SWCommandUtils.getAnnotation(method, SWCommand.Register.class);
SWCommand.RegisterHelp registerHelp = SWCommandUtils.getAnnotation(method, SWCommand.RegisterHelp.class);
if (register != null) {
subCommand = register.value();
} else if (registerHelp != null) {
subCommand = registerHelp.value();
} else {
throw new SecurityException();
}
this.subCommand = subCommand;
arguments = new TypeMapper[parameters.length - 1];
for (int i = 1; i < parameters.length; i++) {
@ -71,17 +63,7 @@ class SubCommand {
for (Enum<?> enumConstant : enumClass.getEnumConstants()) {
tabCompletes.add(enumConstant.name().toLowerCase());
}
arguments[i] = new TypeMapper<Object>() {
@Override
public Object map(String s) {
return SWCommandUtils.ENUM_MAPPER.apply(enumClass, s);
}
@Override
public List<String> tabCompletes(String s) {
return tabCompletes;
}
};
arguments[i] = SWCommandUtils.createMapper(s -> SWCommandUtils.ENUM_MAPPER.apply(enumClass, s), s -> tabCompletes);
continue;
}
@ -112,6 +94,9 @@ class SubCommand {
}
List<String> tabComplete(CommandSender commandSender, String[] args) {
if (!varArgs && args.length < arguments.length - 1) {
return Collections.emptyList();
}
List<String> argsList = Arrays.asList(args);
for (String value : subCommand) {
String s = argsList.remove(0);