From 4874f9d0510675ba9cb70c6af42494a382eb5b11 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 20 Nov 2016 20:41:39 +0000 Subject: [PATCH] Use faster wildcard parsing method --- .../processors/WildcardProcessor.java | 29 +++++++++++-------- .../luckperms/sponge/LPSpongePlugin.java | 9 ++---- .../calculators/SpongeWildcardProcessor.java | 29 +++++++++++-------- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/calculators/processors/WildcardProcessor.java b/common/src/main/java/me/lucko/luckperms/common/calculators/processors/WildcardProcessor.java index e460b4987..21f6a815b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/calculators/processors/WildcardProcessor.java +++ b/common/src/main/java/me/lucko/luckperms/common/calculators/processors/WildcardProcessor.java @@ -26,27 +26,32 @@ import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.common.calculators.PermissionProcessor; import java.util.Map; +import java.util.regex.Pattern; public class WildcardProcessor implements PermissionProcessor { + private static final Pattern SPLIT = Pattern.compile("\\."); private Map map = null; @Override - public Tristate hasPermission(String permission) { - String node = permission; + public Tristate hasPermission(String s) { + if (s.startsWith(".") || !s.contains(".")) { + throw new IllegalArgumentException(); + } - while (true) { - int endIndex = node.lastIndexOf('.'); - if (endIndex == -1) { - break; + String[] parts = SPLIT.split(s); + StringBuilder sb = new StringBuilder(); + + for (int i = parts.length - 2; i >= 0; i--) { + for (int i1 = 0; i1 <= i; i1++) { + sb.append(parts[i1]).append("."); } - node = node.substring(0, endIndex); - if (!node.isEmpty()) { - Boolean b = map.get(node + ".*"); - if (b != null) { - return Tristate.fromBoolean(b); - } + Boolean b = map.get(sb.append("*").toString()); + if (b != null) { + return Tristate.fromBoolean(b); } + + sb.setLength(0); } Boolean b = map.get("'*'"); diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java index dcb82c5d8..3b1d8dbc9 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java @@ -70,6 +70,7 @@ import org.spongepowered.api.event.game.state.GamePreInitializationEvent; import org.spongepowered.api.event.game.state.GameStoppingServerEvent; import org.spongepowered.api.plugin.Dependency; import org.spongepowered.api.plugin.Plugin; +import org.spongepowered.api.plugin.PluginContainer; import org.spongepowered.api.scheduler.AsynchronousExecutor; import org.spongepowered.api.scheduler.Scheduler; import org.spongepowered.api.scheduler.SpongeExecutorService; @@ -365,7 +366,7 @@ public class LPSpongePlugin implements LuckPermsPlugin { @Override public Object getPlugin(String name) { - return game.getPluginManager().getPlugin(name).get().getInstance().get(); + return game.getPluginManager().getPlugin(name).map(PluginContainer::getInstance).orElse(null); } @Override @@ -375,11 +376,7 @@ public class LPSpongePlugin implements LuckPermsPlugin { @Override public UUID getUUID(String playerName) { - try { - return game.getServer().getPlayer(playerName).get().getUniqueId(); - } catch (Exception e) { - return null; - } + return game.getServer().getPlayer(playerName).map(Player::getUniqueId).orElse(null); } @Override diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/calculators/SpongeWildcardProcessor.java b/sponge/src/main/java/me/lucko/luckperms/sponge/calculators/SpongeWildcardProcessor.java index 4ebe9c222..152306b5b 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/calculators/SpongeWildcardProcessor.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/calculators/SpongeWildcardProcessor.java @@ -26,27 +26,32 @@ import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.common.calculators.PermissionProcessor; import java.util.Map; +import java.util.regex.Pattern; public class SpongeWildcardProcessor implements PermissionProcessor { + private static final Pattern SPLIT = Pattern.compile("\\."); private Map map = null; @Override - public Tristate hasPermission(String permission) { - String node = permission; + public Tristate hasPermission(String s) { + if (s.startsWith(".") || !s.contains(".")) { + throw new IllegalArgumentException(); + } - while (true) { - int endIndex = node.lastIndexOf('.'); - if (endIndex == -1) { - break; + String[] parts = SPLIT.split(s); + StringBuilder sb = new StringBuilder(); + + for (int i = parts.length - 2; i >= 0; i--) { + for (int i1 = 0; i1 <= i; i1++) { + sb.append(parts[i1]).append("."); } - node = node.substring(0, endIndex); - if (!node.isEmpty()) { - Boolean b = map.get(node); - if (b != null) { - return Tristate.fromBoolean(b); - } + Boolean b = map.get(sb.deleteCharAt(sb.length() - 1).toString()); + if (b != null) { + return Tristate.fromBoolean(b); } + + sb.setLength(0); } return Tristate.UNDEFINED;