1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-22 01:48:50 +00:00

feat: 完善Material解析和字符串限制

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-10-13 20:12:57 +08:00
parent 335cdeeaad
commit 9ac40f8756

View File

@ -1,25 +1,19 @@
package pw.yumc.YumCore.commands; 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.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.commands.annotation.Default; import pw.yumc.YumCore.commands.annotation.Default;
import pw.yumc.YumCore.commands.annotation.KeyValue; import pw.yumc.YumCore.commands.annotation.KeyValue;
import pw.yumc.YumCore.commands.annotation.Limit; import pw.yumc.YumCore.commands.annotation.Limit;
import pw.yumc.YumCore.commands.exception.CommandException;
import pw.yumc.YumCore.commands.exception.CommandParseException; import pw.yumc.YumCore.commands.exception.CommandParseException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.*;
/** /**
* 命令参数解析 * 命令参数解析
* *
@ -28,6 +22,7 @@ import pw.yumc.YumCore.commands.exception.CommandParseException;
*/ */
public class CommandParse { public class CommandParse {
private static Map<Class, Parse> allparses = new HashMap<>(); private static Map<Class, Parse> allparses = new HashMap<>();
static { static {
new IntegerParse(); new IntegerParse();
new LongParse(); new LongParse();
@ -35,6 +30,7 @@ public class CommandParse {
new PlayerParse(); new PlayerParse();
new StringParse(); new StringParse();
} }
private List<Parse> parse = new LinkedList<>(); private List<Parse> parse = new LinkedList<>();
private boolean isMain; private boolean isMain;
@ -168,7 +164,7 @@ public class CommandParse {
try { try {
return Material.valueOf(arg); return Material.valueOf(arg);
} catch (Exception e) { } catch (Exception e) {
throw new CommandParseException("玩家 " + arg + "不存在或不在线!"); throw new CommandParseException(String.format("%s 不是一个有效的Material枚举", arg), e);
} }
} }
} }
@ -213,8 +209,12 @@ public class CommandParse {
throwRange(null); throwRange(null);
} }
public void throwException(String str, Object... objects) {
throw new CommandParseException(String.format(str, objects));
}
public void throwRange(String str) { public void throwRange(String str) {
throw new CommandException(String.format(str == null ? "必须在 %s 到 %s 之间!" : str, min, max)); throw new CommandParseException(String.format(str == null ? "范围必须在 %s 到 %s 之间!" : str, min, max));
} }
} }
@ -239,7 +239,7 @@ public class CommandParse {
public StringParse() { public StringParse() {
allparses.put(String.class, this); allparses.put(String.class, this);
if (attrs.containsKey("option")) { if (attrs.containsKey("option")) {
options = Arrays.asList(attrs.get("option").split(","));
} }
} }
@ -248,6 +248,9 @@ public class CommandParse {
if (min > arg.length() || arg.length() > max) { if (min > arg.length() || arg.length() > max) {
throwRange("长度必须在 %s 和 %s 之间!"); throwRange("长度必须在 %s 和 %s 之间!");
} }
if (options != null && !options.contains(arg)) {
throwException("参数 %s 不是一个有效的选项 有效值为 %s", arg, options);
}
return arg; return arg;
} }
} }