From 8706146317d61863229d6877246b8e0e3d6a9592 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 25 May 2021 12:16:55 +0200 Subject: [PATCH] Add SWCommand multilingual --- .../src/de/steamwar/command/SWCommand.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 35779c5..708ed53 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -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 commandHelpList = new ArrayList<>(); private final Map> localTypeMapper = new HashMap<>(); + private BiFunction 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 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 void add(Class annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class returnType, BiConsumer consumer) { T[] anno = SWCommandUtils.getAnnotation(method, annotation); if (anno == null || anno.length == 0) return;