SteamWar/SpigotCore
Archiviert
13
0

CommandFramework3 #94

Manuell gemergt
Zeanon hat 71 Commits von CommandFramework3 nach master 2021-03-30 21:15:40 +02:00 zusammengeführt
5 geänderte Dateien mit 74 neuen und 31 gelöschten Zeilen
Nur Änderungen aus Commit f0bf20e960 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -43,7 +43,9 @@ class InternalCommand {
}
boolean invoke(CommandSender commandSender, String[] args) {
if (args.length < parameters.length - 1) return false;
if (args.length < parameters.length - 1) {
return false;
}
try {
Object[] objects = SWCommandUtils.generateArgumentArray(commandSender, method, parameters, args);
method.setAccessible(true);
@ -59,18 +61,26 @@ class InternalCommand {
List<String> tabComplete(String[] args) {
if (args.length > parameters.length - increment) {
if (parameters[parameters.length - 1].isVarArgs()) {
Class<?> clazz = parameters[parameters.length - 1].getType().getComponentType();
String name = parameters[parameters.length - 1].getType().getComponentType().getTypeName();
SWCommand.Mapper mapper = parameters[parameters.length - 1].getAnnotation(SWCommand.Mapper.class);
if (mapper != null) {
name = mapper.mapper();
}
try {
return SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(clazz, SWCommandUtils.ERROR_FUNCTION).tabCompletes(args[args.length - 1]);
return SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION).tabCompletes(args[args.length - 1]);
} catch (Exception e) {
// Ignored
}
}
return Collections.emptyList();
}
Class<?> clazz = parameters[args.length - increment + 1].getType();
String name = parameters[args.length - increment + 1].getType().getTypeName();
SWCommand.Mapper mapper = parameters[args.length - increment + 1].getAnnotation(SWCommand.Mapper.class);
if (mapper != null) {
name = mapper.mapper();
}
try {
return SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(clazz, SWCommandUtils.ERROR_FUNCTION).tabCompletes(args[args.length - 1]);
return SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION).tabCompletes(args[args.length - 1]);
} catch (Exception e) {
return Collections.emptyList();
}

Datei anzeigen

@ -38,7 +38,9 @@ class InternalTabComplete {
}
SWCommandUtils.TabComplete invoke(CommandSender commandSender, String[] args) {
if (args.length < parameters.length - 1) return null;
if (args.length < parameters.length - 1) {
return null;
}
try {
Object[] objects = SWCommandUtils.generateArgumentArray(commandSender, method, parameters, args);
method.setAccessible(true);

Datei anzeigen

@ -36,7 +36,8 @@ public abstract class SWCommand {
private final Set<InternalCommand> commandSet = new HashSet<>();
private final Set<InternalTabComplete> tabCompleteSet = new HashSet<>();
private final Map<Integer, Set<String>> subCommandTabCompletes = new HashMap<>();
private Consumer<CommandSender> helpMessage = sender -> {};
private Consumer<CommandSender> helpMessage = sender -> {
};
protected SWCommand(String command) {
this(command, new String[0]);
@ -82,12 +83,16 @@ public abstract class SWCommand {
for (Method method : getClass().getDeclaredMethods()) {
Register register = method.getDeclaredAnnotation(Register.class);
if (register == null) continue;
if (register == null) {
continue;
}
for (int i = 0; i < register.subCommand().length; i++) {
subCommandTabCompletes.computeIfAbsent(i, integer -> new HashSet<>()).add(register.subCommand()[i]);
}
if (!validMethod(method)) continue;
if (!validMethod(method)) {
continue;
}
if (method.getReturnType() == Void.TYPE) {
commandSet.add(new InternalCommand(this, method));
}
@ -99,16 +104,27 @@ public abstract class SWCommand {
private boolean validMethod(Method method) {
Parameter[] parameters = method.getParameters();
if (parameters.length == 0) return false;
if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) return false;
if (parameters.length == 0) {
return false;
}
if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) {
return false;
}
for (int i = 1; i < parameters.length; i++) {
Parameter parameter = parameters[i];
Class<?> clazz = parameter.getType();
if (parameter.isVarArgs() && i == parameters.length - 1) {
clazz = parameter.getType().getComponentType();
}
if (clazz.isEnum()) continue;
if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz)) return false;
if (clazz.isEnum()) {
continue;
}
String name = clazz.getTypeName();
Mapper mapper = parameter.getAnnotation(Mapper.class);
if (mapper != null) {
name = mapper.mapper();
}
if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) return false;
}
return true;
}
@ -128,4 +144,10 @@ public abstract class SWCommand {
String[] subCommand() default {};
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
protected @interface Mapper {
String mapper();
}
}

Datei anzeigen

@ -38,7 +38,7 @@ class SWCommandUtils {
throw new IllegalStateException("Utility Class");
}
static final Map<Class<?>, TypeMapper<?>> MAPPER_FUNCTIONS = new HashMap<>();
static final Map<String, TypeMapper<?>> MAPPER_FUNCTIONS = new HashMap<>();
static final TypeMapper<?> ERROR_FUNCTION = new TypeMapper<>() {
@Override
@ -120,7 +120,7 @@ class SWCommandUtils {
}
}
});
MAPPER_FUNCTIONS.put(String.class, new TypeMapper<String>() {
MAPPER_FUNCTIONS.put(String.class.getTypeName(), new TypeMapper<String>() {
@Override
public String map(String s) {
return s;
@ -131,7 +131,7 @@ class SWCommandUtils {
return Collections.singletonList(s);
}
});
MAPPER_FUNCTIONS.put(StringBuilder.class, new TypeMapper<StringBuilder>() {
MAPPER_FUNCTIONS.put(StringBuilder.class.getTypeName(), new TypeMapper<StringBuilder>() {
@Override
public StringBuilder map(String s) {
return new StringBuilder(s);
@ -142,7 +142,7 @@ class SWCommandUtils {
return Collections.singletonList(s);
}
});
MAPPER_FUNCTIONS.put(Player.class, new TypeMapper<Player>() {
MAPPER_FUNCTIONS.put(Player.class.getTypeName(), new TypeMapper<Player>() {
@Override
public Player map(String s) {
return Bukkit.getPlayer(s);
@ -156,8 +156,8 @@ class SWCommandUtils {
}
private static void addMapper(Class<?> clazz, Class<?> alternativeClazz, TypeMapper<?> mapper) {
MAPPER_FUNCTIONS.put(clazz, mapper);
MAPPER_FUNCTIONS.put(alternativeClazz, mapper);
MAPPER_FUNCTIONS.put(clazz.getTypeName(), mapper);
MAPPER_FUNCTIONS.put(alternativeClazz.getTypeName(), mapper);
}
static final CommandMap commandMap;
@ -189,14 +189,13 @@ class SWCommandUtils {
}
for (int i = subCommandIndex; i < parameters.length - (varArgs ? 1 : 0); i++) {
Class<?> clazz = parameters[i].getType();
arguments[i] = mapper(clazz).apply(args[i - 1]);
arguments[i] = mapper(parameters[i]).apply(args[i - 1]);
}
if (varArgs) {
Object[] varArgument = new Object[args.length - parameters.length + 2];
arguments[arguments.length - 1] = varArgument;
Function<String, Object> mapper = mapper(parameters[parameters.length - 1].getType().getComponentType());
Function<String, Object> mapper = mapper(parameters[parameters.length - 1]);
int index = 0;
for (int i = parameters.length - 2; i < args.length; i++) {
@ -208,13 +207,25 @@ class SWCommandUtils {
}
@SuppressWarnings("unchecked")
private static Function<String, Object> mapper(Class<?> clazz) {
private static Function<String, Object> mapper(Parameter parameter) {
Class<?> clazz = parameter.getType();
if (parameter.isVarArgs()) {
clazz = clazz.getComponentType();
}
if (clazz.isEnum()) {
Class<Enum<?>> enumClass = (Class<Enum<?>>) clazz;
return s -> ENUM_MAPPER.apply(enumClass, s);
} else {
return s -> MAPPER_FUNCTIONS.getOrDefault(clazz, ERROR_FUNCTION).map(s);
}
String name = clazz.getTypeName();
SWCommand.Mapper mapper = parameter.getAnnotation(SWCommand.Mapper.class);
if (mapper != null) {
name = mapper.mapper();
}
TypeMapper<?> typeMapper = MAPPER_FUNCTIONS.getOrDefault(name, ERROR_FUNCTION);
return typeMapper::map;
}
public static class TabComplete {
@ -225,10 +236,9 @@ class SWCommandUtils {
}
}
public static void addMapper(Class<?> clazz, TypeMapper<?> mapper) {
if (clazz.isEnum()) return;
if (MAPPER_FUNCTIONS.containsKey(clazz)) return;
MAPPER_FUNCTIONS.put(clazz, mapper);
public static void addMapper(String name, TypeMapper<?> mapper) {
if (MAPPER_FUNCTIONS.containsKey(name)) return;
MAPPER_FUNCTIONS.put(name, mapper);
}
}

Datei anzeigen

@ -22,8 +22,7 @@ package de.steamwar.command;
import java.util.List;
public interface TypeMapper<T> {
T map(String s);
List<String> tabCompletes(String s);
List<String> tabCompletes(String s);
}