Cache Vault lookups & properly sort listnodes output

This commit is contained in:
Luck 2016-09-27 20:18:06 +01:00
parent ab6b011225
commit 4c21957b04
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
12 changed files with 339 additions and 56 deletions

View File

@ -90,8 +90,12 @@ class BukkitListener extends AbstractListener implements Listener {
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
public void onPlayerQuit(PlayerQuitEvent e) { public void onPlayerQuit(PlayerQuitEvent e) {
plugin.getWorldCalculator().getWorldCache().remove(plugin.getUuidCache().getUUID(e.getPlayer().getUniqueId())); final UUID internal = plugin.getUuidCache().getUUID(e.getPlayer().getUniqueId());
plugin.getWorldCalculator().getWorldCache().remove(internal);
onLeave(e.getPlayer().getUniqueId()); onLeave(e.getPlayer().getUniqueId());
if (plugin.getVaultHook() != null && plugin.getVaultHook().isHooked()) {
plugin.getVaultHook().getPermissionHook().getVaultUserManager().clearUser(internal);
}
} }
@EventHandler @EventHandler

View File

@ -62,6 +62,8 @@ import java.util.stream.Collectors;
@Getter @Getter
public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
private VaultHook vaultHook = null;
private final Set<UUID> ignoringLogs = ConcurrentHashMap.newKeySet(); private final Set<UUID> ignoringLogs = ConcurrentHashMap.newKeySet();
private LPConfiguration configuration; private LPConfiguration configuration;
private BukkitUserManager userManager; private BukkitUserManager userManager;
@ -142,7 +144,8 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
getLog().info("Attempting to hook into Vault..."); getLog().info("Attempting to hook into Vault...");
try { try {
if (getServer().getPluginManager().isPluginEnabled("Vault")) { if (getServer().getPluginManager().isPluginEnabled("Vault")) {
VaultHook.hook(this); vaultHook = new VaultHook();
vaultHook.hook(this);
getLog().info("Registered Vault permission & chat hook."); getLog().info("Registered Vault permission & chat hook.");
} else { } else {
getLog().info("Vault not found."); getLog().info("Vault not found.");
@ -181,6 +184,10 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
getLog().info("Unregistering API..."); getLog().info("Unregistering API...");
LuckPerms.unregisterProvider(); LuckPerms.unregisterProvider();
getServer().getServicesManager().unregisterAll(this); getServer().getServicesManager().unregisterAll(this);
if (vaultHook != null) {
vaultHook.unhook(this);
}
} }
@Override @Override

View File

@ -23,8 +23,6 @@
package me.lucko.luckperms.api.vault; package me.lucko.luckperms.api.vault;
import lombok.NonNull; import lombok.NonNull;
import lombok.Setter;
import me.lucko.luckperms.LPBukkitPlugin;
import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.Node;
import me.lucko.luckperms.contexts.Contexts; import me.lucko.luckperms.contexts.Contexts;
import me.lucko.luckperms.core.PermissionHolder; import me.lucko.luckperms.core.PermissionHolder;
@ -55,12 +53,9 @@ import static me.lucko.luckperms.utils.ArgumentChecker.unescapeCharacters;
* Node that special characters used within LuckPerms are escaped: * Node that special characters used within LuckPerms are escaped:
* See {@link me.lucko.luckperms.utils.ArgumentChecker#escapeCharacters(String)} * See {@link me.lucko.luckperms.utils.ArgumentChecker#escapeCharacters(String)}
* *
* Registered on normal priority so other plugins can override. * Registered on normal priority so other perms.getPlugin()s can override.
*/ */
public class VaultChatHook extends Chat { public class VaultChatHook extends Chat {
@Setter
private LPBukkitPlugin plugin;
private final VaultPermissionHook perms; private final VaultPermissionHook perms;
VaultChatHook(VaultPermissionHook perms) { VaultChatHook(VaultPermissionHook perms) {
@ -191,47 +186,47 @@ public class VaultChatHook extends Chat {
} }
public String getPlayerPrefix(String world, @NonNull String player) { public String getPlayerPrefix(String world, @NonNull String player) {
final User user = plugin.getUserManager().get(player); final User user = perms.getPlugin().getUserManager().get(player);
return getChatMeta(true, user, world); return getChatMeta(true, user, world);
} }
public void setPlayerPrefix(String world, @NonNull String player, @NonNull String prefix) { public void setPlayerPrefix(String world, @NonNull String player, @NonNull String prefix) {
final User user = plugin.getUserManager().get(player); final User user = perms.getPlugin().getUserManager().get(player);
setChatMeta(true, user, prefix, world); setChatMeta(true, user, prefix, world);
} }
public String getPlayerSuffix(String world, @NonNull String player) { public String getPlayerSuffix(String world, @NonNull String player) {
final User user = plugin.getUserManager().get(player); final User user = perms.getPlugin().getUserManager().get(player);
return getChatMeta(false, user, world); return getChatMeta(false, user, world);
} }
public void setPlayerSuffix(String world, @NonNull String player, @NonNull String suffix) { public void setPlayerSuffix(String world, @NonNull String player, @NonNull String suffix) {
final User user = plugin.getUserManager().get(player); final User user = perms.getPlugin().getUserManager().get(player);
setChatMeta(false, user, suffix, world); setChatMeta(false, user, suffix, world);
} }
public String getGroupPrefix(String world, @NonNull String group) { public String getGroupPrefix(String world, @NonNull String group) {
final Group g = plugin.getGroupManager().get(group); final Group g = perms.getPlugin().getGroupManager().get(group);
return getChatMeta(false, g, world); return getChatMeta(false, g, world);
} }
public void setGroupPrefix(String world, @NonNull String group, @NonNull String prefix) { public void setGroupPrefix(String world, @NonNull String group, @NonNull String prefix) {
final Group g = plugin.getGroupManager().get(group); final Group g = perms.getPlugin().getGroupManager().get(group);
setChatMeta(true, g, prefix, world); setChatMeta(true, g, prefix, world);
} }
public String getGroupSuffix(String world, @NonNull String group) { public String getGroupSuffix(String world, @NonNull String group) {
final Group g = plugin.getGroupManager().get(group); final Group g = perms.getPlugin().getGroupManager().get(group);
return getChatMeta(false, g, world); return getChatMeta(false, g, world);
} }
public void setGroupSuffix(String world, @NonNull String group, @NonNull String suffix) { public void setGroupSuffix(String world, @NonNull String group, @NonNull String suffix) {
final Group g = plugin.getGroupManager().get(group); final Group g = perms.getPlugin().getGroupManager().get(group);
setChatMeta(false, g, suffix, world); setChatMeta(false, g, suffix, world);
} }
public int getPlayerInfoInteger(String world, @NonNull String player, @NonNull String node, int defaultValue) { public int getPlayerInfoInteger(String world, @NonNull String player, @NonNull String node, int defaultValue) {
final User user = plugin.getUserManager().get(player); final User user = perms.getPlugin().getUserManager().get(player);
try { try {
return Integer.parseInt(getMeta(user, world, node, String.valueOf(defaultValue))); return Integer.parseInt(getMeta(user, world, node, String.valueOf(defaultValue)));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -240,12 +235,12 @@ public class VaultChatHook extends Chat {
} }
public void setPlayerInfoInteger(String world, @NonNull String player, @NonNull String node, int value) { public void setPlayerInfoInteger(String world, @NonNull String player, @NonNull String node, int value) {
final User user = plugin.getUserManager().get(player); final User user = perms.getPlugin().getUserManager().get(player);
saveMeta(user, world, node, String.valueOf(value)); saveMeta(user, world, node, String.valueOf(value));
} }
public int getGroupInfoInteger(String world, @NonNull String group, @NonNull String node, int defaultValue) { public int getGroupInfoInteger(String world, @NonNull String group, @NonNull String node, int defaultValue) {
final Group g = plugin.getGroupManager().get(group); final Group g = perms.getPlugin().getGroupManager().get(group);
try { try {
return Integer.parseInt(getMeta(g, world, node, String.valueOf(defaultValue))); return Integer.parseInt(getMeta(g, world, node, String.valueOf(defaultValue)));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -254,12 +249,12 @@ public class VaultChatHook extends Chat {
} }
public void setGroupInfoInteger(String world, @NonNull String group, @NonNull String node, int value) { public void setGroupInfoInteger(String world, @NonNull String group, @NonNull String node, int value) {
final Group g = plugin.getGroupManager().get(group); final Group g = perms.getPlugin().getGroupManager().get(group);
saveMeta(g, world, node, String.valueOf(value)); saveMeta(g, world, node, String.valueOf(value));
} }
public double getPlayerInfoDouble(String world, @NonNull String player, @NonNull String node, double defaultValue) { public double getPlayerInfoDouble(String world, @NonNull String player, @NonNull String node, double defaultValue) {
final User user = plugin.getUserManager().get(player); final User user = perms.getPlugin().getUserManager().get(player);
try { try {
return Double.parseDouble(getMeta(user, world, node, String.valueOf(defaultValue))); return Double.parseDouble(getMeta(user, world, node, String.valueOf(defaultValue)));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -268,12 +263,12 @@ public class VaultChatHook extends Chat {
} }
public void setPlayerInfoDouble(String world, @NonNull String player, @NonNull String node, double value) { public void setPlayerInfoDouble(String world, @NonNull String player, @NonNull String node, double value) {
final User user = plugin.getUserManager().get(player); final User user = perms.getPlugin().getUserManager().get(player);
saveMeta(user, world, node, String.valueOf(value)); saveMeta(user, world, node, String.valueOf(value));
} }
public double getGroupInfoDouble(String world, @NonNull String group, @NonNull String node, double defaultValue) { public double getGroupInfoDouble(String world, @NonNull String group, @NonNull String node, double defaultValue) {
final Group g = plugin.getGroupManager().get(group); final Group g = perms.getPlugin().getGroupManager().get(group);
try { try {
return Double.parseDouble(getMeta(g, world, node, String.valueOf(defaultValue))); return Double.parseDouble(getMeta(g, world, node, String.valueOf(defaultValue)));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -282,12 +277,12 @@ public class VaultChatHook extends Chat {
} }
public void setGroupInfoDouble(String world, @NonNull String group, @NonNull String node, double value) { public void setGroupInfoDouble(String world, @NonNull String group, @NonNull String node, double value) {
final Group g = plugin.getGroupManager().get(group); final Group g = perms.getPlugin().getGroupManager().get(group);
saveMeta(g, world, node, String.valueOf(value)); saveMeta(g, world, node, String.valueOf(value));
} }
public boolean getPlayerInfoBoolean(String world, @NonNull String player, @NonNull String node, boolean defaultValue) { public boolean getPlayerInfoBoolean(String world, @NonNull String player, @NonNull String node, boolean defaultValue) {
final User user = plugin.getUserManager().get(player); final User user = perms.getPlugin().getUserManager().get(player);
String s = getMeta(user, world, node, String.valueOf(defaultValue)); String s = getMeta(user, world, node, String.valueOf(defaultValue));
if (!s.equalsIgnoreCase("true") && !s.equalsIgnoreCase("false")) { if (!s.equalsIgnoreCase("true") && !s.equalsIgnoreCase("false")) {
return defaultValue; return defaultValue;
@ -296,12 +291,12 @@ public class VaultChatHook extends Chat {
} }
public void setPlayerInfoBoolean(String world, @NonNull String player, @NonNull String node, boolean value) { public void setPlayerInfoBoolean(String world, @NonNull String player, @NonNull String node, boolean value) {
final User user = plugin.getUserManager().get(player); final User user = perms.getPlugin().getUserManager().get(player);
saveMeta(user, world, node, String.valueOf(value)); saveMeta(user, world, node, String.valueOf(value));
} }
public boolean getGroupInfoBoolean(String world, @NonNull String group, @NonNull String node, boolean defaultValue) { public boolean getGroupInfoBoolean(String world, @NonNull String group, @NonNull String node, boolean defaultValue) {
final Group g = plugin.getGroupManager().get(group); final Group g = perms.getPlugin().getGroupManager().get(group);
String s = getMeta(g, world, node, String.valueOf(defaultValue)); String s = getMeta(g, world, node, String.valueOf(defaultValue));
if (!s.equalsIgnoreCase("true") && !s.equalsIgnoreCase("false")) { if (!s.equalsIgnoreCase("true") && !s.equalsIgnoreCase("false")) {
return defaultValue; return defaultValue;
@ -310,27 +305,27 @@ public class VaultChatHook extends Chat {
} }
public void setGroupInfoBoolean(String world, @NonNull String group, @NonNull String node, boolean value) { public void setGroupInfoBoolean(String world, @NonNull String group, @NonNull String node, boolean value) {
final Group g = plugin.getGroupManager().get(group); final Group g = perms.getPlugin().getGroupManager().get(group);
saveMeta(g, world, node, String.valueOf(value)); saveMeta(g, world, node, String.valueOf(value));
} }
public String getPlayerInfoString(String world, @NonNull String player, @NonNull String node, String defaultValue) { public String getPlayerInfoString(String world, @NonNull String player, @NonNull String node, String defaultValue) {
final User user = plugin.getUserManager().get(player); final User user = perms.getPlugin().getUserManager().get(player);
return getMeta(user, world, node, defaultValue); return getMeta(user, world, node, defaultValue);
} }
public void setPlayerInfoString(String world, @NonNull String player, @NonNull String node, String value) { public void setPlayerInfoString(String world, @NonNull String player, @NonNull String node, String value) {
final User user = plugin.getUserManager().get(player); final User user = perms.getPlugin().getUserManager().get(player);
saveMeta(user, world, node, value); saveMeta(user, world, node, value);
} }
public String getGroupInfoString(String world, @NonNull String group, @NonNull String node, String defaultValue) { public String getGroupInfoString(String world, @NonNull String group, @NonNull String node, String defaultValue) {
final Group g = plugin.getGroupManager().get(group); final Group g = perms.getPlugin().getGroupManager().get(group);
return getMeta(g, world, node, defaultValue); return getMeta(g, world, node, defaultValue);
} }
public void setGroupInfoString(String world, @NonNull String group, @NonNull String node, String value) { public void setGroupInfoString(String world, @NonNull String group, @NonNull String node, String value) {
final Group g = plugin.getGroupManager().get(group); final Group g = perms.getPlugin().getGroupManager().get(group);
saveMeta(g, world, node, value); saveMeta(g, world, node, value);
} }

View File

@ -22,18 +22,19 @@
package me.lucko.luckperms.api.vault; package me.lucko.luckperms.api.vault;
import lombok.Getter;
import me.lucko.luckperms.LPBukkitPlugin; import me.lucko.luckperms.LPBukkitPlugin;
import net.milkbowl.vault.chat.Chat; import net.milkbowl.vault.chat.Chat;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
import org.bukkit.plugin.ServicePriority; import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.ServicesManager; import org.bukkit.plugin.ServicesManager;
@Getter
public class VaultHook { public class VaultHook {
private VaultChatHook chatHook = null;
private VaultPermissionHook permissionHook = null;
private static VaultChatHook chatHook = null; public void hook(LPBukkitPlugin plugin) {
private static VaultPermissionHook permissionHook = null;
public static void hook(LPBukkitPlugin plugin) {
try { try {
if (permissionHook == null) { if (permissionHook == null) {
permissionHook = new VaultPermissionHook(); permissionHook = new VaultPermissionHook();
@ -41,21 +42,33 @@ public class VaultHook {
permissionHook.setPlugin(plugin); permissionHook.setPlugin(plugin);
permissionHook.setServer(plugin.getConfiguration().getVaultServer()); permissionHook.setServer(plugin.getConfiguration().getVaultServer());
permissionHook.setIncludeGlobal(plugin.getConfiguration().isVaultIncludingGlobal()); permissionHook.setIncludeGlobal(plugin.getConfiguration().isVaultIncludingGlobal());
permissionHook.setup();
if (chatHook == null) { if (chatHook == null) {
chatHook = new VaultChatHook(permissionHook); chatHook = new VaultChatHook(permissionHook);
} }
chatHook.setPlugin(plugin);
final ServicesManager sm = plugin.getServer().getServicesManager(); final ServicesManager sm = plugin.getServer().getServicesManager();
sm.unregisterAll(plugin);
sm.register(Permission.class, permissionHook, plugin, ServicePriority.High); sm.register(Permission.class, permissionHook, plugin, ServicePriority.High);
sm.register(Chat.class, chatHook, plugin, ServicePriority.Low); sm.register(Chat.class, chatHook, plugin, ServicePriority.High);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
}
public void unhook(LPBukkitPlugin plugin) {
final ServicesManager sm = plugin.getServer().getServicesManager();
if (permissionHook != null) {
sm.unregister(Permission.class, permissionHook);
}
if (chatHook != null) {
sm.unregister(Chat.class, chatHook);
}
}
public boolean isHooked() {
return permissionHook != null && chatHook != null;
} }
} }

View File

@ -27,6 +27,9 @@ import lombok.NonNull;
import lombok.Setter; import lombok.Setter;
import me.lucko.luckperms.LPBukkitPlugin; import me.lucko.luckperms.LPBukkitPlugin;
import me.lucko.luckperms.api.data.Callback; import me.lucko.luckperms.api.data.Callback;
import me.lucko.luckperms.api.vault.cache.ContextData;
import me.lucko.luckperms.api.vault.cache.VaultUserCache;
import me.lucko.luckperms.api.vault.cache.VaultUserManager;
import me.lucko.luckperms.contexts.Contexts; import me.lucko.luckperms.contexts.Contexts;
import me.lucko.luckperms.core.PermissionHolder; import me.lucko.luckperms.core.PermissionHolder;
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
@ -41,9 +44,13 @@ import java.util.Map;
public class VaultPermissionHook extends Permission { public class VaultPermissionHook extends Permission {
@Getter
@Setter @Setter
private LPBukkitPlugin plugin; private LPBukkitPlugin plugin;
@Getter
private VaultUserManager vaultUserManager;
@Getter @Getter
@Setter @Setter
private String server = "global"; private String server = "global";
@ -67,8 +74,12 @@ public class VaultPermissionHook extends Permission {
return true; return true;
} }
private boolean objectHas(String world, PermissionHolder object, String permission) { public void setup() {
if (object == null) return false; vaultUserManager = new VaultUserManager(plugin, this);
}
private boolean objectHas(String world, Group group, String permission) {
if (group == null) return false;
Map<String, String> context = new HashMap<>(); Map<String, String> context = new HashMap<>();
if (world != null && !world.equals("")) { if (world != null && !world.equals("")) {
@ -76,7 +87,7 @@ public class VaultPermissionHook extends Permission {
} }
context.put("server", server); context.put("server", server);
Map<String, Boolean> toApply = object.exportNodes( Map<String, Boolean> toApply = group.exportNodes(
new Contexts(context, includeGlobal, includeGlobal, true, true, true), new Contexts(context, includeGlobal, includeGlobal, true, true, true),
Collections.emptyList(), true Collections.emptyList(), true
); );
@ -126,7 +137,21 @@ public class VaultPermissionHook extends Permission {
@Override @Override
public boolean playerHas(String world, @NonNull String player, @NonNull String permission) { public boolean playerHas(String world, @NonNull String player, @NonNull String permission) {
return objectHas(world, plugin.getUserManager().get(player), permission); User user = plugin.getUserManager().get(player);
if (user == null) return false;
if (!vaultUserManager.containsUser(user.getUuid())) {
return false;
}
VaultUserCache vaultUser = vaultUserManager.getUser(user.getUuid());
Map<String, String> context = new HashMap<>();
context.put("server", server);
if (world != null) {
context.put("world", world);
}
return vaultUser.hasPermission(context, permission);
} }
@Override @Override
@ -204,23 +229,22 @@ public class VaultPermissionHook extends Permission {
@Override @Override
public String[] getPlayerGroups(String world, @NonNull String player) { public String[] getPlayerGroups(String world, @NonNull String player) {
final User user = plugin.getUserManager().get(player); User user = plugin.getUserManager().get(player);
if (user == null) { if (user == null) return new String[0];
if (!vaultUserManager.containsUser(user.getUuid())) {
return new String[0]; return new String[0];
} }
VaultUserCache vaultUser = vaultUserManager.getUser(user.getUuid());
Map<String, String> context = new HashMap<>(); Map<String, String> context = new HashMap<>();
if (world != null && !world.equals("")) { context.put("server", server);
if (world != null) {
context.put("world", world); context.put("world", world);
} }
context.put("server", server);
Map<String, Boolean> toApply = user.exportNodes( ContextData cd = vaultUser.getContextData().computeIfAbsent(context, map -> vaultUser.calculatePermissions(map, false));
new Contexts(context, includeGlobal, includeGlobal, true, true, true), return cd.getPermissionCache().entrySet().stream()
Collections.emptyList(), true
);
return toApply.entrySet().stream()
.filter(Map.Entry::getValue) .filter(Map.Entry::getValue)
.filter(e -> e.getKey().startsWith("group.")) .filter(e -> e.getKey().startsWith("group."))
.map(e -> e.getKey().substring("group.".length())) .map(e -> e.getKey().substring("group.".length()))

View File

@ -0,0 +1,75 @@
/*
* 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.api.vault.cache;
import lombok.Getter;
import lombok.NonNull;
import me.lucko.luckperms.LuckPermsPlugin;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.calculators.PermissionCalculator;
import me.lucko.luckperms.calculators.PermissionProcessor;
import me.lucko.luckperms.inject.DefaultsProvider;
import me.lucko.luckperms.inject.LPPermissible;
import me.lucko.luckperms.users.BukkitUser;
import me.lucko.luckperms.users.User;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ContextData {
@Getter
private final Map<String, String> context;
@Getter
private final Map<String, Boolean> permissionCache = new ConcurrentHashMap<>();
private final PermissionCalculator calculator;
public ContextData(User user, Map<String, String> context, LuckPermsPlugin plugin, DefaultsProvider defaultsProvider) {
this.context = context;
List<PermissionProcessor> processors = new ArrayList<>(5);
processors.add(new PermissionCalculator.MapProcessor(permissionCache));
if (plugin.getConfiguration().isApplyingWildcards()) {
processors.add(new PermissionCalculator.WildcardProcessor(permissionCache));
}
if (plugin.getConfiguration().isApplyingRegex()) {
processors.add(new PermissionCalculator.RegexProcessor(permissionCache));
}
processors.add(new LPPermissible.BukkitDefaultsProcessor(() -> ((BukkitUser) user).getLpPermissible().isOp(), defaultsProvider));
calculator = new PermissionCalculator(plugin, user.getName(), plugin.getConfiguration().isDebugPermissionChecks(), processors);
}
public void invalidateCache() {
calculator.invalidateCache();
}
public Tristate getPermissionValue(@NonNull String permission) {
return calculator.getPermissionValue(permission);
}
}

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.api.vault.cache;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import me.lucko.luckperms.LPBukkitPlugin;
import me.lucko.luckperms.api.vault.VaultPermissionHook;
import me.lucko.luckperms.contexts.Contexts;
import me.lucko.luckperms.users.User;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@RequiredArgsConstructor
public class VaultUserCache {
private final LPBukkitPlugin plugin;
private final VaultPermissionHook vault;
@Getter
private final User user;
@Getter
private final Map<Map<String, String>, ContextData> contextData = new ConcurrentHashMap<>();
public boolean hasPermission(Map<String, String> context, String permission) {
ContextData cd = contextData.computeIfAbsent(context, map -> calculatePermissions(map, false));
return cd.getPermissionValue(permission).asBoolean();
}
public ContextData calculatePermissions(Map<String, String> context, boolean apply) {
Map<String, Boolean> toApply = user.exportNodes(
new Contexts(context, vault.isIncludeGlobal(), true, true, true, true),
Collections.emptyList(),
true
);
ContextData existing = contextData.get(context);
if (existing == null) {
existing = new ContextData(user, context, plugin, plugin.getDefaultsProvider());
if (apply) {
contextData.put(context, existing);
}
}
boolean different = false;
if (toApply.size() != existing.getPermissionCache().size()) {
different = true;
} else {
for (Map.Entry<String, Boolean> e : existing.getPermissionCache().entrySet()) {
if (toApply.containsKey(e.getKey()) && toApply.get(e.getKey()) == e.getValue()) {
continue;
}
different = true;
break;
}
}
if (!different) return existing;
existing.getPermissionCache().clear();
existing.invalidateCache();
existing.getPermissionCache().putAll(toApply);
return existing;
}
}

View File

@ -0,0 +1,66 @@
/*
* 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.api.vault.cache;
import lombok.RequiredArgsConstructor;
import me.lucko.luckperms.LPBukkitPlugin;
import me.lucko.luckperms.api.vault.VaultPermissionHook;
import me.lucko.luckperms.users.User;
import org.bukkit.World;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@RequiredArgsConstructor
public class VaultUserManager {
private final LPBukkitPlugin plugin;
private final VaultPermissionHook vault;
private final Map<UUID, VaultUserCache> userCache = new ConcurrentHashMap<>();
public void setupUser(User user) {
VaultUserCache vaultUser = userCache.computeIfAbsent(user.getUuid(), uuid -> new VaultUserCache(plugin, vault, user));
vaultUser.calculatePermissions(Collections.singletonMap("server", vault.getServer()), true);
for (World world : plugin.getServer().getWorlds()) {
Map<String, String> context = new HashMap<>();
context.put("server", vault.getServer());
context.put("world", world.getName());
vaultUser.calculatePermissions(context, true);
}
}
public void clearUser(UUID uuid) {
userCache.remove(uuid);
}
public boolean containsUser(UUID uuid) {
return userCache.containsKey(uuid);
}
public VaultUserCache getUser(UUID uuid) {
return userCache.get(uuid);
}
}

View File

@ -279,7 +279,7 @@ public class LPPermissible extends PermissibleBase {
} }
@AllArgsConstructor @AllArgsConstructor
private static class BukkitDefaultsProcessor implements PermissionProcessor { public static class BukkitDefaultsProcessor implements PermissionProcessor {
private final Supplier<Boolean> isOp; private final Supplier<Boolean> isOp;
private final DefaultsProvider defaultsProvider; private final DefaultsProvider defaultsProvider;

View File

@ -119,5 +119,9 @@ public class BukkitUser extends User {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
if (plugin.getVaultHook() != null && plugin.getVaultHook().isHooked()) {
plugin.getVaultHook().getPermissionHook().getVaultUserManager().setupUser(this);
}
} }
} }

View File

@ -26,7 +26,9 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.Node;
import java.text.Collator;
import java.util.Comparator; import java.util.Comparator;
import java.util.Locale;
@NoArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor(access = AccessLevel.PRIVATE)
public class PriorityComparator implements Comparator<Node> { public class PriorityComparator implements Comparator<Node> {
@ -40,6 +42,8 @@ public class PriorityComparator implements Comparator<Node> {
return INSTANCE.reversed(); return INSTANCE.reversed();
} }
private final Collator collator = Collator.getInstance(Locale.ENGLISH);
@Override @Override
public int compare(Node o1, Node o2) { public int compare(Node o1, Node o2) {
if (o1.equals(o2)) { if (o1.equals(o2)) {
@ -74,6 +78,6 @@ public class PriorityComparator implements Comparator<Node> {
return o1.getWildcardLevel() > o2.getWildcardLevel() ? 1 : -1; return o1.getWildcardLevel() > o2.getWildcardLevel() ? 1 : -1;
} }
return o1.getPermission().compareToIgnoreCase(o2.getPermission()) == 1 ? -1 : 1; return collator.compare(o1.getPermission(), o2.getPermission()) == 1 ? -1 : 1;
} }
} }

View File

@ -67,7 +67,7 @@ public class ContextData {
} }
public Tristate getPermissionValue(@NonNull String permission) { public Tristate getPermissionValue(@NonNull String permission) {
me.lucko.luckperms.api.Tristate t = calculator.getPermissionValue(permission); me.lucko.luckperms.api.Tristate t = calculator.getPermissionValue(permission);
if (t != me.lucko.luckperms.api.Tristate.UNDEFINED) { if (t != me.lucko.luckperms.api.Tristate.UNDEFINED) {
return Tristate.fromBoolean(t.asBoolean()); return Tristate.fromBoolean(t.asBoolean());
} else { } else {