diff --git a/TabooLib R2.iml b/TabooLib R2.iml index c709ad4..305d1b0 100644 --- a/TabooLib R2.iml +++ b/TabooLib R2.iml @@ -1,5 +1,12 @@ + + + + + + diff --git a/src/main/scala/io/izzel/taboolib/module/command/base/BaseMainCommand.java b/src/main/scala/io/izzel/taboolib/module/command/base/BaseMainCommand.java index 596957f..770d82a 100644 --- a/src/main/scala/io/izzel/taboolib/module/command/base/BaseMainCommand.java +++ b/src/main/scala/io/izzel/taboolib/module/command/base/BaseMainCommand.java @@ -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) { diff --git a/src/main/scala/io/izzel/taboolib/module/command/base/BaseSubCommand.java b/src/main/scala/io/izzel/taboolib/module/command/base/BaseSubCommand.java index 6d6b40e..83076d5 100644 --- a/src/main/scala/io/izzel/taboolib/module/command/base/BaseSubCommand.java +++ b/src/main/scala/io/izzel/taboolib/module/command/base/BaseSubCommand.java @@ -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); diff --git a/src/main/scala/io/izzel/taboolib/module/command/base/SubCommand.java b/src/main/scala/io/izzel/taboolib/module/command/base/SubCommand.java index cace75a..279f3fe 100644 --- a/src/main/scala/io/izzel/taboolib/module/command/base/SubCommand.java +++ b/src/main/scala/io/izzel/taboolib/module/command/base/SubCommand.java @@ -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; }