SteamWar/SpigotCore
Archiviert
13
0

Add better Enum handling for Custom TypeMapper

Add SWCommandUtils.getAnnotation
Add GameMode mapper for better gamemode handling
Dieser Commit ist enthalten in:
yoyosource 2021-03-25 20:46:02 +01:00
Ursprung 05f8129705
Commit 4bedbe2275
3 geänderte Dateien mit 54 neuen und 10 gelöschten Zeilen

Datei anzeigen

@ -22,11 +22,16 @@ package de.steamwar.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import java.lang.annotation.*;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.*;
import static de.steamwar.command.SWCommandUtils.getAnnotation;
public abstract class SWCommand {
private final Set<SubCommand> commandSet = new HashSet<>();
@ -101,7 +106,7 @@ public abstract class SWCommand {
clazz = parameter.getType().getComponentType();
}
Mapper mapper = parameter.getAnnotation(Mapper.class);
if (clazz.isEnum() && mapper == null) {
if (clazz.isEnum() && mapper == null && !SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz.getTypeName())) {
continue;
}
String name = clazz.getTypeName();
@ -151,11 +156,6 @@ public abstract class SWCommand {
}
}
private <T extends Annotation> T getAnnotation(Method method, Class<T> annotation) {
if (method.getAnnotations().length != 1) return null;
return method.getAnnotation(annotation);
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
protected @interface Register {

Datei anzeigen

@ -20,10 +20,13 @@
package de.steamwar.command;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.command.CommandMap;
import org.bukkit.entity.Player;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
@ -149,6 +152,35 @@ class SWCommandUtils {
return Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList());
}
});
MAPPER_FUNCTIONS.put(GameMode.class.getTypeName(), new TypeMapper<GameMode>() {
@Override
public GameMode map(String s) {
switch (s.toLowerCase()) {
case "s":
case "survival":
case "0":
return GameMode.SURVIVAL;
case "c":
case "creative":
case "1":
return GameMode.CREATIVE;
case "sp":
case "spectator":
case "3":
return GameMode.SPECTATOR;
case "a":
case "adventure":
case "2":
return GameMode.ADVENTURE;
}
throw new SecurityException();
}
@Override
public List<String> tabCompletes(String s) {
return Arrays.asList("s", "survival", "0", "c", "creative", "1", "sp", "specator", "3", "a", "adventure", "2");
}
});
}
private static void addMapper(Class<?> clazz, Class<?> alternativeClazz, TypeMapper<?> mapper) {
@ -202,4 +234,9 @@ class SWCommandUtils {
MAPPER_FUNCTIONS.put(name, mapper);
}
static <T extends Annotation> T getAnnotation(Method method, Class<T> annotation) {
if (method.getAnnotations().length != 1) return null;
return method.getAnnotation(annotation);
}
}

Datei anzeigen

@ -45,8 +45,15 @@ class SubCommand {
Parameter[] parameters = method.getParameters();
commandSenderFunction = sender -> parameters[0].getType().cast(sender);
SWCommand.Register register = method.getAnnotation(SWCommand.Register.class);
subCommand = register.value();
SWCommand.Register register = SWCommandUtils.getAnnotation(method, SWCommand.Register.class);
SWCommand.RegisterHelp registerHelp = SWCommandUtils.getAnnotation(method, SWCommand.RegisterHelp.class);
if (register != null) {
subCommand = register.value();
} else if (registerHelp != null) {
subCommand = registerHelp.value();
} else {
throw new SecurityException();
}
arguments = new TypeMapper[parameters.length - 1];
for (int i = 1; i < parameters.length; i++) {
@ -58,7 +65,7 @@ class SubCommand {
}
SWCommand.Mapper mapper = parameter.getAnnotation(SWCommand.Mapper.class);
if (clazz.isEnum() && mapper == null) {
if (clazz.isEnum() && mapper == null && !SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz.getTypeName())) {
Class<Enum<?>> enumClass = (Class<Enum<?>>) clazz;
List<String> tabCompletes = new ArrayList<>();
for (Enum<?> enumConstant : enumClass.getEnumConstants()) {