From 46503e2c060abd5bfabf5cde2c6624bfb5b7e39e Mon Sep 17 00:00:00 2001 From: 502647092 Date: Fri, 21 Apr 2017 17:33:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E5=A4=84=E7=90=86=E5=B1=9E=E6=80=A7=E4=B8=A2?= =?UTF-8?q?=E5=A4=B1=E7=9A=84=E9=97=AE=E9=A2=98=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=B8=80=E5=A4=84=E7=A9=BA=E6=8C=87=E9=92=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- src/main/java/pw/yumc/YumCore/bukkit/Log.java | 4 +- .../yumc/YumCore/commands/CommandParse.java | 53 +++++++++---------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/main/java/pw/yumc/YumCore/bukkit/Log.java b/src/main/java/pw/yumc/YumCore/bukkit/Log.java index ec31f3a..8f624f5 100644 --- a/src/main/java/pw/yumc/YumCore/bukkit/Log.java +++ b/src/main/java/pw/yumc/YumCore/bukkit/Log.java @@ -367,7 +367,7 @@ public class Log { public static String osn(List classes) { StringBuilder str = new StringBuilder("["); - classes.forEach(c -> str.append(c.getClass().getSimpleName()).append(", ")); + classes.forEach(c -> str.append(c == null ? null : c.getClass().getSimpleName()).append(", ")); return classes.isEmpty() ? "[]" : str.substring(0, str.length() - 2) + "]"; } @@ -377,7 +377,7 @@ public class Log { public static String csn(List classes) { StringBuilder str = new StringBuilder("["); - classes.forEach(c -> str.append(c.getSimpleName()).append(", ")); + classes.forEach(c -> str.append(c == null ? null : c.getSimpleName()).append(", ")); return classes.isEmpty() ? "[]" : str.substring(0, str.length() - 2) + "]"; } diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandParse.java b/src/main/java/pw/yumc/YumCore/commands/CommandParse.java index 1d95924..e50b899 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandParse.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandParse.java @@ -25,14 +25,14 @@ import pw.yumc.YumCore.commands.exception.ParseException; * @since 2016年10月5日 下午4:02:04 */ public class CommandParse { - private static Map allparses = new HashMap<>(); + private static Map allparses = new HashMap<>(); private static Map primitiveMap = new HashMap<>(); private boolean isMain; - private List parse = new LinkedList<>(); + private List parses = new LinkedList<>(); static { - register(File.class, new FileParse()); - register(Player.class, new PlayerParse()); - register(String.class, new StringParse()); + register(File.class, FileParse.class); + register(Player.class, PlayerParse.class); + register(String.class, StringParse.class); primitiveMap.put("boolean", Boolean.class); primitiveMap.put("byte", Byte.class); primitiveMap.put("char", Character.class); @@ -55,7 +55,10 @@ public class CommandParse { Annotation[] annotations = annons[i]; Parse parse = null; if (allparses.containsKey(clazz)) { - parse = allparses.get(clazz); + try { + parse = (Parse) allparses.get(clazz).newInstance(); + } catch (InstantiationException | IllegalAccessException | NullPointerException ignored) { + } } else { try { parse = new ValueOfParse(clazz, clazz.getDeclaredMethod("valueOf", String.class)); @@ -63,9 +66,9 @@ public class CommandParse { } } if (parse == null) { throw new ParseException(String.format("存在无法解析的参数类型 %s", clazz.getName())); } - this.parse.add(parse.clone().parseAnnotation(annotations).handleAttrs()); + this.parses.add(parse.parseAnnotation(annotations).handleAttrs()); } - Log.d("命令解析器 %s", Log.osn(parse)); + Log.d("命令解析器 %s", Log.osn(parses)); } public static CommandParse get(Method method) { @@ -90,23 +93,24 @@ public class CommandParse { return str.length() > split.length() ? str.toString().substring(0, str.length() - split.length()) : str.toString(); } - public static void register(Class clazz, Parse parse) { + public static void register(Class clazz, Class parse) { allparses.put(clazz, parse); } public Object[] parse(CommandSender sender, String label, String[] args) { List pobjs = new LinkedList<>(); pobjs.add(sender); - for (int i = 0; i < parse.size(); i++) { + for (int i = 0; i < parses.size(); i++) { try { - Parse p = parse.get(i); - String param = i < args.length ? args[i] : p.def; + Parse p = parses.get(i); + String param = i < args.length ? args[i] : p.getDefault(); // 参数大于解析器 并且为最后一个参数 - if (i + 1 == parse.size() && args.length >= parse.size()) { + if (i + 1 == parses.size() && args.length >= parses.size()) { param = join(Arrays.copyOfRange(args, i, args.length), " "); } pobjs.add(param == null ? null : p.parse(sender, param)); } catch (Exception e) { + Log.fd(e); throw new ParseException(String.format("第 %s 个参数 %s", isMain ? 1 : 2 + i, e.getMessage())); } } @@ -114,21 +118,12 @@ public class CommandParse { return pobjs.toArray(); } - public static abstract class Parse implements Cloneable { + public static abstract class Parse { protected Map attrs = new HashMap<>(); protected String def; protected int max = Integer.MAX_VALUE; protected int min = 0; - @Override - public Parse clone() { - try { - return (Parse) super.clone(); - } catch (CloneNotSupportedException e) { - return null; - } - } - public String getDefault() { return def; } @@ -158,9 +153,11 @@ public class CommandParse { public Parse handleAttrs() { if (attrs.containsKey("def")) { def = String.valueOf(attrs.get("def")); - } else if (attrs.containsKey("min")) { + } + if (attrs.containsKey("min")) { min = Integer.parseInt(String.valueOf(attrs.get("min"))); - } else if (attrs.containsKey("max")) { + } + if (attrs.containsKey("max")) { max = Integer.parseInt(String.valueOf(attrs.get("max"))); } return this; @@ -170,8 +167,8 @@ public class CommandParse { throw new ParseException(String.format(str, objects)); } - public T throwRange() { - return throwRange(""); + public void throwRange() { + throwRange(""); } public T throwRange(String str) { @@ -239,6 +236,7 @@ public class CommandParse { @Override public Parse handleAttrs() { + super.handleAttrs(); check = attrs.containsKey("check"); return this; } @@ -256,6 +254,7 @@ public class CommandParse { @Override public Parse handleAttrs() { + super.handleAttrs(); if (attrs.containsKey("option")) { options = Arrays.asList(attrs.get("option").split(",")); }