refactor contexts slightly

This commit is contained in:
Luck 2016-11-18 22:39:00 +00:00
parent c60067e733
commit c0734fc29e
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
8 changed files with 121 additions and 59 deletions

View File

@ -32,6 +32,7 @@ import me.lucko.luckperms.common.core.NodeFactory;
import me.lucko.luckperms.common.core.PermissionHolder;
import me.lucko.luckperms.common.groups.Group;
import me.lucko.luckperms.common.users.User;
import me.lucko.luckperms.common.utils.ExtractedContexts;
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
import me.lucko.luckperms.exceptions.ObjectLacksException;
import net.milkbowl.vault.chat.Chat;
@ -214,7 +215,8 @@ public class VaultChatHook extends Chat {
context.put("world", world);
}
for (Node n : group.getAllNodes(null, new Contexts(ContextSet.fromMap(context), perms.isIncludeGlobal(), true, true, true, true, false))) {
ExtractedContexts ec = ExtractedContexts.generate(new Contexts(ContextSet.fromMap(context), perms.isIncludeGlobal(), true, true, true, true, false));
for (Node n : group.getAllNodes(null, ec)) {
if (!n.getValue()) {
continue;
}

View File

@ -26,6 +26,7 @@ import lombok.AllArgsConstructor;
import lombok.NonNull;
import me.lucko.luckperms.api.*;
import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.common.utils.ExtractedContexts;
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
import me.lucko.luckperms.exceptions.ObjectLacksException;
@ -64,17 +65,17 @@ public class PermissionHolderLink implements PermissionHolder {
@Override
public Set<Node> getAllNodes() {
return Collections.unmodifiableSet(master.getAllNodes(null, Contexts.allowAll()));
return Collections.unmodifiableSet(master.getAllNodes(null, ExtractedContexts.generate(Contexts.allowAll())));
}
@Override
public SortedSet<LocalizedNode> getAllNodes(@NonNull Contexts contexts) {
return master.getAllNodes(null, contexts);
return master.getAllNodes(null, ExtractedContexts.generate(contexts));
}
@Override
public Set<LocalizedNode> getAllNodesFiltered(@NonNull Contexts contexts) {
return master.getAllNodesFiltered(contexts);
return master.getAllNodesFiltered(ExtractedContexts.generate(contexts));
}
@Override

View File

@ -36,6 +36,7 @@ import me.lucko.luckperms.api.caching.PermissionData;
import me.lucko.luckperms.api.caching.UserData;
import me.lucko.luckperms.common.calculators.CalculatorFactory;
import me.lucko.luckperms.common.users.User;
import me.lucko.luckperms.common.utils.ExtractedContexts;
import java.util.Set;
@ -78,7 +79,7 @@ public class UserCache implements UserData {
@Override
public ListenableFuture<MetaCache> reload(Contexts contexts, MetaCache oldData) {
oldData.loadMeta(user.accumulateMeta(null, null, contexts));
oldData.loadMeta(user.accumulateMeta(null, null, ExtractedContexts.generate(contexts)));
return Futures.immediateFuture(oldData);
}
});
@ -103,7 +104,7 @@ public class UserCache implements UserData {
@Override
public MetaCache calculateMeta(@NonNull Contexts contexts) {
MetaCache data = new MetaCache();
data.loadMeta(user.accumulateMeta(null, null, contexts));
data.loadMeta(user.accumulateMeta(null, null, ExtractedContexts.generate(contexts)));
return data;
}

View File

@ -33,6 +33,7 @@ import me.lucko.luckperms.common.commands.utils.Util;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.core.PermissionHolder;
import me.lucko.luckperms.common.utils.ExtractedContexts;
import me.lucko.luckperms.common.utils.Predicates;
import java.util.*;
@ -49,7 +50,7 @@ public class MetaInfo extends SharedSubCommand {
Set<LocalizedNode> meta = new HashSet<>();
// Collect data
for (LocalizedNode node : holder.getAllNodes(null, Contexts.allowAll())) {
for (LocalizedNode node : holder.getAllNodes(null, ExtractedContexts.generate(Contexts.allowAll()))) {
if (!node.isSuffix() && !node.isPrefix() && !node.isMeta()) {
continue;
}

View File

@ -33,7 +33,6 @@ import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.LocalizedNode;
import me.lucko.luckperms.api.Node;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.api.event.events.*;
import me.lucko.luckperms.common.LuckPermsPlugin;
import me.lucko.luckperms.common.api.internal.GroupLink;
@ -42,6 +41,7 @@ import me.lucko.luckperms.common.caching.MetaHolder;
import me.lucko.luckperms.common.commands.utils.Util;
import me.lucko.luckperms.common.groups.Group;
import me.lucko.luckperms.common.utils.Cache;
import me.lucko.luckperms.common.utils.ExtractedContexts;
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
import me.lucko.luckperms.exceptions.ObjectLacksException;
@ -241,10 +241,10 @@ public abstract class PermissionHolder {
/**
* Resolves inherited nodes and returns them
* @param excludedGroups a list of groups to exclude
* @param context context to decide if groups should be applied
* @param contexts context to decide if groups should be applied
* @return a set of nodes
*/
public SortedSet<LocalizedNode> getAllNodes(List<String> excludedGroups, Contexts context) {
public SortedSet<LocalizedNode> getAllNodes(List<String> excludedGroups, ExtractedContexts contexts) {
SortedSet<LocalizedNode> all = new TreeSet<>((SortedSet<LocalizedNode>) getPermissions(true));
if (excludedGroups == null) {
@ -259,16 +259,14 @@ public abstract class PermissionHolder {
.filter(Node::isGroupNode)
.collect(Collectors.toSet());
MutableContextSet contexts = MutableContextSet.fromSet(context.getContexts());
String server = contexts.getValues("server").stream().findAny().orElse(null);
String world = contexts.getValues("world").stream().findAny().orElse(null);
contexts.removeAll("server");
contexts.removeAll("world");
Contexts context = contexts.getContexts();
String server = contexts.getServer();
String world = contexts.getWorld();
parents.removeIf(node ->
!node.shouldApplyOnServer(server, context.isApplyGlobalGroups(), plugin.getConfiguration().isApplyingRegex()) ||
!node.shouldApplyOnWorld(world, context.isApplyGlobalWorldGroups(), plugin.getConfiguration().isApplyingRegex()) ||
!node.shouldApplyWithContext(contexts, false)
!node.shouldApplyWithContext(contexts.getContextSet(), false)
);
TreeSet<Map.Entry<Integer, Node>> sortedParents = new TreeSet<>(Util.META_COMPARATOR.reversed());
@ -293,7 +291,7 @@ public abstract class PermissionHolder {
}
inherited:
for (LocalizedNode inherited : group.getAllNodes(excludedGroups, context)) {
for (LocalizedNode inherited : group.getAllNodes(excludedGroups, contexts)) {
for (LocalizedNode existing : all) {
if (existing.getNode().almostEquals(inherited.getNode())) {
continue inherited;
@ -307,7 +305,7 @@ public abstract class PermissionHolder {
return all;
}
public MetaHolder accumulateMeta(MetaHolder holder, List<String> excludedGroups, Contexts context) {
public MetaHolder accumulateMeta(MetaHolder holder, List<String> excludedGroups, ExtractedContexts contexts) {
if (holder == null) {
holder = new MetaHolder();
}
@ -318,11 +316,9 @@ public abstract class PermissionHolder {
excludedGroups.add(getObjectName().toLowerCase());
MutableContextSet contexts = MutableContextSet.fromSet(context.getContexts());
String server = contexts.getValues("server").stream().findAny().orElse(null);
String world = contexts.getValues("world").stream().findAny().orElse(null);
contexts.removeAll("server");
contexts.removeAll("world");
Contexts context = contexts.getContexts();
String server = contexts.getServer();
String world = contexts.getWorld();
SortedSet<LocalizedNode> all = new TreeSet<>((SortedSet<LocalizedNode>) getPermissions(true));
for (LocalizedNode ln : all) {
@ -332,7 +328,7 @@ public abstract class PermissionHolder {
if (!n.isMeta() && !n.isPrefix() && !n.isSuffix()) continue;
if (!n.shouldApplyOnServer(server, context.isIncludeGlobal(), false)) continue;
if (!n.shouldApplyOnWorld(world, context.isIncludeGlobalWorld(), false)) continue;
if (!n.shouldApplyWithContext(contexts, false)) continue;
if (!n.shouldApplyWithContext(contexts.getContextSet(), false)) continue;
holder.accumulateNode(n);
}
@ -346,7 +342,7 @@ public abstract class PermissionHolder {
parents.removeIf(node ->
!node.shouldApplyOnServer(server, context.isApplyGlobalGroups(), plugin.getConfiguration().isApplyingRegex()) ||
!node.shouldApplyOnWorld(world, context.isApplyGlobalWorldGroups(), plugin.getConfiguration().isApplyingRegex()) ||
!node.shouldApplyWithContext(contexts, false)
!node.shouldApplyWithContext(contexts.getContextSet(), false)
);
TreeSet<Map.Entry<Integer, Node>> sortedParents = new TreeSet<>(Util.META_COMPARATOR.reversed());
@ -370,7 +366,7 @@ public abstract class PermissionHolder {
continue;
}
group.accumulateMeta(holder, excludedGroups, context);
group.accumulateMeta(holder, excludedGroups, contexts);
}
return holder;
@ -378,28 +374,26 @@ public abstract class PermissionHolder {
/**
* Gets all of the nodes that this holder has (and inherits), given the context
* @param context the context for this request
* @param contexts the context for this request
* @return a map of permissions
*/
public Set<LocalizedNode> getAllNodesFiltered(Contexts context) {
public Set<LocalizedNode> getAllNodesFiltered(ExtractedContexts contexts) {
SortedSet<LocalizedNode> allNodes;
Contexts context = contexts.getContexts();
String server = contexts.getServer();
String world = contexts.getWorld();
if (context.isApplyGroups()) {
allNodes = getAllNodes(null, context);
allNodes = getAllNodes(null, contexts);
} else {
allNodes = new TreeSet<>((SortedSet<LocalizedNode>) getPermissions(true));
}
MutableContextSet contexts = MutableContextSet.fromSet(context.getContexts());
String server = contexts.getValues("server").stream().findAny().orElse(null);
String world = contexts.getValues("world").stream().findAny().orElse(null);
contexts.removeAll("server");
contexts.removeAll("world");
allNodes.removeIf(node ->
!node.shouldApplyOnServer(server, context.isIncludeGlobal(), plugin.getConfiguration().isApplyingRegex()) ||
!node.shouldApplyOnWorld(world, context.isIncludeGlobalWorld(), plugin.getConfiguration().isApplyingRegex()) ||
!node.shouldApplyWithContext(contexts, false)
!node.shouldApplyWithContext(contexts.getContextSet(), false)
);
Set<LocalizedNode> perms = ConcurrentHashMap.newKeySet();
@ -420,14 +414,14 @@ public abstract class PermissionHolder {
}
/**
* Converts the output of {@link #getAllNodesFiltered(Contexts)}, and expands shorthand perms
* Converts the output of {@link #getAllNodesFiltered(ExtractedContexts)}, and expands shorthand perms
* @param context the context for this request
* @return a map of permissions
*/
public Map<String, Boolean> exportNodes(Contexts context, boolean lowerCase) {
Map<String, Boolean> perms = new HashMap<>();
for (LocalizedNode ln : getAllNodesFiltered(context)) {
for (LocalizedNode ln : getAllNodesFiltered(ExtractedContexts.generate(context))) {
Node node = ln.getNode();
perms.put(lowerCase ? node.getPermission().toLowerCase() : node.getPermission(), node.getValue());
@ -527,7 +521,7 @@ public abstract class PermissionHolder {
* @return the result of the lookup
*/
public InheritanceInfo inheritsPermissionInfo(Node node) {
for (LocalizedNode n : getAllNodes(null, Contexts.allowAll())) {
for (LocalizedNode n : getAllNodes(null, ExtractedContexts.generate(Contexts.allowAll()))) {
if (n.getNode().almostEquals(node)) {
return InheritanceInfo.of(n);
}

View File

@ -0,0 +1,65 @@
/*
* 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 lombok.Getter;
import lombok.ToString;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.api.context.MutableContextSet;
@Getter
@ToString
public class ExtractedContexts {
public static ExtractedContexts generate(Contexts contexts) {
return new ExtractedContexts(contexts);
}
public static ExtractedContexts generate(ContextSet contexts) {
return new ExtractedContexts(contexts);
}
private Contexts contexts;
private ContextSet contextSet;
private String server;
private String world;
private ExtractedContexts(Contexts context) {
this.contexts = context;
setup(context.getContexts());
}
private ExtractedContexts(ContextSet contexts) {
setup(contexts);
}
private void setup(ContextSet contexts) {
MutableContextSet contextSet = MutableContextSet.fromSet(contexts);
server = contextSet.getValues("server").stream().findAny().orElse(null);
world = contextSet.getValues("world").stream().findAny().orElse(null);
contextSet.removeAll("server");
contextSet.removeAll("world");
this.contextSet = contextSet.makeImmutable();
}
}

View File

@ -24,10 +24,10 @@ package me.lucko.luckperms.sponge.migration;
import lombok.experimental.UtilityClass;
import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.common.core.NodeBuilder;
import me.lucko.luckperms.common.core.NodeFactory;
import me.lucko.luckperms.common.core.PermissionHolder;
import me.lucko.luckperms.common.utils.ExtractedContexts;
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
import me.lucko.luckperms.sponge.service.LuckPermsService;
import org.spongepowered.api.service.context.Context;
@ -47,11 +47,10 @@ public class MigrationUtils {
for (Map.Entry<Set<Context>, Map<String, Boolean>> e : perms.entrySet()) {
ContextSet context = LuckPermsService.convertContexts(e.getKey());
MutableContextSet contexts = MutableContextSet.fromSet(context);
String server = contexts.getValues("server").stream().findAny().orElse(null);
String world = contexts.getValues("world").stream().findAny().orElse(null);
contexts.removeAll("server");
contexts.removeAll("world");
ExtractedContexts extractedContexts = ExtractedContexts.generate(context);
ContextSet contexts = extractedContexts.getContextSet();
String server = extractedContexts.getServer();
String world = extractedContexts.getWorld();
for (Map.Entry<String, Boolean> perm : e.getValue().entrySet()) {
try {
@ -66,11 +65,10 @@ public class MigrationUtils {
for (Map.Entry<Set<Context>, Map<String, String>> e : opts.entrySet()) {
ContextSet context = LuckPermsService.convertContexts(e.getKey());
MutableContextSet contexts = MutableContextSet.fromSet(context);
String server = contexts.getValues("server").stream().findAny().orElse(null);
String world = contexts.getValues("world").stream().findAny().orElse(null);
contexts.removeAll("server");
contexts.removeAll("world");
ExtractedContexts extractedContexts = ExtractedContexts.generate(context);
ContextSet contexts = extractedContexts.getContextSet();
String server = extractedContexts.getServer();
String world = extractedContexts.getWorld();
for (Map.Entry<String, String> opt : e.getValue().entrySet()) {
if (opt.getKey().equalsIgnoreCase("prefix")) {
@ -98,11 +96,10 @@ public class MigrationUtils {
for (Map.Entry<Set<Context>, List<Subject>> e : parents.entrySet()) {
ContextSet context = LuckPermsService.convertContexts(e.getKey());
MutableContextSet contexts = MutableContextSet.fromSet(context);
String server = contexts.getValues("server").stream().findAny().orElse(null);
String world = contexts.getValues("world").stream().findAny().orElse(null);
contexts.removeAll("server");
contexts.removeAll("world");
ExtractedContexts extractedContexts = ExtractedContexts.generate(context);
ContextSet contexts = extractedContexts.getContextSet();
String server = extractedContexts.getServer();
String world = extractedContexts.getWorld();
for (Subject s : e.getValue()) {
if (!s.getContainingCollection().getIdentifier().equalsIgnoreCase(PermissionService.SUBJECTS_GROUP)) {

View File

@ -31,6 +31,7 @@ import me.lucko.luckperms.api.LocalizedNode;
import me.lucko.luckperms.api.Node;
import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.common.groups.Group;
import me.lucko.luckperms.common.utils.ExtractedContexts;
import me.lucko.luckperms.sponge.timings.LPTiming;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.service.permission.NodeTree;
@ -84,7 +85,7 @@ public class LuckPermsGroupSubject extends LuckPermsSubject {
@Override
public Tristate getPermissionValue(ContextSet contexts, String permission) {
try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.GROUP_GET_PERMISSION_VALUE)) {
Map<String, Boolean> permissions = group.getAllNodesFiltered(service.calculateContexts(contexts)).stream()
Map<String, Boolean> permissions = group.getAllNodesFiltered(ExtractedContexts.generate(service.calculateContexts(contexts))).stream()
.map(LocalizedNode::getNode)
.collect(Collectors.toMap(Node::getPermission, Node::getValue));
@ -113,7 +114,7 @@ public class LuckPermsGroupSubject extends LuckPermsSubject {
@Override
public List<Subject> getParents(ContextSet contexts) {
try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.GROUP_GET_PARENTS)) {
List<Subject> subjects = group.getAllNodesFiltered(service.calculateContexts(contexts)).stream()
List<Subject> subjects = group.getAllNodesFiltered(ExtractedContexts.generate(service.calculateContexts(contexts))).stream()
.map(LocalizedNode::getNode)
.filter(Node::isGroupNode)
.map(Node::getGroupName)
@ -165,7 +166,7 @@ public class LuckPermsGroupSubject extends LuckPermsSubject {
int priority = Integer.MIN_VALUE;
String meta = null;
for (Node n : group.getAllNodesFiltered(service.calculateContexts(contexts))) {
for (Node n : group.getAllNodesFiltered(ExtractedContexts.generate(service.calculateContexts(contexts)))) {
if (!n.getValue()) {
continue;
}
@ -185,7 +186,7 @@ public class LuckPermsGroupSubject extends LuckPermsSubject {
}
private Optional<String> getMeta(ContextSet contexts, String key) {
for (Node n : group.getAllNodesFiltered(service.calculateContexts(contexts))) {
for (Node n : group.getAllNodesFiltered(ExtractedContexts.generate(service.calculateContexts(contexts)))) {
if (!n.getValue()) {
continue;
}