mirror of
https://e.coding.net/circlecloud/YumCore.git
synced 2024-11-25 02:18:50 +00:00
fix: 修复命令解析错误的问题 修复Help注解初始化的问题
Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
parent
399a0b4668
commit
7711cb3d74
@ -105,7 +105,8 @@ public class CommandManager implements TabExecutor {
|
||||
/**
|
||||
* 命令管理器
|
||||
*
|
||||
* @param name 注册的命令
|
||||
* @param name
|
||||
* 注册的命令
|
||||
*/
|
||||
public CommandManager(String name) {
|
||||
cmd = plugin.getCommand(name);
|
||||
@ -126,8 +127,10 @@ public class CommandManager implements TabExecutor {
|
||||
/**
|
||||
* 命令管理器
|
||||
*
|
||||
* @param name 注册的命令
|
||||
* @param executor 命令执行类
|
||||
* @param name
|
||||
* 注册的命令
|
||||
* @param executor
|
||||
* 命令执行类
|
||||
*/
|
||||
public CommandManager(String name, CommandExecutor... executor) {
|
||||
this(name);
|
||||
@ -154,13 +157,12 @@ public class CommandManager implements TabExecutor {
|
||||
List<String> completions = new ArrayList<>();
|
||||
String token = args[args.length - 1];
|
||||
if (args.length == 1) {
|
||||
StringUtil.copyPartialMatches(args[0], cmdNameCache, completions);
|
||||
} else if (args.length >= 2) {
|
||||
for (CommandTabInfo tab : tabs) {
|
||||
StringUtil.copyPartialMatches(token, tab.execute(sender, command, token, args), completions);
|
||||
}
|
||||
StringUtil.copyPartialMatches(token, getPlayerTabComplete(sender, command, alias, args), completions);
|
||||
StringUtil.copyPartialMatches(token, cmdNameCache, completions);
|
||||
}
|
||||
for (CommandTabInfo tab : tabs) {
|
||||
StringUtil.copyPartialMatches(token, tab.execute(sender, command, token, args), completions);
|
||||
}
|
||||
StringUtil.copyPartialMatches(token, getPlayerTabComplete(sender, command, alias, args), completions);
|
||||
Collections.sort(completions, String.CASE_INSENSITIVE_ORDER);
|
||||
return completions;
|
||||
}
|
||||
@ -168,7 +170,8 @@ public class CommandManager implements TabExecutor {
|
||||
/**
|
||||
* 通过注解读取命令并注册
|
||||
*
|
||||
* @param clazzs 子命令处理类
|
||||
* @param clazzs
|
||||
* 子命令处理类
|
||||
*/
|
||||
public void register(CommandExecutor... clazzs) {
|
||||
for (CommandExecutor clazz : clazzs) {
|
||||
@ -187,7 +190,8 @@ public class CommandManager implements TabExecutor {
|
||||
/**
|
||||
* 设置帮助解析器
|
||||
*
|
||||
* @param helpParse 帮助解析器
|
||||
* @param helpParse
|
||||
* 帮助解析器
|
||||
*/
|
||||
public void setHelpParse(CommandHelpParse helpParse) {
|
||||
help.setHelpParse(helpParse);
|
||||
@ -208,7 +212,8 @@ public class CommandManager implements TabExecutor {
|
||||
/**
|
||||
* 检查缓存并获得命令
|
||||
*
|
||||
* @param subcmd 子命令
|
||||
* @param subcmd
|
||||
* 子命令
|
||||
* @return 命令信息
|
||||
*/
|
||||
private CommandInfo getByCache(String subcmd) {
|
||||
@ -229,10 +234,14 @@ public class CommandManager implements TabExecutor {
|
||||
/**
|
||||
* 获取玩家命令补全
|
||||
*
|
||||
* @param sender 命令发送者
|
||||
* @param command 命令
|
||||
* @param alias 别名
|
||||
* @param args 数组
|
||||
* @param sender
|
||||
* 命令发送者
|
||||
* @param command
|
||||
* 命令
|
||||
* @param alias
|
||||
* 别名
|
||||
* @param args
|
||||
* 数组
|
||||
* @return 在线玩家数组
|
||||
*/
|
||||
private List<String> getPlayerTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
||||
@ -251,8 +260,10 @@ public class CommandManager implements TabExecutor {
|
||||
/**
|
||||
* 转移数组
|
||||
*
|
||||
* @param args 原数组
|
||||
* @param start 数组开始位置
|
||||
* @param args
|
||||
* 原数组
|
||||
* @param start
|
||||
* 数组开始位置
|
||||
* @return 转移后的数组字符串
|
||||
*/
|
||||
private String[] moveStrings(String[] args, int start) {
|
||||
@ -264,23 +275,29 @@ public class CommandManager implements TabExecutor {
|
||||
/**
|
||||
* 注册命令
|
||||
*
|
||||
* @param method 方法
|
||||
* @param clazz 调用对象
|
||||
* @param method
|
||||
* 方法
|
||||
* @param clazz
|
||||
* 调用对象
|
||||
* @return 是否成功
|
||||
*/
|
||||
private boolean registerCommand(Method method, CommandExecutor clazz) {
|
||||
CommandInfo ci = CommandInfo.parse(method, clazz);
|
||||
if (ci != null) {
|
||||
Class<?>[] params = method.getParameterTypes();
|
||||
Class[] params = method.getParameterTypes();
|
||||
Log.d("命令 %s 参数类型: %s", ci.getName(), Arrays.toString(params));
|
||||
if (params.length > 0 && params[0].isAssignableFrom(CommandSender.class)) {
|
||||
if (method.getReturnType() == boolean.class) {
|
||||
defCmd = ci;
|
||||
} else {
|
||||
cmds.add(ci);
|
||||
cmdCache.put(ci.getName(), ci);
|
||||
if (params.length > 0) {
|
||||
try {
|
||||
Class<? extends CommandSender> sender = params[0];
|
||||
if (method.getReturnType() == boolean.class) {
|
||||
defCmd = ci;
|
||||
} else {
|
||||
cmds.add(ci);
|
||||
cmdCache.put(ci.getName(), ci);
|
||||
}
|
||||
return true;
|
||||
} catch (ClassCastException ignored) {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Log.warning(String.format(argumentTypeError, method.getName(), clazz.getClass().getName()));
|
||||
}
|
||||
@ -290,8 +307,10 @@ public class CommandManager implements TabExecutor {
|
||||
/**
|
||||
* 注册Tab补全
|
||||
*
|
||||
* @param method 方法
|
||||
* @param clazz 调用对象
|
||||
* @param method
|
||||
* 方法
|
||||
* @param clazz
|
||||
* 调用对象
|
||||
* @return 是否成功
|
||||
*/
|
||||
private boolean registerTab(Method method, CommandExecutor clazz) {
|
||||
|
@ -1,19 +1,24 @@
|
||||
package pw.yumc.YumCore.commands;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import pw.yumc.YumCore.bukkit.Log;
|
||||
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.CommandParseException;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 命令参数解析
|
||||
*
|
||||
@ -37,12 +42,10 @@ public class CommandParse {
|
||||
|
||||
public CommandParse(Class[] classes, Annotation[][] annons, boolean isMain) {
|
||||
this.isMain = isMain;
|
||||
for (int i = 0; i < classes.length; i++) {
|
||||
// 第一个参数实现了CommandSender忽略
|
||||
for (int i = 1; i < classes.length; i++) {
|
||||
Class clazz = classes[i];
|
||||
Annotation[] annotations = annons[i];
|
||||
if (clazz.isAssignableFrom(CommandSender.class)) {
|
||||
continue;
|
||||
}
|
||||
Parse parse = allparses.get(clazz);
|
||||
if (parse == null) {
|
||||
if (!clazz.isEnum()) {
|
||||
@ -72,10 +75,10 @@ public class CommandParse {
|
||||
String param = i < args.length ? args[i] : p.def;
|
||||
pobjs.add(param == null ? null : p.parse(cmdArgs.getSender(), param));
|
||||
} catch (Exception e) {
|
||||
Log.debug(e);
|
||||
throw new CommandParseException(String.format("第 %s 个参数 ", isMain ? 1 : 2 + i) + e.getMessage());
|
||||
}
|
||||
}
|
||||
Log.d("解析参数: %s => %s", Arrays.toString(args), pobjs);
|
||||
return pobjs.toArray();
|
||||
}
|
||||
|
||||
@ -205,14 +208,14 @@ public class CommandParse {
|
||||
return this;
|
||||
}
|
||||
|
||||
public void throwRange() {
|
||||
throwRange(null);
|
||||
}
|
||||
|
||||
public void throwException(String str, Object... objects) {
|
||||
throw new CommandParseException(String.format(str, objects));
|
||||
}
|
||||
|
||||
public void throwRange() {
|
||||
throwRange(null);
|
||||
}
|
||||
|
||||
public void throwRange(String str) {
|
||||
throw new CommandParseException(String.format(str == null ? "范围必须在 %s 到 %s 之间!" : str, min, max));
|
||||
}
|
||||
|
@ -15,23 +15,6 @@ import java.lang.annotation.Target;
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Help {
|
||||
public static Help DEFAULT = new Help() {
|
||||
@Override
|
||||
public Class<? extends Annotation> annotationType() {
|
||||
return Help.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String possibleArguments() {
|
||||
return "这家伙很懒";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String value() {
|
||||
return "没写帮助信息";
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @return 当前命令可能需要的参数
|
||||
*/
|
||||
|
@ -1,5 +1,6 @@
|
||||
package pw.yumc.YumCore.commands.info;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
@ -24,8 +25,8 @@ import pw.yumc.YumCore.commands.exception.CommandParseException;
|
||||
/**
|
||||
* 命令信息存储类
|
||||
*
|
||||
* @since 2016年7月23日 上午9:56:42
|
||||
* @author 喵♂呜
|
||||
* @since 2016年7月23日 上午9:56:42
|
||||
*/
|
||||
public class CommandInfo {
|
||||
public static CommandInfo Unknow = new CommandInfo();
|
||||
@ -35,6 +36,22 @@ public class CommandInfo {
|
||||
private static String cmdErr = "§6错误原因: §4命令参数不正确!";
|
||||
private static String cmdUse = "§6使用方法: §e/%s %s %s";
|
||||
private static String cmdDes = "§6命令描述: §3%s";
|
||||
private static Help defHelp = new Help() {
|
||||
@Override
|
||||
public Class<? extends Annotation> annotationType() {
|
||||
return Help.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String possibleArguments() {
|
||||
return "这家伙很懒";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String value() {
|
||||
return "没写帮助信息";
|
||||
}
|
||||
};
|
||||
private Object origin;
|
||||
private Method method;
|
||||
private String name;
|
||||
@ -55,7 +72,7 @@ public class CommandInfo {
|
||||
this.executors = Arrays.asList(command.executor());
|
||||
this.executorStr = eS(executors);
|
||||
this.command = command;
|
||||
this.help = help;
|
||||
this.help = help != null ? help : defHelp;
|
||||
this.async = async;
|
||||
this.sort = sort;
|
||||
this.parse = parse;
|
||||
@ -78,10 +95,8 @@ public class CommandInfo {
|
||||
/**
|
||||
* 解析CommandInfo
|
||||
*
|
||||
* @param method
|
||||
* 方法
|
||||
* @param origin
|
||||
* 源对象
|
||||
* @param method 方法
|
||||
* @param origin 源对象
|
||||
* @return {@link CommandInfo}
|
||||
*/
|
||||
public static CommandInfo parse(Method method, Object origin) {
|
||||
@ -91,7 +106,7 @@ public class CommandInfo {
|
||||
Async async = method.getAnnotation(Async.class);
|
||||
Sort sort = method.getAnnotation(Sort.class);
|
||||
CommandParse cp = CommandParse.get(method);
|
||||
return new CommandInfo(method, origin, command, help != null ? help : Help.DEFAULT, async != null, sort != null ? sort.value() : 50, cp);
|
||||
return new CommandInfo(method, origin, command, help, async != null, sort != null ? sort.value() : 50, cp);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -110,8 +125,7 @@ public class CommandInfo {
|
||||
/**
|
||||
* 执行命令
|
||||
*
|
||||
* @param cmdArgs
|
||||
* 命令参数
|
||||
* @param cmdArgs 命令参数
|
||||
* @return 是否执行成功
|
||||
*/
|
||||
public boolean execute(final CommandArgument cmdArgs) {
|
||||
@ -183,8 +197,7 @@ public class CommandInfo {
|
||||
/**
|
||||
* 验证命令是否匹配
|
||||
*
|
||||
* @param cmd
|
||||
* 需验证命令
|
||||
* @param cmd 需验证命令
|
||||
* @return 是否匹配
|
||||
*/
|
||||
public boolean isValid(String cmd) {
|
||||
@ -194,8 +207,7 @@ public class CommandInfo {
|
||||
/**
|
||||
* 检查命令
|
||||
*
|
||||
* @param info
|
||||
* 命令信息
|
||||
* @param cmdArgs 命令信息
|
||||
* @return 是否验证通过
|
||||
*/
|
||||
private boolean check(CommandArgument cmdArgs) {
|
||||
@ -206,8 +218,8 @@ public class CommandInfo {
|
||||
private boolean checkArgs(CommandSender sender, CommandArgument cmdArgs) {
|
||||
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.value()));
|
||||
Log.toSender(sender, cmdUse, cmdArgs.getAlias(), getName(), help.possibleArguments());
|
||||
Log.toSender(sender, cmdDes, help.value());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@ -215,8 +227,8 @@ public class CommandInfo {
|
||||
|
||||
private boolean checkPerm(CommandSender sender) {
|
||||
String perm = command.permission();
|
||||
if (perm != null && !"".equals(perm) && !sender.hasPermission(perm)) {
|
||||
Log.toSender(sender, String.format(losePerm, perm));
|
||||
if (!"".equals(perm) && !sender.hasPermission(perm)) {
|
||||
Log.toSender(sender, losePerm, perm);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@ -224,7 +236,7 @@ public class CommandInfo {
|
||||
|
||||
private boolean checkSender(CommandSender sender) {
|
||||
if (!executors.contains(Executor.ALL) && !executors.contains(Executor.valueOf(sender))) {
|
||||
Log.toSender(sender, String.format(onlyExecutor, executorStr));
|
||||
Log.toSender(sender, onlyExecutor, executorStr);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
Loading…
Reference in New Issue
Block a user