mirror of
https://e.coding.net/circlecloud/YumCore.git
synced 2024-11-22 01:48:50 +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;
|
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 2016年7月23日 上午10:12:32
|
* @since 2016年7月23日 上午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 2016年7月23日 下午4:17:18
|
* @since 2016年7月23日 下午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()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
@ -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 2016年7月23日 上午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 2016年7月23日 上午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 "";
|
||||||
|
}
|
||||||
|
@ -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 当前命令可能需要的参数
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package pw.yumc.YumCore.commands.annotation;
|
package pw.yumc.YumCore.commands.annotation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 命令排序注解
|
||||||
*
|
*
|
||||||
* @since 2016年7月23日 上午9:04:56
|
* @since 2016年7月23日 上午9:04:56
|
||||||
* @author 喵♂呜
|
* @author 喵♂呜
|
||||||
@ -9,5 +10,5 @@ public @interface Sort {
|
|||||||
/**
|
/**
|
||||||
* @return 命令排序
|
* @return 命令排序
|
||||||
*/
|
*/
|
||||||
int sort();
|
int value();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user