From 571c27024840dd0f17a8448f22482dbc7168901d Mon Sep 17 00:00:00 2001 From: 502647092 Date: Wed, 26 Oct 2016 20:12:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0=E4=B8=A2=E5=A4=B1?= =?UTF-8?q?=E7=9A=84=E8=A7=A3=E6=9E=90=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../yumc/YumCore/commands/CommandParse.java | 47 +++++++++++++------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandParse.java b/src/main/java/pw/yumc/YumCore/commands/CommandParse.java index f9b37d5..4ee4729 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandParse.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandParse.java @@ -1,25 +1,20 @@ package pw.yumc.YumCore.commands; -import java.io.File; -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.io.File; +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; +import java.util.*; + /** * 命令参数解析 * @@ -31,12 +26,14 @@ public class CommandParse { private boolean isMain; private List parse = new LinkedList<>(); static { - new IntegerParse(); - new LongParse(); new BooleanParse(); + new FileParse(); + new IntegerParse(); + new DoubleParse(); + new LongParse(); + new MaterialParse(); new PlayerParse(); new StringParse(); - new FileParse(); } public CommandParse(Class[] classes, Annotation[][] annons, boolean isMain) { @@ -68,7 +65,7 @@ public class CommandParse { * @return 字符串 */ public static String join(Object[] arr, String split) { - StringBuffer str = new StringBuffer(); + StringBuilder str = new StringBuilder(); for (Object s : arr) { str.append(s.toString()); str.append(split); @@ -144,7 +141,7 @@ public class CommandParse { @Override public File parse(CommandSender sender, String arg) throws CommandParseException { File file = new File(arg); - if (attrs.containsKey("check") && !file.exists()) { throw new CommandParseException("文件 " + arg + "不存在!"); } + if (attrs.containsKey("check") && !file.exists()) { throw new CommandParseException("文件 " + arg + " 不存在!"); } return file; } } @@ -169,6 +166,26 @@ public class CommandParse { } } + public static class DoubleParse extends Parse { + public DoubleParse() { + register(Double.class, this); + register(double.class, this); + } + + @Override + public Double parse(CommandSender sender, String arg) { + try { + double result = Double.parseDouble(arg); + if (min > result || result > max) { + throwRange(); + } + return result; + } catch (NumberFormatException e) { + throw new CommandParseException("必须为数字!", e); + } + } + } + public static class LongParse extends Parse { public LongParse() { register(Long.class, this);