From e7ca79d2513ed595e1167d9e2abb6d2c41f1955e Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 12 Sep 2016 19:00:37 +0100 Subject: [PATCH] Use Splitter instead of Patterns, don't split arguments in quotes --- .../me/lucko/luckperms/BukkitCommand.java | 11 +- .../me/lucko/luckperms/BukkitListener.java | 2 +- .../lucko/luckperms/inject/LPPermissible.java | 4 +- .../me/lucko/luckperms/BungeeCommand.java | 11 +- .../me/lucko/luckperms/commands/Util.java | 16 +++ .../group/subcommands/GroupSetPermission.java | 3 +- .../subcommands/GroupSetTempPermission.java | 3 +- .../subcommands/GroupUnSetPermission.java | 3 +- .../subcommands/GroupUnsetTempPermission.java | 3 +- .../user/subcommands/UserSetPermission.java | 3 +- .../subcommands/UserSetTempPermission.java | 3 +- .../user/subcommands/UserUnSetPermission.java | 3 +- .../subcommands/UserUnsetTempPermission.java | 3 +- .../lucko/luckperms/constants/Patterns.java | 12 +-- .../me/lucko/luckperms/data/Importer.java | 4 +- .../java/me/lucko/luckperms/utils/Node.java | 101 ++++++++++-------- .../me/lucko/luckperms/SpongeCommand.java | 25 ++--- .../java/me/lucko/luckperms/SpongeConfig.java | 4 +- 18 files changed, 121 insertions(+), 93 deletions(-) diff --git a/bukkit/src/main/java/me/lucko/luckperms/BukkitCommand.java b/bukkit/src/main/java/me/lucko/luckperms/BukkitCommand.java index 289c174f2..25c5fbde7 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/BukkitCommand.java +++ b/bukkit/src/main/java/me/lucko/luckperms/BukkitCommand.java @@ -22,8 +22,12 @@ package me.lucko.luckperms; +import com.google.common.base.Joiner; +import com.google.common.base.Splitter; import me.lucko.luckperms.api.data.Callback; import me.lucko.luckperms.commands.CommandManager; +import me.lucko.luckperms.commands.Util; +import me.lucko.luckperms.constants.Patterns; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -39,7 +43,12 @@ class BukkitCommand extends CommandManager implements CommandExecutor, TabExecut @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - onCommand(BukkitSenderFactory.get().wrap(sender), label, Arrays.asList(args), Callback.empty()); + onCommand( + BukkitSenderFactory.get().wrap(sender), + label, + Util.stripQuotes(Splitter.on(Patterns.COMMAND_SEPARATOR).omitEmptyStrings().splitToList(Joiner.on(' ').join(args))), + Callback.empty() + ); return true; } diff --git a/bukkit/src/main/java/me/lucko/luckperms/BukkitListener.java b/bukkit/src/main/java/me/lucko/luckperms/BukkitListener.java index 0a5ee1625..c5f776a4d 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/BukkitListener.java +++ b/bukkit/src/main/java/me/lucko/luckperms/BukkitListener.java @@ -23,8 +23,8 @@ package me.lucko.luckperms; import me.lucko.luckperms.constants.Message; -import me.lucko.luckperms.inject.LPPermissible; import me.lucko.luckperms.inject.Injector; +import me.lucko.luckperms.inject.LPPermissible; import me.lucko.luckperms.users.BukkitUser; import me.lucko.luckperms.users.User; import me.lucko.luckperms.utils.AbstractListener; diff --git a/bukkit/src/main/java/me/lucko/luckperms/inject/LPPermissible.java b/bukkit/src/main/java/me/lucko/luckperms/inject/LPPermissible.java index c56223b52..8320660dd 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/inject/LPPermissible.java +++ b/bukkit/src/main/java/me/lucko/luckperms/inject/LPPermissible.java @@ -22,11 +22,11 @@ package me.lucko.luckperms.inject; +import com.google.common.base.Splitter; import lombok.Getter; import lombok.NonNull; import me.lucko.luckperms.LuckPermsPlugin; import me.lucko.luckperms.api.Tristate; -import me.lucko.luckperms.constants.Patterns; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.permissions.*; @@ -99,7 +99,7 @@ public class LPPermissible extends PermissibleBase { } String node = ""; - String[] permParts = Patterns.DOT.split(permission); + Iterable permParts = Splitter.on('.').split(permission); for (String s : permParts) { if (node.equals("")) { node = s; diff --git a/bungee/src/main/java/me/lucko/luckperms/BungeeCommand.java b/bungee/src/main/java/me/lucko/luckperms/BungeeCommand.java index 1794b5527..6d3445012 100644 --- a/bungee/src/main/java/me/lucko/luckperms/BungeeCommand.java +++ b/bungee/src/main/java/me/lucko/luckperms/BungeeCommand.java @@ -22,8 +22,12 @@ package me.lucko.luckperms; +import com.google.common.base.Joiner; +import com.google.common.base.Splitter; import me.lucko.luckperms.api.data.Callback; import me.lucko.luckperms.commands.CommandManager; +import me.lucko.luckperms.commands.Util; +import me.lucko.luckperms.constants.Patterns; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.plugin.Command; import net.md_5.bungee.api.plugin.TabExecutor; @@ -40,7 +44,12 @@ class BungeeCommand extends Command implements TabExecutor { @Override public void execute(CommandSender sender, String[] args) { - manager.onCommand(BungeeSenderFactory.get().wrap(sender), "bperms", Arrays.asList(args), Callback.empty()); + manager.onCommand( + BungeeSenderFactory.get().wrap(sender), + "bperms", + Util.stripQuotes(Splitter.on(Patterns.COMMAND_SEPARATOR).omitEmptyStrings().splitToList(Joiner.on(' ').join(args))), + Callback.empty() + ); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/commands/Util.java b/common/src/main/java/me/lucko/luckperms/commands/Util.java index ee2355243..2c2a73b57 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/Util.java +++ b/common/src/main/java/me/lucko/luckperms/commands/Util.java @@ -37,6 +37,22 @@ public class Util { sender.sendMessage(color(Message.PREFIX + message)); } + public static List stripQuotes(List input) { + input = new ArrayList<>(input); + ListIterator iterator = input.listIterator(); + while (iterator.hasNext()) { + String value = iterator.next(); + if (!(value.length() >= 3)) { + continue; + } + + if (value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') { + iterator.set(value.substring(1, value.length() - 1)); + } + } + return input; + } + public static String color(String s) { return translateAlternateColorCodes('&', s); } diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupSetPermission.java b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupSetPermission.java index 42f770641..9d6e3cf4f 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupSetPermission.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupSetPermission.java @@ -28,7 +28,6 @@ import me.lucko.luckperms.commands.Predicate; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.SubCommand; import me.lucko.luckperms.constants.Message; -import me.lucko.luckperms.constants.Patterns; import me.lucko.luckperms.constants.Permission; import me.lucko.luckperms.data.LogEntry; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; @@ -53,7 +52,7 @@ public class GroupSetPermission extends SubCommand { return CommandResult.INVALID_ARGS; } - if (Patterns.GROUP_MATCH.matcher(node).matches()) { + if (node.toLowerCase().startsWith("group.")) { Message.GROUP_USE_INHERIT.send(sender); return CommandResult.INVALID_ARGS; } diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupSetTempPermission.java b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupSetTempPermission.java index 08f80795d..227828302 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupSetTempPermission.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupSetTempPermission.java @@ -28,7 +28,6 @@ import me.lucko.luckperms.commands.Predicate; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.SubCommand; import me.lucko.luckperms.constants.Message; -import me.lucko.luckperms.constants.Patterns; import me.lucko.luckperms.constants.Permission; import me.lucko.luckperms.data.LogEntry; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; @@ -55,7 +54,7 @@ public class GroupSetTempPermission extends SubCommand { return CommandResult.INVALID_ARGS; } - if (Patterns.GROUP_MATCH.matcher(node).matches()) { + if (node.toLowerCase().startsWith("group.")) { Message.GROUP_USE_INHERIT.send(sender); return CommandResult.INVALID_ARGS; } diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupUnSetPermission.java b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupUnSetPermission.java index a27a68efc..eed487b95 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupUnSetPermission.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupUnSetPermission.java @@ -28,7 +28,6 @@ import me.lucko.luckperms.commands.Predicate; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.SubCommand; import me.lucko.luckperms.constants.Message; -import me.lucko.luckperms.constants.Patterns; import me.lucko.luckperms.constants.Permission; import me.lucko.luckperms.data.LogEntry; import me.lucko.luckperms.exceptions.ObjectLacksException; @@ -52,7 +51,7 @@ public class GroupUnSetPermission extends SubCommand { return CommandResult.INVALID_ARGS; } - if (Patterns.GROUP_MATCH.matcher(node).matches()) { + if (node.toLowerCase().startsWith("group.")) { Message.GROUP_USE_UNINHERIT.send(sender); return CommandResult.INVALID_ARGS; } diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupUnsetTempPermission.java b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupUnsetTempPermission.java index d8044b348..75c85c051 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupUnsetTempPermission.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupUnsetTempPermission.java @@ -28,7 +28,6 @@ import me.lucko.luckperms.commands.Predicate; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.SubCommand; import me.lucko.luckperms.constants.Message; -import me.lucko.luckperms.constants.Patterns; import me.lucko.luckperms.constants.Permission; import me.lucko.luckperms.data.LogEntry; import me.lucko.luckperms.exceptions.ObjectLacksException; @@ -53,7 +52,7 @@ public class GroupUnsetTempPermission extends SubCommand { return CommandResult.INVALID_ARGS; } - if (Patterns.GROUP_MATCH.matcher(node).matches()) { + if (node.toLowerCase().startsWith("group.")) { Message.GROUP_USE_UNINHERIT.send(sender); return CommandResult.INVALID_ARGS; } diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetPermission.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetPermission.java index 8c8aae5df..a9fb8230a 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetPermission.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetPermission.java @@ -28,7 +28,6 @@ import me.lucko.luckperms.commands.Predicate; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.SubCommand; import me.lucko.luckperms.constants.Message; -import me.lucko.luckperms.constants.Patterns; import me.lucko.luckperms.constants.Permission; import me.lucko.luckperms.data.LogEntry; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; @@ -54,7 +53,7 @@ public class UserSetPermission extends SubCommand { return CommandResult.INVALID_ARGS; } - if (Patterns.GROUP_MATCH.matcher(node).matches()) { + if (node.toLowerCase().startsWith("group.")) { Message.USER_USE_ADDGROUP.send(sender); return CommandResult.INVALID_ARGS; } diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetTempPermission.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetTempPermission.java index 385bfced6..a27a5e7eb 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetTempPermission.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserSetTempPermission.java @@ -28,7 +28,6 @@ import me.lucko.luckperms.commands.Predicate; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.SubCommand; import me.lucko.luckperms.constants.Message; -import me.lucko.luckperms.constants.Patterns; import me.lucko.luckperms.constants.Permission; import me.lucko.luckperms.data.LogEntry; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; @@ -55,7 +54,7 @@ public class UserSetTempPermission extends SubCommand { return CommandResult.INVALID_ARGS; } - if (Patterns.GROUP_MATCH.matcher(node).matches()) { + if (node.toLowerCase().startsWith("group.")) { Message.USER_USE_ADDGROUP.send(sender); return CommandResult.INVALID_ARGS; } diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserUnSetPermission.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserUnSetPermission.java index ba071033a..a5059e5d8 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserUnSetPermission.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserUnSetPermission.java @@ -28,7 +28,6 @@ import me.lucko.luckperms.commands.Predicate; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.SubCommand; import me.lucko.luckperms.constants.Message; -import me.lucko.luckperms.constants.Patterns; import me.lucko.luckperms.constants.Permission; import me.lucko.luckperms.data.LogEntry; import me.lucko.luckperms.exceptions.ObjectLacksException; @@ -52,7 +51,7 @@ public class UserUnSetPermission extends SubCommand { return CommandResult.INVALID_ARGS; } - if (Patterns.GROUP_MATCH.matcher(node).matches()) { + if (node.toLowerCase().startsWith("group.")) { Message.USER_USE_REMOVEGROUP.send(sender); return CommandResult.INVALID_ARGS; } diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserUnsetTempPermission.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserUnsetTempPermission.java index 048679f73..cf28cad7e 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserUnsetTempPermission.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserUnsetTempPermission.java @@ -28,7 +28,6 @@ import me.lucko.luckperms.commands.Predicate; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.SubCommand; import me.lucko.luckperms.constants.Message; -import me.lucko.luckperms.constants.Patterns; import me.lucko.luckperms.constants.Permission; import me.lucko.luckperms.data.LogEntry; import me.lucko.luckperms.exceptions.ObjectLacksException; @@ -53,7 +52,7 @@ public class UserUnsetTempPermission extends SubCommand { return CommandResult.INVALID_ARGS; } - if (Patterns.GROUP_MATCH.matcher(node).matches()) { + if (node.toLowerCase().startsWith("group.")) { Message.USER_USE_REMOVEGROUP.send(sender); return CommandResult.INVALID_ARGS; } diff --git a/common/src/main/java/me/lucko/luckperms/constants/Patterns.java b/common/src/main/java/me/lucko/luckperms/constants/Patterns.java index 57d66f713..8db1c0a38 100644 --- a/common/src/main/java/me/lucko/luckperms/constants/Patterns.java +++ b/common/src/main/java/me/lucko/luckperms/constants/Patterns.java @@ -33,15 +33,9 @@ import java.util.regex.PatternSyntaxException; public class Patterns { private static final Map CACHE = new ConcurrentHashMap<>(); - public static final Pattern SPACE = Pattern.compile(" "); - public static final Pattern SERVER_DELIMITER = Pattern.compile("\\/"); - public static final Pattern WORLD_DELIMITER = Pattern.compile("\\-"); - public static final Pattern TEMP_DELIMITER = Pattern.compile("\\$"); - public static final Pattern DOT = Pattern.compile("\\."); - public static final Pattern VERTICAL_BAR = Pattern.compile("\\|"); - public static final Pattern GROUP_MATCH = Pattern.compile("group\\..*"); - public static final Pattern NON_ALPHA_NUMERIC = Pattern.compile("[\\/\\$\\.\\-]"); - public static final Pattern NON_USERNAME = Pattern.compile("[^A-Za-z0-9_]"); + public static final Pattern COMMAND_SEPARATOR = Pattern.compile(" (?=([^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)"); + public static final Pattern NON_ALPHA_NUMERIC = Pattern.compile("[\\/\\$\\.\\- ]"); + public static final Pattern NON_USERNAME = Pattern.compile("[^A-Za-z0-9_ ]"); public static final Pattern SHORTHAND_NODE = Pattern.compile("\\.\\([^.]+\\)"); public static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)" + String.valueOf('ยง') + "[0-9A-FK-OR]"); public static final Pattern NODE_CONTEXTS = Pattern.compile("\\(.+\\).*"); diff --git a/common/src/main/java/me/lucko/luckperms/data/Importer.java b/common/src/main/java/me/lucko/luckperms/data/Importer.java index ddcabe6c2..be62ba875 100644 --- a/common/src/main/java/me/lucko/luckperms/data/Importer.java +++ b/common/src/main/java/me/lucko/luckperms/data/Importer.java @@ -22,6 +22,7 @@ package me.lucko.luckperms.data; +import com.google.common.base.Splitter; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.Setter; @@ -31,7 +32,6 @@ import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.constants.Constants; import me.lucko.luckperms.constants.Message; -import me.lucko.luckperms.constants.Patterns; import me.lucko.luckperms.constants.Permission; import java.util.*; @@ -98,7 +98,7 @@ public class Importer { executing = index; try { - CommandResult result = commandManager.onCommand(fake, "perms", Arrays.asList(Patterns.SPACE.split(command))); + CommandResult result = commandManager.onCommand(fake, "perms", Splitter.on(' ').splitToList(command)); getResult(index, command).setResult(result); } catch (Exception e) { diff --git a/common/src/main/java/me/lucko/luckperms/utils/Node.java b/common/src/main/java/me/lucko/luckperms/utils/Node.java index 37c53fdd9..4ffa8bedf 100644 --- a/common/src/main/java/me/lucko/luckperms/utils/Node.java +++ b/common/src/main/java/me/lucko/luckperms/utils/Node.java @@ -22,6 +22,7 @@ package me.lucko.luckperms.utils; +import com.google.common.base.Splitter; import com.google.common.collect.ImmutableMap; import lombok.*; import me.lucko.luckperms.api.Tristate; @@ -41,6 +42,7 @@ import java.util.stream.IntStream; public class Node implements me.lucko.luckperms.api.Node { private static final Pattern PREFIX_PATTERN = Pattern.compile("(?i)prefix\\.-?\\d+\\..*"); private static final Pattern SUFFIX_PATTERN = Pattern.compile("(?i)suffix\\.-?\\d+\\..*"); + private static final Pattern META_PATTERN = Pattern.compile("meta\\..*\\..*"); public static me.lucko.luckperms.api.Node fromSerialisedNode(String s, Boolean b) { return builderFromSerialisedNode(s, b).build(); @@ -48,36 +50,36 @@ public class Node implements me.lucko.luckperms.api.Node { public static me.lucko.luckperms.api.Node.Builder builderFromSerialisedNode(String s, Boolean b) { if (s.contains("/")) { - String[] parts = Patterns.SERVER_DELIMITER.split(s, 2); + List parts = Splitter.on('/').limit(2).splitToList(s); // 0=server(+world) 1=node // WORLD SPECIFIC - if (parts[0].contains("-")) { - String[] serverParts = Patterns.WORLD_DELIMITER.split(parts[0], 2); + if (parts.get(0).contains("-")) { + List serverParts = Splitter.on('-').limit(2).splitToList(parts.get(0)); // 0=server 1=world - if (parts[1].contains("$")) { - String[] tempParts = Patterns.TEMP_DELIMITER.split(parts[1], 2); - return new Node.Builder(tempParts[0], true).setServerRaw(serverParts[0]).setWorld(serverParts[1]) - .setExpiry(Long.parseLong(tempParts[1])).setValue(b); + if (parts.get(1).contains("$")) { + List tempParts = Splitter.on('$').limit(2).splitToList(parts.get(1)); + return new Node.Builder(tempParts.get(0), true).setServerRaw(serverParts.get(0)).setWorld(serverParts.get(1)) + .setExpiry(Long.parseLong(tempParts.get(1))).setValue(b); } else { - return new Node.Builder(parts[1], true).setServerRaw(serverParts[0]).setWorld(serverParts[1]).setValue(b); + return new Node.Builder(parts.get(1), true).setServerRaw(serverParts.get(0)).setWorld(serverParts.get(1)).setValue(b); } } else { // SERVER BUT NOT WORLD SPECIFIC - if (parts[1].contains("$")) { - String[] tempParts = Patterns.TEMP_DELIMITER.split(parts[1], 2); - return new Node.Builder(tempParts[0], true).setServerRaw(parts[0]).setExpiry(Long.parseLong(tempParts[1])).setValue(b); + if (parts.get(1).contains("$")) { + List tempParts = Splitter.on('$').limit(2).splitToList(parts.get(1)); + return new Node.Builder(tempParts.get(0), true).setServerRaw(parts.get(0)).setExpiry(Long.parseLong(tempParts.get(1))).setValue(b); } else { - return new Node.Builder(parts[1], true).setServerRaw(parts[0]).setValue(b); + return new Node.Builder(parts.get(1), true).setServerRaw(parts.get(0)).setValue(b); } } } else { // NOT SERVER SPECIFIC if (s.contains("$")) { - String[] tempParts = Patterns.TEMP_DELIMITER.split(s, 2); - return new Node.Builder(tempParts[0], true).setExpiry(Long.parseLong(tempParts[1])).setValue(b); + List tempParts = Splitter.on('$').limit(2).splitToList(s); + return new Node.Builder(tempParts.get(0), true).setExpiry(Long.parseLong(tempParts.get(1))).setValue(b); } else { return new Node.Builder(s, true).setValue(b); } @@ -100,6 +102,11 @@ public class Node implements me.lucko.luckperms.api.Node { private final Map extraContexts = new HashMap<>(); + // Cache the state + private Tristate isPrefix = Tristate.UNDEFINED; + private Tristate isSuffix = Tristate.UNDEFINED; + private Tristate isMeta = Tristate.UNDEFINED; + /** * Make an immutable node instance * @param permission the actual permission node @@ -203,7 +210,7 @@ public class Node implements me.lucko.luckperms.api.Node { if (world.startsWith("(") && world.endsWith(")") && world.contains("|")) { final String bits = world.substring(1, world.length() - 1); - String[] parts = Patterns.VERTICAL_BAR.split(bits); + Iterable parts = Splitter.on('|').split(bits); for (String s : parts) { if (s.equalsIgnoreCase(thisWorld)) { @@ -313,7 +320,7 @@ public class Node implements me.lucko.luckperms.api.Node { return Collections.emptyList(); } - String[] parts = Patterns.DOT.split(getPermission()); + Iterable parts = Splitter.on('.').split(getPermission()); List> nodeParts = new ArrayList<>(); for (String s : parts) { @@ -324,13 +331,13 @@ public class Node implements me.lucko.luckperms.api.Node { final String bits = s.substring(1, s.length() - 1); if (s.contains("|")) { - nodeParts.add(new HashSet<>(Arrays.asList(Patterns.VERTICAL_BAR.split(bits)))); + nodeParts.add(new HashSet<>(Splitter.on('|').splitToList(bits))); } else { - String[] range = Patterns.WORLD_DELIMITER.split(bits, 2); - if (isChar(range[0], range[1])) { - nodeParts.add(getCharRange(range[0].charAt(0), range[1].charAt(0))); - } else if (isInt(range[0], range[1])) { - nodeParts.add(IntStream.rangeClosed(Integer.parseInt(range[0]), Integer.parseInt(range[1])).boxed() + List range = Splitter.on('-').limit(2).splitToList(bits); + if (isChar(range.get(0), range.get(1))) { + nodeParts.add(getCharRange(range.get(0).charAt(0), range.get(1).charAt(0))); + } else if (isInt(range.get(0), range.get(1))) { + nodeParts.add(IntStream.rangeClosed(Integer.parseInt(range.get(0)), Integer.parseInt(range.get(1))).boxed() .map(i -> "" + i) .collect(Collectors.toSet()) ); @@ -423,7 +430,7 @@ public class Node implements me.lucko.luckperms.api.Node { @Override public boolean isGroupNode() { - return Patterns.GROUP_MATCH.matcher(getPermission()).matches(); + return getPermission().toLowerCase().startsWith("group."); } @Override @@ -447,7 +454,11 @@ public class Node implements me.lucko.luckperms.api.Node { @Override public boolean isMeta() { - return getPermission().matches("meta\\..*\\..*"); + if (isMeta == Tristate.UNDEFINED) { + isMeta = Tristate.fromBoolean(META_PATTERN.matcher(getPermission()).matches()); + } + + return isMeta.asBoolean(); } @Override @@ -462,7 +473,11 @@ public class Node implements me.lucko.luckperms.api.Node { @Override public boolean isPrefix() { - return PREFIX_PATTERN.matcher(getPermission()).matches(); + if (isPrefix == Tristate.UNDEFINED) { + isPrefix = Tristate.fromBoolean(PREFIX_PATTERN.matcher(getPermission()).matches()); + } + + return isPrefix.asBoolean(); } @Override @@ -471,15 +486,18 @@ public class Node implements me.lucko.luckperms.api.Node { throw new IllegalStateException(); } - String[] prefixPart = Patterns.DOT.split(getPermission().substring("prefix.".length()), 2); - Integer i = Integer.parseInt(prefixPart[0]); - - return new AbstractMap.SimpleEntry<>(i, prefixPart[1]); + List prefixPart = Splitter.on('.').limit(2).splitToList(getPermission().substring("prefix.".length())); + Integer i = Integer.parseInt(prefixPart.get(0)); + return new AbstractMap.SimpleEntry<>(i, prefixPart.get(1)); } @Override public boolean isSuffix() { - return SUFFIX_PATTERN.matcher(getPermission()).matches(); + if (isSuffix == Tristate.UNDEFINED) { + isSuffix = Tristate.fromBoolean(SUFFIX_PATTERN.matcher(getPermission()).matches()); + } + + return isSuffix.asBoolean(); } @Override @@ -488,10 +506,9 @@ public class Node implements me.lucko.luckperms.api.Node { throw new IllegalStateException(); } - String[] suffixPart = Patterns.DOT.split(getPermission().substring("suffix.".length()), 2); - Integer i = Integer.parseInt(suffixPart[0]); - - return new AbstractMap.SimpleEntry<>(i, suffixPart[1]); + List suffixPart = Splitter.on('.').limit(2).splitToList(getPermission().substring("suffix.".length())); + Integer i = Integer.parseInt(suffixPart.get(0)); + return new AbstractMap.SimpleEntry<>(i, suffixPart.get(1)); } @Override @@ -635,18 +652,14 @@ public class Node implements me.lucko.luckperms.api.Node { if (!Patterns.NODE_CONTEXTS.matcher(permission).matches()) { this.permission = permission; } else { - String[] contextParts = permission.substring(1).split("\\)", 2); + List contextParts = Splitter.on(')').limit(2).splitToList(permission.substring(1)); // 0 = context, 1 = node - this.permission = contextParts[1]; - for (String s : contextParts[0].split("\\,")) { - if (!s.contains("=")) { - // Not valid - continue; - } - - String[] context = s.split("\\=", 2); - extraContexts.put(context[0], context[1]); + this.permission = contextParts.get(1); + try { + extraContexts.putAll(Splitter.on(',').withKeyValueSeparator('=').split(contextParts.get(0))); + } catch (IllegalArgumentException e) { + e.printStackTrace(); } } } diff --git a/sponge/src/main/java/me/lucko/luckperms/SpongeCommand.java b/sponge/src/main/java/me/lucko/luckperms/SpongeCommand.java index 7132d37f9..88b1ae792 100644 --- a/sponge/src/main/java/me/lucko/luckperms/SpongeCommand.java +++ b/sponge/src/main/java/me/lucko/luckperms/SpongeCommand.java @@ -22,8 +22,10 @@ package me.lucko.luckperms; +import com.google.common.base.Splitter; import me.lucko.luckperms.api.data.Callback; import me.lucko.luckperms.commands.CommandManager; +import me.lucko.luckperms.commands.Util; import me.lucko.luckperms.constants.Patterns; import org.spongepowered.api.command.CommandCallable; import org.spongepowered.api.command.CommandException; @@ -34,8 +36,6 @@ import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -47,27 +47,22 @@ class SpongeCommand extends CommandManager implements CommandCallable { @Override public CommandResult process(CommandSource source, String s) throws CommandException { - onCommand(SpongeSenderFactory.get().wrap(source), "perms", Arrays.asList(Patterns.SPACE.split(s)), Callback.empty()); + onCommand( + SpongeSenderFactory.get().wrap(source), + "perms", + Util.stripQuotes(Splitter.on(Patterns.COMMAND_SEPARATOR).omitEmptyStrings().splitToList(s)), + Callback.empty() + ); return CommandResult.success(); } @Override public List getSuggestions(CommandSource source, String s, @Nullable Location location) throws CommandException { - List args = new ArrayList<>(Arrays.asList(Patterns.SPACE.split(s))); - if (s.endsWith(" ")) { - args.add(""); - } - - return onTabComplete(SpongeSenderFactory.get().wrap(source), args); + return onTabComplete(SpongeSenderFactory.get().wrap(source), Splitter.on(' ').splitToList(s)); } public List getSuggestions(CommandSource source, String s) throws CommandException { - List args = new ArrayList<>(Arrays.asList(Patterns.SPACE.split(s))); - if (s.endsWith(" ")) { - args.add(""); - } - - return onTabComplete(SpongeSenderFactory.get().wrap(source), args); + return onTabComplete(SpongeSenderFactory.get().wrap(source), Splitter.on(' ').splitToList(s)); } @Override diff --git a/sponge/src/main/java/me/lucko/luckperms/SpongeConfig.java b/sponge/src/main/java/me/lucko/luckperms/SpongeConfig.java index b3e49aaf5..b868a0a77 100644 --- a/sponge/src/main/java/me/lucko/luckperms/SpongeConfig.java +++ b/sponge/src/main/java/me/lucko/luckperms/SpongeConfig.java @@ -22,7 +22,7 @@ package me.lucko.luckperms; -import me.lucko.luckperms.constants.Patterns; +import com.google.common.base.Splitter; import me.lucko.luckperms.core.LPConfiguration; import ninja.leaping.configurate.ConfigurationNode; import ninja.leaping.configurate.commented.CommentedConfigurationNode; @@ -70,7 +70,7 @@ class SpongeConfig extends LPConfiguration { } private ConfigurationNode getNode(String path) { - String[] paths = Patterns.DOT.split(path); + Iterable paths = Splitter.on('.').split(path); ConfigurationNode node = root; for (String s : paths) {