From 2263ac395d03654971f167c9d8f3acc7d369107f Mon Sep 17 00:00:00 2001 From: 502647092 Date: Mon, 10 Oct 2016 20:43:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8E=BB=E9=99=A4=E5=BC=83=E7=94=A8?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../YumCore/commands/CommandArgument.java | 2 - .../pw/yumc/YumCore/commands/CommandHelp.java | 4 +- .../yumc/YumCore/commands/CommandParse.java | 87 ++++--------------- .../yumc/YumCore/commands/annotation/Cmd.java | 76 ++++------------ .../YumCore/commands/annotation/Default.java | 18 ++++ .../YumCore/commands/annotation/KeyValue.java | 26 ++++++ .../YumCore/commands/annotation/Limit.java | 26 ++++++ .../YumCore/commands/info/CommandInfo.java | 14 --- 8 files changed, 108 insertions(+), 145 deletions(-) create mode 100644 src/main/java/pw/yumc/YumCore/commands/annotation/Default.java create mode 100644 src/main/java/pw/yumc/YumCore/commands/annotation/KeyValue.java create mode 100644 src/main/java/pw/yumc/YumCore/commands/annotation/Limit.java diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandArgument.java b/src/main/java/pw/yumc/YumCore/commands/CommandArgument.java index 6caa052..2547c07 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandArgument.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandArgument.java @@ -15,14 +15,12 @@ public class CommandArgument { private final Command command; private final String alias; private final String[] args; - private final CommandParse parses; public CommandArgument(final CommandSender sender, final Command command, final String alias, final String[] args) { this.sender = sender; this.command = command; this.alias = alias; this.args = args; - parses = null; } /** diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java b/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java index 4499fb7..7c70082 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java @@ -149,8 +149,8 @@ public class CommandHelp { if (page == 1 && defCmd != null) { helpList.add(formatCommand(defCmd, label)); } - final int start = this.LINES_PER_PAGE * (page - 1); - final int end = start + this.LINES_PER_PAGE; + final int start = LINES_PER_PAGE * (page - 1); + final int end = start + LINES_PER_PAGE; for (int i = start; i < end; i++) { if (this.cmdlist.size() > i) { // 帮助列表 diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandParse.java b/src/main/java/pw/yumc/YumCore/commands/CommandParse.java index 271af3d..0ec8774 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandParse.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandParse.java @@ -1,10 +1,6 @@ package pw.yumc.YumCore.commands; import java.lang.annotation.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.util.HashMap; import java.util.LinkedList; @@ -15,6 +11,9 @@ import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import pw.yumc.YumCore.commands.annotation.Default; +import pw.yumc.YumCore.commands.annotation.KeyValue; +import pw.yumc.YumCore.commands.annotation.Limit; import pw.yumc.YumCore.commands.exception.CommandException; import pw.yumc.YumCore.commands.exception.CommandParseException; @@ -47,18 +46,7 @@ public class CommandParse { throw new CommandParseException(String.format("无法解析的参数类型 %s !", clazz.getName())); } final Parse parse = allparses.get(clazz).clone(); - for (final Annotation annotation : annotations) { - if (annotation.annotationType() == Default.class) { - parse.setAttr("default", ((Default) annotation).value()); - } else if (annotation.annotationType() == Limit.class) { - parse.setAttr("min", ((Limit) annotation).min()); - parse.setAttr("max", ((Limit) annotation).max()); - } else if (annotation.annotationType() == KeyValue.class) { - final KeyValue kv = (KeyValue) annotation; - parse.setAttr(kv.key(), kv.value()); - } - } - this.parse.add(parse); + this.parse.add(parse.parseAnnotation(annotations)); } } @@ -105,18 +93,6 @@ public class CommandParse { } } - /** - * 默认参数 - * - * @since 2016年7月23日 上午9:00:27 - * @author 喵♂呜 - */ - @Target(ElementType.METHOD) - @Retention(RetentionPolicy.RUNTIME) - public @interface Default { - String value(); - } - public static class IntegerParse extends Parse { public IntegerParse() { allparses.put(Integer.class, this); @@ -137,46 +113,6 @@ public class CommandParse { } } - /** - * 自定义参数 - * - * @since 2016年7月23日 上午9:00:27 - * @author 喵♂呜 - */ - @Target(ElementType.METHOD) - @Retention(RetentionPolicy.RUNTIME) - public @interface KeyValue { - /** - * @return 键 - */ - String key(); - - /** - * @return 值 - */ - String value() default ""; - } - - /** - * 参数限制 - * - * @since 2016年7月23日 上午9:00:27 - * @author 喵♂呜 - */ - @Target(ElementType.METHOD) - @Retention(RetentionPolicy.RUNTIME) - public @interface Limit { - /** - * @return 最大长度(最大值) - */ - int max() default 255; - - /** - * @return 最小长度(或最小值) - */ - int min(); - } - public static class LongParse extends Parse { public LongParse() { allparses.put(Long.class, this); @@ -224,6 +160,21 @@ public class CommandParse { public abstract RT parse(String arg) throws CommandParseException; + public Parse parseAnnotation(final Annotation[] annotations) { + for (final Annotation annotation : annotations) { + if (annotation.annotationType() == Default.class) { + setAttr("default", ((Default) annotation).value()); + } else if (annotation.annotationType() == Limit.class) { + setAttr("min", ((Limit) annotation).min()); + setAttr("max", ((Limit) annotation).max()); + } else if (annotation.annotationType() == KeyValue.class) { + final KeyValue kv = (KeyValue) annotation; + setAttr(kv.key(), kv.value()); + } + } + return this; + } + public void setAttr(final String name, final Object value) { attrs.put(name, value); } diff --git a/src/main/java/pw/yumc/YumCore/commands/annotation/Cmd.java b/src/main/java/pw/yumc/YumCore/commands/annotation/Cmd.java index 80da585..3ad249f 100644 --- a/src/main/java/pw/yumc/YumCore/commands/annotation/Cmd.java +++ b/src/main/java/pw/yumc/YumCore/commands/annotation/Cmd.java @@ -21,8 +21,7 @@ import org.bukkit.entity.minecart.CommandMinecart; * aliases 命令别名 * minimumArguments 最小参数 默认0 * permission 权限 - * onlyPlayer 只允许玩家 false - * onlyConsole 只允许控制台 false + * executor 执行者 所有 * * * @since 2016年7月23日 上午8:59:05 @@ -46,20 +45,6 @@ public @interface Cmd { */ int minimumArguments() default 0; - /** - * @deprecated 详见 {@link Executor} - * @return 只允许控制台执行 - */ - @Deprecated - boolean onlyConsole() default false; - - /** - * @deprecated 详见 {@link Executor} - * @return 只允许玩家执行 - */ - @Deprecated - boolean onlyPlayer() default false; - /** * @return 当前命令权限 */ @@ -80,66 +65,37 @@ public @interface Cmd { /** * 玩家 */ - PLAYER { - @Override - public String getName() { - return "玩家"; - } - }, + PLAYER("玩家"), /** * 控制台 */ - CONSOLE { - @Override - public String getName() { - return "控制台"; - } - }, + CONSOLE("控制台"), /** * 命令方块 */ - BLOCK { - @Override - public String getName() { - return "命令方块"; - } - }, + BLOCK("命令方块"), /** * 命令矿车 */ - COMMANDMINECART { - @Override - public String getName() { - return "命令矿车"; - } - }, + COMMANDMINECART("命令矿车"), /** * 远程控制台 */ - REMOTECONSOLE { - @Override - public String getName() { - return "远程控制台"; - } - }, + REMOTECONSOLE("远程控制台"), /** * 所有 */ - ALL { - @Override - public String getName() { - return "所有执行者"; - } - }, + ALL("所有执行者"), /** * 未知 */ - UNKNOW { - @Override - public String getName() { - return "未知"; - } - }; + UNKNOW("未知"); + private String name; + + private Executor(final String name) { + this.name = name; + } + /** * 解析Executor * @@ -166,6 +122,8 @@ public @interface Cmd { /** * @return 执行者名称 */ - public abstract String getName(); + public String getName() { + return name; + } } } diff --git a/src/main/java/pw/yumc/YumCore/commands/annotation/Default.java b/src/main/java/pw/yumc/YumCore/commands/annotation/Default.java new file mode 100644 index 0000000..b2c64c6 --- /dev/null +++ b/src/main/java/pw/yumc/YumCore/commands/annotation/Default.java @@ -0,0 +1,18 @@ +package pw.yumc.YumCore.commands.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 默认参数 + * + * @since 2016年7月23日 上午9:00:27 + * @author 喵♂呜 + */ +@Target(ElementType.PARAMETER) +@Retention(RetentionPolicy.RUNTIME) +public @interface Default { + String value(); +} \ No newline at end of file diff --git a/src/main/java/pw/yumc/YumCore/commands/annotation/KeyValue.java b/src/main/java/pw/yumc/YumCore/commands/annotation/KeyValue.java new file mode 100644 index 0000000..0439435 --- /dev/null +++ b/src/main/java/pw/yumc/YumCore/commands/annotation/KeyValue.java @@ -0,0 +1,26 @@ +package pw.yumc.YumCore.commands.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 自定义参数 + * + * @since 2016年7月23日 上午9:00:27 + * @author 喵♂呜 + */ +@Target(ElementType.PARAMETER) +@Retention(RetentionPolicy.RUNTIME) +public @interface KeyValue { + /** + * @return 键 + */ + String key(); + + /** + * @return 值 + */ + String value() default ""; +} \ No newline at end of file diff --git a/src/main/java/pw/yumc/YumCore/commands/annotation/Limit.java b/src/main/java/pw/yumc/YumCore/commands/annotation/Limit.java new file mode 100644 index 0000000..f03691f --- /dev/null +++ b/src/main/java/pw/yumc/YumCore/commands/annotation/Limit.java @@ -0,0 +1,26 @@ +package pw.yumc.YumCore.commands.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 参数限制 + * + * @since 2016年7月23日 上午9:00:27 + * @author 喵♂呜 + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface Limit { + /** + * @return 最大长度(最大值) + */ + int max() default 255; + + /** + * @return 最小长度(或最小值) + */ + int min(); +} \ No newline at end of file diff --git a/src/main/java/pw/yumc/YumCore/commands/info/CommandInfo.java b/src/main/java/pw/yumc/YumCore/commands/info/CommandInfo.java index db75e1b..3cc5a0b 100644 --- a/src/main/java/pw/yumc/YumCore/commands/info/CommandInfo.java +++ b/src/main/java/pw/yumc/YumCore/commands/info/CommandInfo.java @@ -7,7 +7,6 @@ import java.util.List; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.P; @@ -28,8 +27,6 @@ import pw.yumc.YumCore.commands.exception.CommandException; */ public class CommandInfo { public static final CommandInfo Unknow = new CommandInfo(); - private static final String onlyPlayer = "§c控制台无法使用此命令(§4请在游戏内执行§c)!"; - private static final String onlyConsole = "§c玩家无法使用此命令(§4请使用控制台执行§c)!"; private static final String onlyExecutor = "§c当前命令仅允许 §b%s §c执行!"; private static final String losePerm = "§c你需要有 %s 的权限才能执行此命令!"; private static final String cmdErr = "§6错误原因: §4命令参数不正确!"; @@ -225,17 +222,6 @@ public class CommandInfo { Log.toSender(sender, String.format(onlyExecutor, executorStr)); return false; } - if (sender instanceof Player) { - if (command.onlyConsole()) { - Log.toSender(sender, onlyConsole); - return false; - } - } else { - if (command.onlyPlayer()) { - Log.toSender(sender, onlyPlayer); - return false; - } - } return true; }