SteamWar/SpigotCore
Archiviert
13
0

Streamify SWCommand

Dieser Commit ist enthalten in:
yoyosource 2021-05-05 09:21:05 +02:00
Ursprung 96b7652324
Commit a42188f7a0

Datei anzeigen

@ -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() {
SWCommandUtils.knownCommandMap.remove(command.getName());
for (String alias : command.getAliases()) {
SWCommandUtils.knownCommandMap.remove(alias);
}
command.getAliases().forEach(SWCommandUtils.knownCommandMap::remove);
command.unregister(SWCommandUtils.commandMap);
}