12
0

Add SWCommand multilingual

Dieser Commit ist enthalten in:
yoyosource 2021-05-25 12:16:55 +02:00
Ursprung 4d1b73dafc
Commit 8706146317

Datei anzeigen

@ -26,10 +26,10 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import java.lang.annotation.*;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.*;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.IntPredicate;
import java.util.logging.Level;
import java.util.stream.Collectors;
@ -43,6 +43,8 @@ public abstract class SWCommand {
private final List<SubCommand> commandHelpList = new ArrayList<>();
private final Map<String, TypeMapper<?>> localTypeMapper = new HashMap<>();
private BiFunction<String, CommandSender, String> message = (s, commandSender) -> s;
protected SWCommand(String command) {
this(command, new String[0]);
}
@ -71,6 +73,8 @@ public abstract class SWCommand {
unregister();
register();
messageSystem();
List<Method> methods = methods();
for (Method method : methods) {
addMapper(Mapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> {
@ -141,6 +145,26 @@ public abstract class SWCommand {
}
}
private void messageSystem() {
try {
Field field = getClass().getDeclaredField("MESSAGE");
if (!field.getType().getTypeName().equals("de.steamwar.message.Message")) {
return;
}
Object o = Modifier.isStatic(field.getModifiers()) ? field.get(null) : field.get(this);
Method method = o.getClass().getMethod("parse");
message = (s, commandSender) -> {
try {
return (String) method.invoke(o, s, commandSender);
} catch (IllegalAccessException | InvocationTargetException e) {
return s;
}
};
} catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) {
// Ignored
}
}
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);
if (anno == null || anno.length == 0) return;