From 9ac40f8756378fa153dd210ed5d706b1fa11c233 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Thu, 13 Oct 2016 20:12:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84Material=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E5=92=8C=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../yumc/YumCore/commands/CommandParse.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandParse.java b/src/main/java/pw/yumc/YumCore/commands/CommandParse.java index 338f7bf..95f4d25 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandParse.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandParse.java @@ -1,25 +1,19 @@ 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.CommandException; 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 { private static Map allparses = new HashMap<>(); + static { new IntegerParse(); new LongParse(); @@ -35,6 +30,7 @@ public class CommandParse { new PlayerParse(); new StringParse(); } + private List parse = new LinkedList<>(); private boolean isMain; @@ -168,7 +164,7 @@ public class CommandParse { try { return Material.valueOf(arg); } 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); } + public void throwException(String str, Object... objects) { + throw new CommandParseException(String.format(str, objects)); + } + 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() { allparses.put(String.class, this); 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) { throwRange("长度必须在 %s 和 %s 之间!"); } + if (options != null && !options.contains(arg)) { + throwException("参数 %s 不是一个有效的选项 有效值为 %s", arg, options); + } return arg; } }