From c0734fc29e98d846b281995d774b59d8e35713b6 Mon Sep 17 00:00:00 2001 From: Luck Date: Fri, 18 Nov 2016 22:39:00 +0000 Subject: [PATCH] refactor contexts slightly --- .../luckperms/bukkit/vault/VaultChatHook.java | 4 +- .../api/internal/PermissionHolderLink.java | 7 +- .../luckperms/common/caching/UserCache.java | 5 +- .../commands/generic/meta/MetaInfo.java | 3 +- .../common/core/PermissionHolder.java | 58 ++++++++--------- .../common/utils/ExtractedContexts.java | 65 +++++++++++++++++++ .../sponge/migration/MigrationUtils.java | 29 ++++----- .../sponge/service/LuckPermsGroupSubject.java | 9 +-- 8 files changed, 121 insertions(+), 59 deletions(-) create mode 100644 common/src/main/java/me/lucko/luckperms/common/utils/ExtractedContexts.java 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 43d5e0df3..feb655f2e 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 @@ -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; } diff --git a/common/src/main/java/me/lucko/luckperms/common/api/internal/PermissionHolderLink.java b/common/src/main/java/me/lucko/luckperms/common/api/internal/PermissionHolderLink.java index b2655919b..cbbcf4cfb 100644 --- a/common/src/main/java/me/lucko/luckperms/common/api/internal/PermissionHolderLink.java +++ b/common/src/main/java/me/lucko/luckperms/common/api/internal/PermissionHolderLink.java @@ -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 getAllNodes() { - return Collections.unmodifiableSet(master.getAllNodes(null, Contexts.allowAll())); + return Collections.unmodifiableSet(master.getAllNodes(null, ExtractedContexts.generate(Contexts.allowAll()))); } @Override public SortedSet getAllNodes(@NonNull Contexts contexts) { - return master.getAllNodes(null, contexts); + return master.getAllNodes(null, ExtractedContexts.generate(contexts)); } @Override public Set getAllNodesFiltered(@NonNull Contexts contexts) { - return master.getAllNodesFiltered(contexts); + return master.getAllNodesFiltered(ExtractedContexts.generate(contexts)); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/caching/UserCache.java b/common/src/main/java/me/lucko/luckperms/common/caching/UserCache.java index 290bda287..b0b7a1941 100644 --- a/common/src/main/java/me/lucko/luckperms/common/caching/UserCache.java +++ b/common/src/main/java/me/lucko/luckperms/common/caching/UserCache.java @@ -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 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; } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaInfo.java b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaInfo.java index 5b360de17..858983a7d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaInfo.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/generic/meta/MetaInfo.java @@ -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 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; } diff --git a/common/src/main/java/me/lucko/luckperms/common/core/PermissionHolder.java b/common/src/main/java/me/lucko/luckperms/common/core/PermissionHolder.java index db4cdd7d7..5d92488ee 100644 --- a/common/src/main/java/me/lucko/luckperms/common/core/PermissionHolder.java +++ b/common/src/main/java/me/lucko/luckperms/common/core/PermissionHolder.java @@ -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 getAllNodes(List excludedGroups, Contexts context) { + public SortedSet getAllNodes(List excludedGroups, ExtractedContexts contexts) { SortedSet all = new TreeSet<>((SortedSet) 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> 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 excludedGroups, Contexts context) { + public MetaHolder accumulateMeta(MetaHolder holder, List 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 all = new TreeSet<>((SortedSet) 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> 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 getAllNodesFiltered(Contexts context) { + public Set getAllNodesFiltered(ExtractedContexts contexts) { SortedSet 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) 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 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 exportNodes(Contexts context, boolean lowerCase) { Map 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); } diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/ExtractedContexts.java b/common/src/main/java/me/lucko/luckperms/common/utils/ExtractedContexts.java new file mode 100644 index 000000000..7c989f969 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/utils/ExtractedContexts.java @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * 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(); + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationUtils.java b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationUtils.java index 818132697..a0a840fa6 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationUtils.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationUtils.java @@ -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, Map> 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 perm : e.getValue().entrySet()) { try { @@ -66,11 +65,10 @@ public class MigrationUtils { for (Map.Entry, Map> 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 opt : e.getValue().entrySet()) { if (opt.getKey().equalsIgnoreCase("prefix")) { @@ -98,11 +96,10 @@ public class MigrationUtils { for (Map.Entry, List> 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)) { diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsGroupSubject.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsGroupSubject.java index 55446ac0a..0673c78ca 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsGroupSubject.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsGroupSubject.java @@ -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 permissions = group.getAllNodesFiltered(service.calculateContexts(contexts)).stream() + Map 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 getParents(ContextSet contexts) { try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.GROUP_GET_PARENTS)) { - List subjects = group.getAllNodesFiltered(service.calculateContexts(contexts)).stream() + List 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 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; }