From 02bbe0892b1659c73eb6f7df001028b776a2e3d7 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Mon, 25 Jul 2016 15:45:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B0=83=E6=95=B4=E6=B3=A8=E8=A7=A3?= =?UTF-8?q?=E5=8F=82=E6=95=B0=20=E6=B7=BB=E5=8A=A0doc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../pw/yumc/YumCore/commands/CommandHelp.java | 242 +++++++++--------- .../pw/yumc/YumCore/commands/CommandInfo.java | 10 +- .../yumc/YumCore/commands/annotation/Cmd.java | 102 ++++---- .../YumCore/commands/annotation/Help.java | 4 +- .../YumCore/commands/annotation/Sort.java | 3 +- 5 files changed, 186 insertions(+), 175 deletions(-) diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java b/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java index 55c80e5..eb7f081 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandHelp.java @@ -1,121 +1,121 @@ -package pw.yumc.YumCore.commands; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.plugin.Plugin; - -import pw.yumc.YumCore.bukkit.P; -import pw.yumc.YumCore.commands.annotation.Help; - -/** - * 命令帮助生成类 - * - * @since 2016年7月23日 上午10:12:32 - * @author 喵♂呜 - */ -public class CommandHelp { - /** - * 插件实例 - */ - Plugin plugin = P.instance; - /** - * 消息配置 - */ - String prefix = String.format("§6[§b%s§6] ", P.instance.getName()); - String commandNotFound = prefix + "§c当前插件未注册任何子命令!"; - String pageNotFound = prefix + "§c不存在的帮助页面 §b请输入 /%s help §e1-%s"; - String helpTitle = String.format("§6========= %s §6插件帮助列表=========", prefix); - String helpBody = "§6/%1$s §a%2$s §e%3$s §6- §b%4$s"; - String helpFooter = "§6查看更多的帮助页面 §b请输入 /%s help §e1-%s"; - /** - * 已排序的命令列表 - */ - List cmdlist; - /** - * 帮助页面数量 - */ - private final int HELPPAGECOUNT; - /** - * 帮助页面每页行数 - */ - private final int LINES_PER_PAGE = 7; - /** - * 帮助列表缓存 - */ - private final Map cacheHelp = new HashMap<>(); - - public CommandHelp(final Collection list) { - cmdlist = new LinkedList<>(list); - Collections.sort(cmdlist, new CommandComparator()); - this.HELPPAGECOUNT = (int) Math.ceil((double) cmdlist.size() / LINES_PER_PAGE); - } - - /** - * 发送帮助 - * - * @param ca - * 命令参数 - */ - public void send(final CommandSender sender, final Command command, final String label, final String[] args) { - if (this.HELPPAGECOUNT == 0) { - sender.sendMessage(commandNotFound); - return; - } - int page = 1; - try { - page = Integer.parseInt(args[1]); - page = page == 0 ? 1 : page; - } catch (final Exception e) { - } - final String helpkey = label + page; - if (!cacheHelp.containsKey(helpkey)) { - final List helpList = new ArrayList<>(); - if (page > this.HELPPAGECOUNT || page < 1) { - // 帮助页面不存在 - helpList.add(String.format(commandNotFound, HELPPAGECOUNT)); - } else { - // 帮助标题 - helpList.add(helpTitle); - final int start = this.LINES_PER_PAGE * (page - 1); - final int end = start + this.LINES_PER_PAGE; - for (int i = start; i < end; i++) { - if (this.cmdlist.size() > i) { - final CommandInfo ci = cmdlist.get(i); - final String aliases = Arrays.toString(ci.getCommand().aliases()); - final String cmd = ci.getName() + (aliases.length() == 2 ? "" : "§7" + aliases); - final Help help = ci.getHelp(); - // 帮助列表 - helpList.add(String.format(helpBody, label, cmd, help.possibleArguments(), help.description())); - } - } - } - // 帮助结尾 - helpList.add(String.format(helpFooter, label, HELPPAGECOUNT)); - cacheHelp.put(helpkey, helpList.toArray(new String[0])); - } - sender.sendMessage(cacheHelp.get(helpkey)); - } - - /** - * 命令排序比较器 - * - * @since 2016年7月23日 下午4:17:18 - * @author 喵♂呜 - */ - static class CommandComparator implements Comparator { - @Override - public int compare(final CommandInfo o1, final CommandInfo o2) { - return Integer.valueOf(o1.getSort()).compareTo(Integer.valueOf(o2.getSort())); - } - } -} +package pw.yumc.YumCore.commands; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.plugin.Plugin; + +import pw.yumc.YumCore.bukkit.P; +import pw.yumc.YumCore.commands.annotation.Help; + +/** + * 命令帮助生成类 + * + * @since 2016年7月23日 上午10:12:32 + * @author 喵♂呜 + */ +public class CommandHelp { + /** + * 插件实例 + */ + Plugin plugin = P.instance; + /** + * 消息配置 + */ + String prefix = String.format("§6[§b%s§6] ", P.instance.getName()); + String commandNotFound = prefix + "§c当前插件未注册任何子命令!"; + String pageNotFound = prefix + "§c不存在的帮助页面 §b请输入 /%s help §e1-%s"; + String helpTitle = String.format("§6========= %s §6插件帮助列表=========", prefix); + String helpBody = "§6/%1$s §a%2$s §e%3$s §6- §b%4$s"; + String helpFooter = "§6查看更多的帮助页面 §b请输入 /%s help §e1-%s"; + /** + * 已排序的命令列表 + */ + List cmdlist; + /** + * 帮助页面数量 + */ + private final int HELPPAGECOUNT; + /** + * 帮助页面每页行数 + */ + private final int LINES_PER_PAGE = 7; + /** + * 帮助列表缓存 + */ + private final Map cacheHelp = new HashMap<>(); + + public CommandHelp(final Collection list) { + cmdlist = new LinkedList<>(list); + Collections.sort(cmdlist, new CommandComparator()); + this.HELPPAGECOUNT = (int) Math.ceil((double) cmdlist.size() / LINES_PER_PAGE); + } + + /** + * 发送帮助 + * + * @param ca + * 命令参数 + */ + public void send(final CommandSender sender, final Command command, final String label, final String[] args) { + if (this.HELPPAGECOUNT == 0) { + sender.sendMessage(commandNotFound); + return; + } + int page = 1; + try { + page = Integer.parseInt(args[1]); + page = page == 0 ? 1 : page; + } catch (final Exception e) { + } + final String helpkey = label + page; + if (!cacheHelp.containsKey(helpkey)) { + final List helpList = new ArrayList<>(); + if (page > this.HELPPAGECOUNT || page < 1) { + // 帮助页面不存在 + helpList.add(String.format(commandNotFound, HELPPAGECOUNT)); + } else { + // 帮助标题 + helpList.add(helpTitle); + final int start = this.LINES_PER_PAGE * (page - 1); + final int end = start + this.LINES_PER_PAGE; + for (int i = start; i < end; i++) { + if (this.cmdlist.size() > i) { + final CommandInfo ci = cmdlist.get(i); + final String aliases = Arrays.toString(ci.getCommand().aliases()); + final String cmd = ci.getName() + (aliases.length() == 2 ? "" : "§7" + aliases); + final Help help = ci.getHelp(); + // 帮助列表 + helpList.add(String.format(helpBody, label, cmd, help.possibleArguments(), help.value())); + } + } + } + // 帮助结尾 + helpList.add(String.format(helpFooter, label, HELPPAGECOUNT)); + cacheHelp.put(helpkey, helpList.toArray(new String[0])); + } + sender.sendMessage(cacheHelp.get(helpkey)); + } + + /** + * 命令排序比较器 + * + * @since 2016年7月23日 下午4:17:18 + * @author 喵♂呜 + */ + static class CommandComparator implements Comparator { + @Override + public int compare(final CommandInfo o1, final CommandInfo o2) { + return Integer.valueOf(o1.getSort()).compareTo(Integer.valueOf(o2.getSort())); + } + } +} diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandInfo.java b/src/main/java/pw/yumc/YumCore/commands/CommandInfo.java index 77dc142..803442b 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandInfo.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandInfo.java @@ -43,7 +43,7 @@ public class CommandInfo { public CommandInfo(final Method method, final Object origin, final Cmd command, final Help help, final boolean async, final int sort) { this.method = method; this.origin = origin; - this.name = "".equals(command.name()) ? method.getName().toLowerCase() : command.name(); + this.name = "".equals(command.value()) ? method.getName().toLowerCase() : command.value(); this.aliases = Arrays.asList(command.aliases()); this.command = command; this.help = help; @@ -77,7 +77,7 @@ public class CommandInfo { final Help help = method.getAnnotation(Help.class); final Async async = method.getAnnotation(Async.class); final Sort sort = method.getAnnotation(Sort.class); - return new CommandInfo(method, origin, command, help != null ? help : Help.DEFAULT, async != null, sort != null ? sort.sort() : 50); + return new CommandInfo(method, origin, command, help != null ? help : Help.DEFAULT, async != null, sort != null ? sort.value() : 50); } return null; } @@ -184,11 +184,11 @@ public class CommandInfo { if (cmdArgs.getArgs().length < command.minimumArguments()) { Log.toSender(sender, cmdErr); Log.toSender(sender, String.format(cmdUse, cmdArgs.getAlias(), getName(), help.possibleArguments())); - Log.toSender(sender, String.format(cmdDes, help.description())); + Log.toSender(sender, String.format(cmdDes, help.value())); } - if (sender instanceof Player && command.onlyConsoleExecutable()) { + if (sender instanceof Player && command.onlyConsole()) { Log.toSender(sender, onlyConsole); - } else if (command.onlyPlayerExecutable()) { + } else if (command.onlyPlayer()) { Log.toSender(sender, onlyPlayer); } final String perm = command.permission(); 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 f84d6ce..44fc472 100644 --- a/src/main/java/pw/yumc/YumCore/commands/annotation/Cmd.java +++ b/src/main/java/pw/yumc/YumCore/commands/annotation/Cmd.java @@ -1,46 +1,56 @@ -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日 上午8:59:05 - * @author 喵♂呜 - */ -@Target(ElementType.METHOD) -@Retention(RetentionPolicy.RUNTIME) -public @interface Cmd { - /** - * @return 命令别名 - */ - String[] aliases() default ""; - - /** - * @return 命令最小参数 - */ - int minimumArguments() default 0; - - /** - * @return 命令名称 - */ - String name() default ""; - - /** - * @return 只允许控制台执行 - */ - boolean onlyConsoleExecutable() default false; - - /** - * @return 只允许玩家执行 - */ - boolean onlyPlayerExecutable() default false; - - /** - * @return 当前命令权限 - */ - String permission() default ""; -} +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; + +/** + * 命令注解 + * + *
+ * 参数名称            描述       默认值
+ * name              命令名称     方法名称
+ * aliases           命令别名
+ * minimumArguments  最小参数     默认0
+ * permission        权限
+ * onlyPlayer        只允许玩家   false
+ * onlyConsole       只允许控制台 false
+ * 
+ * + * @since 2016年7月23日 上午8:59:05 + * @author 喵♂呜 + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface Cmd { + /** + * @return 命令别名 + */ + String[] aliases() default ""; + + /** + * @return 命令最小参数 + */ + int minimumArguments() default 0; + + /** + * @return 命令名称 + */ + String value() default ""; + + /** + * @return 只允许控制台执行 + */ + boolean onlyConsole() default false; + + /** + * @return 只允许玩家执行 + */ + boolean onlyPlayer() default false; + + /** + * @return 当前命令权限 + */ + String permission() default ""; +} diff --git a/src/main/java/pw/yumc/YumCore/commands/annotation/Help.java b/src/main/java/pw/yumc/YumCore/commands/annotation/Help.java index d2c7a2a..0668f28 100644 --- a/src/main/java/pw/yumc/YumCore/commands/annotation/Help.java +++ b/src/main/java/pw/yumc/YumCore/commands/annotation/Help.java @@ -22,7 +22,7 @@ public @interface Help { } @Override - public String description() { + public String value() { return "没写帮助信息"; } @@ -35,7 +35,7 @@ public @interface Help { /** * @return 命令描述 */ - String description(); + String value(); /** * @return 当前命令可能需要的参数 diff --git a/src/main/java/pw/yumc/YumCore/commands/annotation/Sort.java b/src/main/java/pw/yumc/YumCore/commands/annotation/Sort.java index 22f88c3..624f0a9 100644 --- a/src/main/java/pw/yumc/YumCore/commands/annotation/Sort.java +++ b/src/main/java/pw/yumc/YumCore/commands/annotation/Sort.java @@ -1,6 +1,7 @@ package pw.yumc.YumCore.commands.annotation; /** + * 命令排序注解 * * @since 2016年7月23日 上午9:04:56 * @author 喵♂呜 @@ -9,5 +10,5 @@ public @interface Sort { /** * @return 命令排序 */ - int sort(); + int value(); }