SteamWar/SpigotCore
Archiviert
13
0

Update SWCommand

Dieser Commit ist enthalten in:
yoyosource 2021-05-25 13:07:10 +02:00
Ursprung 6859d9bc10
Commit 7ea204b289

Datei anzeigen

@ -19,7 +19,6 @@
package de.steamwar.command; package de.steamwar.command;
import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -32,7 +31,6 @@ import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.IntPredicate; import java.util.function.IntPredicate;
import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public abstract class SWCommand { public abstract class SWCommand {
@ -86,19 +84,14 @@ public abstract class SWCommand {
}); });
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> { add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
if (!anno.help()) return; if (!anno.help()) return;
List<String> errors = new ArrayList<>();
if (parameters.length != 2) { if (parameters.length != 2) {
errors.add("The method '" + method.toString() + "' is lacking parameters or has too many"); throw new SecurityException("The method '" + method.toString() + "' is lacking parameters or has too many");
} }
if (!parameters[parameters.length - 1].isVarArgs()) { if (!parameters[parameters.length - 1].isVarArgs()) {
errors.add("The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument"); throw new SecurityException("The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument");
} }
if (parameters[parameters.length - 1].getType().getComponentType() != String.class) { if (parameters[parameters.length - 1].getType().getComponentType() != String.class) {
errors.add("The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); throw new SecurityException("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<>())); commandHelpList.add(new SubCommand(this, method, anno.description(), anno.value(), new HashMap<>()));
}); });
@ -118,8 +111,7 @@ public abstract class SWCommand {
} }
String name = mapper != null ? mapper.value() : clazz.getTypeName(); String name = mapper != null ? mapper.value() : clazz.getTypeName();
if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name) && !localTypeMapper.containsKey(name)) { if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name) && !localTypeMapper.containsKey(name)) {
Bukkit.getLogger().log(Level.WARNING, "The parameter '" + parameter.toString() + "' is using an unsupported Mapper of type '" + name + "'"); throw new SecurityException("The parameter '" + parameter.toString() + "' is using an unsupported Mapper of type '" + name + "'");
return;
} }
} }
commandList.add(new SubCommand(this, method, anno.description(), anno.value(), localTypeMapper)); commandList.add(new SubCommand(this, method, anno.description(), anno.value(), localTypeMapper));
@ -174,19 +166,14 @@ public abstract class SWCommand {
if (anno == null || anno.length == 0) return; if (anno == null || anno.length == 0) return;
Parameter[] parameters = method.getParameters(); Parameter[] parameters = method.getParameters();
List<String> errors = new ArrayList<>();
if (!parameterTester.test(parameters.length)) { if (!parameterTester.test(parameters.length)) {
errors.add("The method '" + method.toString() + "' is lacking parameters or has too many"); throw new SecurityException("The method '" + method.toString() + "' is lacking parameters or has too many");
} }
if (firstParameter && !CommandSender.class.isAssignableFrom(parameters[0].getType())) { if (firstParameter && !CommandSender.class.isAssignableFrom(parameters[0].getType())) {
errors.add("The method '" + method.toString() + "' is lacking the first parameter of type '" + CommandSender.class.getTypeName() + "'"); throw new SecurityException("The method '" + method.toString() + "' is lacking the first parameter of type '" + CommandSender.class.getTypeName() + "'");
} }
if (returnType != null && method.getReturnType() != returnType) { if (returnType != null && method.getReturnType() != returnType) {
errors.add("The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'"); throw new SecurityException("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)); Arrays.stream(anno).forEach(t -> consumer.accept(t, parameters));
} }