Add SWCommand.Register.Registeres #104
@ -30,12 +30,13 @@ import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.IntPredicate;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class SWCommand {
|
||||
|
||||
private final Command command;
|
||||
private final List<SubCommand> commandSet = new ArrayList<>();
|
||||
private final List<SubCommand> commandHelpSet = new ArrayList<>();
|
||||
private final List<SubCommand> commandList = new ArrayList<>();
|
||||
private final List<SubCommand> commandHelpList = new ArrayList<>();
|
||||
private final Map<String, TypeMapper<?>> localTypeMapper = new HashMap<>();
|
||||
|
||||
protected SWCommand(String command) {
|
||||
@ -46,15 +47,13 @@ public abstract class SWCommand {
|
||||
this.command = new Command(command, "", "/" + command, Arrays.asList(aliases)) {
|
||||
@Override
|
||||
public boolean execute(CommandSender sender, String alias, String[] args) {
|
||||
for (SubCommand subCommand : commandSet) {
|
||||
if (subCommand.invoke(sender, args)) {
|
||||
return false;
|
||||
}
|
||||
// if (commandList.stream().filter(s -> s.invoke(sender, args)).findFirst().isEmpty()) return false;
|
||||
// if (commandHelpList.stream().filter(s -> s.invoke(sender, args)).findFirst().isEmpty()) return false;
|
||||
for (SubCommand subCommand : commandList) {
|
||||
if (subCommand.invoke(sender, args)) return false;
|
||||
}
|
||||
for (SubCommand subCommand : commandHelpSet) {
|
||||
if (subCommand.invoke(sender, args)) {
|
||||
return false;
|
||||
}
|
||||
for (SubCommand subCommand : commandHelpList) {
|
||||
if (subCommand.invoke(sender, args)) return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -62,19 +61,15 @@ public abstract class SWCommand {
|
||||
@Override
|
||||
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
|
||||
List<String> strings = new ArrayList<>();
|
||||
for (SubCommand subCommand : commandSet) {
|
||||
for (SubCommand subCommand : commandList) {
|
||||
List<String> tabCompletes = subCommand.tabComplete(sender, args);
|
||||
if (tabCompletes != null) {
|
||||
strings.addAll(tabCompletes);
|
||||
}
|
||||
if (tabCompletes != null) strings.addAll(tabCompletes);
|
||||
}
|
||||
strings = new ArrayList<>(strings);
|
||||
for (int i = strings.size() - 1; i >= 0; i--) {
|
||||
if (!strings.get(i).toLowerCase().startsWith(args[args.length - 1].toLowerCase())) {
|
||||
strings.remove(i);
|
||||
}
|
||||
}
|
||||
return strings;
|
||||
return strings.stream()
|
||||
.filter(s -> !s.isEmpty())
|
||||
.filter(s -> !s.isBlank())
|
||||
.filter(s -> s.toLowerCase().startsWith(args[args.length - 1].toLowerCase()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
};
|
||||
unregister();
|
||||
@ -83,23 +78,13 @@ public abstract class SWCommand {
|
||||
Method[] methods = getClass().getDeclaredMethods();
|
||||
for (Method method : methods) {
|
||||
addMapper(Mapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> {
|
||||
if (anno.local()) {
|
||||
localTypeMapper.put(anno.value(), typeMapper);
|
||||
} else {
|
||||
SWCommandUtils.addMapper(anno.value(), typeMapper);
|
||||
}
|
||||
(anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value(), typeMapper);
|
||||
});
|
||||
addMapper(ClassMapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> {
|
||||
if (anno.local()) {
|
||||
localTypeMapper.put(anno.value().getTypeName(), typeMapper);
|
||||
} else {
|
||||
SWCommandUtils.addMapper(anno.value().getTypeName(), typeMapper);
|
||||
}
|
||||
(anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value().getTypeName(), typeMapper);
|
||||
});
|
||||
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
|
||||
if (!anno.help()) {
|
||||
return;
|
||||
}
|
||||
if (!anno.help()) return;
|
||||
if (parameters.length != 2) {
|
||||
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking parameters or has too many");
|
||||
}
|
||||
@ -110,14 +95,12 @@ 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");
|
||||
return;
|
||||
}
|
||||
commandHelpSet.add(new SubCommand(this, method, anno.value(), new HashMap<>()));
|
||||
commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>()));
|
||||
});
|
||||
}
|
||||
for (Method method : methods) {
|
||||
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
|
||||
if (anno.help()) {
|
||||
return;
|
||||
}
|
||||
if (anno.help()) return;
|
||||
for (int i = 1; i < parameters.length; i++) {
|
||||
Parameter parameter = parameters[i];
|
||||
Class<?> clazz = parameter.getType();
|
||||
@ -137,10 +120,10 @@ public abstract class SWCommand {
|
||||
return;
|
||||
}
|
||||
}
|
||||
commandSet.add(new SubCommand(this, method, anno.value(), localTypeMapper));
|
||||
commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper));
|
||||
});
|
||||
|
||||
this.commandSet.sort((o1, o2) -> {
|
||||
this.commandList.sort((o1, o2) -> {
|
||||
int compare = Integer.compare(-o1.subCommand.length, -o2.subCommand.length);
|
||||
if (compare != 0) {
|
||||
return compare;
|
||||
@ -150,15 +133,13 @@ public abstract class SWCommand {
|
||||
return Integer.compare(i1, i2);
|
||||
}
|
||||
});
|
||||
commandHelpSet.sort(Comparator.comparingInt(o -> -o.subCommand.length));
|
||||
commandHelpList.sort(Comparator.comparingInt(o -> -o.subCommand.length));
|
||||
}
|
||||
}
|
||||
|
||||
private <T extends Annotation> void add(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, Parameter[]> consumer) {
|
||||
T[] anno = SWCommandUtils.getAnnotation(method, annotation);
|
||||
if (anno == null || anno.length == 0) {
|
||||
return;
|
||||
}
|
||||
if (anno == null || anno.length == 0) return;
|
||||
|
||||
Parameter[] parameters = method.getParameters();
|
||||
if (!parameterTester.test(parameters.length)) {
|
||||
@ -173,17 +154,14 @@ public abstract class SWCommand {
|
||||
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'");
|
||||
return;
|
||||
}
|
||||
for (T a : anno) {
|
||||
consumer.accept(a, parameters);
|
||||
}
|
||||
Arrays.stream(anno).forEach(t -> consumer.accept(t, parameters));
|
||||
}
|
||||
|
||||
private <T extends Annotation> void addMapper(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, TypeMapper<?>> consumer) {
|
||||
add(annotation, method, parameterTester, firstParameter, returnType, (anno, parameters) -> {
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
Object object = method.invoke(this);
|
||||
consumer.accept(anno, (TypeMapper<?>) object);
|
||||
consumer.accept(anno, (TypeMapper<?>) method.invoke(this));
|
||||
} catch (Exception e) {
|
||||
throw new SecurityException(e.getMessage(), e);
|
||||
}
|
||||
@ -192,9 +170,7 @@ public abstract class SWCommand {
|
||||
|
||||
public void unregister() {
|
||||
YoyoNow markierte diese Unterhaltung als gelöst
Veraltet
|
||||
SWCommandUtils.knownCommandMap.remove(command.getName());
|
||||
for (String alias : command.getAliases()) {
|
||||
SWCommandUtils.knownCommandMap.remove(alias);
|
||||
}
|
||||
command.getAliases().forEach(SWCommandUtils.knownCommandMap::remove);
|
||||
command.unregister(SWCommandUtils.commandMap);
|
||||
}
|
||||
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren
Das wirkt mir nach einem sehr dreckigen workaround, geht das nicht schöner?
Dies ist eigentlich kein so dreckiger workdaround, dies ist dafür da, dass man die Befehle erst nachdem alle Plugins initialisiert sind geladen werden. Dies ist besonders interessant bei so sachen, wenn man WE Command oder so überschreibt, wenn diese später als das hier erstellt werden. Ansonsten hat Zeanon die Änderungen getestet und keine Fehler gefunden. Ich glaube sogar das es etwas besser läuft, was ich aber noch nicht nachgewiesen habe.
Das ist nicht nötig, dann soll man das zumindest als soft-dependend markieren, dann können die Befehle auch sofort überschrieben werden.
Wenn du meinst. Dann schmeiße ich es dir noch raus.