mirror of
https://e.coding.net/circlecloud/YumCore.git
synced 2024-11-21 01:38:51 +00:00
feat: 调整注解参数 添加doc
Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
parent
8f9597bcce
commit
02bbe0892b
@ -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<CommandInfo> cmdlist;
|
||||
/**
|
||||
* 帮助页面数量
|
||||
*/
|
||||
private final int HELPPAGECOUNT;
|
||||
/**
|
||||
* 帮助页面每页行数
|
||||
*/
|
||||
private final int LINES_PER_PAGE = 7;
|
||||
/**
|
||||
* 帮助列表缓存
|
||||
*/
|
||||
private final Map<String, String[]> cacheHelp = new HashMap<>();
|
||||
|
||||
public CommandHelp(final Collection<? extends CommandInfo> 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<String> 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<CommandInfo> {
|
||||
@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<CommandInfo> cmdlist;
|
||||
/**
|
||||
* 帮助页面数量
|
||||
*/
|
||||
private final int HELPPAGECOUNT;
|
||||
/**
|
||||
* 帮助页面每页行数
|
||||
*/
|
||||
private final int LINES_PER_PAGE = 7;
|
||||
/**
|
||||
* 帮助列表缓存
|
||||
*/
|
||||
private final Map<String, String[]> cacheHelp = new HashMap<>();
|
||||
|
||||
public CommandHelp(final Collection<? extends CommandInfo> 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<String> 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<CommandInfo> {
|
||||
@Override
|
||||
public int compare(final CommandInfo o1, final CommandInfo o2) {
|
||||
return Integer.valueOf(o1.getSort()).compareTo(Integer.valueOf(o2.getSort()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
* 命令注解
|
||||
*
|
||||
* <pre>
|
||||
* 参数名称 描述 默认值
|
||||
* name 命令名称 方法名称
|
||||
* aliases 命令别名
|
||||
* minimumArguments 最小参数 默认0
|
||||
* permission 权限
|
||||
* onlyPlayer 只允许玩家 false
|
||||
* onlyConsole 只允许控制台 false
|
||||
* </pre>
|
||||
*
|
||||
* @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 "";
|
||||
}
|
||||
|
@ -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 当前命令可能需要的参数
|
||||
|
@ -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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user