Streamify SWCommand
Dieser Commit ist enthalten in:
Ursprung
96b7652324
Commit
a42188f7a0
@ -30,12 +30,13 @@ import java.util.*;
|
|||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.IntPredicate;
|
import java.util.function.IntPredicate;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public abstract class SWCommand {
|
public abstract class SWCommand {
|
||||||
|
|
||||||
private final Command command;
|
private final Command command;
|
||||||
private final List<SubCommand> commandSet = new ArrayList<>();
|
private final List<SubCommand> commandList = new ArrayList<>();
|
||||||
private final List<SubCommand> commandHelpSet = new ArrayList<>();
|
private final List<SubCommand> commandHelpList = new ArrayList<>();
|
||||||
private final Map<String, TypeMapper<?>> localTypeMapper = new HashMap<>();
|
private final Map<String, TypeMapper<?>> localTypeMapper = new HashMap<>();
|
||||||
|
|
||||||
protected SWCommand(String command) {
|
protected SWCommand(String command) {
|
||||||
@ -46,15 +47,13 @@ public abstract class SWCommand {
|
|||||||
this.command = new Command(command, "", "/" + command, Arrays.asList(aliases)) {
|
this.command = new Command(command, "", "/" + command, Arrays.asList(aliases)) {
|
||||||
@Override
|
@Override
|
||||||
public boolean execute(CommandSender sender, String alias, String[] args) {
|
public boolean execute(CommandSender sender, String alias, String[] args) {
|
||||||
for (SubCommand subCommand : commandSet) {
|
// if (commandList.stream().filter(s -> s.invoke(sender, args)).findFirst().isEmpty()) return false;
|
||||||
if (subCommand.invoke(sender, args)) {
|
// if (commandHelpList.stream().filter(s -> s.invoke(sender, args)).findFirst().isEmpty()) return false;
|
||||||
return false;
|
for (SubCommand subCommand : commandList) {
|
||||||
}
|
if (subCommand.invoke(sender, args)) return false;
|
||||||
}
|
}
|
||||||
for (SubCommand subCommand : commandHelpSet) {
|
for (SubCommand subCommand : commandHelpList) {
|
||||||
if (subCommand.invoke(sender, args)) {
|
if (subCommand.invoke(sender, args)) return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -62,19 +61,15 @@ public abstract class SWCommand {
|
|||||||
@Override
|
@Override
|
||||||
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
|
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
|
||||||
List<String> strings = new ArrayList<>();
|
List<String> strings = new ArrayList<>();
|
||||||
for (SubCommand subCommand : commandSet) {
|
for (SubCommand subCommand : commandList) {
|
||||||
List<String> tabCompletes = subCommand.tabComplete(sender, args);
|
List<String> tabCompletes = subCommand.tabComplete(sender, args);
|
||||||
if (tabCompletes != null) {
|
if (tabCompletes != null) strings.addAll(tabCompletes);
|
||||||
strings.addAll(tabCompletes);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
strings = new ArrayList<>(strings);
|
return strings.stream()
|
||||||
for (int i = strings.size() - 1; i >= 0; i--) {
|
.filter(s -> !s.isEmpty())
|
||||||
if (!strings.get(i).toLowerCase().startsWith(args[args.length - 1].toLowerCase())) {
|
.filter(s -> !s.isBlank())
|
||||||
strings.remove(i);
|
.filter(s -> s.toLowerCase().startsWith(args[args.length - 1].toLowerCase()))
|
||||||
}
|
.collect(Collectors.toList());
|
||||||
}
|
|
||||||
return strings;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
unregister();
|
unregister();
|
||||||
@ -83,23 +78,13 @@ public abstract class SWCommand {
|
|||||||
Method[] methods = getClass().getDeclaredMethods();
|
Method[] methods = getClass().getDeclaredMethods();
|
||||||
for (Method method : methods) {
|
for (Method method : methods) {
|
||||||
addMapper(Mapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> {
|
addMapper(Mapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> {
|
||||||
if (anno.local()) {
|
(anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value(), typeMapper);
|
||||||
localTypeMapper.put(anno.value(), typeMapper);
|
|
||||||
} else {
|
|
||||||
SWCommandUtils.addMapper(anno.value(), typeMapper);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
addMapper(ClassMapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> {
|
addMapper(ClassMapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> {
|
||||||
if (anno.local()) {
|
(anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value().getTypeName(), typeMapper);
|
||||||
localTypeMapper.put(anno.value().getTypeName(), typeMapper);
|
|
||||||
} else {
|
|
||||||
SWCommandUtils.addMapper(anno.value().getTypeName(), typeMapper);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
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()) {
|
if (!anno.help()) return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (parameters.length != 2) {
|
if (parameters.length != 2) {
|
||||||
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking parameters or has too many");
|
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");
|
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument");
|
||||||
return;
|
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) {
|
for (Method method : methods) {
|
||||||
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()) {
|
if (anno.help()) return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (int i = 1; i < parameters.length; i++) {
|
for (int i = 1; i < parameters.length; i++) {
|
||||||
Parameter parameter = parameters[i];
|
Parameter parameter = parameters[i];
|
||||||
Class<?> clazz = parameter.getType();
|
Class<?> clazz = parameter.getType();
|
||||||
@ -137,10 +120,10 @@ public abstract class SWCommand {
|
|||||||
return;
|
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);
|
int compare = Integer.compare(-o1.subCommand.length, -o2.subCommand.length);
|
||||||
if (compare != 0) {
|
if (compare != 0) {
|
||||||
return compare;
|
return compare;
|
||||||
@ -150,15 +133,13 @@ public abstract class SWCommand {
|
|||||||
return Integer.compare(i1, i2);
|
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) {
|
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);
|
T[] anno = SWCommandUtils.getAnnotation(method, annotation);
|
||||||
if (anno == null || anno.length == 0) {
|
if (anno == null || anno.length == 0) return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Parameter[] parameters = method.getParameters();
|
Parameter[] parameters = method.getParameters();
|
||||||
if (!parameterTester.test(parameters.length)) {
|
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() + "'");
|
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (T a : anno) {
|
Arrays.stream(anno).forEach(t -> consumer.accept(t, parameters));
|
||||||
consumer.accept(a, parameters);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T extends Annotation> void addMapper(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, TypeMapper<?>> consumer) {
|
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) -> {
|
add(annotation, method, parameterTester, firstParameter, returnType, (anno, parameters) -> {
|
||||||
try {
|
try {
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
Object object = method.invoke(this);
|
consumer.accept(anno, (TypeMapper<?>) method.invoke(this));
|
||||||
consumer.accept(anno, (TypeMapper<?>) object);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new SecurityException(e.getMessage(), e);
|
throw new SecurityException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
@ -192,9 +170,7 @@ public abstract class SWCommand {
|
|||||||
|
|
||||||
public void unregister() {
|
public void unregister() {
|
||||||
SWCommandUtils.knownCommandMap.remove(command.getName());
|
SWCommandUtils.knownCommandMap.remove(command.getName());
|
||||||
for (String alias : command.getAliases()) {
|
command.getAliases().forEach(SWCommandUtils.knownCommandMap::remove);
|
||||||
SWCommandUtils.knownCommandMap.remove(alias);
|
|
||||||
}
|
|
||||||
command.unregister(SWCommandUtils.commandMap);
|
command.unregister(SWCommandUtils.commandMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren