feat: 调整注解参数 添加doc

Signed-off-by: 502647092 <admin@yumc.pw>
merge/1/MERGE
502647092 2016-07-25 15:45:02 +08:00
parent 8f9597bcce
commit 02bbe0892b
5 changed files with 186 additions and 175 deletions

View File

@ -1,121 +1,121 @@
package pw.yumc.YumCore.commands; package pw.yumc.YumCore.commands;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.commands.annotation.Help; import pw.yumc.YumCore.commands.annotation.Help;
/** /**
* *
* *
* @since 2016723 10:12:32 * @since 2016723 10:12:32
* @author * @author
*/ */
public class CommandHelp { public class CommandHelp {
/** /**
* *
*/ */
Plugin plugin = P.instance; Plugin plugin = P.instance;
/** /**
* *
*/ */
String prefix = String.format("§6[§b%s§6] ", P.instance.getName()); String prefix = String.format("§6[§b%s§6] ", P.instance.getName());
String commandNotFound = prefix + "§c当前插件未注册任何子命令!"; String commandNotFound = prefix + "§c当前插件未注册任何子命令!";
String pageNotFound = prefix + "§c不存在的帮助页面 §b请输入 /%s help §e1-%s"; String pageNotFound = prefix + "§c不存在的帮助页面 §b请输入 /%s help §e1-%s";
String helpTitle = String.format("§6========= %s §6插件帮助列表=========", prefix); String helpTitle = String.format("§6========= %s §6插件帮助列表=========", prefix);
String helpBody = "§6/%1$s §a%2$s §e%3$s §6- §b%4$s"; String helpBody = "§6/%1$s §a%2$s §e%3$s §6- §b%4$s";
String helpFooter = "§6查看更多的帮助页面 §b请输入 /%s help §e1-%s"; String helpFooter = "§6查看更多的帮助页面 §b请输入 /%s help §e1-%s";
/** /**
* *
*/ */
List<CommandInfo> cmdlist; List<CommandInfo> cmdlist;
/** /**
* *
*/ */
private final int HELPPAGECOUNT; private final int HELPPAGECOUNT;
/** /**
* *
*/ */
private final int LINES_PER_PAGE = 7; private final int LINES_PER_PAGE = 7;
/** /**
* *
*/ */
private final Map<String, String[]> cacheHelp = new HashMap<>(); private final Map<String, String[]> cacheHelp = new HashMap<>();
public CommandHelp(final Collection<? extends CommandInfo> list) { public CommandHelp(final Collection<? extends CommandInfo> list) {
cmdlist = new LinkedList<>(list); cmdlist = new LinkedList<>(list);
Collections.sort(cmdlist, new CommandComparator()); Collections.sort(cmdlist, new CommandComparator());
this.HELPPAGECOUNT = (int) Math.ceil((double) cmdlist.size() / LINES_PER_PAGE); this.HELPPAGECOUNT = (int) Math.ceil((double) cmdlist.size() / LINES_PER_PAGE);
} }
/** /**
* *
* *
* @param ca * @param ca
* *
*/ */
public void send(final CommandSender sender, final Command command, final String label, final String[] args) { public void send(final CommandSender sender, final Command command, final String label, final String[] args) {
if (this.HELPPAGECOUNT == 0) { if (this.HELPPAGECOUNT == 0) {
sender.sendMessage(commandNotFound); sender.sendMessage(commandNotFound);
return; return;
} }
int page = 1; int page = 1;
try { try {
page = Integer.parseInt(args[1]); page = Integer.parseInt(args[1]);
page = page == 0 ? 1 : page; page = page == 0 ? 1 : page;
} catch (final Exception e) { } catch (final Exception e) {
} }
final String helpkey = label + page; final String helpkey = label + page;
if (!cacheHelp.containsKey(helpkey)) { if (!cacheHelp.containsKey(helpkey)) {
final List<String> helpList = new ArrayList<>(); final List<String> helpList = new ArrayList<>();
if (page > this.HELPPAGECOUNT || page < 1) { if (page > this.HELPPAGECOUNT || page < 1) {
// 帮助页面不存在 // 帮助页面不存在
helpList.add(String.format(commandNotFound, HELPPAGECOUNT)); helpList.add(String.format(commandNotFound, HELPPAGECOUNT));
} else { } else {
// 帮助标题 // 帮助标题
helpList.add(helpTitle); helpList.add(helpTitle);
final int start = this.LINES_PER_PAGE * (page - 1); final int start = this.LINES_PER_PAGE * (page - 1);
final int end = start + this.LINES_PER_PAGE; final int end = start + this.LINES_PER_PAGE;
for (int i = start; i < end; i++) { for (int i = start; i < end; i++) {
if (this.cmdlist.size() > i) { if (this.cmdlist.size() > i) {
final CommandInfo ci = cmdlist.get(i); final CommandInfo ci = cmdlist.get(i);
final String aliases = Arrays.toString(ci.getCommand().aliases()); final String aliases = Arrays.toString(ci.getCommand().aliases());
final String cmd = ci.getName() + (aliases.length() == 2 ? "" : "§7" + aliases); final String cmd = ci.getName() + (aliases.length() == 2 ? "" : "§7" + aliases);
final Help help = ci.getHelp(); final Help help = ci.getHelp();
// 帮助列表 // 帮助列表
helpList.add(String.format(helpBody, label, cmd, help.possibleArguments(), help.description())); helpList.add(String.format(helpBody, label, cmd, help.possibleArguments(), help.value()));
} }
} }
} }
// 帮助结尾 // 帮助结尾
helpList.add(String.format(helpFooter, label, HELPPAGECOUNT)); helpList.add(String.format(helpFooter, label, HELPPAGECOUNT));
cacheHelp.put(helpkey, helpList.toArray(new String[0])); cacheHelp.put(helpkey, helpList.toArray(new String[0]));
} }
sender.sendMessage(cacheHelp.get(helpkey)); sender.sendMessage(cacheHelp.get(helpkey));
} }
/** /**
* *
* *
* @since 2016723 4:17:18 * @since 2016723 4:17:18
* @author * @author
*/ */
static class CommandComparator implements Comparator<CommandInfo> { static class CommandComparator implements Comparator<CommandInfo> {
@Override @Override
public int compare(final CommandInfo o1, final CommandInfo o2) { public int compare(final CommandInfo o1, final CommandInfo o2) {
return Integer.valueOf(o1.getSort()).compareTo(Integer.valueOf(o2.getSort())); return Integer.valueOf(o1.getSort()).compareTo(Integer.valueOf(o2.getSort()));
} }
} }
} }

