From e71ef834c0f19fecac72910af9f21850e4dc2568 Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 21 Nov 2017 16:44:20 +0000 Subject: [PATCH] fix issue with the LP Vault impl and servers with more than one server context --- .../luckperms/bukkit/vault/VaultChatHook.java | 14 +-- .../bukkit/vault/VaultPermissionHook.java | 93 ++++++++++++------- .../luckperms/common/config/ConfigKeys.java | 7 +- 3 files changed, 74 insertions(+), 40 deletions(-) diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultChatHook.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultChatHook.java index aeb4bac7..0becab53 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultChatHook.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultChatHook.java @@ -254,13 +254,13 @@ public class VaultChatHook extends Chat { } String finalWorld = perms.correctWorld(world); - perms.log("Setting meta: '" + key + "' for " + holder.getObjectName() + " on world " + world + ", server " + perms.getServer()); + perms.log("Setting meta: '" + key + "' for " + holder.getObjectName() + " on world " + world + ", server " + perms.getVaultServer()); perms.getExecutor().execute(() -> { holder.removeIf(n -> n.isMeta() && n.getMeta().getKey().equals(key)); Node.Builder metaNode = NodeFactory.makeMetaNode(key, value).setValue(true); - metaNode.setServer(perms.getServer()); + metaNode.setServer(perms.getVaultServer()); metaNode.setWorld(finalWorld); holder.setPermission(metaNode.build()); @@ -274,7 +274,7 @@ public class VaultChatHook extends Chat { } String finalWorld = perms.correctWorld(world); - perms.log("Setting " + type.name().toLowerCase() + " for " + holder.getObjectName() + " on world " + world + ", server " + perms.getServer()); + perms.log("Setting " + type.name().toLowerCase() + " for " + holder.getObjectName() + " on world " + world + ", server " + perms.getVaultServer()); perms.getExecutor().execute(() -> { // remove all prefixes/suffixes directly set on the user/group @@ -286,7 +286,7 @@ public class VaultChatHook extends Chat { .mapToInt(e -> e).max().orElse(0) + 10; Node.Builder chatMetaNode = NodeFactory.makeChatMetaNode(type, priority, value); - chatMetaNode.setServer(perms.getServer()); + chatMetaNode.setServer(perms.getVaultServer()); chatMetaNode.setWorld(finalWorld); holder.setPermission(chatMetaNode.build()); @@ -300,7 +300,6 @@ public class VaultChatHook extends Chat { } world = perms.correctWorld(world); - perms.log("Getting meta: '" + node + "' for holder " + holder.getFriendlyName() + " on world " + world + ", server " + perms.getServer()); Contexts contexts; if (holder instanceof User) { @@ -309,6 +308,8 @@ public class VaultChatHook extends Chat { contexts = perms.createContextForWorldLookup(world); } + perms.log("Getting meta: '" + node + "' for holder " + holder.getFriendlyName() + " in contexts " + contexts); + String ret = holder.getCachedData().getMetaData(contexts).getMeta().get(node); return ret != null ? ret : defaultValue; } @@ -319,7 +320,6 @@ public class VaultChatHook extends Chat { } world = perms.correctWorld(world); - perms.log("Getting " + type.name().toLowerCase() + " for holder " + holder.getFriendlyName() + " on world " + world + ", server " + perms.getServer()); Contexts contexts; if (holder instanceof User) { @@ -328,6 +328,8 @@ public class VaultChatHook extends Chat { contexts = perms.createContextForWorldLookup(world); } + perms.log("Getting " + type.name().toLowerCase() + " for holder " + holder.getFriendlyName() + " in contexts " + contexts); + MetaData data = holder.getCachedData().getMetaData(contexts); String ret = type == ChatMetaType.PREFIX ? data.getPrefix() : data.getSuffix(); return ret != null ? ret : ""; diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultPermissionHook.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultPermissionHook.java index 33c62843..69a743d2 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultPermissionHook.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/vault/VaultPermissionHook.java @@ -32,6 +32,7 @@ import com.google.common.base.Preconditions; import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Node; +import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.context.MutableContextSet; import me.lucko.luckperms.bukkit.LPBukkitPlugin; import me.lucko.luckperms.common.config.ConfigKeys; @@ -50,8 +51,6 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; -import java.util.Map; - /** * An implementation of the Vault {@link Permission} API using LuckPerms. * @@ -131,7 +130,6 @@ public class VaultPermissionHook extends Permission { private boolean playerHas(String world, Player player, String permission) { world = correctWorld(world); - log("Checking if player " + player + " has permission: " + permission + " on world " + world + ", server " + getServer()); if (player == null) { return false; @@ -142,8 +140,11 @@ public class VaultPermissionHook extends Permission { return false; } + Contexts contexts = createContextForWorldLookup(player, world); + log("Checking if player " + player + " has permission: " + permission + " in contexts " + contexts); + // Effectively fallback to the standard Bukkit #hasPermission check. - return user.getCachedData().getPermissionData(createContextForWorldLookup(player, world)).getPermissionValue(permission, CheckOrigin.INTERNAL).asBoolean(); + return user.getCachedData().getPermissionData(contexts).getPermissionValue(permission, CheckOrigin.INTERNAL).asBoolean(); } @Override @@ -164,7 +165,7 @@ public class VaultPermissionHook extends Permission { private boolean playerAdd(String world, Player player, String permission) { world = correctWorld(world); - log("Adding permission to player " + player + ": '" + permission + "' on world " + world + ", server " + getServer()); + log("Adding permission to player " + player + ": '" + permission + "' on world " + world + ", server " + getVaultServer()); if (player == null) { return false; @@ -197,7 +198,7 @@ public class VaultPermissionHook extends Permission { private boolean playerRemove(String world, Player player, String permission) { world = correctWorld(world); - log("Removing permission from player " + player + ": '" + permission + "' on world " + world + ", server " + getServer()); + log("Removing permission from player " + player + ": '" + permission + "' on world " + world + ", server " + getVaultServer()); if (player == null) { return false; @@ -215,20 +216,23 @@ public class VaultPermissionHook extends Permission { @Override public boolean groupHas(String world, @NonNull String groupName, @NonNull String permission) { world = correctWorld(world); - log("Checking if group " + groupName + " has permission: " + permission + " on world " + world + ", server " + getServer()); final Group group = plugin.getGroupManager().getByDisplayName(groupName); - if (group == null) return false; + if (group == null) { + return false; + } - // This is a nasty call. Groups aren't cached. :( - Map permissions = group.exportNodesAndShorthand(createContextForWorldLookup(world), true); - return permissions.containsKey(permission.toLowerCase()) && permissions.get(permission.toLowerCase()); + Contexts contexts = createContextForWorldLookup(world); + log("Checking if group " + groupName + " has permission: " + permission + " in contexts " + contexts); + + // Effectively fallback to the standard Bukkit #hasPermission check. + return group.getCachedData().getPermissionData(createContextForWorldLookup(world)).getPermissionValue(permission, CheckOrigin.INTERNAL).asBoolean(); } @Override public boolean groupAdd(String world, @NonNull String groupName, @NonNull String permission) { world = correctWorld(world); - log("Adding permission to group " + groupName + ": '" + permission + "' on world " + world + ", server " + getServer()); + log("Adding permission to group " + groupName + ": '" + permission + "' on world " + world + ", server " + getVaultServer()); final Group group = plugin.getGroupManager().getByDisplayName(groupName); if (group == null) return false; @@ -240,7 +244,7 @@ public class VaultPermissionHook extends Permission { @Override public boolean groupRemove(String world, @NonNull String groupName, @NonNull String permission) { world = correctWorld(world); - log("Removing permission from group " + groupName + ": '" + permission + "' on world " + world + ", server " + getServer()); + log("Removing permission from group " + groupName + ": '" + permission + "' on world " + world + ", server " + getVaultServer()); final Group group = plugin.getGroupManager().getByDisplayName(groupName); if (group == null) return false; @@ -342,7 +346,6 @@ public class VaultPermissionHook extends Permission { private String[] getPlayerGroups(String world, Player player) { world = correctWorld(world); - log("Getting groups of player: " + player + ", on world " + world + ", server " + getServer()); if (player == null) { return new String[0]; @@ -353,10 +356,12 @@ public class VaultPermissionHook extends Permission { return new String[0]; } - String w = world; // screw effectively final + ContextSet contexts = createContextForWorldLookup(player, world).getContexts(); + log("Getting groups of player: " + player + " in contexts " + contexts); + return user.getEnduringNodes().values().stream() .filter(Node::isGroupNode) - .filter(n -> n.shouldApplyWithContext(createContextForWorldLookup(player, w).getContexts())) + .filter(n -> n.shouldApplyWithContext(contexts)) .map(n -> { Group group = plugin.getGroupManager().getIfLoaded(n.getGroupName()); if (group != null) { @@ -422,7 +427,7 @@ public class VaultPermissionHook extends Permission { Preconditions.checkNotNull(permission, "permission is null"); Preconditions.checkArgument(!permission.isEmpty(), "permission is an empty string"); executor.execute(() -> { - if (holder.setPermission(NodeFactory.make(permission, true, getServer(), world)).asBoolean()) { + if (holder.setPermission(NodeFactory.make(permission, true, getVaultServer(), world)).asBoolean()) { holderSave(holder); } }); @@ -432,7 +437,7 @@ public class VaultPermissionHook extends Permission { Preconditions.checkNotNull(permission, "permission is null"); Preconditions.checkArgument(!permission.isEmpty(), "permission is an empty string"); executor.execute(() -> { - if (holder.unsetPermission(NodeFactory.make(permission, getServer(), world)).asBoolean()) { + if (holder.unsetPermission(NodeFactory.make(permission, getVaultServer(), world)).asBoolean()) { holderSave(holder); } }); @@ -456,22 +461,29 @@ public class VaultPermissionHook extends Permission { if (world != null && !world.equals("") && !world.equalsIgnoreCase("global")) { context.add("world", world.toLowerCase()); } - context.add("server", getServer()); + context.add("server", getVaultServer()); return new Contexts(context, isIncludeGlobal(), true, true, true, true, false); } public Contexts createContextForWorldLookup(String world) { MutableContextSet context = plugin.getContextManager().getStaticContext().mutableCopy(); - // worlds & servers get set depending on the config setting - context.removeAll("world"); - context.removeAll("server"); + if (useVaultServer()) { + // remove already accumulated worlds + context.removeAll("world"); + // add the vault world + if (world != null && !world.isEmpty() && !world.equalsIgnoreCase("global")) { + context.add("world", world.toLowerCase()); + } - // add the vault settings - if (world != null && !world.isEmpty() && !world.equalsIgnoreCase("global")) { - context.add("world", world.toLowerCase()); + // remove the server context from global + context.remove("server", getServer()); + + // add the vault specific server + if (!getVaultServer().equals("global")) { + context.add("server", getVaultServer()); + } } - context.add("server", getServer()); return new Contexts(context, isIncludeGlobal(), true, true, true, true, false); } @@ -479,15 +491,22 @@ public class VaultPermissionHook extends Permission { public Contexts createContextForWorldLookup(@NonNull Player player, String world) { MutableContextSet context = plugin.getContextManager().getApplicableContext(player).mutableCopy(); - // worlds & servers get set depending on the config setting - context.removeAll("world"); - context.removeAll("server"); + if (useVaultServer()) { + // remove already accumulated worlds + context.removeAll("world"); + // add the vault world + if (world != null && !world.isEmpty() && !world.equalsIgnoreCase("global")) { + context.add("world", world.toLowerCase()); + } - // add the vault settings - if (world != null && !world.isEmpty() && !world.equalsIgnoreCase("global")) { - context.add("world", world.toLowerCase()); + // remove the server context from global + context.remove("server", getServer()); + + // add the vault specific server + if (!getVaultServer().equals("global")) { + context.add("server", getVaultServer()); + } } - context.add("server", getServer()); return new Contexts(context, isIncludeGlobal(), true, true, true, true, false); } @@ -495,9 +514,17 @@ public class VaultPermissionHook extends Permission { // helper methods to just pull values from the config. String getServer() { + return plugin.getConfiguration().get(ConfigKeys.SERVER); + } + + String getVaultServer() { return plugin.getConfiguration().get(ConfigKeys.VAULT_SERVER); } + boolean useVaultServer() { + return plugin.getConfiguration().get(ConfigKeys.USE_VAULT_SERVER); + } + boolean isIncludeGlobal() { return plugin.getConfiguration().get(ConfigKeys.VAULT_INCLUDING_GLOBAL); } diff --git a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java index 2352da05..7196087f 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java @@ -303,12 +303,17 @@ public class ConfigKeys { */ public static final ConfigKey COMMANDS_ALLOW_OP = EnduringKey.wrap(BooleanKey.of("commands-allow-op", true)); + /** + * If the vault server option should be used + */ + public static final ConfigKey USE_VAULT_SERVER = BooleanKey.of("use-vault-server", true); + /** * The name of the server to use for Vault. */ public static final ConfigKey VAULT_SERVER = AbstractKey.of(c -> { // default to true for backwards compatibility - if (c.getBoolean("use-vault-server", true)) { + if (USE_VAULT_SERVER.get(c)) { return c.getString("vault-server", "global").toLowerCase(); } else { return SERVER.get(c);