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 final Set<SubCommand> commandSet = new HashSet<>();
private final Set<SubCommand> commandHelpSet = new HashSet<>();
private final Map<String, TypeMapper<?>> localTypeMapper = new HashMap<>();
protected SWCommand(String command) {
this(command, new String[0]);
@ -64,7 +65,7 @@ public abstract class SWCommand {
if (!enabled) return Collections.emptyList();
List<String> strings = new ArrayList<>();
for (SubCommand subCommand : commandSet) {
List<String> tabCompletes = subCommand.tabComplete(sender, args);
List<String> tabCompletes = subCommand.tabComplete(args);
if (tabCompletes != null) {
strings.addAll(tabCompletes);
}
@ -98,6 +99,15 @@ public abstract class SWCommand {
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) -> {
if (!parameters[parameters.length - 1].isVarArgs()) {
return;
@ -176,11 +186,16 @@ public abstract class SWCommand {
String value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
protected @interface ClassMapper {
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;
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.method = method;
@ -54,7 +58,7 @@ class SubCommand {
}
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;
List<String> tabCompletes = new ArrayList<>();
for (Enum<?> enumConstant : enumClass.getEnumConstants()) {
@ -68,8 +72,11 @@ class SubCommand {
if (mapper != null) {
name = mapper.value();
}
arguments[i - 1] = SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION);
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);
}
}
}
@ -93,7 +100,7 @@ class SubCommand {
return true;
}
List<String> tabComplete(CommandSender commandSender, String[] args) {
List<String> tabComplete(String[] args) {
if (!varArgs && args.length < arguments.length - 1) {
return Collections.emptyList();
}