View File

@ -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) { public CommandInfo(final Method method, final Object origin, final Cmd command, final Help help, final boolean async, final int sort) {
this.method = method; this.method = method;
this.origin = origin; 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.aliases = Arrays.asList(command.aliases());
this.command = command; this.command = command;
this.help = help; this.help = help;
@ -77,7 +77,7 @@ public class CommandInfo {
final Help help = method.getAnnotation(Help.class); final Help help = method.getAnnotation(Help.class);
final Async async = method.getAnnotation(Async.class); final Async async = method.getAnnotation(Async.class);
final Sort sort = method.getAnnotation(Sort.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; return null;
} }
@ -184,11 +184,11 @@ public class CommandInfo {
if (cmdArgs.getArgs().length < command.minimumArguments()) { if (cmdArgs.getArgs().length < command.minimumArguments()) {
Log.toSender(sender, cmdErr); Log.toSender(sender, cmdErr);
Log.toSender(sender, String.format(cmdUse, cmdArgs.getAlias(), getName(), help.possibleArguments())); 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); Log.toSender(sender, onlyConsole);
} else if (command.onlyPlayerExecutable()) { } else if (command.onlyPlayer()) {
Log.toSender(sender, onlyPlayer); Log.toSender(sender, onlyPlayer);
} }
final String perm = command.permission(); final String perm = command.permission();

View File

@ -1,46 +1,56 @@
package pw.yumc.YumCore.commands.annotation; package pw.yumc.YumCore.commands.annotation;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/** /**
* *
* *
* @since 2016723 8:59:05 * <pre>
* @author *
*/ * name
@Target(ElementType.METHOD) * aliases
@Retention(RetentionPolicy.RUNTIME) * minimumArguments 0
public @interface Cmd { * permission
/** * onlyPlayer false
* @return * onlyConsole false
*/ * </pre>
String[] aliases() default ""; *
* @since 2016723 8:59:05
/** * @author
* @return */
*/ @Target(ElementType.METHOD)
int minimumArguments() default 0; @Retention(RetentionPolicy.RUNTIME)
public @interface Cmd {
/** /**
* @return * @return
*/ */
String name() default ""; String[] aliases() default "";
/** /**
* @return * @return
*/ */
boolean onlyConsoleExecutable() default false; int minimumArguments() default 0;
/** /**
* @return * @return
*/ */
boolean onlyPlayerExecutable() default false; String value() default "";
/** /**
* @return * @return
*/ */
String permission() default ""; boolean onlyConsole() default false;
}
/**
* @return
*/
boolean onlyPlayer() default false;
/**
* @return
*/
String permission() default "";
}

View File

@ -22,7 +22,7 @@ public @interface Help {
} }
@Override @Override
public String description() { public String value() {
return "没写帮助信息"; return "没写帮助信息";
} }
@ -35,7 +35,7 @@ public @interface Help {
/** /**
* @return * @return
*/ */
String description(); String value();
/** /**
* @return * @return

View File

@ -1,6 +1,7 @@
package pw.yumc.YumCore.commands.annotation; package pw.yumc.YumCore.commands.annotation;
/** /**
*
* *
* @since 2016723 9:04:56 * @since 2016723 9:04:56
* @author * @author
@ -9,5 +10,5 @@ public @interface Sort {
/** /**
* @return * @return
*/ */
int sort(); int value();
} }