SteamWar/SpigotCore
Archiviert
13
0

Simplify SWCommand

Dieser Commit ist enthalten in:
yoyosource 2021-05-14 21:19:30 +02:00
Ursprung 78985f15de
Commit 96630e13d2

Datei anzeigen

@ -79,16 +79,17 @@ public abstract class SWCommand {
addMapper(ClassMapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> {
(anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value().getTypeName(), typeMapper);
});
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
add(Register.class, method, i -> i != 2, true, null, (anno, parameters) -> {
if (!anno.help()) return;
if (parameters.length != 2) {
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking parameters or has too many");
}
List<String> errors = new ArrayList<>();
if (!parameters[parameters.length - 1].isVarArgs()) {
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument");
errors.add("The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument");
}
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");
errors.add("The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument");
}
if (!errors.isEmpty()) {
errors.forEach(s -> Bukkit.getLogger().log(Level.WARNING, s));
return;
}
commandHelpList.add(new SubCommand(this, method, anno.description(), anno.value(), new HashMap<>()));
@ -130,8 +131,8 @@ public abstract class SWCommand {
if (compare != 0) {
return compare;
} else {
return Integer.compare(o1.method.getDeclaringClass() == SWCommand.class ? 0 : 1,
o2.method.getDeclaringClass() == SWCommand.class ? 0 : 1);
return Integer.compare(o1.method.getDeclaringClass() == SWCommand.class ? 1 : 0,
o2.method.getDeclaringClass() == SWCommand.class ? 1 : 0);
}
});
}
@ -142,16 +143,18 @@ public abstract class SWCommand {
if (anno == null || anno.length == 0) return;
Parameter[] parameters = method.getParameters();
List<String> errors = new ArrayList<>();
if (!parameterTester.test(parameters.length)) {
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking parameters or has too many");
return;
errors.add("The method '" + method.toString() + "' is lacking parameters or has too many");
}
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;
errors.add("The method '" + method.toString() + "' is lacking the first parameter of type '" + CommandSender.class.getTypeName() + "'");
}
if (returnType != null && method.getReturnType() != returnType) {
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'");
errors.add("The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'");
}
if (!errors.isEmpty()) {
errors.forEach(s -> Bukkit.getLogger().log(Level.WARNING, s));
return;
}
Arrays.stream(anno).forEach(t -> consumer.accept(t, parameters));