Add help command System to SWCommand
Dieser Commit ist enthalten in:
Ursprung
1b10c96dc6
Commit
472b6f46d5
@ -22,20 +22,15 @@ 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.ElementType;
|
import java.lang.annotation.*;
|
||||||
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 java.util.function.Consumer;
|
|
||||||
|
|
||||||
public abstract class SWCommand {
|
public abstract class SWCommand {
|
||||||
|
|
||||||
private final Set<SubCommand> commandSet = new HashSet<>();
|
private final Set<SubCommand> commandSet = new HashSet<>();
|
||||||
private Consumer<CommandSender> helpMessage = sender -> {
|
private final Set<SubCommand> commandHelpSet = new HashSet<>();
|
||||||
};
|
|
||||||
|
|
||||||
protected SWCommand(String command) {
|
protected SWCommand(String command) {
|
||||||
this(command, new String[0]);
|
this(command, new String[0]);
|
||||||
@ -50,7 +45,11 @@ public abstract class SWCommand {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
helpMessage.accept(sender);
|
for (SubCommand subCommand : commandHelpSet) {
|
||||||
|
if (subCommand.invoke(sender, args)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,14 +76,14 @@ public abstract class SWCommand {
|
|||||||
for (Method method : getClass().getDeclaredMethods()) {
|
for (Method method : getClass().getDeclaredMethods()) {
|
||||||
addMapper(method);
|
addMapper(method);
|
||||||
addCommand(method, commandMethods);
|
addCommand(method, commandMethods);
|
||||||
|
addHelp(method);
|
||||||
}
|
}
|
||||||
commandMethods.forEach(method -> commandSet.add(new SubCommand(this, method)));
|
commandMethods.forEach(method -> commandSet.add(new SubCommand(this, method)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addCommand(Method method, Set<Method> commandMethods) {
|
private void addCommand(Method method, Set<Method> commandMethods) {
|
||||||
Register register = method.getDeclaredAnnotation(Register.class);
|
Register register = getAnnotation(method, Register.class);
|
||||||
Mapper methodMapper = method.getDeclaredAnnotation(Mapper.class);
|
if (register == null) {
|
||||||
if (register == null || methodMapper != null) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,10 +113,25 @@ public abstract class SWCommand {
|
|||||||
commandMethods.add(method);
|
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) {
|
private void addMapper(Method method) {
|
||||||
Register register = method.getDeclaredAnnotation(Register.class);
|
Mapper mapper = getAnnotation(method, Mapper.class);
|
||||||
Mapper methodMapper = method.getDeclaredAnnotation(Mapper.class);
|
if (mapper == null) {
|
||||||
if (register != null || methodMapper == null) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,19 +145,15 @@ public abstract class SWCommand {
|
|||||||
try {
|
try {
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
Object object = method.invoke(this);
|
Object object = method.invoke(this);
|
||||||
SWCommandUtils.addMapper(methodMapper.value(), (TypeMapper<?>) object);
|
SWCommandUtils.addMapper(mapper.value(), (TypeMapper<?>) object);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new SecurityException(e.getMessage(), e);
|
throw new SecurityException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final void setHelpMessage(Consumer<CommandSender> helpMessage) {
|
private <T extends Annotation> T getAnnotation(Method method, Class<T> annotation) {
|
||||||
assert helpMessage != null;
|
if (method.getAnnotations().length != 1) return null;
|
||||||
this.helpMessage = helpMessage;
|
return method.getAnnotation(annotation);
|
||||||
}
|
|
||||||
|
|
||||||
protected final <T extends CommandSender> void sendHelpMessage(T sender) {
|
|
||||||
helpMessage.accept(sender);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@ -152,6 +162,12 @@ public abstract class SWCommand {
|
|||||||
String[] value() default {};
|
String[] value() default {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.METHOD})
|
||||||
|
protected @interface RegisterHelp {
|
||||||
|
String[] value() default {};
|
||||||
|
}
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
||||||
protected @interface Mapper {
|
protected @interface Mapper {
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren