SteamWar/SpigotCore
Archiviert
13
0

Add help command System to SWCommand

Dieser Commit ist enthalten in:
yoyosource 2021-03-25 20:26:44 +01:00
Ursprung 1b10c96dc6
Commit 472b6f46d5

Datei anzeigen

@ -22,20 +22,15 @@ package de.steamwar.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.*;
import java.util.function.Consumer;
public abstract class SWCommand {
private final Set<SubCommand> commandSet = new HashSet<>();
private Consumer<CommandSender> helpMessage = sender -> {
};
private final Set<SubCommand> commandHelpSet = new HashSet<>();
protected SWCommand(String command) {
this(command, new String[0]);
@ -50,7 +45,11 @@ public abstract class SWCommand {
return false;
}
}
helpMessage.accept(sender);
for (SubCommand subCommand : commandHelpSet) {
if (subCommand.invoke(sender, args)) {
return false;
}
}
return false;
}
@ -77,14 +76,14 @@ public abstract class SWCommand {
for (Method method : getClass().getDeclaredMethods()) {
addMapper(method);
addCommand(method, commandMethods);
addHelp(method);
}
commandMethods.forEach(method -> commandSet.add(new SubCommand(this, method)));
}
private void addCommand(Method method, Set<Method> commandMethods) {
Register register = method.getDeclaredAnnotation(Register.class);
Mapper methodMapper = method.getDeclaredAnnotation(Mapper.class);
if (register == null || methodMapper != null) {
Register register = getAnnotation(method, Register.class);
if (register == null) {
return;
}
@ -114,10 +113,25 @@ public abstract class SWCommand {
commandMethods.add(method);
}
private void addHelp(Method method) {
RegisterHelp registerHelp = getAnnotation(method, RegisterHelp.class);
if (registerHelp == null) {
return;
}
Parameter[] parameters = method.getParameters();
if (parameters.length != 1) {
return;
}
if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) {
return;
}
commandHelpSet.add(new SubCommand(this, method));
}
private void addMapper(Method method) {
Register register = method.getDeclaredAnnotation(Register.class);
Mapper methodMapper = method.getDeclaredAnnotation(Mapper.class);
if (register != null || methodMapper == null) {
Mapper mapper = getAnnotation(method, Mapper.class);
if (mapper == null) {
return;
}
@ -131,19 +145,15 @@ public abstract class SWCommand {
try {
method.setAccessible(true);
Object object = method.invoke(this);
SWCommandUtils.addMapper(methodMapper.value(), (TypeMapper<?>) object);
SWCommandUtils.addMapper(mapper.value(), (TypeMapper<?>) object);
} catch (Exception e) {
throw new SecurityException(e.getMessage(), e);
}
}
protected final void setHelpMessage(Consumer<CommandSender> helpMessage) {
assert helpMessage != null;
this.helpMessage = helpMessage;
}
protected final <T extends CommandSender> void sendHelpMessage(T sender) {
helpMessage.accept(sender);
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)
@ -152,6 +162,12 @@ public abstract class SWCommand {
String[] value() default {};
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
protected @interface RegisterHelp {
String[] value() default {};
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.METHOD})
protected @interface Mapper {