mirror of
https://e.coding.net/circlecloud/YumCore.git
synced 2024-11-22 01:48:50 +00:00
feat: 添加基本类型的参数解析
Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
parent
837cf64f48
commit
e65d38445d
@ -26,12 +26,21 @@ import pw.yumc.YumCore.commands.exception.ParseException;
|
|||||||
*/
|
*/
|
||||||
public class CommandParse {
|
public class CommandParse {
|
||||||
private static Map<Class, Parse> allparses = new HashMap<>();
|
private static Map<Class, Parse> allparses = new HashMap<>();
|
||||||
|
private static Map<String, Class> primitiveMap = new HashMap<>();
|
||||||
private boolean isMain;
|
private boolean isMain;
|
||||||
private List<Parse> parse = new LinkedList<>();
|
private List<Parse> parse = new LinkedList<>();
|
||||||
static {
|
static {
|
||||||
new FileParse();
|
register(File.class, new FileParse());
|
||||||
new PlayerParse();
|
register(Player.class, new PlayerParse());
|
||||||
new StringParse();
|
register(String.class, new StringParse());
|
||||||
|
primitiveMap.put("boolean", Boolean.class);
|
||||||
|
primitiveMap.put("byte", Byte.class);
|
||||||
|
primitiveMap.put("char", Character.class);
|
||||||
|
primitiveMap.put("short", Short.class);
|
||||||
|
primitiveMap.put("int", Integer.class);
|
||||||
|
primitiveMap.put("long", Long.class);
|
||||||
|
primitiveMap.put("float", Float.class);
|
||||||
|
primitiveMap.put("double", Double.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandParse(Class[] classes, Annotation[][] annons, boolean isMain) {
|
public CommandParse(Class[] classes, Annotation[][] annons, boolean isMain) {
|
||||||
@ -39,15 +48,24 @@ public class CommandParse {
|
|||||||
// 第一个参数实现了CommandSender忽略
|
// 第一个参数实现了CommandSender忽略
|
||||||
for (int i = 1; i < classes.length; i++) {
|
for (int i = 1; i < classes.length; i++) {
|
||||||
Class clazz = classes[i];
|
Class clazz = classes[i];
|
||||||
|
if (clazz.isPrimitive()) {
|
||||||
|
// boolean, byte, char, short, int, long, float, and double.
|
||||||
|
clazz = primitiveMap.get(clazz.getName());
|
||||||
|
}
|
||||||
Annotation[] annotations = annons[i];
|
Annotation[] annotations = annons[i];
|
||||||
Parse parse = allparses.get(clazz);
|
Parse parse = null;
|
||||||
try {
|
if (allparses.containsKey(clazz)) {
|
||||||
parse = new ValueOfParse(clazz, clazz.getDeclaredMethod("valueOf", String.class));
|
parse = allparses.get(clazz);
|
||||||
} catch (NoSuchMethodException ignored) {
|
} else {
|
||||||
|
try {
|
||||||
|
parse = new ValueOfParse(clazz, clazz.getDeclaredMethod("valueOf", String.class));
|
||||||
|
} catch (NoSuchMethodException ignored) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (parse == null) { throw new ParseException(String.format("存在无法解析的参数类型 %s", clazz.getName())); }
|
if (parse == null) { throw new ParseException(String.format("存在无法解析的参数类型 %s", clazz.getName())); }
|
||||||
this.parse.add(parse.clone().parseAnnotation(annotations).handleAttrs());
|
this.parse.add(parse.clone().parseAnnotation(annotations).handleAttrs());
|
||||||
}
|
}
|
||||||
|
Log.d("命令解析器 %s", String.valueOf(parse));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CommandParse get(Method method) {
|
public static CommandParse get(Method method) {
|
||||||
@ -201,10 +219,6 @@ public class CommandParse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static class FileParse extends Parse<File> {
|
public static class FileParse extends Parse<File> {
|
||||||
public FileParse() {
|
|
||||||
register(File.class, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File parse(CommandSender sender, String arg) throws ParseException {
|
public File parse(CommandSender sender, String arg) throws ParseException {
|
||||||
File file = new File(arg);
|
File file = new File(arg);
|
||||||
@ -216,10 +230,6 @@ public class CommandParse {
|
|||||||
public static class PlayerParse extends Parse<Player> {
|
public static class PlayerParse extends Parse<Player> {
|
||||||
boolean check = false;
|
boolean check = false;
|
||||||
|
|
||||||
public PlayerParse() {
|
|
||||||
register(Player.class, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Player parse(CommandSender sender, String arg) {
|
public Player parse(CommandSender sender, String arg) {
|
||||||
Player p = Bukkit.getPlayerExact(arg);
|
Player p = Bukkit.getPlayerExact(arg);
|
||||||
@ -237,10 +247,6 @@ public class CommandParse {
|
|||||||
public static class StringParse extends Parse<String> {
|
public static class StringParse extends Parse<String> {
|
||||||
List<String> options;
|
List<String> options;
|
||||||
|
|
||||||
public StringParse() {
|
|
||||||
register(String.class, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String parse(CommandSender sender, String arg) {
|
public String parse(CommandSender sender, String arg) {
|
||||||
if (min > arg.length() || arg.length() > max) { return throwRange("长度必须在 %s 和 %s 之间!"); }
|
if (min > arg.length() || arg.length() > max) { return throwRange("长度必须在 %s 和 %s 之间!"); }
|
||||||
|
Loading…
Reference in New Issue
Block a user