SteamWar/SpigotCore
Archiviert
13
0

Simplify SubCommand to conform to TypeMapper.tabComplete

Add messages to why it did not work
Dieser Commit ist enthalten in:
yoyosource 2021-03-30 08:55:18 +02:00
Ursprung 1d1d95b78f
Commit 35758f1000
2 geänderte Dateien mit 20 neuen und 10 gelöschten Zeilen

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.command;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@ -28,6 +29,7 @@ import java.lang.reflect.Parameter;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.IntPredicate;
import java.util.logging.Level;
public abstract class SWCommand {
@ -94,9 +96,11 @@ public abstract class SWCommand {
return;
}
if (!parameters[parameters.length - 1].isVarArgs()) {
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument");
return;
}
if (parameters[parameters.length - 1].getType().getComponentType() != String.class) {
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument");
return;
}
commandHelpSet.add(new SubCommand(this, method, anno.value()));
@ -121,7 +125,10 @@ public abstract class SWCommand {
if (mapper != null) {
name = mapper.value();
}
if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) return;
if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) {
Bukkit.getLogger().log(Level.WARNING, "The parameter '" + parameter.toString() + "' is using an unsupported Mapper of type '" + name + "'");
return;
}
}
commandSet.add(new SubCommand(this, method, anno.value()));
});
@ -136,12 +143,15 @@ public abstract class SWCommand {
Parameter[] parameters = method.getParameters();
if (!parameterTester.test(parameters.length)) {
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking parameters or has too many");
return;
}
if (firstParameter && !CommandSender.class.isAssignableFrom(parameters[0].getType())) {
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the first parameter of type '" + CommandSender.class.getTypeName() + "'");
return;
}
if (returnType != null && method.getReturnType() != returnType) {
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'");
return;
}
consumer.accept(anno, parameters);

Datei anzeigen

@ -94,9 +94,9 @@ class SubCommand {
objects[0] = commandSenderFunction.apply(commandSender);
method.setAccessible(true);
method.invoke(swCommand, objects);
} catch (IllegalArgumentException | IllegalAccessException e) {
} catch (IllegalAccessException | RuntimeException e) {
throw new SecurityException(e.getMessage(), e);
} catch (InvocationTargetException | RuntimeException e) {
} catch (InvocationTargetException e) {
Bukkit.getLogger().log(Level.INFO, e.getMessage(), e);
return false;
}
@ -105,23 +105,23 @@ class SubCommand {
List<String> tabComplete(CommandSender commandSender, String[] args) {
if (!varArgs && args.length < arguments.length - 1) {
return Collections.emptyList();
return null;
}
List<String> argsList = new LinkedList<>(Arrays.asList(args));
for (String value : subCommand) {
String s = argsList.remove(0);
if (argsList.isEmpty()) return Collections.singletonList(value);
if (!value.equals(s)) return Collections.emptyList();
if (!value.equals(s)) return null;
}
for (TypeMapper<?> argument : arguments) {
String s = argsList.remove(0);
if (argsList.isEmpty()) return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s);
try {
if (argument.map(s) == null) {
return Collections.emptyList();
return null;
}
} catch (Exception e) {
return Collections.emptyList();
return null;
}
}
if (varArgs && !argsList.isEmpty()) {
@ -130,13 +130,13 @@ class SubCommand {
if (argsList.isEmpty()) return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s);
try {
if (arguments[arguments.length - 1].map(s) == null) {
return Collections.emptyList();
return null;
}
} catch (Exception e) {
return Collections.emptyList();
return null;
}
}
}
return Collections.emptyList();
return null;
}
}