From 942fd3c4ce78552f6c07e1f6846e8cfd82f2501a Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 11 May 2017 20:04:11 +0100 Subject: [PATCH] Fix vault lookups with static context --- .../luckperms/bukkit/vault/VaultChatHook.java | 22 ++----- .../bukkit/vault/VaultPermissionHook.java | 62 ++++++++++++++----- .../common/contexts/ContextManager.java | 8 +-- 3 files changed, 58 insertions(+), 34 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 304528d0..4949dc75 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 @@ -30,7 +30,6 @@ import lombok.NonNull; import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.caching.MetaData; -import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.common.caching.MetaAccumulator; import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.core.model.Group; @@ -40,7 +39,6 @@ import me.lucko.luckperms.common.utils.ExtractedContexts; import net.milkbowl.vault.chat.Chat; -import java.util.HashMap; import java.util.Map; /** @@ -97,7 +95,7 @@ public class VaultChatHook extends Chat { holder.removeIf(n -> prefix ? n.isPrefix() : n.isSuffix()); // find the max inherited priority & add 10 - MetaAccumulator metaAccumulator = holder.accumulateMeta(null, null, ExtractedContexts.generate(perms.createContextForWorld(finalWorld))); + MetaAccumulator metaAccumulator = holder.accumulateMeta(null, null, ExtractedContexts.generate(perms.createContextForWorldSet(finalWorld))); int priority = (prefix ? metaAccumulator.getPrefixes() : metaAccumulator.getSuffixes()).keySet().stream() .mapToInt(e -> e).max().orElse(0) + 10; @@ -120,7 +118,7 @@ public class VaultChatHook extends Chat { perms.log("Getting meta: '" + node + "' for user " + user.getFriendlyName() + " on world " + world + ", server " + perms.getServer()); - String ret = user.getUserData().getMetaData(perms.createContextForWorld(world)).getMeta().get(node); + String ret = user.getUserData().getMetaData(perms.createContextForWorldLookup(perms.getPlugin().getPlayer(user), world)).getMeta().get(node); return ret != null ? ret : defaultValue; } @@ -130,7 +128,7 @@ public class VaultChatHook extends Chat { perms.log("Getting " + (prefix ? "prefix" : "suffix") + " for user " + user.getFriendlyName() + " on world " + world + ", server " + perms.getServer()); - MetaData data = user.getUserData().getMetaData(perms.createContextForWorld(world)); + MetaData data = user.getUserData().getMetaData(perms.createContextForWorldLookup(perms.getPlugin().getPlayer(user), world)); String ret = prefix ? data.getPrefix() : data.getSuffix(); return ret != null ? ret : ""; } @@ -144,8 +142,7 @@ public class VaultChatHook extends Chat { for (Node n : group.mergePermissionsToList()) { if (!n.getValue()) continue; if (!n.isMeta()) continue; - if (!n.shouldApplyOnServer(perms.getServer(), perms.isIncludeGlobal(), false)) continue; - if (!n.shouldApplyOnWorld(world, perms.isIncludeGlobal(), false)) continue; + if (!n.shouldApplyWithContext(perms.createContextForWorldLookup(world).getContexts())) continue; Map.Entry meta = n.getMeta(); if (meta.getKey().equalsIgnoreCase(node)) { @@ -165,18 +162,11 @@ public class VaultChatHook extends Chat { int priority = Integer.MIN_VALUE; String meta = null; - Map context = new HashMap<>(); - context.put("server", perms.getServer()); - if (world != null) { - context.put("world", world); - } - - ExtractedContexts ec = ExtractedContexts.generate(new Contexts(ContextSet.fromMap(context), perms.isIncludeGlobal(), true, true, true, true, false)); + ExtractedContexts ec = ExtractedContexts.generate(Contexts.of(perms.createContextForWorldLookup(world).getContexts(), perms.isIncludeGlobal(), true, true, true, true, false)); for (Node n : group.getAllNodes(ec)) { if (!n.getValue()) continue; if (prefix ? !n.isPrefix() : !n.isSuffix()) continue; - if (!n.shouldApplyOnServer(perms.getServer(), perms.isIncludeGlobal(), false)) continue; - if (!n.shouldApplyOnWorld(world, perms.isIncludeGlobal(), false)) continue; + if (!n.shouldApplyWithContext(perms.createContextForWorldLookup(world).getContexts())) continue; Map.Entry value = prefix ? n.getPrefix() : n.getSuffix(); if (value.getKey() > priority) { 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 043f0157..df5d3ad1 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 @@ -33,8 +33,8 @@ import me.lucko.luckperms.api.DataMutateResult; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.caching.PermissionData; -import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet; +import me.lucko.luckperms.api.context.MutableContextSet; import me.lucko.luckperms.bukkit.LPBukkitPlugin; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.core.NodeFactory; @@ -45,7 +45,8 @@ import me.lucko.luckperms.common.utils.ExtractedContexts; import net.milkbowl.vault.permission.Permission; -import java.util.HashMap; +import org.bukkit.entity.Player; + import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; @@ -147,13 +148,48 @@ public class VaultPermissionHook extends Permission { } } - public Contexts createContextForWorld(String world) { - Map context = new HashMap<>(); + public Contexts createContextForWorldSet(String world) { + MutableContextSet context = MutableContextSet.create(); if (world != null && !world.equals("") && !world.equalsIgnoreCase("global")) { - context.put("world", world); + context.add("world", world); } - context.put("server", getServer()); - return new Contexts(ContextSet.fromMap(context), isIncludeGlobal(), true, true, true, true, false); + context.add("server", getServer()); + return new Contexts(context, isIncludeGlobal(), true, true, true, true, false); + } + + public Contexts createContextForWorldLookup(String world) { + MutableContextSet context = MutableContextSet.create(); + if (world != null && !world.equals("") && !world.equalsIgnoreCase("global")) { + context.add("world", world); + } + context.add("server", getServer()); + context.addAll(plugin.getConfiguration().getContextsFile().getStaticContexts()); + return new Contexts(context, isIncludeGlobal(), true, true, true, true, false); + } + + public Contexts createContextForWorldLookup(Player player, String world) { + MutableContextSet context = MutableContextSet.create(); + + // use player context + if (player != null) { + ImmutableContextSet applicableContext = plugin.getContextManager().getApplicableContext(player); + context.addAll(applicableContext); + } else { + // at least given them the static context defined for this instance + context.addAll(plugin.getConfiguration().getContextsFile().getStaticContexts()); + } + + // worlds & servers get set depending on the config setting + context.removeAll("world"); + context.removeAll("server"); + + // add the vault settings + if (world != null && !world.equals("") && !world.equalsIgnoreCase("global")) { + context.add("world", world); + } + context.add("server", getServer()); + + return new Contexts(context, isIncludeGlobal(), true, true, true, true, false); } @Override @@ -165,7 +201,7 @@ public class VaultPermissionHook extends Permission { if (user == null) return false; // Effectively fallback to the standard Bukkit #hasPermission check. - return user.getUserData().getPermissionData(createContextForWorld(world)).getPermissionValue(permission).asBoolean(); + return user.getUserData().getPermissionData(createContextForWorldLookup(plugin.getPlayer(user), world)).getPermissionValue(permission).asBoolean(); } @Override @@ -201,7 +237,7 @@ public class VaultPermissionHook extends Permission { if (group == null) return false; // This is a nasty call. Groups aren't cached. :( - Map permissions = group.exportNodes(ExtractedContexts.generate(createContextForWorld(world)), true); + Map permissions = group.exportNodes(ExtractedContexts.generate(createContextForWorldLookup(world)), true); return permissions.containsKey(permission.toLowerCase()) && permissions.get(permission.toLowerCase()); } @@ -240,8 +276,7 @@ public class VaultPermissionHook extends Permission { String w = world; // screw effectively final return user.getNodes().values().stream() .filter(Node::isGroupNode) - .filter(n -> n.shouldApplyOnServer(getServer(), isIncludeGlobal(), false)) - .filter(n -> n.shouldApplyOnWorld(w, true, false)) + .filter(n -> n.shouldApplyWithContext(createContextForWorldLookup(plugin.getPlayer(user), w).getContexts())) .map(Node::getGroupName) .anyMatch(s -> s.equalsIgnoreCase(group)); } @@ -311,8 +346,7 @@ public class VaultPermissionHook extends Permission { String w = world; // screw effectively final return user.getNodes().values().stream() .filter(Node::isGroupNode) - .filter(n -> n.shouldApplyOnServer(getServer(), isIncludeGlobal(), false)) - .filter(n -> n.shouldApplyOnWorld(w, true, false)) + .filter(n -> n.shouldApplyWithContext(createContextForWorldLookup(plugin.getPlayer(user), w).getContexts())) .map(Node::getGroupName) .toArray(String[]::new); } @@ -336,7 +370,7 @@ public class VaultPermissionHook extends Permission { // we need to do the complex PGO checking. (it's been enabled in the config.) if (isPgoCheckInherited()) { // we can just check the cached data - PermissionData data = user.getUserData().getPermissionData(createContextForWorld(world)); + PermissionData data = user.getUserData().getPermissionData(createContextForWorldLookup(plugin.getPlayer(user), world)); for (Map.Entry e : data.getImmutableBacking().entrySet()) { if (!e.getValue()) continue; if (!e.getKey().toLowerCase().startsWith("vault.primarygroup.")) continue; diff --git a/common/src/main/java/me/lucko/luckperms/common/contexts/ContextManager.java b/common/src/main/java/me/lucko/luckperms/common/contexts/ContextManager.java index 7d6867d5..08df6c2b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/contexts/ContextManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/contexts/ContextManager.java @@ -29,7 +29,7 @@ import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.LoadingCache; import me.lucko.luckperms.api.context.ContextCalculator; -import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.api.context.MutableContextSet; import java.util.List; @@ -41,7 +41,7 @@ public class ContextManager { private final List> calculators = new CopyOnWriteArrayList<>(); private final List> staticCalculators = new CopyOnWriteArrayList<>(); - private final LoadingCache cache = Caffeine.newBuilder() + private final LoadingCache cache = Caffeine.newBuilder() .weakKeys() .expireAfterWrite(50L, TimeUnit.MILLISECONDS) .build(t -> calculateApplicableContext(t, MutableContextSet.create()).makeImmutable()); @@ -53,7 +53,7 @@ public class ContextManager { return accumulator; } - public ContextSet getApplicableContext(T subject) { + public ImmutableContextSet getApplicableContext(T subject) { return cache.get(subject); } @@ -66,7 +66,7 @@ public class ContextManager { staticCalculators.add(0, calculator); } - public ContextSet getStaticContexts() { + public ImmutableContextSet getStaticContexts() { MutableContextSet accumulator = MutableContextSet.create(); for (ContextCalculator calculator : staticCalculators) { calculator.giveApplicableContext(null, accumulator);