SteamWar/SpigotCore
Archiviert
13
0

Removing Message reflections

Dieser Commit ist enthalten in:
yoyosource 2021-06-05 17:07:27 +02:00
Ursprung da17dd99bc
Commit 5987f71f72

Datei anzeigen

@ -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<SubCommand> commandHelpList = new ArrayList<>();
private final Map<String, TypeMapper<?>> localTypeMapper = new HashMap<>();
private BiFunction<String, CommandSender, String> 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<Method> 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<Object> 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 <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;
@ -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());
});