From 26afb69a14fc4e922b36bba8957d629d2998df22 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 26 Feb 2017 01:00:02 +0000 Subject: [PATCH] Cleanup LuckPermsSubjectData (fixes #194) --- .../java/me/lucko/luckperms/api/Node.java | 10 + .../common/core/model/ImmutableNode.java | 14 + .../common/core/model/PermissionHolder.java | 30 ++ .../luckperms/sponge/LPSpongePlugin.java | 2 +- .../sponge/contexts/SpongeCalculatorLink.java | 2 +- .../sponge/managers/SpongeGroupManager.java | 4 +- .../sponge/managers/SpongeUserManager.java | 4 +- .../sponge/migration/MigrationUtils.java | 2 +- .../luckperms/sponge/model/SpongeGroup.java | 4 +- .../luckperms/sponge/model/SpongeUser.java | 2 +- .../sponge/service/LuckPermsService.java | 6 +- .../sponge/service/LuckPermsSubjectData.java | 370 ++++++------------ .../service/ServiceCacheHousekeepingTask.java | 4 +- .../calculated/CalculatedSubjectData.java | 4 +- .../persisted/PersistedCollection.java | 4 +- .../service/persisted/PersistedSubject.java | 2 +- .../service/{base => proxy}/LPSubject.java | 6 +- .../{base => proxy}/LPSubjectCollection.java | 4 +- .../{base => proxy}/LPSubjectData.java | 6 +- .../sponge/service/{base => proxy}/Util.java | 2 +- .../SubjectCollectionReference.java | 2 +- .../service/references/SubjectReference.java | 2 +- 22 files changed, 203 insertions(+), 283 deletions(-) rename sponge/src/main/java/me/lucko/luckperms/sponge/service/{base => proxy}/LPSubject.java (96%) rename sponge/src/main/java/me/lucko/luckperms/sponge/service/{base => proxy}/LPSubjectCollection.java (96%) rename sponge/src/main/java/me/lucko/luckperms/sponge/service/{base => proxy}/LPSubjectData.java (96%) rename sponge/src/main/java/me/lucko/luckperms/sponge/service/{base => proxy}/Util.java (98%) diff --git a/api/src/main/java/me/lucko/luckperms/api/Node.java b/api/src/main/java/me/lucko/luckperms/api/Node.java index 2b747e9c..78428088 100644 --- a/api/src/main/java/me/lucko/luckperms/api/Node.java +++ b/api/src/main/java/me/lucko/luckperms/api/Node.java @@ -203,11 +203,21 @@ public interface Node extends Map.Entry { boolean hasExpired(); /** + * Gets the extra contexts required for this node to apply + * * @return the extra contexts required for this node to apply * @since 2.13 */ ContextSet getContexts(); + /** + * The same as {@link #getContexts()}, but also includes values for "server" and "world" keys if present. + * + * @return the full contexts required for this node to apply + * @since 3.1 + */ + ContextSet getFullContexts(); + /** * Converts this node into a serialized form * diff --git a/common/src/main/java/me/lucko/luckperms/common/core/model/ImmutableNode.java b/common/src/main/java/me/lucko/luckperms/common/core/model/ImmutableNode.java index a0a6811a..7d8f98c7 100644 --- a/common/src/main/java/me/lucko/luckperms/common/core/model/ImmutableNode.java +++ b/common/src/main/java/me/lucko/luckperms/common/core/model/ImmutableNode.java @@ -36,6 +36,7 @@ import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.Tristate; 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.common.constants.Patterns; import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.utils.ShorthandParser; @@ -148,6 +149,9 @@ public class ImmutableNode implements Node { @Getter private final ImmutableContextSet contexts; + @Getter + private final ImmutableContextSet fullContexts; + // Cached state private final boolean isGroup; private String groupName; @@ -234,6 +238,16 @@ public class ImmutableNode implements Node { resolvedShorthand = ImmutableList.copyOf(ShorthandParser.parseShorthand(getPermission())); serializedNode = calculateSerializedNode(); + + MutableContextSet fullContexts = this.contexts.mutableCopy(); + if (isServerSpecific()) { + fullContexts.add("server", this.server); + } + if (isWorldSpecific()) { + fullContexts.add("world", this.world); + } + + this.fullContexts = fullContexts.makeImmutable(); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/core/model/PermissionHolder.java b/common/src/main/java/me/lucko/luckperms/common/core/model/PermissionHolder.java index a3348776..febbf095 100644 --- a/common/src/main/java/me/lucko/luckperms/common/core/model/PermissionHolder.java +++ b/common/src/main/java/me/lucko/luckperms/common/core/model/PermissionHolder.java @@ -714,6 +714,12 @@ public abstract class PermissionHolder { plugin.getApiProvider().getEventFactory().handleNodeAdd(node, this, before, after); } + public void setPermissionUnchecked(Node node) { + try { + setPermission(node); + } catch (ObjectAlreadyHasException ignored) {} + } + /** * Sets a permission node, applying a temporary modifier if the node is temporary. * @param node the node to set @@ -783,6 +789,12 @@ public abstract class PermissionHolder { return node; } + public void setPermissionUnchecked(Node node, TemporaryModifier modifier) { + try { + setPermission(node, modifier); + } catch (ObjectAlreadyHasException ignored) {} + } + /** * Sets a transient permission node * @@ -806,6 +818,12 @@ public abstract class PermissionHolder { plugin.getApiProvider().getEventFactory().handleNodeAdd(node, this, before, after); } + public void setTransientPermissionUnchecked(Node node) { + try { + setTransientPermission(node); + } catch (ObjectAlreadyHasException ignored) {} + } + public void setPermission(String node, boolean value) throws ObjectAlreadyHasException { setPermission(buildNode(node).setValue(value).build()); } @@ -852,6 +870,12 @@ public abstract class PermissionHolder { plugin.getApiProvider().getEventFactory().handleNodeRemove(node, this, before, after); } + public void unsetPermissionUnchecked(Node node) { + try { + unsetPermission(node); + } catch (ObjectLacksException ignored) {} + } + /** * Unsets a transient permission node * @@ -874,6 +898,12 @@ public abstract class PermissionHolder { plugin.getApiProvider().getEventFactory().handleNodeRemove(node, this, before, after); } + public void unsetTransientPermissionUnchecked(Node node) { + try { + unsetTransientPermission(node); + } catch (ObjectLacksException ignored) {} + } + public void unsetPermission(String node, boolean temporary) throws ObjectLacksException { unsetPermission(buildNode(node).setExpiry(temporary ? 10L : 0L).build()); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java index f9df8d23..e375ccc5 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java @@ -70,8 +70,8 @@ import me.lucko.luckperms.sponge.managers.SpongeUserManager; import me.lucko.luckperms.sponge.messaging.BungeeMessagingService; import me.lucko.luckperms.sponge.service.LuckPermsService; import me.lucko.luckperms.sponge.service.ServiceCacheHousekeepingTask; -import me.lucko.luckperms.sponge.service.base.LPSubjectCollection; import me.lucko.luckperms.sponge.service.persisted.PersistedCollection; +import me.lucko.luckperms.sponge.service.proxy.LPSubjectCollection; import me.lucko.luckperms.sponge.timings.LPTimings; import me.lucko.luckperms.sponge.utils.VersionData; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/SpongeCalculatorLink.java b/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/SpongeCalculatorLink.java index ce90d7a7..2bce1bef 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/SpongeCalculatorLink.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/SpongeCalculatorLink.java @@ -26,7 +26,7 @@ import lombok.AllArgsConstructor; import me.lucko.luckperms.api.context.ContextCalculator; import me.lucko.luckperms.api.context.MutableContextSet; -import me.lucko.luckperms.sponge.service.base.Util; +import me.lucko.luckperms.sponge.service.proxy.Util; import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.permission.Subject; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeGroupManager.java b/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeGroupManager.java index 26923797..8267f75f 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeGroupManager.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeGroupManager.java @@ -42,8 +42,8 @@ import me.lucko.luckperms.common.utils.ImmutableCollectors; import me.lucko.luckperms.sponge.LPSpongePlugin; import me.lucko.luckperms.sponge.model.SpongeGroup; import me.lucko.luckperms.sponge.service.LuckPermsService; -import me.lucko.luckperms.sponge.service.base.LPSubject; -import me.lucko.luckperms.sponge.service.base.LPSubjectCollection; +import me.lucko.luckperms.sponge.service.proxy.LPSubject; +import me.lucko.luckperms.sponge.service.proxy.LPSubjectCollection; import me.lucko.luckperms.sponge.service.references.SubjectReference; import me.lucko.luckperms.sponge.timings.LPTiming; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeUserManager.java b/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeUserManager.java index 3d91c69f..9ca1574b 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeUserManager.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeUserManager.java @@ -43,8 +43,8 @@ import me.lucko.luckperms.common.utils.ImmutableCollectors; import me.lucko.luckperms.sponge.LPSpongePlugin; import me.lucko.luckperms.sponge.model.SpongeUser; import me.lucko.luckperms.sponge.service.LuckPermsService; -import me.lucko.luckperms.sponge.service.base.LPSubject; -import me.lucko.luckperms.sponge.service.base.LPSubjectCollection; +import me.lucko.luckperms.sponge.service.proxy.LPSubject; +import me.lucko.luckperms.sponge.service.proxy.LPSubjectCollection; import me.lucko.luckperms.sponge.service.references.SubjectReference; import me.lucko.luckperms.sponge.timings.LPTiming; 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 e854a58b..d5a1dd4d 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 @@ -30,7 +30,7 @@ import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.core.model.PermissionHolder; import me.lucko.luckperms.common.utils.ExtractedContexts; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; -import me.lucko.luckperms.sponge.service.base.Util; +import me.lucko.luckperms.sponge.service.proxy.Util; import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.permission.PermissionService; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeGroup.java b/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeGroup.java index 66068c0f..ad8089e7 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeGroup.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeGroup.java @@ -39,8 +39,8 @@ import me.lucko.luckperms.common.utils.ExtractedContexts; import me.lucko.luckperms.sponge.LPSpongePlugin; import me.lucko.luckperms.sponge.service.LuckPermsService; import me.lucko.luckperms.sponge.service.LuckPermsSubjectData; -import me.lucko.luckperms.sponge.service.base.LPSubject; -import me.lucko.luckperms.sponge.service.base.Util; +import me.lucko.luckperms.sponge.service.proxy.LPSubject; +import me.lucko.luckperms.sponge.service.proxy.Util; import me.lucko.luckperms.sponge.service.references.SubjectCollectionReference; import me.lucko.luckperms.sponge.service.references.SubjectReference; import me.lucko.luckperms.sponge.timings.LPTiming; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeUser.java b/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeUser.java index 51cfd619..d930c749 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeUser.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeUser.java @@ -33,7 +33,7 @@ import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.sponge.LPSpongePlugin; import me.lucko.luckperms.sponge.service.LuckPermsService; import me.lucko.luckperms.sponge.service.LuckPermsSubjectData; -import me.lucko.luckperms.sponge.service.base.LPSubject; +import me.lucko.luckperms.sponge.service.proxy.LPSubject; import me.lucko.luckperms.sponge.service.references.SubjectCollectionReference; import me.lucko.luckperms.sponge.service.references.SubjectReference; import me.lucko.luckperms.sponge.timings.LPTiming; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java index 66ebaf65..971ab9ea 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java @@ -54,14 +54,14 @@ import me.lucko.luckperms.sponge.contexts.SpongeCalculatorLink; import me.lucko.luckperms.sponge.managers.SpongeGroupManager; import me.lucko.luckperms.sponge.managers.SpongeUserManager; import me.lucko.luckperms.sponge.model.SpongeGroup; -import me.lucko.luckperms.sponge.service.base.LPSubject; -import me.lucko.luckperms.sponge.service.base.LPSubjectCollection; -import me.lucko.luckperms.sponge.service.base.LPSubjectData; import me.lucko.luckperms.sponge.service.calculated.CalculatedSubjectData; import me.lucko.luckperms.sponge.service.calculated.OptionLookup; import me.lucko.luckperms.sponge.service.calculated.PermissionLookup; import me.lucko.luckperms.sponge.service.persisted.PersistedCollection; import me.lucko.luckperms.sponge.service.persisted.SubjectStorage; +import me.lucko.luckperms.sponge.service.proxy.LPSubject; +import me.lucko.luckperms.sponge.service.proxy.LPSubjectCollection; +import me.lucko.luckperms.sponge.service.proxy.LPSubjectData; import me.lucko.luckperms.sponge.service.references.SubjectReference; import me.lucko.luckperms.sponge.timings.LPTiming; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java index a5ab2283..443f80ff 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java @@ -33,7 +33,6 @@ import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.Tristate; 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.common.caching.MetaHolder; import me.lucko.luckperms.common.core.NodeBuilder; import me.lucko.luckperms.common.core.NodeFactory; @@ -41,25 +40,23 @@ import me.lucko.luckperms.common.core.model.Group; import me.lucko.luckperms.common.core.model.PermissionHolder; import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.utils.ExtractedContexts; -import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; -import me.lucko.luckperms.exceptions.ObjectLacksException; -import me.lucko.luckperms.sponge.service.base.LPSubject; -import me.lucko.luckperms.sponge.service.base.LPSubjectData; +import me.lucko.luckperms.sponge.service.proxy.LPSubject; +import me.lucko.luckperms.sponge.service.proxy.LPSubjectData; import me.lucko.luckperms.sponge.service.references.SubjectReference; import me.lucko.luckperms.sponge.timings.LPTiming; -import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.permission.PermissionService; import co.aikar.timings.Timing; -import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Consumer; import java.util.stream.Collectors; +import java.util.stream.Stream; @SuppressWarnings({"OptionalGetWithoutIsPresent", "unused"}) @AllArgsConstructor @@ -73,33 +70,13 @@ public class LuckPermsSubjectData implements LPSubjectData { @Getter LPSubject parentSubject; - private void objectSave(PermissionHolder t) { - if (t instanceof User) { - service.getPlugin().getStorage().saveUser(((User) t)) - .thenRunAsync(() -> ((User) t).getRefreshBuffer().request(), service.getPlugin().getScheduler().getAsyncExecutor()); - } - if (t instanceof Group) { - service.getPlugin().getStorage().saveGroup((Group) t) - .thenRunAsync(() -> service.getPlugin().getUpdateTaskBuffer().request(), service.getPlugin().getScheduler().getAsyncExecutor()); - } - } - @Override public Map> getPermissions() { try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_GET_PERMISSIONS)) { Map> perms = new HashMap<>(); for (Node n : enduring ? holder.getNodes() : holder.getTransientNodes()) { - MutableContextSet contexts = MutableContextSet.fromSet(n.getContexts()); - - if (n.isServerSpecific()) { - contexts.add(new Context(LuckPermsService.SERVER_CONTEXT, n.getServer().get())); - } - - if (n.isWorldSpecific()) { - contexts.add(new Context(Context.WORLD_KEY, n.getWorld().get())); - } - + ContextSet contexts = n.getFullContexts(); perms.computeIfAbsent(contexts.makeImmutable(), cs -> new HashMap<>()).put(n.getPermission(), n.getValue()); } @@ -118,13 +95,10 @@ public class LuckPermsSubjectData implements LPSubjectData { // Unset Node node = new NodeBuilder(permission).withExtraContext(contexts).build(); - try { - if (enduring) { - holder.unsetPermission(node); - } else { - holder.unsetTransientPermission(node); - } - } catch (ObjectLacksException ignored) { + if (enduring) { + holder.unsetPermissionUnchecked(node); + } else { + holder.unsetTransientPermissionUnchecked(node); } objectSave(holder); @@ -134,22 +108,16 @@ public class LuckPermsSubjectData implements LPSubjectData { Node node = new NodeBuilder(permission).setValue(tristate.asBoolean()).withExtraContext(contexts).build(); // Workaround: unset the inverse, to allow false -> true, true -> false overrides. - try { - if (enduring) { - holder.unsetPermission(node); - } else { - holder.unsetTransientPermission(node); - } - } catch (ObjectLacksException ignored) { + if (enduring) { + holder.unsetPermissionUnchecked(node); + } else { + holder.unsetTransientPermissionUnchecked(node); } - try { - if (enduring) { - holder.setPermission(node); - } else { - holder.setTransientPermission(node); - } - } catch (ObjectAlreadyHasException ignored) { + if (enduring) { + holder.setPermissionUnchecked(node); + } else { + holder.setTransientPermissionUnchecked(node); } objectSave(holder); @@ -165,41 +133,24 @@ public class LuckPermsSubjectData implements LPSubjectData { } else { holder.clearTransientNodes(); } + + if (holder instanceof User) { + service.getPlugin().getUserManager().giveDefaultIfNeeded(((User) holder), false); + } + objectSave(holder); return true; } } @Override - public boolean clearPermissions(@NonNull ContextSet c) { + public boolean clearPermissions(@NonNull ContextSet set) { try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_CLEAR_PERMISSIONS)) { - List toRemove = new ArrayList<>(); - for (Node n : enduring ? holder.getNodes() : holder.getTransientNodes()) { - MutableContextSet contexts = MutableContextSet.fromSet(n.getContexts()); + List toRemove = streamNodes(enduring) + .filter(n -> n.getFullContexts().equals(set)) + .collect(Collectors.toList()); - if (n.isServerSpecific()) { - contexts.add(new Context(LuckPermsService.SERVER_CONTEXT, n.getServer().get())); - } - - if (n.isWorldSpecific()) { - contexts.add(new Context(Context.WORLD_KEY, n.getWorld().get())); - } - - if (contexts.equals(c)) { - toRemove.add(n); - } - } - - toRemove.forEach(n -> { - try { - if (enduring) { - holder.unsetPermission(n); - } else { - holder.unsetTransientPermission(n); - } - } catch (ObjectLacksException ignored) { - } - }); + toRemove.forEach(makeUnsetConsumer(enduring)); if (holder instanceof User) { service.getPlugin().getUserManager().giveDefaultIfNeeded(((User) holder), false); @@ -216,20 +167,9 @@ public class LuckPermsSubjectData implements LPSubjectData { Map> parents = new HashMap<>(); for (Node n : enduring ? holder.getNodes() : holder.getTransientNodes()) { - if (!n.isGroupNode()) { - continue; - } - - MutableContextSet contexts = MutableContextSet.fromSet(n.getContexts()); - - if (n.isServerSpecific()) { - contexts.add(new Context(LuckPermsService.SERVER_CONTEXT, n.getServer().get())); - } - - if (n.isWorldSpecific()) { - contexts.add(new Context(Context.WORLD_KEY, n.getWorld().get())); - } + if (!n.isGroupNode()) continue; + ContextSet contexts = n.getFullContexts(); parents.computeIfAbsent(contexts.makeImmutable(), cs -> new HashSet<>()).add(service.getGroupSubjects().get(n.getGroupName()).toReference()); } @@ -247,17 +187,14 @@ public class LuckPermsSubjectData implements LPSubjectData { if (subject.getCollection().equals(PermissionService.SUBJECTS_GROUP)) { LPSubject permsSubject = subject.resolve(service); - try { - if (enduring) { - holder.setPermission(new NodeBuilder("group." + permsSubject.getIdentifier()) - .withExtraContext(contexts) - .build()); - } else { - holder.setTransientPermission(new NodeBuilder("group." + permsSubject.getIdentifier()) - .withExtraContext(contexts) - .build()); - } - } catch (ObjectAlreadyHasException ignored) { + if (enduring) { + holder.setPermissionUnchecked(new NodeBuilder("group." + permsSubject.getIdentifier()) + .withExtraContext(contexts) + .build()); + } else { + holder.setTransientPermissionUnchecked(new NodeBuilder("group." + permsSubject.getIdentifier()) + .withExtraContext(contexts) + .build()); } objectSave(holder); @@ -273,17 +210,14 @@ public class LuckPermsSubjectData implements LPSubjectData { if (subject.getCollection().equals(PermissionService.SUBJECTS_GROUP)) { LPSubject permsSubject = subject.resolve(service); - try { - if (enduring) { - holder.unsetPermission(new NodeBuilder("group." + permsSubject.getIdentifier()) - .withExtraContext(contexts) - .build()); - } else { - holder.unsetTransientPermission(new NodeBuilder("group." + permsSubject.getIdentifier()) - .withExtraContext(contexts) - .build()); - } - } catch (ObjectLacksException ignored) { + if (enduring) { + holder.unsetPermissionUnchecked(new NodeBuilder("group." + permsSubject.getIdentifier()) + .withExtraContext(contexts) + .build()); + } else { + holder.unsetTransientPermissionUnchecked(new NodeBuilder("group." + permsSubject.getIdentifier()) + .withExtraContext(contexts) + .build()); } objectSave(holder); @@ -296,20 +230,11 @@ public class LuckPermsSubjectData implements LPSubjectData { @Override public boolean clearParents() { try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_CLEAR_PARENTS)) { - List toRemove = (enduring ? holder.getNodes() : holder.getTransientNodes()).stream() + List toRemove = streamNodes(enduring) .filter(Node::isGroupNode) .collect(Collectors.toList()); - toRemove.forEach(n -> { - try { - if (enduring) { - holder.unsetPermission(n); - } else { - holder.unsetTransientPermission(n); - } - } catch (ObjectLacksException ignored) { - } - }); + toRemove.forEach(makeUnsetConsumer(enduring)); if (holder instanceof User) { service.getPlugin().getUserManager().giveDefaultIfNeeded(((User) holder), false); @@ -323,37 +248,12 @@ public class LuckPermsSubjectData implements LPSubjectData { @Override public boolean clearParents(@NonNull ContextSet set) { try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_CLEAR_PARENTS)) { - List toRemove = new ArrayList<>(); - for (Node n : enduring ? holder.getNodes() : holder.getTransientNodes()) { - if (!n.isGroupNode()) { - continue; - } + List toRemove = streamNodes(enduring) + .filter(Node::isGroupNode) + .filter(n -> n.getFullContexts().equals(set)) + .collect(Collectors.toList()); - MutableContextSet contexts = MutableContextSet.fromSet(n.getContexts()); - - if (n.isServerSpecific()) { - contexts.add(new Context(LuckPermsService.SERVER_CONTEXT, n.getServer().get())); - } - - if (n.isWorldSpecific()) { - contexts.add(new Context(Context.WORLD_KEY, n.getWorld().get())); - } - - if (contexts.equals(set)) { - toRemove.add(n); - } - } - - toRemove.forEach(n -> { - try { - if (enduring) { - holder.unsetPermission(n); - } else { - holder.unsetTransientPermission(n); - } - } catch (ObjectLacksException ignored) { - } - }); + toRemove.forEach(makeUnsetConsumer(enduring)); if (holder instanceof User) { service.getPlugin().getUserManager().giveDefaultIfNeeded(((User) holder), false); @@ -372,25 +272,10 @@ public class LuckPermsSubjectData implements LPSubjectData { Map minSuffixPriority = new HashMap<>(); for (Node n : enduring ? holder.getNodes() : holder.getTransientNodes()) { - if (!n.getValue()) { - continue; - } + if (!n.getValue()) continue; + if (!n.isMeta() && !n.isPrefix() && !n.isSuffix()) continue; - if (!n.isMeta() && !n.isPrefix() && !n.isSuffix()) { - continue; - } - - MutableContextSet contexts = MutableContextSet.fromSet(n.getContexts()); - - if (n.isServerSpecific()) { - contexts.add(new Context(LuckPermsService.SERVER_CONTEXT, n.getServer().get())); - } - - if (n.isWorldSpecific()) { - contexts.add(new Context(Context.WORLD_KEY, n.getWorld().get())); - } - - ImmutableContextSet immutableContexts = contexts.makeImmutable(); + ImmutableContextSet immutableContexts = n.getFullContexts().makeImmutable(); if (!options.containsKey(immutableContexts)) { options.put(immutableContexts, new HashMap<>()); @@ -436,54 +321,41 @@ public class LuckPermsSubjectData implements LPSubjectData { if (key.equalsIgnoreCase("prefix") || key.equalsIgnoreCase("suffix")) { // special handling. String type = key.toLowerCase(); + boolean prefix = type.equals("prefix"); // remove all prefixes/suffixes from the user - List toRemove = holder.getNodes().stream() - .filter(n -> type.equals("prefix") ? n.isPrefix() : n.isSuffix()) + List toRemove = streamNodes(enduring) + .filter(n -> prefix ? n.isPrefix() : n.isSuffix()) + .filter(n -> n.getFullContexts().equals(context)) .collect(Collectors.toList()); - toRemove.forEach(n -> { - try { - if (enduring) { - holder.unsetPermission(n); - } else { - holder.unsetTransientPermission(n); - } - } catch (ObjectLacksException ignored) {} - }); + toRemove.forEach(makeUnsetConsumer(enduring)); MetaHolder metaHolder = holder.accumulateMeta(null, null, ExtractedContexts.generate(service.calculateContexts(context))); int priority = (type.equals("prefix") ? metaHolder.getPrefixes() : metaHolder.getSuffixes()).keySet().stream() .mapToInt(e -> e).max().orElse(0); priority += 10; - try { - if (enduring) { - holder.setPermission(NodeFactory.makeChatMetaNode(type.equals("prefix"), priority, value).withExtraContext(context).build()); - } else { - holder.setTransientPermission(NodeFactory.makeChatMetaNode(type.equals("prefix"), priority, value).withExtraContext(context).build()); - } - } catch (ObjectAlreadyHasException ignored) {} + if (enduring) { + holder.setPermissionUnchecked(NodeFactory.makeChatMetaNode(type.equals("prefix"), priority, value).withExtraContext(context).build()); + } else { + holder.setTransientPermissionUnchecked(NodeFactory.makeChatMetaNode(type.equals("prefix"), priority, value).withExtraContext(context).build()); + } } else { // standard remove - List toRemove = holder.getNodes().stream() + List toRemove = streamNodes(enduring) .filter(n -> n.isMeta() && n.getMeta().getKey().equals(key)) + .filter(n -> n.getFullContexts().equals(context)) .collect(Collectors.toList()); - toRemove.forEach(n -> { - try { - holder.unsetPermission(n); - } catch (ObjectLacksException ignored) {} - }); + toRemove.forEach(makeUnsetConsumer(enduring)); - try { - if (enduring) { - holder.setPermission(NodeFactory.makeMetaNode(key, value).withExtraContext(context).build()); - } else { - holder.setTransientPermission(NodeFactory.makeMetaNode(key, value).withExtraContext(context).build()); - } - } catch (ObjectAlreadyHasException ignored) {} + if (enduring) { + holder.setPermissionUnchecked(NodeFactory.makeMetaNode(key, value).withExtraContext(context).build()); + } else { + holder.setTransientPermissionUnchecked(NodeFactory.makeMetaNode(key, value).withExtraContext(context).build()); + } } objectSave(holder); @@ -492,21 +364,22 @@ public class LuckPermsSubjectData implements LPSubjectData { } @Override - public boolean unsetOption(ContextSet contexts, String key) { + public boolean unsetOption(ContextSet set, String key) { try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_SET_OPTION)) { - List toRemove = (enduring ? holder.getNodes() : holder.getTransientNodes()).stream() - .filter(n -> n.isMeta() && n.getMeta().getKey().equals(key)) + List toRemove = streamNodes(enduring) + .filter(n -> { + if (key.equalsIgnoreCase("prefix")) { + return n.isPrefix(); + } else if (key.equalsIgnoreCase("suffix")) { + return n.isSuffix(); + } else { + return n.isMeta() && n.getMeta().getKey().equals(key); + } + }) + .filter(n -> n.getFullContexts().equals(set)) .collect(Collectors.toList()); - toRemove.forEach(n -> { - try { - if (enduring) { - holder.unsetPermission(n); - } else { - holder.unsetTransientPermission(n); - } - } catch (ObjectLacksException ignored) {} - }); + toRemove.forEach(makeUnsetConsumer(enduring)); objectSave(holder); return true; @@ -516,36 +389,12 @@ public class LuckPermsSubjectData implements LPSubjectData { @Override public boolean clearOptions(@NonNull ContextSet set) { try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_CLEAR_OPTIONS)) { - List toRemove = new ArrayList<>(); - for (Node n : enduring ? holder.getNodes() : holder.getTransientNodes()) { - if (!n.isMeta() && !n.isPrefix() && !n.isSuffix()) { - continue; - } + List toRemove = streamNodes(enduring) + .filter(n -> n.isMeta() || n.isPrefix() || n.isSuffix()) + .filter(n -> n.getFullContexts().equals(set)) + .collect(Collectors.toList()); - MutableContextSet contexts = MutableContextSet.fromSet(n.getContexts()); - - if (n.isServerSpecific()) { - contexts.add(new Context(LuckPermsService.SERVER_CONTEXT, n.getServer().get())); - } - - if (n.isWorldSpecific()) { - contexts.add(new Context(Context.WORLD_KEY, n.getWorld().get())); - } - - if (contexts.equals(set)) { - toRemove.add(n); - } - } - - toRemove.forEach(n -> { - try { - if (enduring) { - holder.unsetPermission(n); - } else { - holder.unsetTransientPermission(n); - } - } catch (ObjectLacksException ignored) {} - }); + toRemove.forEach(makeUnsetConsumer(enduring)); objectSave(holder); return !toRemove.isEmpty(); @@ -555,22 +404,39 @@ public class LuckPermsSubjectData implements LPSubjectData { @Override public boolean clearOptions() { try (Timing i = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_CLEAR_OPTIONS)) { - List toRemove = (enduring ? holder.getNodes() : holder.getTransientNodes()).stream() + List toRemove = streamNodes(enduring) .filter(n -> n.isMeta() || n.isPrefix() || n.isSuffix()) .collect(Collectors.toList()); - toRemove.forEach(n -> { - try { - if (enduring) { - holder.unsetPermission(n); - } else { - holder.unsetTransientPermission(n); - } - } catch (ObjectLacksException ignored) {} - }); + toRemove.forEach(makeUnsetConsumer(enduring)); objectSave(holder); return !toRemove.isEmpty(); } } + + private Stream streamNodes(boolean enduring) { + return (enduring ? holder.getNodes() : holder.getTransientNodes()).stream(); + } + + private Consumer makeUnsetConsumer(boolean enduring) { + return n -> { + if (enduring) { + holder.unsetPermissionUnchecked(n); + } else { + holder.unsetTransientPermissionUnchecked(n); + } + }; + } + + private void objectSave(PermissionHolder t) { + if (t instanceof User) { + service.getPlugin().getStorage().saveUser(((User) t)) + .thenRunAsync(() -> ((User) t).getRefreshBuffer().request(), service.getPlugin().getScheduler().getAsyncExecutor()); + } + if (t instanceof Group) { + service.getPlugin().getStorage().saveGroup((Group) t) + .thenRunAsync(() -> service.getPlugin().getUpdateTaskBuffer().request(), service.getPlugin().getScheduler().getAsyncExecutor()); + } + } } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/ServiceCacheHousekeepingTask.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/ServiceCacheHousekeepingTask.java index 9e910992..857e4bde 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/ServiceCacheHousekeepingTask.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/ServiceCacheHousekeepingTask.java @@ -24,8 +24,8 @@ package me.lucko.luckperms.sponge.service; import lombok.RequiredArgsConstructor; -import me.lucko.luckperms.sponge.service.base.LPSubject; -import me.lucko.luckperms.sponge.service.base.LPSubjectCollection; +import me.lucko.luckperms.sponge.service.proxy.LPSubject; +import me.lucko.luckperms.sponge.service.proxy.LPSubjectCollection; @RequiredArgsConstructor public class ServiceCacheHousekeepingTask implements Runnable { diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/calculated/CalculatedSubjectData.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/calculated/CalculatedSubjectData.java index 0c8623cd..ba3eeac2 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/calculated/CalculatedSubjectData.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/calculated/CalculatedSubjectData.java @@ -41,8 +41,8 @@ import me.lucko.luckperms.common.calculators.PermissionProcessor; import me.lucko.luckperms.common.calculators.processors.MapProcessor; import me.lucko.luckperms.sponge.calculators.SpongeWildcardProcessor; import me.lucko.luckperms.sponge.service.LuckPermsService; -import me.lucko.luckperms.sponge.service.base.LPSubject; -import me.lucko.luckperms.sponge.service.base.LPSubjectData; +import me.lucko.luckperms.sponge.service.proxy.LPSubject; +import me.lucko.luckperms.sponge.service.proxy.LPSubjectData; import me.lucko.luckperms.sponge.service.references.SubjectReference; import java.util.Comparator; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedCollection.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedCollection.java index 20e8baf0..6a68f96c 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedCollection.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedCollection.java @@ -36,8 +36,8 @@ import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.common.utils.ImmutableCollectors; import me.lucko.luckperms.sponge.service.LuckPermsService; -import me.lucko.luckperms.sponge.service.base.LPSubject; -import me.lucko.luckperms.sponge.service.base.LPSubjectCollection; +import me.lucko.luckperms.sponge.service.proxy.LPSubject; +import me.lucko.luckperms.sponge.service.proxy.LPSubjectCollection; import me.lucko.luckperms.sponge.service.references.SubjectReference; import java.util.Collection; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedSubject.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedSubject.java index f8fda279..fb5609d6 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedSubject.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedSubject.java @@ -35,10 +35,10 @@ import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.common.utils.BufferedRequest; import me.lucko.luckperms.sponge.service.LuckPermsService; -import me.lucko.luckperms.sponge.service.base.LPSubject; import me.lucko.luckperms.sponge.service.calculated.CalculatedSubjectData; import me.lucko.luckperms.sponge.service.calculated.OptionLookup; import me.lucko.luckperms.sponge.service.calculated.PermissionLookup; +import me.lucko.luckperms.sponge.service.proxy.LPSubject; import me.lucko.luckperms.sponge.service.references.SubjectCollectionReference; import me.lucko.luckperms.sponge.service.references.SubjectReference; import me.lucko.luckperms.sponge.timings.LPTiming; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/base/LPSubject.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/proxy/LPSubject.java similarity index 96% rename from sponge/src/main/java/me/lucko/luckperms/sponge/service/base/LPSubject.java rename to sponge/src/main/java/me/lucko/luckperms/sponge/service/proxy/LPSubject.java index 5a9d9126..4cf8d29b 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/base/LPSubject.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/proxy/LPSubject.java @@ -20,7 +20,7 @@ * SOFTWARE. */ -package me.lucko.luckperms.sponge.service.base; +package me.lucko.luckperms.sponge.service.proxy; import lombok.NonNull; @@ -39,8 +39,8 @@ import java.util.List; import java.util.Optional; import java.util.Set; -import static me.lucko.luckperms.sponge.service.base.Util.convertContexts; -import static me.lucko.luckperms.sponge.service.base.Util.convertTristate; +import static me.lucko.luckperms.sponge.service.proxy.Util.convertContexts; +import static me.lucko.luckperms.sponge.service.proxy.Util.convertTristate; public interface LPSubject extends Subject { diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/base/LPSubjectCollection.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/proxy/LPSubjectCollection.java similarity index 96% rename from sponge/src/main/java/me/lucko/luckperms/sponge/service/base/LPSubjectCollection.java rename to sponge/src/main/java/me/lucko/luckperms/sponge/service/proxy/LPSubjectCollection.java index 52e15896..1aff4a89 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/base/LPSubjectCollection.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/proxy/LPSubjectCollection.java @@ -20,7 +20,7 @@ * SOFTWARE. */ -package me.lucko.luckperms.sponge.service.base; +package me.lucko.luckperms.sponge.service.proxy; import lombok.NonNull; @@ -38,7 +38,7 @@ import java.util.Collection; import java.util.Map; import java.util.Set; -import static me.lucko.luckperms.sponge.service.base.Util.convertContexts; +import static me.lucko.luckperms.sponge.service.proxy.Util.convertContexts; public interface LPSubjectCollection extends SubjectCollection { diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/base/LPSubjectData.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/proxy/LPSubjectData.java similarity index 96% rename from sponge/src/main/java/me/lucko/luckperms/sponge/service/base/LPSubjectData.java rename to sponge/src/main/java/me/lucko/luckperms/sponge/service/proxy/LPSubjectData.java index 700e1e81..ce690947 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/base/LPSubjectData.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/proxy/LPSubjectData.java @@ -20,7 +20,7 @@ * SOFTWARE. */ -package me.lucko.luckperms.sponge.service.base; +package me.lucko.luckperms.sponge.service.proxy; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -41,8 +41,8 @@ import java.util.Set; import javax.annotation.Nullable; -import static me.lucko.luckperms.sponge.service.base.Util.convertContexts; -import static me.lucko.luckperms.sponge.service.base.Util.convertTristate; +import static me.lucko.luckperms.sponge.service.proxy.Util.convertContexts; +import static me.lucko.luckperms.sponge.service.proxy.Util.convertTristate; public interface LPSubjectData extends SubjectData { diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/base/Util.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/proxy/Util.java similarity index 98% rename from sponge/src/main/java/me/lucko/luckperms/sponge/service/base/Util.java rename to sponge/src/main/java/me/lucko/luckperms/sponge/service/proxy/Util.java index e0eaa4ae..b5a094f1 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/base/Util.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/proxy/Util.java @@ -20,7 +20,7 @@ * SOFTWARE. */ -package me.lucko.luckperms.sponge.service.base; +package me.lucko.luckperms.sponge.service.proxy; import lombok.NonNull; import lombok.experimental.UtilityClass; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/references/SubjectCollectionReference.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/references/SubjectCollectionReference.java index 8ab99992..6f2ecc79 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/references/SubjectCollectionReference.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/references/SubjectCollectionReference.java @@ -28,7 +28,7 @@ import lombok.RequiredArgsConstructor; import lombok.ToString; import me.lucko.luckperms.sponge.service.LuckPermsService; -import me.lucko.luckperms.sponge.service.base.LPSubjectCollection; +import me.lucko.luckperms.sponge.service.proxy.LPSubjectCollection; import java.lang.ref.WeakReference; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/references/SubjectReference.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/references/SubjectReference.java index 3a6bb451..8e6c7173 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/references/SubjectReference.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/references/SubjectReference.java @@ -30,7 +30,7 @@ import lombok.ToString; import com.google.common.base.Splitter; import me.lucko.luckperms.sponge.service.LuckPermsService; -import me.lucko.luckperms.sponge.service.base.LPSubject; +import me.lucko.luckperms.sponge.service.proxy.LPSubject; import org.spongepowered.api.service.permission.Subject;