From cf456cff811076ca8f0a878167f62d0f89870cdb Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 24 Nov 2016 19:07:01 +0000 Subject: [PATCH] Implement tab completion for permissions --- .../luckperms/bukkit/LPBukkitPlugin.java | 37 ++++++-- .../bukkit/model/ChildPermissionProvider.java | 4 +- .../bukkit/model/DefaultsProvider.java | 33 +++++-- .../luckperms/bungee/LPBungeePlugin.java | 9 +- .../luckperms/common/LuckPermsPlugin.java | 7 ++ .../calculators/PermissionCalculator.java | 3 +- .../luckperms/common/commands/SubCommand.java | 56 +++++++++++- .../generic/permission/PermissionCheck.java | 7 ++ .../permission/PermissionCheckInherits.java | 7 ++ .../generic/permission/PermissionSet.java | 7 +- .../generic/permission/PermissionSetTemp.java | 7 +- .../luckperms/common/utils/DebugHandler.java | 39 +++++++- .../common/utils/PermissionCache.java | 91 +++++++++++++++++++ .../luckperms/sponge/LPSpongePlugin.java | 9 +- .../sponge/service/LuckPermsService.java | 2 + 15 files changed, 278 insertions(+), 40 deletions(-) create mode 100644 common/src/main/java/me/lucko/luckperms/common/utils/PermissionCache.java diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java index 3f20d72c..26fe641e 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitPlugin.java @@ -22,6 +22,7 @@ package me.lucko.luckperms.bukkit; +import com.google.common.collect.ImmutableMap; import lombok.Getter; import me.lucko.luckperms.ApiHandler; import me.lucko.luckperms.api.Contexts; @@ -54,10 +55,7 @@ import me.lucko.luckperms.common.tasks.UpdateTask; import me.lucko.luckperms.common.tracks.TrackManager; import me.lucko.luckperms.common.users.User; import me.lucko.luckperms.common.users.UserManager; -import me.lucko.luckperms.common.utils.BufferedRequest; -import me.lucko.luckperms.common.utils.DebugHandler; -import me.lucko.luckperms.common.utils.LocaleManager; -import me.lucko.luckperms.common.utils.LogFactory; +import me.lucko.luckperms.common.utils.*; import org.bukkit.World; import org.bukkit.command.PluginCommand; import org.bukkit.entity.Player; @@ -70,13 +68,11 @@ import java.io.File; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executor; -import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.stream.Collectors; @Getter public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { - private ExecutorService executorService; private Executor syncExecutor; private Executor asyncExecutor; private VaultHook vaultHook = null; @@ -103,17 +99,19 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { private boolean started = false; private DebugHandler debugHandler; private BukkitSenderFactory senderFactory; + private PermissionCache permissionCache; @Override public void onEnable() { // Used whilst the server is still starting - executorService = Executors.newCachedThreadPool(); - asyncExecutor = executorService; + asyncExecutor = Executors.newCachedThreadPool(); syncExecutor = r -> getServer().getScheduler().runTask(this, r); + Executor bukkitAsyncExecutor = r -> getServer().getScheduler().runTaskAsynchronously(this, r); log = LogFactory.wrap(getLogger()); - debugHandler = new DebugHandler(); + debugHandler = new DebugHandler(bukkitAsyncExecutor); senderFactory = new BukkitSenderFactory(this); + permissionCache = new PermissionCache(bukkitAsyncExecutor); getLog().info("Loading configuration..."); configuration = new BukkitConfig(this); @@ -126,6 +124,25 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { getServer().getScheduler().runTaskLater(this, () -> { defaultsProvider.refresh(); childPermissionProvider.setup(); + + getServer().getScheduler().runTaskAsynchronously(this, () -> { + for (Map.Entry e : defaultsProvider.getOpDefaults().entrySet()) { + permissionCache.offer(e.getKey()); + } + + for (Map.Entry e : defaultsProvider.getNonOpDefaults().entrySet()) { + permissionCache.offer(e.getKey()); + } + + ImmutableMap, ImmutableMap> permissions = childPermissionProvider.getPermissions(); + for (Map.Entry, ImmutableMap> e : permissions.entrySet()) { + permissionCache.offer(e.getKey().getKey()); + for (Map.Entry e1 : e.getValue().entrySet()) { + permissionCache.offer(e1.getKey()); + } + } + }); + }, 1L); // register events @@ -230,7 +247,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { // replace the temporary executor when the Bukkit one starts getServer().getScheduler().runTaskAsynchronously(this, () -> { - asyncExecutor = r -> getServer().getScheduler().runTaskAsynchronously(this, r); + asyncExecutor = bukkitAsyncExecutor; }); started = true; diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/ChildPermissionProvider.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/ChildPermissionProvider.java index 47dff669..1c1b5fc2 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/ChildPermissionProvider.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/ChildPermissionProvider.java @@ -38,7 +38,7 @@ public class ChildPermissionProvider { private ImmutableMap, ImmutableMap> permissions = ImmutableMap.of(); public void setup() { - ImmutableMap.Builder, ImmutableMap> permissions = ImmutableMap.builder(); + Map, ImmutableMap> permissions = new HashMap<>(); for (Permission permission : Bukkit.getServer().getPluginManager().getPermissions()) { // handle true @@ -52,7 +52,7 @@ public class ChildPermissionProvider { permissions.put(Maps.immutableEntry(permission.getName().toLowerCase(), false), ImmutableMap.copyOf(nChildren)); } - this.permissions = permissions.build(); + this.permissions = ImmutableMap.copyOf(permissions); } private static void resolveChildren(Map accumulator, Map children, boolean invert) { diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/DefaultsProvider.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/DefaultsProvider.java index c9bc7ea7..be5cd59c 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/DefaultsProvider.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/model/DefaultsProvider.java @@ -22,7 +22,9 @@ package me.lucko.luckperms.bukkit.model; +import com.google.common.collect.ImmutableMap; import lombok.AllArgsConstructor; +import lombok.Getter; import me.lucko.luckperms.api.Tristate; import org.bukkit.Bukkit; import org.bukkit.permissions.Permissible; @@ -41,8 +43,11 @@ public class DefaultsProvider { private final DummyPermissible opDummy = new DummyPermissible(this::refreshOp); private final DummyPermissible nonOpDummy = new DummyPermissible(this::refreshNonOp); - private final Map op = new HashMap<>(); - private final Map nonOp = new HashMap<>(); + @Getter + private Map opDefaults = ImmutableMap.of(); + + @Getter + private Map nonOpDefaults = ImmutableMap.of(); public void refresh() { refreshOp(); @@ -50,14 +55,24 @@ public class DefaultsProvider { } private void refreshOp() { - calculateDefaults(op, opDummy, true); + unregisterDefaults(opDefaults, opDummy); + + Map builder = new HashMap<>(); + calculateDefaults(builder, opDummy, true); + + opDefaults = ImmutableMap.copyOf(builder); } private void refreshNonOp() { - calculateDefaults(nonOp, nonOpDummy, false); + unregisterDefaults(nonOpDefaults, nonOpDummy); + + Map builder = new HashMap<>(); + calculateDefaults(builder, nonOpDummy, false); + + nonOpDefaults = ImmutableMap.copyOf(builder); } - private static void calculateDefaults(Map map, DummyPermissible p, boolean op) { + private static void unregisterDefaults(Map map, DummyPermissible p) { Set perms = map.keySet(); for (String name : perms) { @@ -66,9 +81,9 @@ public class DefaultsProvider { Bukkit.getServer().getPluginManager().unsubscribeFromDefaultPerms(false, p); Bukkit.getServer().getPluginManager().unsubscribeFromDefaultPerms(true, p); + } - map.clear(); - + private static void calculateDefaults(Map map, DummyPermissible p, boolean op) { Set defaults = Bukkit.getServer().getPluginManager().getDefaultPermissions(op); Bukkit.getServer().getPluginManager().subscribeToDefaultPerms(op, p); @@ -96,14 +111,14 @@ public class DefaultsProvider { } public Tristate hasDefault(String permission, boolean isOp) { - Map map = isOp ? op : nonOp; + Map map = isOp ? opDefaults : nonOpDefaults; Boolean b = map.get(permission); return b == null ? Tristate.UNDEFINED : Tristate.fromBoolean(b); } public int size() { - return op.size() + nonOp.size(); + return opDefaults.size() + nonOpDefaults.size(); } @AllArgsConstructor diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java index 52a2806b..94cb10f0 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/LPBungeePlugin.java @@ -49,10 +49,7 @@ import me.lucko.luckperms.common.tasks.UpdateTask; import me.lucko.luckperms.common.tracks.TrackManager; import me.lucko.luckperms.common.users.User; import me.lucko.luckperms.common.users.UserManager; -import me.lucko.luckperms.common.utils.BufferedRequest; -import me.lucko.luckperms.common.utils.DebugHandler; -import me.lucko.luckperms.common.utils.LocaleManager; -import me.lucko.luckperms.common.utils.LogFactory; +import me.lucko.luckperms.common.utils.*; import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.plugin.Plugin; @@ -89,13 +86,15 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin { private BufferedRequest updateTaskBuffer; private DebugHandler debugHandler; private BungeeSenderFactory senderFactory; + private PermissionCache permissionCache; @Override public void onEnable() { executor = r -> getProxy().getScheduler().runAsync(this, r); log = LogFactory.wrap(getLogger()); - debugHandler = new DebugHandler(); + debugHandler = new DebugHandler(executor); senderFactory = new BungeeSenderFactory(this); + permissionCache = new PermissionCache(executor); getLog().info("Loading configuration..."); configuration = new BungeeConfig(this); diff --git a/common/src/main/java/me/lucko/luckperms/common/LuckPermsPlugin.java b/common/src/main/java/me/lucko/luckperms/common/LuckPermsPlugin.java index 4a326377..f0c9d881 100644 --- a/common/src/main/java/me/lucko/luckperms/common/LuckPermsPlugin.java +++ b/common/src/main/java/me/lucko/luckperms/common/LuckPermsPlugin.java @@ -44,6 +44,7 @@ import me.lucko.luckperms.common.users.UserManager; import me.lucko.luckperms.common.utils.BufferedRequest; import me.lucko.luckperms.common.utils.DebugHandler; import me.lucko.luckperms.common.utils.LocaleManager; +import me.lucko.luckperms.common.utils.PermissionCache; import java.io.File; import java.util.*; @@ -147,6 +148,12 @@ public interface LuckPermsPlugin { */ DebugHandler getDebugHandler(); + /** + * Gets the permission caching instance for the platform. + * @return the permission cache instance + */ + PermissionCache getPermissionCache(); + /** * Execute a runnable asynchronously * @param r the task to run diff --git a/common/src/main/java/me/lucko/luckperms/common/calculators/PermissionCalculator.java b/common/src/main/java/me/lucko/luckperms/common/calculators/PermissionCalculator.java index eb4fcd95..0bc51bce 100644 --- a/common/src/main/java/me/lucko/luckperms/common/calculators/PermissionCalculator.java +++ b/common/src/main/java/me/lucko/luckperms/common/calculators/PermissionCalculator.java @@ -56,7 +56,8 @@ public class PermissionCalculator { public Tristate getPermissionValue(String permission) { permission = permission.toLowerCase(); Tristate t = cache.getUnchecked(permission); - plugin.getDebugHandler().printOutput(objectName, permission, t); + plugin.getDebugHandler().offer(objectName, permission, t); + plugin.getPermissionCache().offer(permission); return t; } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/SubCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/SubCommand.java index c6ab0170..5770ca28 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/SubCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/SubCommand.java @@ -22,6 +22,7 @@ package me.lucko.luckperms.common.commands; +import com.google.common.base.Splitter; import lombok.Getter; import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.commands.sender.Sender; @@ -31,11 +32,9 @@ import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.groups.Group; import me.lucko.luckperms.common.tracks.Track; import me.lucko.luckperms.common.users.User; +import me.lucko.luckperms.common.utils.PermissionCache; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -101,6 +100,55 @@ public abstract class SubCommand extends Command { } } + public static List getPermissionTabComplete(List args, PermissionCache cache) { + if (args.size() <= 1) { + if (args.isEmpty() || args.get(0).equals("")) { + return cache.getRootNode().getChildren() + .map(Map::keySet) + .map(s -> s.stream().collect(Collectors.toList())) + .orElse(Collections.emptyList()); + } + + String start = args.get(0).toLowerCase(); + List parts = new ArrayList<>(Splitter.on('.').splitToList(start)); + PermissionCache.Node root = cache.getRootNode(); + + if (parts.size() <= 1) { + if (!root.getChildren().isPresent()) { + return Collections.emptyList(); + } + + return root.getChildren().get().keySet().stream().filter(s -> s.startsWith(start)).collect(Collectors.toList()); + } + + String incomplete = parts.remove(parts.size() - 1); + + for (String s : parts) { + if (!root.getChildren().isPresent()) { + return Collections.emptyList(); + } + + PermissionCache.Node n = root.getChildren().get().get(s); + if (n == null) { + return Collections.emptyList(); + } + + root = n; + } + + if (!root.getChildren().isPresent()) { + return Collections.emptyList(); + } + + return root.getChildren().get().keySet().stream() + .filter(s -> s.startsWith(incomplete)) + .map(s -> parts.stream().collect(Collectors.joining(".")) + "." + s) + .collect(Collectors.toList()); + } + + return Collections.emptyList(); + } + private static List getTabComplete(List options, List args) { if (args.size() <= 1) { if (args.isEmpty() || args.get(0).equalsIgnoreCase("")) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionCheck.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionCheck.java index 25c3dc78..2dc41967 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionCheck.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionCheck.java @@ -38,6 +38,8 @@ import me.lucko.luckperms.common.utils.Predicates; import java.util.List; +import static me.lucko.luckperms.common.commands.SubCommand.getPermissionTabComplete; + public class PermissionCheck extends SharedSubCommand { public PermissionCheck() { super("check", "Checks to see if the object has a certain permission node", Permission.USER_PERM_CHECK, @@ -70,4 +72,9 @@ public class PermissionCheck extends SharedSubCommand { return CommandResult.SUCCESS; } + + @Override + public List onTabComplete(LuckPermsPlugin plugin, Sender sender, List args) { + return getPermissionTabComplete(args, plugin.getPermissionCache()); + } } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionCheckInherits.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionCheckInherits.java index be5d221e..ccb21ba1 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionCheckInherits.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionCheckInherits.java @@ -39,6 +39,8 @@ import me.lucko.luckperms.common.utils.Predicates; import java.util.List; +import static me.lucko.luckperms.common.commands.SubCommand.getPermissionTabComplete; + public class PermissionCheckInherits extends SharedSubCommand { public PermissionCheckInherits() { super("checkinherits", "Checks to see if the object inherits a certain permission node", @@ -79,4 +81,9 @@ public class PermissionCheckInherits extends SharedSubCommand { (location != null ? " &7(inherited from &a" + location + "&7)" : "")); return CommandResult.SUCCESS; } + + @Override + public List onTabComplete(LuckPermsPlugin plugin, Sender sender, List args) { + return getPermissionTabComplete(args, plugin.getPermissionCache()); + } } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionSet.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionSet.java index 08be3f64..20f7a553 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionSet.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionSet.java @@ -41,6 +41,7 @@ import java.util.List; import java.util.stream.Collectors; import static me.lucko.luckperms.common.commands.SubCommand.getBoolTabComplete; +import static me.lucko.luckperms.common.commands.SubCommand.getPermissionTabComplete; public class PermissionSet extends SharedSubCommand { public PermissionSet() { @@ -94,6 +95,10 @@ public class PermissionSet extends SharedSubCommand { @Override public List onTabComplete(LuckPermsPlugin plugin, Sender sender, List args) { - return getBoolTabComplete(args); + List ret = getBoolTabComplete(args); + if (!ret.isEmpty()) { + return ret; + } + return getPermissionTabComplete(args, plugin.getPermissionCache()); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionSetTemp.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionSetTemp.java index a08729d0..ea36d1bd 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionSetTemp.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/permission/PermissionSetTemp.java @@ -42,6 +42,7 @@ import java.util.List; import java.util.stream.Collectors; import static me.lucko.luckperms.common.commands.SubCommand.getBoolTabComplete; +import static me.lucko.luckperms.common.commands.SubCommand.getPermissionTabComplete; public class PermissionSetTemp extends SharedSubCommand { public PermissionSetTemp() { @@ -104,6 +105,10 @@ public class PermissionSetTemp extends SharedSubCommand { @Override public List onTabComplete(LuckPermsPlugin plugin, Sender sender, List args) { - return getBoolTabComplete(args); + List ret = getBoolTabComplete(args); + if (!ret.isEmpty()) { + return ret; + } + return getPermissionTabComplete(args, plugin.getPermissionCache()); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/DebugHandler.java b/common/src/main/java/me/lucko/luckperms/common/utils/DebugHandler.java index 6ea5d1fd..39775b2c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/utils/DebugHandler.java +++ b/common/src/main/java/me/lucko/luckperms/common/utils/DebugHandler.java @@ -32,13 +32,36 @@ import me.lucko.luckperms.common.constants.Message; import java.util.List; import java.util.Map; +import java.util.Queue; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executor; public class DebugHandler { - private final Map> listeners = new ConcurrentHashMap<>(); + private final Map> listeners; + private final Queue queue; - public void printOutput(String checked, String node, Tristate value) { + public DebugHandler(Executor executor) { + listeners = new ConcurrentHashMap<>(); + queue = new ConcurrentLinkedQueue<>(); + + executor.execute(() -> { + while (true) { + for (Data e; (e = queue.poll()) != null;) { + handleOutput(e.getChecked(), e.getNode(), e.getValue()); + } + + try { + Thread.sleep(200); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }); + } + + private void handleOutput(String checked, String node, Tristate value) { all: for (Map.Entry> e : listeners.entrySet()) { for (String filter : e.getValue()) { @@ -57,6 +80,10 @@ public class DebugHandler { } } + public void offer(String checked, String node, Tristate value) { + queue.offer(new Data(checked, node, value)); + } + public void register(Sender sender, List filters) { listeners.put(new Receiver(sender.getUuid(), sender), ImmutableList.copyOf(filters)); } @@ -72,4 +99,12 @@ public class DebugHandler { private final UUID uuid; private final Sender sender; } + + @Getter + @AllArgsConstructor + private static final class Data { + private final String checked; + private final String node; + private final Tristate value; + } } diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/PermissionCache.java b/common/src/main/java/me/lucko/luckperms/common/utils/PermissionCache.java new file mode 100644 index 00000000..18ed806e --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/utils/PermissionCache.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.common.utils; + +import com.google.common.base.Splitter; +import lombok.Getter; +import lombok.NonNull; + +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Queue; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executor; + +public class PermissionCache { + + @Getter + private final Node rootNode; + private final Queue queue; + + public PermissionCache(Executor executor) { + rootNode = new Node(); + queue = new ConcurrentLinkedQueue<>(); + + executor.execute(() -> { + while (true) { + for (String e; (e = queue.poll()) != null;) { + insert(e.toLowerCase()); + } + + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }); + } + + public void offer(@NonNull String permission) { + queue.offer(permission); + } + + private void insert(String permission) { + List parts = Splitter.on('.').splitToList(permission); + + Node current = rootNode; + for (String part : parts) { + current = current.getChildMap().computeIfAbsent(part, s -> new Node()); + } + } + + public static class Node { + private Map children = null; + + // lazy init + private synchronized Map getChildMap() { + if (children == null) { + children = new ConcurrentHashMap<>(); + } + return children; + } + + public Optional> getChildren() { + return Optional.ofNullable(children); + } + } + +} 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 9960788d..38bd58dd 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java @@ -49,10 +49,7 @@ import me.lucko.luckperms.common.tasks.UpdateTask; import me.lucko.luckperms.common.tracks.TrackManager; import me.lucko.luckperms.common.users.User; import me.lucko.luckperms.common.users.UserManager; -import me.lucko.luckperms.common.utils.BufferedRequest; -import me.lucko.luckperms.common.utils.DebugHandler; -import me.lucko.luckperms.common.utils.LocaleManager; -import me.lucko.luckperms.common.utils.LogFactory; +import me.lucko.luckperms.common.utils.*; import me.lucko.luckperms.sponge.commands.SpongeMainCommand; import me.lucko.luckperms.sponge.contexts.WorldCalculator; import me.lucko.luckperms.sponge.service.LuckPermsService; @@ -136,12 +133,14 @@ public class LPSpongePlugin implements LuckPermsPlugin { private BufferedRequest updateTaskBuffer; private DebugHandler debugHandler; private SpongeSenderFactory senderFactory; + private PermissionCache permissionCache; @Listener(order = Order.FIRST) public void onEnable(GamePreInitializationEvent event) { log = LogFactory.wrap(logger); - debugHandler = new DebugHandler(); + debugHandler = new DebugHandler(asyncExecutor); senderFactory = new SpongeSenderFactory(this); + permissionCache = new PermissionCache(asyncExecutor); timings = new LPTimings(this); getLog().info("Loading configuration..."); diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java index a5df222a..e96346d3 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java @@ -234,6 +234,8 @@ public class LuckPermsService implements PermissionService { subject.getTransientSubjectData().setPermission(SubjectData.GLOBAL_CONTEXT, id, assignment.getValue()); } + service.getPlugin().getPermissionCache().offer(id); + return d; } }