Add SWCommand.LocalMapper only defined in current class scope overrides any global mapper for this class only
Dieser Commit ist enthalten in:
Ursprung
2ad2dc29e4
Commit
9c7cea96d4
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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,10 +72,13 @@ class SubCommand {
|
||||
if (mapper != null) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean invoke(CommandSender commandSender, String[] args) {
|
||||
if (args.length < arguments.length - 1) {
|
||||
@ -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();
|
||||
}
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren