Implement tab completion for permissions

This commit is contained in:
Luck 2016-11-24 19:07:01 +00:00
parent 69b3c96e09
commit cf456cff81
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
15 changed files with 278 additions and 40 deletions

View File

@ -22,6 +22,7 @@
package me.lucko.luckperms.bukkit; package me.lucko.luckperms.bukkit;
import com.google.common.collect.ImmutableMap;
import lombok.Getter; import lombok.Getter;
import me.lucko.luckperms.ApiHandler; import me.lucko.luckperms.ApiHandler;
import me.lucko.luckperms.api.Contexts; 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.tracks.TrackManager;
import me.lucko.luckperms.common.users.User; import me.lucko.luckperms.common.users.User;
import me.lucko.luckperms.common.users.UserManager; import me.lucko.luckperms.common.users.UserManager;
import me.lucko.luckperms.common.utils.BufferedRequest; import me.lucko.luckperms.common.utils.*;
import me.lucko.luckperms.common.utils.DebugHandler;
import me.lucko.luckperms.common.utils.LocaleManager;
import me.lucko.luckperms.common.utils.LogFactory;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -70,13 +68,11 @@ import java.io.File;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Getter @Getter
public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
private ExecutorService executorService;
private Executor syncExecutor; private Executor syncExecutor;
private Executor asyncExecutor; private Executor asyncExecutor;
private VaultHook vaultHook = null; private VaultHook vaultHook = null;
@ -103,17 +99,19 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
private boolean started = false; private boolean started = false;
private DebugHandler debugHandler; private DebugHandler debugHandler;
private BukkitSenderFactory senderFactory; private BukkitSenderFactory senderFactory;
private PermissionCache permissionCache;
@Override @Override
public void onEnable() { public void onEnable() {
// Used whilst the server is still starting // Used whilst the server is still starting
executorService = Executors.newCachedThreadPool(); asyncExecutor = Executors.newCachedThreadPool();
asyncExecutor = executorService;
syncExecutor = r -> getServer().getScheduler().runTask(this, r); syncExecutor = r -> getServer().getScheduler().runTask(this, r);
Executor bukkitAsyncExecutor = r -> getServer().getScheduler().runTaskAsynchronously(this, r);
log = LogFactory.wrap(getLogger()); log = LogFactory.wrap(getLogger());
debugHandler = new DebugHandler(); debugHandler = new DebugHandler(bukkitAsyncExecutor);
senderFactory = new BukkitSenderFactory(this); senderFactory = new BukkitSenderFactory(this);
permissionCache = new PermissionCache(bukkitAsyncExecutor);
getLog().info("Loading configuration..."); getLog().info("Loading configuration...");
configuration = new BukkitConfig(this); configuration = new BukkitConfig(this);
@ -126,6 +124,25 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
getServer().getScheduler().runTaskLater(this, () -> { getServer().getScheduler().runTaskLater(this, () -> {
defaultsProvider.refresh(); defaultsProvider.refresh();
childPermissionProvider.setup(); childPermissionProvider.setup();
getServer().getScheduler().runTaskAsynchronously(this, () -> {
for (Map.Entry<String, Boolean> e : defaultsProvider.getOpDefaults().entrySet()) {
permissionCache.offer(e.getKey());
}
for (Map.Entry<String, Boolean> e : defaultsProvider.getNonOpDefaults().entrySet()) {
permissionCache.offer(e.getKey());
}
ImmutableMap<Map.Entry<String, Boolean>, ImmutableMap<String, Boolean>> permissions = childPermissionProvider.getPermissions();
for (Map.Entry<Map.Entry<String, Boolean>, ImmutableMap<String, Boolean>> e : permissions.entrySet()) {
permissionCache.offer(e.getKey().getKey());
for (Map.Entry<String, Boolean> e1 : e.getValue().entrySet()) {
permissionCache.offer(e1.getKey());
}
}
});
}, 1L); }, 1L);
// register events // register events
@ -230,7 +247,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
// replace the temporary executor when the Bukkit one starts // replace the temporary executor when the Bukkit one starts
getServer().getScheduler().runTaskAsynchronously(this, () -> { getServer().getScheduler().runTaskAsynchronously(this, () -> {
asyncExecutor = r -> getServer().getScheduler().runTaskAsynchronously(this, r); asyncExecutor = bukkitAsyncExecutor;
}); });
started = true; started = true;

View File

@ -38,7 +38,7 @@ public class ChildPermissionProvider {
private ImmutableMap<Map.Entry<String, Boolean>, ImmutableMap<String, Boolean>> permissions = ImmutableMap.of(); private ImmutableMap<Map.Entry<String, Boolean>, ImmutableMap<String, Boolean>> permissions = ImmutableMap.of();
public void setup() { public void setup() {
ImmutableMap.Builder<Map.Entry<String, Boolean>, ImmutableMap<String, Boolean>> permissions = ImmutableMap.builder(); Map<Map.Entry<String, Boolean>, ImmutableMap<String, Boolean>> permissions = new HashMap<>();
for (Permission permission : Bukkit.getServer().getPluginManager().getPermissions()) { for (Permission permission : Bukkit.getServer().getPluginManager().getPermissions()) {
// handle true // handle true
@ -52,7 +52,7 @@ public class ChildPermissionProvider {
permissions.put(Maps.immutableEntry(permission.getName().toLowerCase(), false), ImmutableMap.copyOf(nChildren)); 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<String, Boolean> accumulator, Map<String, Boolean> children, boolean invert) { private static void resolveChildren(Map<String, Boolean> accumulator, Map<String, Boolean> children, boolean invert) {

View File

@ -22,7 +22,9 @@
package me.lucko.luckperms.bukkit.model; package me.lucko.luckperms.bukkit.model;
import com.google.common.collect.ImmutableMap;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter;
import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.Tristate;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.permissions.Permissible; import org.bukkit.permissions.Permissible;
@ -41,8 +43,11 @@ public class DefaultsProvider {
private final DummyPermissible opDummy = new DummyPermissible(this::refreshOp); private final DummyPermissible opDummy = new DummyPermissible(this::refreshOp);
private final DummyPermissible nonOpDummy = new DummyPermissible(this::refreshNonOp); private final DummyPermissible nonOpDummy = new DummyPermissible(this::refreshNonOp);
private final Map<String, Boolean> op = new HashMap<>(); @Getter
private final Map<String, Boolean> nonOp = new HashMap<>(); private Map<String, Boolean> opDefaults = ImmutableMap.of();
@Getter
private Map<String, Boolean> nonOpDefaults = ImmutableMap.of();
public void refresh() { public void refresh() {
refreshOp(); refreshOp();
@ -50,14 +55,24 @@ public class DefaultsProvider {
} }
private void refreshOp() { private void refreshOp() {
calculateDefaults(op, opDummy, true); unregisterDefaults(opDefaults, opDummy);
Map<String, Boolean> builder = new HashMap<>();
calculateDefaults(builder, opDummy, true);
opDefaults = ImmutableMap.copyOf(builder);
} }
private void refreshNonOp() { private void refreshNonOp() {
calculateDefaults(nonOp, nonOpDummy, false); unregisterDefaults(nonOpDefaults, nonOpDummy);
Map<String, Boolean> builder = new HashMap<>();
calculateDefaults(builder, nonOpDummy, false);
nonOpDefaults = ImmutableMap.copyOf(builder);
} }
private static void calculateDefaults(Map<String, Boolean> map, DummyPermissible p, boolean op) { private static void unregisterDefaults(Map<String, Boolean> map, DummyPermissible p) {
Set<String> perms = map.keySet(); Set<String> perms = map.keySet();
for (String name : perms) { for (String name : perms) {
@ -66,9 +81,9 @@ public class DefaultsProvider {
Bukkit.getServer().getPluginManager().unsubscribeFromDefaultPerms(false, p); Bukkit.getServer().getPluginManager().unsubscribeFromDefaultPerms(false, p);
Bukkit.getServer().getPluginManager().unsubscribeFromDefaultPerms(true, p); Bukkit.getServer().getPluginManager().unsubscribeFromDefaultPerms(true, p);
}
map.clear(); private static void calculateDefaults(Map<String, Boolean> map, DummyPermissible p, boolean op) {
Set<Permission> defaults = Bukkit.getServer().getPluginManager().getDefaultPermissions(op); Set<Permission> defaults = Bukkit.getServer().getPluginManager().getDefaultPermissions(op);
Bukkit.getServer().getPluginManager().subscribeToDefaultPerms(op, p); Bukkit.getServer().getPluginManager().subscribeToDefaultPerms(op, p);
@ -96,14 +111,14 @@ public class DefaultsProvider {
} }
public Tristate hasDefault(String permission, boolean isOp) { public Tristate hasDefault(String permission, boolean isOp) {
Map<String, Boolean> map = isOp ? op : nonOp; Map<String, Boolean> map = isOp ? opDefaults : nonOpDefaults;
Boolean b = map.get(permission); Boolean b = map.get(permission);
return b == null ? Tristate.UNDEFINED : Tristate.fromBoolean(b); return b == null ? Tristate.UNDEFINED : Tristate.fromBoolean(b);
} }
public int size() { public int size() {
return op.size() + nonOp.size(); return opDefaults.size() + nonOpDefaults.size();
} }
@AllArgsConstructor @AllArgsConstructor

View File

@ -49,10 +49,7 @@ import me.lucko.luckperms.common.tasks.UpdateTask;
import me.lucko.luckperms.common.tracks.TrackManager; import me.lucko.luckperms.common.tracks.TrackManager;
import me.lucko.luckperms.common.users.User; import me.lucko.luckperms.common.users.User;
import me.lucko.luckperms.common.users.UserManager; import me.lucko.luckperms.common.users.UserManager;
import me.lucko.luckperms.common.utils.BufferedRequest; import me.lucko.luckperms.common.utils.*;
import me.lucko.luckperms.common.utils.DebugHandler;
import me.lucko.luckperms.common.utils.LocaleManager;
import me.lucko.luckperms.common.utils.LogFactory;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.api.plugin.Plugin;
@ -89,13 +86,15 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
private BufferedRequest<Void> updateTaskBuffer; private BufferedRequest<Void> updateTaskBuffer;
private DebugHandler debugHandler; private DebugHandler debugHandler;
private BungeeSenderFactory senderFactory; private BungeeSenderFactory senderFactory;
private PermissionCache permissionCache;
@Override @Override
public void onEnable() { public void onEnable() {
executor = r -> getProxy().getScheduler().runAsync(this, r); executor = r -> getProxy().getScheduler().runAsync(this, r);
log = LogFactory.wrap(getLogger()); log = LogFactory.wrap(getLogger());
debugHandler = new DebugHandler(); debugHandler = new DebugHandler(executor);
senderFactory = new BungeeSenderFactory(this); senderFactory = new BungeeSenderFactory(this);
permissionCache = new PermissionCache(executor);
getLog().info("Loading configuration..."); getLog().info("Loading configuration...");
configuration = new BungeeConfig(this); configuration = new BungeeConfig(this);

View File

@ -44,6 +44,7 @@ import me.lucko.luckperms.common.users.UserManager;
import me.lucko.luckperms.common.utils.BufferedRequest; import me.lucko.luckperms.common.utils.BufferedRequest;
import me.lucko.luckperms.common.utils.DebugHandler; import me.lucko.luckperms.common.utils.DebugHandler;
import me.lucko.luckperms.common.utils.LocaleManager; import me.lucko.luckperms.common.utils.LocaleManager;
import me.lucko.luckperms.common.utils.PermissionCache;
import java.io.File; import java.io.File;
import java.util.*; import java.util.*;
@ -147,6 +148,12 @@ public interface LuckPermsPlugin {
*/ */
DebugHandler getDebugHandler(); DebugHandler getDebugHandler();
/**
* Gets the permission caching instance for the platform.
* @return the permission cache instance
*/
PermissionCache getPermissionCache();
/** /**
* Execute a runnable asynchronously * Execute a runnable asynchronously
* @param r the task to run * @param r the task to run

View File

@ -56,7 +56,8 @@ public class PermissionCalculator {
public Tristate getPermissionValue(String permission) { public Tristate getPermissionValue(String permission) {
permission = permission.toLowerCase(); permission = permission.toLowerCase();
Tristate t = cache.getUnchecked(permission); Tristate t = cache.getUnchecked(permission);
plugin.getDebugHandler().printOutput(objectName, permission, t); plugin.getDebugHandler().offer(objectName, permission, t);
plugin.getPermissionCache().offer(permission);
return t; return t;
} }

View File

@ -22,6 +22,7 @@
package me.lucko.luckperms.common.commands; package me.lucko.luckperms.common.commands;
import com.google.common.base.Splitter;
import lombok.Getter; import lombok.Getter;
import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.LuckPermsPlugin;
import me.lucko.luckperms.common.commands.sender.Sender; 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.groups.Group;
import me.lucko.luckperms.common.tracks.Track; import me.lucko.luckperms.common.tracks.Track;
import me.lucko.luckperms.common.users.User; import me.lucko.luckperms.common.users.User;
import me.lucko.luckperms.common.utils.PermissionCache;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -101,6 +100,55 @@ public abstract class SubCommand<T> extends Command<T, Void> {
} }
} }
public static List<String> getPermissionTabComplete(List<String> 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<String> 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<String> getTabComplete(List<String> options, List<String> args) { private static List<String> getTabComplete(List<String> options, List<String> args) {
if (args.size() <= 1) { if (args.size() <= 1) {
if (args.isEmpty() || args.get(0).equalsIgnoreCase("")) { if (args.isEmpty() || args.get(0).equalsIgnoreCase("")) {

View File

@ -38,6 +38,8 @@ import me.lucko.luckperms.common.utils.Predicates;
import java.util.List; import java.util.List;
import static me.lucko.luckperms.common.commands.SubCommand.getPermissionTabComplete;
public class PermissionCheck extends SharedSubCommand { public class PermissionCheck extends SharedSubCommand {
public PermissionCheck() { public PermissionCheck() {
super("check", "Checks to see if the object has a certain permission node", Permission.USER_PERM_CHECK, 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; return CommandResult.SUCCESS;
} }
@Override
public List<String> onTabComplete(LuckPermsPlugin plugin, Sender sender, List<String> args) {
return getPermissionTabComplete(args, plugin.getPermissionCache());
}
} }

View File

@ -39,6 +39,8 @@ import me.lucko.luckperms.common.utils.Predicates;
import java.util.List; import java.util.List;
import static me.lucko.luckperms.common.commands.SubCommand.getPermissionTabComplete;
public class PermissionCheckInherits extends SharedSubCommand { public class PermissionCheckInherits extends SharedSubCommand {
public PermissionCheckInherits() { public PermissionCheckInherits() {
super("checkinherits", "Checks to see if the object inherits a certain permission node", 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)" : "")); (location != null ? " &7(inherited from &a" + location + "&7)" : ""));
return CommandResult.SUCCESS; return CommandResult.SUCCESS;
} }
@Override
public List<String> onTabComplete(LuckPermsPlugin plugin, Sender sender, List<String> args) {
return getPermissionTabComplete(args, plugin.getPermissionCache());
}
} }

View File

@ -41,6 +41,7 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static me.lucko.luckperms.common.commands.SubCommand.getBoolTabComplete; import static me.lucko.luckperms.common.commands.SubCommand.getBoolTabComplete;
import static me.lucko.luckperms.common.commands.SubCommand.getPermissionTabComplete;
public class PermissionSet extends SharedSubCommand { public class PermissionSet extends SharedSubCommand {
public PermissionSet() { public PermissionSet() {
@ -94,6 +95,10 @@ public class PermissionSet extends SharedSubCommand {
@Override @Override
public List<String> onTabComplete(LuckPermsPlugin plugin, Sender sender, List<String> args) { public List<String> onTabComplete(LuckPermsPlugin plugin, Sender sender, List<String> args) {
return getBoolTabComplete(args); List<String> ret = getBoolTabComplete(args);
if (!ret.isEmpty()) {
return ret;
}
return getPermissionTabComplete(args, plugin.getPermissionCache());
} }
} }

View File

@ -42,6 +42,7 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static me.lucko.luckperms.common.commands.SubCommand.getBoolTabComplete; import static me.lucko.luckperms.common.commands.SubCommand.getBoolTabComplete;
import static me.lucko.luckperms.common.commands.SubCommand.getPermissionTabComplete;
public class PermissionSetTemp extends SharedSubCommand { public class PermissionSetTemp extends SharedSubCommand {
public PermissionSetTemp() { public PermissionSetTemp() {
@ -104,6 +105,10 @@ public class PermissionSetTemp extends SharedSubCommand {
@Override @Override
public List<String> onTabComplete(LuckPermsPlugin plugin, Sender sender, List<String> args) { public List<String> onTabComplete(LuckPermsPlugin plugin, Sender sender, List<String> args) {
return getBoolTabComplete(args); List<String> ret = getBoolTabComplete(args);
if (!ret.isEmpty()) {
return ret;
}
return getPermissionTabComplete(args, plugin.getPermissionCache());
} }
} }

View File

@ -32,13 +32,36 @@ import me.lucko.luckperms.common.constants.Message;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Queue;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;
public class DebugHandler { public class DebugHandler {
private final Map<Receiver, List<String>> listeners = new ConcurrentHashMap<>(); private final Map<Receiver, List<String>> listeners;
private final Queue<Data> 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: all:
for (Map.Entry<Receiver, List<String>> e : listeners.entrySet()) { for (Map.Entry<Receiver, List<String>> e : listeners.entrySet()) {
for (String filter : e.getValue()) { 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<String> filters) { public void register(Sender sender, List<String> filters) {
listeners.put(new Receiver(sender.getUuid(), sender), ImmutableList.copyOf(filters)); listeners.put(new Receiver(sender.getUuid(), sender), ImmutableList.copyOf(filters));
} }
@ -72,4 +99,12 @@ public class DebugHandler {
private final UUID uuid; private final UUID uuid;
private final Sender sender; private final Sender sender;
} }
@Getter
@AllArgsConstructor
private static final class Data {
private final String checked;
private final String node;
private final Tristate value;
}
} }

View File

@ -0,0 +1,91 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* 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<String> 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<String> 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<String, Node> children = null;
// lazy init
private synchronized Map<String, Node> getChildMap() {
if (children == null) {
children = new ConcurrentHashMap<>();
}
return children;
}
public Optional<Map<String, Node>> getChildren() {
return Optional.ofNullable(children);
}
}
}

View File

@ -49,10 +49,7 @@ import me.lucko.luckperms.common.tasks.UpdateTask;
import me.lucko.luckperms.common.tracks.TrackManager; import me.lucko.luckperms.common.tracks.TrackManager;
import me.lucko.luckperms.common.users.User; import me.lucko.luckperms.common.users.User;
import me.lucko.luckperms.common.users.UserManager; import me.lucko.luckperms.common.users.UserManager;
import me.lucko.luckperms.common.utils.BufferedRequest; import me.lucko.luckperms.common.utils.*;
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.sponge.commands.SpongeMainCommand; import me.lucko.luckperms.sponge.commands.SpongeMainCommand;
import me.lucko.luckperms.sponge.contexts.WorldCalculator; import me.lucko.luckperms.sponge.contexts.WorldCalculator;
import me.lucko.luckperms.sponge.service.LuckPermsService; import me.lucko.luckperms.sponge.service.LuckPermsService;
@ -136,12 +133,14 @@ public class LPSpongePlugin implements LuckPermsPlugin {
private BufferedRequest<Void> updateTaskBuffer; private BufferedRequest<Void> updateTaskBuffer;
private DebugHandler debugHandler; private DebugHandler debugHandler;
private SpongeSenderFactory senderFactory; private SpongeSenderFactory senderFactory;
private PermissionCache permissionCache;
@Listener(order = Order.FIRST) @Listener(order = Order.FIRST)
public void onEnable(GamePreInitializationEvent event) { public void onEnable(GamePreInitializationEvent event) {
log = LogFactory.wrap(logger); log = LogFactory.wrap(logger);
debugHandler = new DebugHandler(); debugHandler = new DebugHandler(asyncExecutor);
senderFactory = new SpongeSenderFactory(this); senderFactory = new SpongeSenderFactory(this);
permissionCache = new PermissionCache(asyncExecutor);
timings = new LPTimings(this); timings = new LPTimings(this);
getLog().info("Loading configuration..."); getLog().info("Loading configuration...");

View File

@ -234,6 +234,8 @@ public class LuckPermsService implements PermissionService {
subject.getTransientSubjectData().setPermission(SubjectData.GLOBAL_CONTEXT, id, assignment.getValue()); subject.getTransientSubjectData().setPermission(SubjectData.GLOBAL_CONTEXT, id, assignment.getValue());
} }
service.getPlugin().getPermissionCache().offer(id);
return d; return d;
} }
} }