SteamWar/SpigotCore
Archiviert
13
0

Add SWCommand.LocalMapper only defined in current class scope overrides any global mapper for this class only

Dieser Commit ist enthalten in:
yoyosource 2021-03-29 22:28:33 +02:00
Ursprung 2ad2dc29e4
Commit 9c7cea96d4
2 geänderte Dateien mit 28 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -36,6 +36,7 @@ public abstract class SWCommand {
private boolean enabled = true; private boolean enabled = true;
private final Set<SubCommand> commandSet = new HashSet<>(); private final Set<SubCommand> commandSet = new HashSet<>();
private final Set<SubCommand> commandHelpSet = new HashSet<>(); private final Set<SubCommand> commandHelpSet = new HashSet<>();
private final Map<String, TypeMapper<?>> localTypeMapper = new HashMap<>();
protected SWCommand(String command) { protected SWCommand(String command) {
this(command, new String[0]); this(command, new String[0]);
@ -64,7 +65,7 @@ public abstract class SWCommand {
if (!enabled) return Collections.emptyList(); if (!enabled) return Collections.emptyList();
List<String> strings = new ArrayList<>(); List<String> strings = new ArrayList<>();
for (SubCommand subCommand : commandSet) { for (SubCommand subCommand : commandSet) {
List<String> tabCompletes = subCommand.tabComplete(sender, args); List<String> tabCompletes = subCommand.tabComplete(args);
if (tabCompletes != null) { if (tabCompletes != null) {
strings.addAll(tabCompletes); strings.addAll(tabCompletes);
} }
@ -98,6 +99,15 @@ public abstract class SWCommand {
throw new SecurityException(e.getMessage(), e); throw new SecurityException(e.getMessage(), e);
} }
}); });
add(LocalMapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, parameters) -> {
try {
method.setAccessible(true);
Object object = method.invoke(this);
localTypeMapper.put(anno.value(), (TypeMapper<?>) object);
} catch (Exception e) {
throw new SecurityException(e.getMessage(), e);
}
});
add(RegisterHelp.class, method, i -> i != 1, true, null, (anno, parameters) -> { add(RegisterHelp.class, method, i -> i != 1, true, null, (anno, parameters) -> {
if (!parameters[parameters.length - 1].isVarArgs()) { if (!parameters[parameters.length - 1].isVarArgs()) {
return; return;
@ -176,11 +186,16 @@ public abstract class SWCommand {
String value(); String value();
} }
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD}) @Target({ElementType.METHOD})
protected @interface ClassMapper { protected @interface ClassMapper {
Class<?> value(); Class<?> value();
} }
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
protected @interface LocalMapper {
String value();
}
} }

Datei anzeigen

@ -37,6 +37,10 @@ class SubCommand {
private Function<CommandSender, ?> commandSenderFunction; private Function<CommandSender, ?> commandSenderFunction;
public SubCommand(SWCommand swCommand, Method method, String[] subCommand) { public SubCommand(SWCommand swCommand, Method method, String[] subCommand) {
this(swCommand, method, subCommand, new HashMap<>());
}
public SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map<String, TypeMapper<?>> localTypeMapper) {
this.swCommand = swCommand; this.swCommand = swCommand;
this.method = method; this.method = method;
@ -54,7 +58,7 @@ class SubCommand {
} }
SWCommand.Mapper mapper = parameter.getAnnotation(SWCommand.Mapper.class); SWCommand.Mapper mapper = parameter.getAnnotation(SWCommand.Mapper.class);
if (clazz.isEnum() && mapper == null && !SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz.getTypeName())) { if (clazz.isEnum() && mapper == null && !SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz.getTypeName()) && !localTypeMapper.containsKey(clazz.getTypeName())) {
Class<Enum<?>> enumClass = (Class<Enum<?>>) clazz; Class<Enum<?>> enumClass = (Class<Enum<?>>) clazz;
List<String> tabCompletes = new ArrayList<>(); List<String> tabCompletes = new ArrayList<>();
for (Enum<?> enumConstant : enumClass.getEnumConstants()) { for (Enum<?> enumConstant : enumClass.getEnumConstants()) {
@ -68,10 +72,13 @@ class SubCommand {
if (mapper != null) { if (mapper != null) {
name = mapper.value(); name = mapper.value();
} }
if (localTypeMapper.containsKey(name)) {
arguments[i - 1] = localTypeMapper.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION);
} else {
arguments[i - 1] = SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION); arguments[i - 1] = SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION);
} }
} }
}
boolean invoke(CommandSender commandSender, String[] args) { boolean invoke(CommandSender commandSender, String[] args) {
if (args.length < arguments.length - 1) { if (args.length < arguments.length - 1) {
@ -93,7 +100,7 @@ class SubCommand {
return true; return true;
} }
List<String> tabComplete(CommandSender commandSender, String[] args) { List<String> tabComplete(String[] args) {
if (!varArgs && args.length < arguments.length - 1) { if (!varArgs && args.length < arguments.length - 1) {
return Collections.emptyList(); return Collections.emptyList();
} }