+ update basecommand

master
坏黑 2019-10-25 22:29:37 +08:00
parent 8b05ff6451
commit fe0efbf297
4 changed files with 77 additions and 19 deletions

View File

@ -1,5 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
<option name="GRADLE_PROJECT_PATH" value=":" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />

View File

@ -16,10 +16,7 @@ import org.bukkit.scheduler.BukkitRunnable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
@ -52,11 +49,42 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
baseMainCommand.getLinkClasses().forEach(clazz -> java.util.Arrays.stream(clazz.getDeclaredMethods()).filter(method -> method.getAnnotation(SubCommand.class) != null).forEach(methods::add));
if (methods.size() > 0) {
methods.sort(Comparator.comparingDouble(a -> a.getAnnotation(SubCommand.class).priority()));
methods.forEach(x -> {
methods.forEach(method -> {
BaseSubCommand subCommand = null;
try {
x.setAccessible(true);
x.invoke(baseMainCommand);
} catch (Exception ignored) {
method.setAccessible(true);
// lite parameter
if (Arrays.equals(method.getParameterTypes(), new Class[] {CommandSender.class, String[].class})) {
subCommand = new BaseSubCommand() {
@Override
public void onCommand(CommandSender sender, Command command, String label, String[] args) {
try {
method.invoke(baseMainCommand, sender, args);
} catch (Throwable t) {
t.printStackTrace();
}
}
};
}
// fully parameter
else if (Arrays.equals(method.getParameterTypes(), new Class[] {CommandSender.class, Command.class, String.class, String[].class})) {
subCommand = new BaseSubCommand() {
@Override
public void onCommand(CommandSender sender, Command command, String label, String[] args) {
try {
method.invoke(baseMainCommand, sender, command, label, args);
} catch (Throwable t) {
t.printStackTrace();
}
}
};
}
if (subCommand != null) {
subCommand.setLabel(method.getName());
subCommand.setAnnotation(method.getAnnotation(SubCommand.class));
baseMainCommand.registerSubCommand(subCommand);
}
} catch (Throwable ignored) {
}
});
}
@ -68,8 +96,9 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
commandField.getField().setAccessible(true);
BaseSubCommand subCommand = (BaseSubCommand) commandField.getField().get(commandField.getParent().newInstance());
subCommand.setLabel(commandField.getField().getName());
subCommand.setAnnotation(commandField.getField().getAnnotation(SubCommand.class));
baseMainCommand.registerSubCommand(subCommand);
} catch (Exception ignored) {
} catch (Throwable ignored) {
}
});
}
@ -230,7 +259,7 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
}
private boolean hasPermission(CommandSender sender, BaseSubCommand baseSubCommand) {
return baseSubCommand == null || baseSubCommand.getPermission() == null || sender.hasPermission(baseSubCommand.getPermission());
return baseSubCommand == null || Strings.isEmpty(baseSubCommand.getPermission()) || sender.hasPermission(baseSubCommand.getPermission());
}
private String[] removeFirst(String[] args) {

View File

@ -1,6 +1,7 @@
package io.izzel.taboolib.module.command.base;
import io.izzel.taboolib.module.locale.TLocale;
import io.izzel.taboolib.util.Strings;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@ -15,6 +16,11 @@ import java.util.stream.IntStream;
public abstract class BaseSubCommand {
private String label;
private SubCommand annotation;
public void setAnnotation(SubCommand annotation) {
this.annotation = annotation;
}
public void setLabel(String label) {
this.label = label;
@ -25,35 +31,35 @@ public abstract class BaseSubCommand {
}
public String getDescription() {
return null;
return annotation.description();
}
public String[] getAliases() {
return new String[0];
return annotation.aliases();
}
public Argument[] getArguments() {
return new Argument[0];
return Arrays.stream(annotation.aliases()).map(a -> a.endsWith("?") ? new Argument(a.substring(0, a.length() - 1), false) : new Argument(a)).toArray(Argument[]::new);
}
public CommandType getType() {
return CommandType.ALL;
return annotation.type();
}
public boolean ignoredLabel() {
return true;
return annotation.ignoredLabel();
}
public boolean requiredPlayer() {
return false;
return annotation.requiredPlayer();
}
public String getPermission() {
return null;
return annotation.permission();
}
public boolean hideInHelp() {
return false;
return annotation.hideInHelp();
}
public boolean isParameterConform(String[] args) {
@ -61,7 +67,7 @@ public abstract class BaseSubCommand {
}
public String getCommandString(String label) {
return TLocale.asString(getDescription() == null ? "COMMANDS.INTERNAL.COMMAND-HELP-EMPTY" : "COMMANDS.INTERNAL.COMMAND-HELP", label, getLabel(), Arrays.stream(getArguments()).map(parameter -> parameter.toString() + " ").collect(Collectors.joining()), getDescription());
return TLocale.asString(Strings.isEmpty(getDescription()) ? "COMMANDS.INTERNAL.COMMAND-HELP-EMPTY" : "COMMANDS.INTERNAL.COMMAND-HELP", label, getLabel(), Arrays.stream(getArguments()).map(parameter -> parameter.toString() + " ").collect(Collectors.joining()), getDescription());
}
abstract public void onCommand(CommandSender sender, Command command, String label, String[] args);

View File

@ -14,4 +14,20 @@ import java.lang.annotation.Target;
public @interface SubCommand {
double priority() default 0;
String permission() default "";
String description() default "";
String[] aliases() default {};
String[] arguments() default {};
boolean ignoredLabel() default true;
boolean requiredPlayer() default false;
boolean hideInHelp() default false;
CommandType type() default CommandType.ALL;
}