Fix vault lookups with static context

This commit is contained in:
Luck 2017-05-11 20:04:11 +01:00
parent 8cf32752e9
commit 942fd3c4ce
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
3 changed files with 58 additions and 34 deletions

View File

@ -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<String, String> 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<String, String> 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<Integer, String> value = prefix ? n.getPrefix() : n.getSuffix();
if (value.getKey() > priority) {

View File

@ -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<String, String> 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<String, Boolean> permissions = group.exportNodes(ExtractedContexts.generate(createContextForWorld(world)), true);
Map<String, Boolean> 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<String, Boolean> e : data.getImmutableBacking().entrySet()) {
if (!e.getValue()) continue;
if (!e.getKey().toLowerCase().startsWith("vault.primarygroup.")) continue;

View File

@ -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<T> {
private final List<ContextCalculator<T>> calculators = new CopyOnWriteArrayList<>();
private final List<ContextCalculator<?>> staticCalculators = new CopyOnWriteArrayList<>();
private final LoadingCache<T, ContextSet> cache = Caffeine.newBuilder()
private final LoadingCache<T, ImmutableContextSet> cache = Caffeine.newBuilder()
.weakKeys()
.expireAfterWrite(50L, TimeUnit.MILLISECONDS)
.build(t -> calculateApplicableContext(t, MutableContextSet.create()).makeImmutable());
@ -53,7 +53,7 @@ public class ContextManager<T> {
return accumulator;
}
public ContextSet getApplicableContext(T subject) {
public ImmutableContextSet getApplicableContext(T subject) {
return cache.get(subject);
}
@ -66,7 +66,7 @@ public class ContextManager<T> {
staticCalculators.add(0, calculator);
}
public ContextSet getStaticContexts() {
public ImmutableContextSet getStaticContexts() {
MutableContextSet accumulator = MutableContextSet.create();
for (ContextCalculator<?> calculator : staticCalculators) {
calculator.giveApplicableContext(null, accumulator);