From 5987f71f72a94e1632c846436b9a90ee1199bd94 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 5 Jun 2021 17:07:27 +0200 Subject: [PATCH] Removing Message reflections --- .../src/de/steamwar/command/SWCommand.java | 44 ++++++------------- 1 file changed, 14 insertions(+), 30 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 72a83da..2f942c2 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -19,17 +19,17 @@ package de.steamwar.command; +import de.steamwar.message.Message; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitRunnable; import java.lang.annotation.*; -import java.lang.reflect.*; +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; import java.util.*; -import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiConsumer; -import java.util.function.BiFunction; import java.util.function.IntPredicate; import java.util.stream.Collectors; @@ -42,13 +42,22 @@ public abstract class SWCommand { private final List commandHelpList = new ArrayList<>(); private final Map> localTypeMapper = new HashMap<>(); - private BiFunction message = (s, commandSender) -> s; + private final Message message; protected SWCommand(String command) { this(command, new String[0]); } + protected SWCommand(String command, Message message) { + this(command, message, new String[0]); + } + protected SWCommand(String command, String... aliases) { + this(command, null, aliases); + } + + protected SWCommand(String command, Message message, String... aliases) { + this.message = message; this.command = new Command(command, "", "/" + command, Arrays.asList(aliases)) { @Override public boolean execute(CommandSender sender, String alias, String[] args) { @@ -72,8 +81,6 @@ 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) -> { @@ -138,29 +145,6 @@ public abstract class SWCommand { } } - private void messageSystem() { - try { - Field field = getClass().getDeclaredField("MESSAGE"); - if (!field.getType().getTypeName().equals("de.steamwar.message.Message")) { - return; - } - AtomicReference o = new AtomicReference<>(null); - Method method = o.getClass().getMethod("parse"); - message = (s, commandSender) -> { - try { - if (o.get() == null) { - o.set(Modifier.isStatic(field.getModifiers()) ? field.get(null) : field.get(this)); - } - return (String) method.invoke(o.get(), s, commandSender); - } catch (IllegalAccessException | InvocationTargetException e) { - return s; - } - }; - } catch (NoSuchFieldException | 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; @@ -240,7 +224,7 @@ public abstract class SWCommand { st.append("§7..."); } if (!subCommand.description.isEmpty()) { - st.append("§8 - §7").append(message.apply(subCommand.description, sender)); + st.append("§8 - §7").append(message.parse(subCommand.description, sender)); } help.add(st.toString()); });