Add better Enum handling for Custom TypeMapper
Add SWCommandUtils.getAnnotation Add GameMode mapper for better gamemode handling
Dieser Commit ist enthalten in:
Ursprung
05f8129705
Commit
4bedbe2275
@ -22,11 +22,16 @@ package de.steamwar.command;
|
|||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
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.Method;
|
||||||
import java.lang.reflect.Parameter;
|
import java.lang.reflect.Parameter;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import static de.steamwar.command.SWCommandUtils.getAnnotation;
|
||||||
|
|
||||||
public abstract class SWCommand {
|
public abstract class SWCommand {
|
||||||
|
|
||||||
private final Set<SubCommand> commandSet = new HashSet<>();
|
private final Set<SubCommand> commandSet = new HashSet<>();
|
||||||
@ -101,7 +106,7 @@ public abstract class SWCommand {
|
|||||||
clazz = parameter.getType().getComponentType();
|
clazz = parameter.getType().getComponentType();
|
||||||
}
|
}
|
||||||
Mapper mapper = parameter.getAnnotation(Mapper.class);
|
Mapper mapper = parameter.getAnnotation(Mapper.class);
|
||||||
if (clazz.isEnum() && mapper == null) {
|
if (clazz.isEnum() && mapper == null && !SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz.getTypeName())) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
String name = clazz.getTypeName();
|
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)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.METHOD})
|
@Target({ElementType.METHOD})
|
||||||
protected @interface Register {
|
protected @interface Register {
|
||||||
|
@ -20,10 +20,13 @@
|
|||||||
package de.steamwar.command;
|
package de.steamwar.command;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.GameMode;
|
||||||
import org.bukkit.command.CommandMap;
|
import org.bukkit.command.CommandMap;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -149,6 +152,35 @@ class SWCommandUtils {
|
|||||||
return Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList());
|
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) {
|
private static void addMapper(Class<?> clazz, Class<?> alternativeClazz, TypeMapper<?> mapper) {
|
||||||
@ -202,4 +234,9 @@ class SWCommandUtils {
|
|||||||
MAPPER_FUNCTIONS.put(name, mapper);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -45,8 +45,15 @@ class SubCommand {
|
|||||||
|
|
||||||
Parameter[] parameters = method.getParameters();
|
Parameter[] parameters = method.getParameters();
|
||||||
commandSenderFunction = sender -> parameters[0].getType().cast(sender);
|
commandSenderFunction = sender -> parameters[0].getType().cast(sender);
|
||||||
SWCommand.Register register = method.getAnnotation(SWCommand.Register.class);
|
SWCommand.Register register = SWCommandUtils.getAnnotation(method, SWCommand.Register.class);
|
||||||
subCommand = register.value();
|
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];
|
arguments = new TypeMapper[parameters.length - 1];
|
||||||
for (int i = 1; i < parameters.length; i++) {
|
for (int i = 1; i < parameters.length; i++) {
|
||||||
@ -58,7 +65,7 @@ class SubCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SWCommand.Mapper mapper = parameter.getAnnotation(SWCommand.Mapper.class);
|
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;
|
Class<Enum<?>> enumClass = (Class<Enum<?>>) clazz;
|
||||||
List<String> tabCompletes = new ArrayList<>();
|
List<String> tabCompletes = new ArrayList<>();
|
||||||
for (Enum<?> enumConstant : enumClass.getEnumConstants()) {
|
for (Enum<?> enumConstant : enumClass.getEnumConstants()) {
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren