SteamWar/SpigotCore
Archiviert
13
0

Simplify SubCommand and SWCommand

Dieser Commit ist enthalten in:
yoyosource 2021-05-05 08:45:15 +02:00
Ursprung 84ce642bc6
Commit d48175234a
2 geänderte Dateien mit 12 neuen und 14 gelöschten Zeilen

Datei anzeigen

@ -109,7 +109,7 @@ public abstract class SWCommand {
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument");
return; return;
} }
commandHelpSet.add(new SubCommand(this, method, anno.value())); commandHelpSet.add(new SubCommand(this, method, anno.value(), new HashMap<>()));
}); });
} }
for (Method method : getClass().getDeclaredMethods()) { for (Method method : getClass().getDeclaredMethods()) {

Datei anzeigen

@ -28,6 +28,8 @@ import java.util.*;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import static de.steamwar.command.SWCommandUtils.*;
class SubCommand { class SubCommand {
private SWCommand swCommand; private SWCommand swCommand;
@ -38,10 +40,6 @@ class SubCommand {
private Function<CommandSender, ?> commandSenderFunction; private Function<CommandSender, ?> commandSenderFunction;
Class<?> varArgType = null; Class<?> varArgType = null;
public SubCommand(SWCommand swCommand, Method method, String[] subCommand) {
this(swCommand, method, subCommand, new HashMap<>());
}
public SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map<String, TypeMapper<?>> localTypeMapper) { public SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map<String, TypeMapper<?>> localTypeMapper) {
this.swCommand = swCommand; this.swCommand = swCommand;
this.method = method; this.method = method;
@ -61,13 +59,13 @@ class SubCommand {
} }
SWCommand.Mapper mapper = parameter.getAnnotation(SWCommand.Mapper.class); SWCommand.Mapper mapper = parameter.getAnnotation(SWCommand.Mapper.class);
if (clazz.isEnum() && mapper == null && !SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz.getTypeName()) && !localTypeMapper.containsKey(clazz.getTypeName())) { if (clazz.isEnum() && mapper == null && !MAPPER_FUNCTIONS.containsKey(clazz.getTypeName()) && !localTypeMapper.containsKey(clazz.getTypeName())) {
Class<Enum<?>> enumClass = (Class<Enum<?>>) clazz; Class<Enum<?>> enumClass = (Class<Enum<?>>) clazz;
List<String> tabCompletes = new ArrayList<>(); List<String> tabCompletes = new ArrayList<>();
for (Enum<?> enumConstant : enumClass.getEnumConstants()) { for (Enum<?> enumConstant : enumClass.getEnumConstants()) {
tabCompletes.add(enumConstant.name().toLowerCase()); tabCompletes.add(enumConstant.name().toLowerCase());
} }
arguments[i - 1] = SWCommandUtils.createMapper(s -> SWCommandUtils.ENUM_MAPPER.apply(enumClass, s), s -> tabCompletes); arguments[i - 1] = SWCommandUtils.createMapper(s -> ENUM_MAPPER.apply(enumClass, s), s -> tabCompletes);
continue; continue;
} }
@ -75,11 +73,9 @@ class SubCommand {
if (mapper != null) { if (mapper != null) {
name = mapper.value(); name = mapper.value();
} }
if (localTypeMapper.containsKey(name)) { arguments[i - 1] = localTypeMapper.containsKey(name)
arguments[i - 1] = localTypeMapper.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION); ? localTypeMapper.get(name)
} else { : MAPPER_FUNCTIONS.getOrDefault(name, ERROR_FUNCTION);
arguments[i - 1] = SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION);
}
} }
} }
@ -120,7 +116,8 @@ class SubCommand {
} }
for (TypeMapper<?> argument : arguments) { for (TypeMapper<?> argument : arguments) {
String s = argsList.remove(0); String s = argsList.remove(0);
if (argsList.isEmpty()) return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); if (argsList.isEmpty())
return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s);
try { try {
if (argument.map(commandSender, Arrays.copyOf(args, index), s) == null) { if (argument.map(commandSender, Arrays.copyOf(args, index), s) == null) {
return null; return null;
@ -133,7 +130,8 @@ class SubCommand {
if (varArgType != null && !argsList.isEmpty()) { if (varArgType != null && !argsList.isEmpty()) {
while (!argsList.isEmpty()) { while (!argsList.isEmpty()) {
String s = argsList.remove(0); String s = argsList.remove(0);
if (argsList.isEmpty()) return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); if (argsList.isEmpty())
return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s);
try { try {
if (arguments[arguments.length - 1].map(commandSender, Arrays.copyOf(args, index), s) == null) { if (arguments[arguments.length - 1].map(commandSender, Arrays.copyOf(args, index), s) == null) {
return null; return null;