From 793015c716af15bc32ff508bdef74a779c731e3d Mon Sep 17 00:00:00 2001 From: Peyang <42040068+peyang-Celeron@users.noreply.github.com> Date: Fri, 2 Apr 2021 19:26:51 +0900 Subject: [PATCH] feat: Strict --- .../p2p/lib/bukkit/EntitySelector.java | 91 ++++++++++++++----- 1 file changed, 68 insertions(+), 23 deletions(-) diff --git a/src/main/java/develop/p2p/lib/bukkit/EntitySelector.java b/src/main/java/develop/p2p/lib/bukkit/EntitySelector.java index 1af8ba3..b7442b5 100644 --- a/src/main/java/develop/p2p/lib/bukkit/EntitySelector.java +++ b/src/main/java/develop/p2p/lib/bukkit/EntitySelector.java @@ -3,7 +3,6 @@ import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.collect.ComparisonChain; -import develop.p2p.lib.bukkit.exception.SelectorException; import develop.p2p.lib.bukkit.exception.SelectorInvalidException; import develop.p2p.lib.bukkit.exception.SelectorMalformedException; import org.bukkit.Bukkit; @@ -53,15 +52,9 @@ public class EntitySelector worldBindingArgs = new String[]{"x", "y", "z", "dx", "dy", "dz", "rm", "r"}; usableArgs = new String[]{"x", "y", "z", "r", "rm", "dx", "dy", "dz", "tag", "team", "c", "l", "lm", "m", "name", "rx", "rxm", "ry", "rym", "type"}; } - - public static Player matchOnePlayer(CommandSender sender, String token) throws SelectorInvalidException, SelectorMalformedException - { - return (Player) matchOneEntity(sender, token, Player.class); - } - - public static Entity matchOneEntity(CommandSender sender, String token, Class clazz) throws SelectorInvalidException, SelectorMalformedException + public static Entity matchOneEntity(CommandSender sender, String token) throws SelectorInvalidException, SelectorMalformedException { - List selectedEntities = matchEntities(sender, token, clazz); + List selectedEntities = matchEntities(sender, token, true); return selectedEntities.size() == 1 ? selectedEntities.get(0): null; } @@ -71,28 +64,64 @@ public static Entity matchOneEntity(CommandSender sender, String token, Class keyMap = getArgumentMap(tokenMatcher.group(2)); - keyMap = pickUpInvalidArgs(keyMap); + keyMap = pickUpInvalidArgs(keyMap, strict); if (keyMap.size() != 0) throw new SelectorInvalidException(keyMap); } - private static Map pickUpInvalidArgs(Map keyMap) + private static Map pickUpInvalidArgs(Map keyMap, boolean strict) { Map result = new HashMap<>(); keyMap.entrySet().stream().parallel().filter(ent -> { for (String key: usableArgs) if (ent.getKey().equals(key)) + { + switch (key) + { + case "type": + if (strict && !isEntityTypeValid(ent.getValue())) + return true; + else if (!strict) + return false; + break; + case "x": + case "y": + case "z": + case "dx": + case "dy": + case "dz": + case "r": + case "rm": + case "c": + case "l": + case "lm": + if (strict && !canConvertNumber(ent.getValue())) + return true; + else if (!strict) + return false; + break; + case "rx": + case "rxm": + case "ry": + case "rym": + if (strict && !canConvertDouble(ent.getValue())) + return true; + else if (!strict) + return false; + break; + } return false; + } Matcher matcher = scorePattern.matcher(ent.getKey()); @@ -102,15 +131,18 @@ private static Map pickUpInvalidArgs(Map keyMap) return result; } - - public static boolean isValidSelectorType(String token) - { - return tokenPattern.matcher(token).matches(); - } - - public static List matchEntities(CommandSender sender, String token, Class clazz) throws SelectorInvalidException, SelectorMalformedException + /** + * エンティティを選択 + * @param sender コマンドを撃った人 + * @param token セレクタ + * @param strict + * @return + * @throws SelectorInvalidException + * @throws SelectorMalformedException + */ + public static List matchEntities(CommandSender sender, String token, boolean strict) throws SelectorInvalidException, SelectorMalformedException { - validateSelector(token); + validateSelector(token, strict); Matcher tokenMatcher = tokenPattern.matcher(token); if (!tokenMatcher.matches()) @@ -147,7 +179,7 @@ public static List matchEntities(CommandSender sender, String token, Cl filtered.addAll(filterResults(keyMap, pres, dist, world, blockPos)); } - return getEntitiesFromPredicates(filtered, keyMap, sender, clazz, dist, blockPos); + return getEntitiesFromPredicates(filtered, keyMap, sender, dist, blockPos); } @@ -187,7 +219,7 @@ public boolean apply(Entity input) } - private static List getEntitiesFromPredicates(List match, Map params, CommandSender sender, Class clazz, String type, Location blockPos) + private static List getEntitiesFromPredicates(List match, Map params, CommandSender sender, String type, Location blockPos) { int count = convertStringToInt(params.get("c"), !type.equals("a") && !type.equals("e") ? 1: 0); @@ -211,7 +243,7 @@ else if (blockPos != null) if (sender instanceof Player) { Entity commandSenderEntity = ((Player) sender); - if (clazz.isAssignableFrom(commandSenderEntity.getClass()) && count == 1 && match.contains(commandSenderEntity) && !type.equals("r")) + if (count == 1 && match.contains(commandSenderEntity) && !type.equals("r")) match = new ArrayList<>(Collections.singletonList(commandSenderEntity)); } @@ -671,6 +703,19 @@ private static Location getBlockPosFromArguments(Map map, Locat return location; } + private static boolean canConvertNumber(String str) + { + try + { + Long.parseLong(str); + return true; + } + catch (Exception ignored) + { + return false; + } + } + private static boolean canConvertDouble(String str) { try