From 1e105b41357575ea8ec13bd54de8b00e8eae9760 Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 25 Nov 2017 15:56:02 +0000 Subject: [PATCH] Cleanup sponge service impl --- .../contexts/AbstractContextManager.java | 14 +- .../api6/PermissionDescriptionProxy.java | 17 ++- .../proxy/api6/PermissionServiceProxy.java | 17 ++- .../proxy/api6/SubjectCollectionProxy.java | 19 ++- .../service/proxy/api6/SubjectDataProxy.java | 62 +++++--- .../service/proxy/api6/SubjectProxy.java | 43 ++++-- .../api7/PermissionDescriptionProxy.java | 17 ++- .../proxy/api7/PermissionServiceProxy.java | 17 ++- .../proxy/api7/SubjectCollectionProxy.java | 19 ++- .../service/proxy/api7/SubjectDataProxy.java | 62 +++++--- .../service/proxy/api7/SubjectProxy.java | 19 ++- .../{model => }/CompatibilityUtil.java | 15 +- .../context/AbstractDelegatingContextSet.java | 63 +++++++++ .../service/context/DelegatingContextSet.java | 41 ++++++ .../DelegatingImmutableContextSet.java | 110 +++++++++++++++ .../context/DelegatingMutableContextSet.java | 133 ++++++++++++++++++ .../service/model/LPPermissionService.java | 4 - .../sponge/service/model/LPSubject.java | 4 - .../service/model/SubjectReference.java | 4 +- .../model/SubjectReferenceFactory.java | 2 +- .../luckperms/sponge/LPSpongePlugin.java | 2 +- .../luckperms/sponge/SpongeSenderFactory.java | 2 +- .../sponge/commands/OptionClear.java | 2 +- .../luckperms/sponge/commands/OptionInfo.java | 4 +- .../luckperms/sponge/commands/OptionSet.java | 2 +- .../sponge/commands/OptionUnset.java | 2 +- .../luckperms/sponge/commands/ParentAdd.java | 2 +- .../sponge/commands/ParentClear.java | 2 +- .../luckperms/sponge/commands/ParentInfo.java | 4 +- .../sponge/commands/ParentRemove.java | 2 +- .../sponge/commands/PermissionClear.java | 2 +- .../sponge/commands/PermissionInfo.java | 4 +- .../sponge/commands/PermissionSet.java | 4 +- ...ongeUtils.java => SpongeCommandUtils.java} | 2 +- .../contexts/ProxiedContextCalculator.java | 24 +--- .../migration/SpongeMigrationUtils.java | 2 +- .../luckperms/sponge/model/SpongeGroup.java | 13 +- .../luckperms/sponge/model/SpongeUser.java | 11 +- .../sponge/service/LuckPermsSubjectData.java | 1 - .../sponge/service/ProxyFactory.java | 1 + .../calculated/CalculatedSubjectData.java | 11 +- .../OptionLookupKey.java | 2 +- .../PermissionLookupKey.java | 2 +- .../service/persisted/PersistedSubject.java | 2 - 44 files changed, 647 insertions(+), 140 deletions(-) rename sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/{model => }/CompatibilityUtil.java (83%) create mode 100644 sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/context/AbstractDelegatingContextSet.java create mode 100644 sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/context/DelegatingContextSet.java create mode 100644 sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/context/DelegatingImmutableContextSet.java create mode 100644 sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/context/DelegatingMutableContextSet.java rename sponge/src/main/java/me/lucko/luckperms/sponge/commands/{SpongeUtils.java => SpongeCommandUtils.java} (99%) rename sponge/src/main/java/me/lucko/luckperms/sponge/service/{calculated => persisted}/OptionLookupKey.java (96%) rename sponge/src/main/java/me/lucko/luckperms/sponge/service/{calculated => persisted}/PermissionLookupKey.java (96%) diff --git a/common/src/main/java/me/lucko/luckperms/common/contexts/AbstractContextManager.java b/common/src/main/java/me/lucko/luckperms/common/contexts/AbstractContextManager.java index 9e491f10..1cd533a9 100644 --- a/common/src/main/java/me/lucko/luckperms/common/contexts/AbstractContextManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/contexts/AbstractContextManager.java @@ -170,7 +170,12 @@ public abstract class AbstractContextManager implements ContextManager { for (ContextCalculator calculator : calculators) { try { - calculator.giveApplicableContext(subject, accumulator); + MutableContextSet ret = calculator.giveApplicableContext(subject, accumulator); + //noinspection ConstantConditions + if (ret == null) { + throw new IllegalStateException(calculator.getClass() + " returned a null context set"); + } + accumulator = ret; } catch (Exception e) { plugin.getLog().warn("An exception was thrown whilst calculating the context of subject " + subject); e.printStackTrace(); @@ -188,7 +193,12 @@ public abstract class AbstractContextManager implements ContextManager { for (StaticContextCalculator calculator : staticCalculators) { try { - calculator.giveApplicableContext(accumulator); + MutableContextSet ret = calculator.giveApplicableContext(accumulator); + //noinspection ConstantConditions + if (ret == null) { + throw new IllegalStateException(calculator.getClass() + " returned a null context set"); + } + accumulator = ret; } catch (Exception e) { plugin.getLog().warn("An exception was thrown whilst calculating static contexts"); e.printStackTrace(); diff --git a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/PermissionDescriptionProxy.java b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/PermissionDescriptionProxy.java index 9aff7944..874b3f4c 100644 --- a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/PermissionDescriptionProxy.java +++ b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/PermissionDescriptionProxy.java @@ -40,7 +40,7 @@ import org.spongepowered.api.text.Text; import java.util.Map; @RequiredArgsConstructor -public class PermissionDescriptionProxy implements PermissionDescription { +public final class PermissionDescriptionProxy implements PermissionDescription { private final LPPermissionService service; private final LPPermissionDescription handle; @@ -67,4 +67,19 @@ public class PermissionDescriptionProxy implements PermissionDescription { Map.Entry::getValue )); } + + @Override + public boolean equals(Object o) { + return o == this || o instanceof PermissionDescriptionProxy && handle.equals(((PermissionDescriptionProxy) o).handle); + } + + @Override + public int hashCode() { + return handle.hashCode(); + } + + @Override + public String toString() { + return "luckperms.api6.PermissionDescriptionProxy(handle=" + this.handle + ")"; + } } diff --git a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/PermissionServiceProxy.java b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/PermissionServiceProxy.java index b74d2414..86088266 100644 --- a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/PermissionServiceProxy.java +++ b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/PermissionServiceProxy.java @@ -44,7 +44,7 @@ import java.util.Map; import java.util.Optional; @RequiredArgsConstructor -public class PermissionServiceProxy implements PermissionService { +public final class PermissionServiceProxy implements PermissionService { private final LPPermissionService handle; @Override @@ -100,4 +100,19 @@ public class PermissionServiceProxy implements PermissionService { public void registerContextCalculator(ContextCalculator contextCalculator) { handle.registerContextCalculator(contextCalculator); } + + @Override + public boolean equals(Object o) { + return o == this || o instanceof PermissionServiceProxy && handle.equals(((PermissionServiceProxy) o).handle); + } + + @Override + public int hashCode() { + return handle.hashCode(); + } + + @Override + public String toString() { + return "luckperms.api6.PermissionServiceProxy(handle=" + this.handle + ")"; + } } diff --git a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectCollectionProxy.java b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectCollectionProxy.java index 9479603b..639dce9e 100644 --- a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectCollectionProxy.java +++ b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectCollectionProxy.java @@ -28,7 +28,7 @@ package me.lucko.luckperms.sponge.service.proxy.api6; import lombok.RequiredArgsConstructor; import me.lucko.luckperms.common.utils.ImmutableCollectors; -import me.lucko.luckperms.sponge.service.model.CompatibilityUtil; +import me.lucko.luckperms.sponge.service.CompatibilityUtil; import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; @@ -43,7 +43,7 @@ import java.util.Set; @SuppressWarnings("unchecked") @RequiredArgsConstructor -public class SubjectCollectionProxy implements SubjectCollection { +public final class SubjectCollectionProxy implements SubjectCollection { private final LPPermissionService service; private final LPSubjectCollection handle; @@ -103,4 +103,19 @@ public class SubjectCollectionProxy implements SubjectCollection { public Subject getDefaults() { return handle.getDefaults().sponge(); } + + @Override + public boolean equals(Object o) { + return o == this || o instanceof SubjectCollectionProxy && handle.equals(((SubjectCollectionProxy) o).handle); + } + + @Override + public int hashCode() { + return handle.hashCode(); + } + + @Override + public String toString() { + return "luckperms.api6.SubjectCollectionProxy(handle=" + this.handle + ")"; + } } diff --git a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectDataProxy.java b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectDataProxy.java index 857fe511..2c2bdfab 100644 --- a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectDataProxy.java +++ b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectDataProxy.java @@ -28,7 +28,7 @@ package me.lucko.luckperms.sponge.service.proxy.api6; import lombok.RequiredArgsConstructor; import me.lucko.luckperms.common.utils.ImmutableCollectors; -import me.lucko.luckperms.sponge.service.model.CompatibilityUtil; +import me.lucko.luckperms.sponge.service.CompatibilityUtil; import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectData; @@ -46,18 +46,18 @@ import java.util.concurrent.CompletableFuture; @SuppressWarnings("unchecked") @RequiredArgsConstructor -public class SubjectDataProxy implements SubjectData { +public final class SubjectDataProxy implements SubjectData { private final LPPermissionService service; private final SubjectReference ref; private final boolean enduring; - private CompletableFuture getHandle() { + private CompletableFuture handle() { return enduring ? ref.resolveLp().thenApply(LPSubject::getSubjectData) : ref.resolveLp().thenApply(LPSubject::getTransientSubjectData); } @Override public Map, Map> getAllPermissions() { - return (Map) getHandle().thenApply(handle -> { + return (Map) handle().thenApply(handle -> { return handle.getAllPermissions().entrySet().stream() .collect(ImmutableCollectors.toMap( e -> CompatibilityUtil.convertContexts(e.getKey()), @@ -68,12 +68,12 @@ public class SubjectDataProxy implements SubjectData { @Override public Map getPermissions(Set contexts) { - return getHandle().thenApply(handle -> handle.getPermissions(CompatibilityUtil.convertContexts(contexts))).join(); + return handle().thenApply(handle -> handle.getPermissions(CompatibilityUtil.convertContexts(contexts))).join(); } @Override public boolean setPermission(Set contexts, String permission, Tristate value) { - getHandle().thenCompose(handle -> handle.setPermission( + handle().thenCompose(handle -> handle.setPermission( CompatibilityUtil.convertContexts(contexts), permission, CompatibilityUtil.convertTristate(value) @@ -83,19 +83,19 @@ public class SubjectDataProxy implements SubjectData { @Override public boolean clearPermissions() { - getHandle().thenCompose(LPSubjectData::clearPermissions); + handle().thenCompose(LPSubjectData::clearPermissions); return true; } @Override public boolean clearPermissions(Set contexts) { - getHandle().thenCompose(handle -> handle.clearPermissions(CompatibilityUtil.convertContexts(contexts))); + handle().thenCompose(handle -> handle.clearPermissions(CompatibilityUtil.convertContexts(contexts))); return true; } @Override public Map, List> getAllParents() { - return (Map) getHandle().thenApply(handle -> handle.getAllParents().entrySet().stream() + return (Map) handle().thenApply(handle -> handle.getAllParents().entrySet().stream() .collect(ImmutableCollectors.toMap( e -> CompatibilityUtil.convertContexts(e.getKey()), e -> e.getValue().stream() @@ -107,14 +107,14 @@ public class SubjectDataProxy implements SubjectData { @Override public List getParents(Set contexts) { - return (List) getHandle().thenApply(handle -> handle.getParents(CompatibilityUtil.convertContexts(contexts)).stream() + return (List) handle().thenApply(handle -> handle.getParents(CompatibilityUtil.convertContexts(contexts)).stream() .map(s -> new SubjectProxy(service, s)) .collect(ImmutableCollectors.toList())).join(); } @Override public boolean addParent(Set contexts, Subject parent) { - getHandle().thenCompose(handle -> handle.addParent( + handle().thenCompose(handle -> handle.addParent( CompatibilityUtil.convertContexts(contexts), service.newSubjectReference( parent.getContainingCollection().getIdentifier(), @@ -126,7 +126,7 @@ public class SubjectDataProxy implements SubjectData { @Override public boolean removeParent(Set contexts, Subject parent) { - getHandle().thenCompose(handle -> handle.removeParent( + handle().thenCompose(handle -> handle.removeParent( CompatibilityUtil.convertContexts(contexts), service.newSubjectReference( parent.getContainingCollection().getIdentifier(), @@ -138,19 +138,19 @@ public class SubjectDataProxy implements SubjectData { @Override public boolean clearParents() { - getHandle().thenCompose(LPSubjectData::clearParents); + handle().thenCompose(LPSubjectData::clearParents); return true; } @Override public boolean clearParents(Set contexts) { - getHandle().thenCompose(handle -> handle.clearParents(CompatibilityUtil.convertContexts(contexts))); + handle().thenCompose(handle -> handle.clearParents(CompatibilityUtil.convertContexts(contexts))); return true; } @Override public Map, Map> getAllOptions() { - return (Map) getHandle().thenApply(handle -> handle.getAllOptions().entrySet().stream() + return (Map) handle().thenApply(handle -> handle.getAllOptions().entrySet().stream() .collect(ImmutableCollectors.toMap( e -> CompatibilityUtil.convertContexts(e.getKey()), Map.Entry::getValue @@ -159,29 +159,51 @@ public class SubjectDataProxy implements SubjectData { @Override public Map getOptions(Set contexts) { - return getHandle().thenApply(handle -> handle.getOptions(CompatibilityUtil.convertContexts(contexts))).join(); + return handle().thenApply(handle -> handle.getOptions(CompatibilityUtil.convertContexts(contexts))).join(); } @Override public boolean setOption(Set contexts, String key, String value) { if (value == null) { - getHandle().thenCompose(handle -> handle.unsetOption(CompatibilityUtil.convertContexts(contexts), key)); + handle().thenCompose(handle -> handle.unsetOption(CompatibilityUtil.convertContexts(contexts), key)); return true; } else { - getHandle().thenCompose(handle -> handle.setOption(CompatibilityUtil.convertContexts(contexts), key, value)); + handle().thenCompose(handle -> handle.setOption(CompatibilityUtil.convertContexts(contexts), key, value)); return true; } } @Override public boolean clearOptions(Set contexts) { - getHandle().thenCompose(handle -> handle.clearOptions(CompatibilityUtil.convertContexts(contexts))); + handle().thenCompose(handle -> handle.clearOptions(CompatibilityUtil.convertContexts(contexts))); return true; } @Override public boolean clearOptions() { - getHandle().thenCompose(LPSubjectData::clearOptions); + handle().thenCompose(LPSubjectData::clearOptions); return true; } + + @Override + public boolean equals(Object o) { + if (o == this) return true; + if (!(o instanceof SubjectDataProxy)) return false; + final SubjectDataProxy other = (SubjectDataProxy) o; + return this.ref.equals(other.ref) && this.enduring == other.enduring; + } + + @Override + public int hashCode() { + final int PRIME = 59; + int result = 1; + result = result * PRIME + this.ref.hashCode(); + result = result * PRIME + (this.enduring ? 79 : 97); + return result; + } + + @Override + public String toString() { + return "luckperms.api6.SubjectDataProxy(ref=" + this.ref + ", enduring=" + this.enduring + ")"; + } } diff --git a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectProxy.java b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectProxy.java index 1446456a..62cbb3da 100644 --- a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectProxy.java +++ b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectProxy.java @@ -29,7 +29,7 @@ import lombok.RequiredArgsConstructor; import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.common.utils.ImmutableCollectors; -import me.lucko.luckperms.sponge.service.model.CompatibilityUtil; +import me.lucko.luckperms.sponge.service.CompatibilityUtil; import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.SubjectReference; @@ -48,17 +48,17 @@ import java.util.concurrent.CompletableFuture; @SuppressWarnings("unchecked") @RequiredArgsConstructor -public class SubjectProxy implements Subject { +public final class SubjectProxy implements Subject { private final LPPermissionService service; private final SubjectReference ref; - private CompletableFuture getHandle() { + private CompletableFuture handle() { return ref.resolveLp(); } @Override public Optional getCommandSource() { - return getHandle().thenApply(LPSubject::getCommandSource).join(); + return handle().thenApply(LPSubject::getCommandSource).join(); } @Override @@ -78,22 +78,22 @@ public class SubjectProxy implements Subject { @Override public boolean hasPermission(Set contexts, String permission) { - return getHandle().thenApply(handle -> handle.getPermissionValue(CompatibilityUtil.convertContexts(contexts), permission).asBoolean()).join(); + return handle().thenApply(handle -> handle.getPermissionValue(CompatibilityUtil.convertContexts(contexts), permission).asBoolean()).join(); } @Override public boolean hasPermission(String permission) { - return getHandle().thenApply(handle -> handle.getPermissionValue(ImmutableContextSet.empty(), permission).asBoolean()).join(); + return handle().thenApply(handle -> handle.getPermissionValue(ImmutableContextSet.empty(), permission).asBoolean()).join(); } @Override public Tristate getPermissionValue(Set contexts, String permission) { - return getHandle().thenApply(handle -> CompatibilityUtil.convertTristate(handle.getPermissionValue(CompatibilityUtil.convertContexts(contexts), permission))).join(); + return handle().thenApply(handle -> CompatibilityUtil.convertTristate(handle.getPermissionValue(CompatibilityUtil.convertContexts(contexts), permission))).join(); } @Override public boolean isChildOf(Subject parent) { - return getHandle().thenApply(handle -> handle.isChildOf( + return handle().thenApply(handle -> handle.isChildOf( ImmutableContextSet.empty(), service.newSubjectReference( parent.getContainingCollection().getIdentifier(), @@ -104,7 +104,7 @@ public class SubjectProxy implements Subject { @Override public boolean isChildOf(Set contexts, Subject parent) { - return getHandle().thenApply(handle -> handle.isChildOf( + return handle().thenApply(handle -> handle.isChildOf( CompatibilityUtil.convertContexts(contexts), service.newSubjectReference( parent.getContainingCollection().getIdentifier(), @@ -115,26 +115,26 @@ public class SubjectProxy implements Subject { @Override public List getParents() { - return (List) getHandle().thenApply(handle -> handle.getParents(ImmutableContextSet.empty()).stream() + return (List) handle().thenApply(handle -> handle.getParents(ImmutableContextSet.empty()).stream() .map(s -> new SubjectProxy(service, s)) .collect(ImmutableCollectors.toList())).join(); } @Override public List getParents(Set contexts) { - return (List) getHandle().thenApply(handle -> handle.getParents(CompatibilityUtil.convertContexts(contexts)).stream() + return (List) handle().thenApply(handle -> handle.getParents(CompatibilityUtil.convertContexts(contexts)).stream() .map(s -> new SubjectProxy(service, s)) .collect(ImmutableCollectors.toList())).join(); } @Override public Optional getOption(Set contexts, String key) { - return getHandle().thenApply(handle -> handle.getOption(CompatibilityUtil.convertContexts(contexts), key)).join(); + return handle().thenApply(handle -> handle.getOption(CompatibilityUtil.convertContexts(contexts), key)).join(); } @Override public Optional getOption(String key) { - return getHandle().thenApply(handle -> handle.getOption(ImmutableContextSet.empty(), key)).join(); + return handle().thenApply(handle -> handle.getOption(ImmutableContextSet.empty(), key)).join(); } @Override @@ -144,6 +144,21 @@ public class SubjectProxy implements Subject { @Override public Set getActiveContexts() { - return getHandle().thenApply(handle -> CompatibilityUtil.convertContexts(handle.getActiveContextSet())).join(); + return handle().thenApply(handle -> CompatibilityUtil.convertContexts(handle.getActiveContextSet())).join(); + } + + @Override + public boolean equals(Object o) { + return o == this || o instanceof SubjectProxy && ref.equals(((SubjectProxy) o).ref); + } + + @Override + public int hashCode() { + return ref.hashCode(); + } + + @Override + public String toString() { + return "luckperms.api6.SubjectProxy(ref=" + this.ref + ")"; } } diff --git a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/PermissionDescriptionProxy.java b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/PermissionDescriptionProxy.java index 5be78af2..2d1445c6 100644 --- a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/PermissionDescriptionProxy.java +++ b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/PermissionDescriptionProxy.java @@ -42,7 +42,7 @@ import java.util.Optional; import java.util.concurrent.CompletableFuture; @RequiredArgsConstructor -public class PermissionDescriptionProxy implements PermissionDescription { +public final class PermissionDescriptionProxy implements PermissionDescription { private final LPPermissionService service; private final LPPermissionDescription handle; @@ -75,4 +75,19 @@ public class PermissionDescriptionProxy implements PermissionDescription { public CompletableFuture> findAssignedSubjects(String s) { return (CompletableFuture) handle.findAssignedSubjects(s); } + + @Override + public boolean equals(Object o) { + return o == this || o instanceof PermissionDescriptionProxy && handle.equals(((PermissionDescriptionProxy) o).handle); + } + + @Override + public int hashCode() { + return handle.hashCode(); + } + + @Override + public String toString() { + return "luckperms.api7.PermissionDescriptionProxy(handle=" + this.handle + ")"; + } } diff --git a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/PermissionServiceProxy.java b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/PermissionServiceProxy.java index 1e1c0efa..a94181b7 100644 --- a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/PermissionServiceProxy.java +++ b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/PermissionServiceProxy.java @@ -51,7 +51,7 @@ import java.util.concurrent.CompletableFuture; import java.util.function.Predicate; @RequiredArgsConstructor -public class PermissionServiceProxy implements PermissionService { +public final class PermissionServiceProxy implements PermissionService { private final LPPermissionService handle; @Override @@ -132,4 +132,19 @@ public class PermissionServiceProxy implements PermissionService { public void registerContextCalculator(ContextCalculator contextCalculator) { handle.registerContextCalculator(contextCalculator); } + + @Override + public boolean equals(Object o) { + return o == this || o instanceof PermissionServiceProxy && handle.equals(((PermissionServiceProxy) o).handle); + } + + @Override + public int hashCode() { + return handle.hashCode(); + } + + @Override + public String toString() { + return "luckperms.api7.PermissionServiceProxy(handle=" + this.handle + ")"; + } } diff --git a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectCollectionProxy.java b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectCollectionProxy.java index 2d923d95..874ea9cd 100644 --- a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectCollectionProxy.java +++ b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectCollectionProxy.java @@ -28,7 +28,7 @@ package me.lucko.luckperms.sponge.service.proxy.api7; import lombok.RequiredArgsConstructor; import me.lucko.luckperms.common.utils.ImmutableCollectors; -import me.lucko.luckperms.sponge.service.model.CompatibilityUtil; +import me.lucko.luckperms.sponge.service.CompatibilityUtil; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; @@ -46,7 +46,7 @@ import java.util.function.Predicate; @SuppressWarnings("unchecked") @RequiredArgsConstructor -public class SubjectCollectionProxy implements SubjectCollection { +public final class SubjectCollectionProxy implements SubjectCollection { private final LPSubjectCollection handle; @Override @@ -132,4 +132,19 @@ public class SubjectCollectionProxy implements SubjectCollection { // unused by lp } + @Override + public boolean equals(Object o) { + return o == this || o instanceof SubjectCollectionProxy && handle.equals(((SubjectCollectionProxy) o).handle); + } + + @Override + public int hashCode() { + return handle.hashCode(); + } + + @Override + public String toString() { + return "luckperms.api7.SubjectCollectionProxy(handle=" + this.handle + ")"; + } + } diff --git a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectDataProxy.java b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectDataProxy.java index 287b9353..e521e41e 100644 --- a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectDataProxy.java +++ b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectDataProxy.java @@ -28,7 +28,7 @@ package me.lucko.luckperms.sponge.service.proxy.api7; import lombok.RequiredArgsConstructor; import me.lucko.luckperms.common.utils.ImmutableCollectors; -import me.lucko.luckperms.sponge.service.model.CompatibilityUtil; +import me.lucko.luckperms.sponge.service.CompatibilityUtil; import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectData; @@ -48,18 +48,18 @@ import javax.annotation.Nullable; @SuppressWarnings("unchecked") @RequiredArgsConstructor -public class SubjectDataProxy implements SubjectData { +public final class SubjectDataProxy implements SubjectData { private final LPPermissionService service; private final SubjectReference ref; private final boolean enduring; - private CompletableFuture getHandle() { + private CompletableFuture handle() { return enduring ? ref.resolveLp().thenApply(LPSubject::getSubjectData) : ref.resolveLp().thenApply(LPSubject::getTransientSubjectData); } @Override public Map, Map> getAllPermissions() { - return (Map) getHandle().thenApply(handle -> handle.getAllPermissions().entrySet().stream() + return (Map) handle().thenApply(handle -> handle.getAllPermissions().entrySet().stream() .collect(ImmutableCollectors.toMap( e -> CompatibilityUtil.convertContexts(e.getKey()), Map.Entry::getValue @@ -68,12 +68,12 @@ public class SubjectDataProxy implements SubjectData { @Override public Map getPermissions(Set contexts) { - return getHandle().thenApply(handle -> handle.getPermissions(CompatibilityUtil.convertContexts(contexts))).join(); + return handle().thenApply(handle -> handle.getPermissions(CompatibilityUtil.convertContexts(contexts))).join(); } @Override public CompletableFuture setPermission(Set contexts, String permission, Tristate value) { - return getHandle().thenCompose(handle -> handle.setPermission( + return handle().thenCompose(handle -> handle.setPermission( CompatibilityUtil.convertContexts(contexts), permission, CompatibilityUtil.convertTristate(value) @@ -82,17 +82,17 @@ public class SubjectDataProxy implements SubjectData { @Override public CompletableFuture clearPermissions() { - return getHandle().thenCompose(LPSubjectData::clearPermissions); + return handle().thenCompose(LPSubjectData::clearPermissions); } @Override public CompletableFuture clearPermissions(Set contexts) { - return getHandle().thenCompose(handle -> handle.clearPermissions(CompatibilityUtil.convertContexts(contexts))); + return handle().thenCompose(handle -> handle.clearPermissions(CompatibilityUtil.convertContexts(contexts))); } @Override public Map, List> getAllParents() { - return (Map) getHandle().thenApply(handle -> handle.getAllParents().entrySet().stream() + return (Map) handle().thenApply(handle -> handle.getAllParents().entrySet().stream() .collect(ImmutableCollectors.toMap( e -> CompatibilityUtil.convertContexts(e.getKey()), Map.Entry::getValue @@ -101,32 +101,32 @@ public class SubjectDataProxy implements SubjectData { @Override public List getParents(Set contexts) { - return (List) getHandle().thenApply(handle -> handle.getParents(CompatibilityUtil.convertContexts(contexts))).join(); + return (List) handle().thenApply(handle -> handle.getParents(CompatibilityUtil.convertContexts(contexts))).join(); } @Override public CompletableFuture addParent(Set contexts, org.spongepowered.api.service.permission.SubjectReference ref) { - return getHandle().thenCompose(handle -> handle.addParent(CompatibilityUtil.convertContexts(contexts), SubjectReferenceFactory.obtain(service, ref))); + return handle().thenCompose(handle -> handle.addParent(CompatibilityUtil.convertContexts(contexts), SubjectReferenceFactory.obtain(service, ref))); } @Override public CompletableFuture removeParent(Set contexts, org.spongepowered.api.service.permission.SubjectReference ref) { - return getHandle().thenCompose(handle -> handle.removeParent(CompatibilityUtil.convertContexts(contexts), SubjectReferenceFactory.obtain(service, ref))); + return handle().thenCompose(handle -> handle.removeParent(CompatibilityUtil.convertContexts(contexts), SubjectReferenceFactory.obtain(service, ref))); } @Override public CompletableFuture clearParents() { - return getHandle().thenCompose(LPSubjectData::clearParents); + return handle().thenCompose(LPSubjectData::clearParents); } @Override public CompletableFuture clearParents(Set contexts) { - return getHandle().thenCompose(handle -> handle.clearParents(CompatibilityUtil.convertContexts(contexts))); + return handle().thenCompose(handle -> handle.clearParents(CompatibilityUtil.convertContexts(contexts))); } @Override public Map, Map> getAllOptions() { - return (Map) getHandle().thenApply(handle -> handle.getAllOptions().entrySet().stream() + return (Map) handle().thenApply(handle -> handle.getAllOptions().entrySet().stream() .collect(ImmutableCollectors.toMap( e -> CompatibilityUtil.convertContexts(e.getKey()), Map.Entry::getValue @@ -135,25 +135,47 @@ public class SubjectDataProxy implements SubjectData { @Override public Map getOptions(Set contexts) { - return getHandle().thenApply(handle -> handle.getOptions(CompatibilityUtil.convertContexts(contexts))).join(); + return handle().thenApply(handle -> handle.getOptions(CompatibilityUtil.convertContexts(contexts))).join(); } @Override public CompletableFuture setOption(Set contexts, String key, @Nullable String value) { if (value == null) { - return getHandle().thenCompose(handle -> handle.unsetOption(CompatibilityUtil.convertContexts(contexts), key)); + return handle().thenCompose(handle -> handle.unsetOption(CompatibilityUtil.convertContexts(contexts), key)); } else { - return getHandle().thenCompose(handle -> handle.setOption(CompatibilityUtil.convertContexts(contexts), key, value)); + return handle().thenCompose(handle -> handle.setOption(CompatibilityUtil.convertContexts(contexts), key, value)); } } @Override public CompletableFuture clearOptions() { - return getHandle().thenCompose(LPSubjectData::clearOptions); + return handle().thenCompose(LPSubjectData::clearOptions); } @Override public CompletableFuture clearOptions(Set contexts) { - return getHandle().thenCompose(handle -> handle.clearOptions(CompatibilityUtil.convertContexts(contexts))); + return handle().thenCompose(handle -> handle.clearOptions(CompatibilityUtil.convertContexts(contexts))); + } + + @Override + public boolean equals(Object o) { + if (o == this) return true; + if (!(o instanceof SubjectDataProxy)) return false; + final SubjectDataProxy other = (SubjectDataProxy) o; + return this.ref.equals(other.ref) && this.enduring == other.enduring; + } + + @Override + public int hashCode() { + final int PRIME = 59; + int result = 1; + result = result * PRIME + this.ref.hashCode(); + result = result * PRIME + (this.enduring ? 79 : 97); + return result; + } + + @Override + public String toString() { + return "luckperms.api7.SubjectDataProxy(ref=" + this.ref + ", enduring=" + this.enduring + ")"; } } diff --git a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectProxy.java b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectProxy.java index c40e9e5e..2b925691 100644 --- a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectProxy.java +++ b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectProxy.java @@ -28,7 +28,7 @@ package me.lucko.luckperms.sponge.service.proxy.api7; import lombok.RequiredArgsConstructor; import me.lucko.luckperms.api.context.ImmutableContextSet; -import me.lucko.luckperms.sponge.service.model.CompatibilityUtil; +import me.lucko.luckperms.sponge.service.CompatibilityUtil; import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.SubjectReference; @@ -48,7 +48,7 @@ import java.util.concurrent.CompletableFuture; @SuppressWarnings("unchecked") @RequiredArgsConstructor -public class SubjectProxy implements Subject { +public final class SubjectProxy implements Subject { private final LPPermissionService service; private final SubjectReference ref; @@ -145,4 +145,19 @@ public class SubjectProxy implements Subject { public Set getActiveContexts() { return getHandle().thenApply(handle -> CompatibilityUtil.convertContexts(handle.getActiveContextSet())).join(); } + + @Override + public boolean equals(Object o) { + return o == this || o instanceof SubjectProxy && ref.equals(((SubjectProxy) o).ref); + } + + @Override + public int hashCode() { + return ref.hashCode(); + } + + @Override + public String toString() { + return "luckperms.api7.SubjectProxy(ref=" + this.ref + ")"; + } } diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/CompatibilityUtil.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/CompatibilityUtil.java similarity index 83% rename from sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/CompatibilityUtil.java rename to sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/CompatibilityUtil.java index d0ab8e58..0a8c486b 100644 --- a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/CompatibilityUtil.java +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/CompatibilityUtil.java @@ -23,7 +23,7 @@ * SOFTWARE. */ -package me.lucko.luckperms.sponge.service.model; +package me.lucko.luckperms.sponge.service; import lombok.NonNull; import lombok.experimental.UtilityClass; @@ -34,7 +34,8 @@ import com.google.common.collect.ImmutableSet; import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet; -import me.lucko.luckperms.common.utils.ImmutableCollectors; +import me.lucko.luckperms.sponge.service.context.DelegatingContextSet; +import me.lucko.luckperms.sponge.service.context.DelegatingImmutableContextSet; import org.spongepowered.api.service.context.Context; import org.spongepowered.api.util.Tristate; @@ -51,15 +52,19 @@ public class CompatibilityUtil { .expireAfterAccess(10, TimeUnit.MINUTES) .build(ImmutableContextSet::fromEntries); - private static final LoadingCache> LP_TO_SPONGE_CACHE = Caffeine.newBuilder() + private static final LoadingCache> LP_TO_SPONGE_CACHE = Caffeine.newBuilder() .expireAfterAccess(10, TimeUnit.MINUTES) - .build(set -> set.toSet().stream().map(e -> new Context(e.getKey(), e.getValue())).collect(ImmutableCollectors.toSet())); + .build(DelegatingImmutableContextSet::new); public static ImmutableContextSet convertContexts(@NonNull Set contexts) { + if (contexts instanceof DelegatingContextSet) { + return ((DelegatingContextSet) contexts).getDelegate().makeImmutable(); + } + return SPONGE_TO_LP_CACHE.get(ImmutableSet.copyOf(contexts)); } - public static ImmutableSet convertContexts(@NonNull ContextSet contexts) { + public static Set convertContexts(@NonNull ContextSet contexts) { return LP_TO_SPONGE_CACHE.get(contexts.makeImmutable()); } diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/context/AbstractDelegatingContextSet.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/context/AbstractDelegatingContextSet.java new file mode 100644 index 00000000..7f24904d --- /dev/null +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/context/AbstractDelegatingContextSet.java @@ -0,0 +1,63 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * 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.sponge.service.context; + +import org.spongepowered.api.service.context.Context; + +import java.util.AbstractSet; +import java.util.Set; + +abstract class AbstractDelegatingContextSet extends AbstractSet implements DelegatingContextSet { + + @Override + public int hashCode() { + return getDelegate().hashCode(); + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } + + if (o instanceof DelegatingContextSet) { + return getDelegate().equals(((DelegatingContextSet) o).getDelegate()); + } + + if (o instanceof Set) { + Set set = (Set) o; + + try { + return size() == set.size() && containsAll(set); + } catch (NullPointerException | ClassCastException ignored) { + return false; + } + } + + return false; + } + +} diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/context/DelegatingContextSet.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/context/DelegatingContextSet.java new file mode 100644 index 00000000..e33b0d44 --- /dev/null +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/context/DelegatingContextSet.java @@ -0,0 +1,41 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * 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.sponge.service.context; + +import me.lucko.luckperms.api.context.ContextSet; + +import org.spongepowered.api.service.context.Context; + +import java.util.Set; + +/** + * Represents an object which is a {@link Set} of {@link Context}s, delegating all calls to a {@link ContextSet}. + */ +public interface DelegatingContextSet extends Set { + + ContextSet getDelegate(); + +} diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/context/DelegatingImmutableContextSet.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/context/DelegatingImmutableContextSet.java new file mode 100644 index 00000000..106e7701 --- /dev/null +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/context/DelegatingImmutableContextSet.java @@ -0,0 +1,110 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * 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.sponge.service.context; + +import lombok.RequiredArgsConstructor; +import lombok.ToString; + +import me.lucko.luckperms.api.context.ImmutableContextSet; + +import org.spongepowered.api.service.context.Context; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +/** + * Implements a {@link Set} of {@link Context}s, delegating all calls to a {@link ImmutableContextSet}. + */ +@ToString +@RequiredArgsConstructor +public class DelegatingImmutableContextSet extends AbstractDelegatingContextSet { + private final ImmutableContextSet delegate; + + @Override + public ImmutableContextSet getDelegate() { + return delegate; + } + + @Override + public int size() { + return delegate.size(); + } + + @Override + public boolean isEmpty() { + return delegate.isEmpty(); + } + + @Override + public boolean contains(Object o) { + if (o instanceof Context) { + Context context = (Context) o; + return delegate.has(context); + } + return false; + } + + @Override + public Iterator iterator() { + return new ContextSetIterator(); + } + + @Override + public boolean add(Context context) { + throw new UnsupportedOperationException("context set is immutable"); + } + + @Override + public boolean remove(Object o) { + throw new UnsupportedOperationException("context set is immutable"); + } + + @Override + public void clear() { + throw new UnsupportedOperationException("context set is immutable"); + } + + private final class ContextSetIterator implements Iterator { + private final Iterator> it = delegate.toSet().iterator(); + + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public Context next() { + Map.Entry next = it.next(); + return new Context(next.getKey(), next.getValue()); + } + + @Override + public void remove() { + throw new UnsupportedOperationException("context set is immutable"); + } + } +} diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/context/DelegatingMutableContextSet.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/context/DelegatingMutableContextSet.java new file mode 100644 index 00000000..683943c6 --- /dev/null +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/context/DelegatingMutableContextSet.java @@ -0,0 +1,133 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * 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.sponge.service.context; + +import lombok.RequiredArgsConstructor; + +import me.lucko.luckperms.api.context.MutableContextSet; + +import org.spongepowered.api.service.context.Context; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +/** + * Implements a {@link Set} of {@link Context}s, delegating all calls to a {@link MutableContextSet}. + */ +@RequiredArgsConstructor +public class DelegatingMutableContextSet extends AbstractDelegatingContextSet { + private final MutableContextSet delegate; + + @Override + public MutableContextSet getDelegate() { + return delegate; + } + + @Override + public int size() { + return delegate.size(); + } + + @Override + public boolean isEmpty() { + return delegate.isEmpty(); + } + + @Override + public boolean contains(Object o) { + if (o instanceof Context) { + Context context = (Context) o; + return delegate.has(context); + } + return false; + } + + @Override + public Iterator iterator() { + return new ContextSetIterator(); + } + + @Override + public boolean add(Context context) { + if (context == null) { + throw new NullPointerException("context"); + } + + boolean has = delegate.has(context); + delegate.add(context); + return !has; + } + + @Override + public boolean remove(Object o) { + if (o instanceof Context) { + Context context = (Context) o; + boolean had = delegate.has(context); + delegate.remove(context.getKey(), context.getValue()); + return had; + } + + return false; + } + + @Override + public void clear() { + delegate.clear(); + } + + private final class ContextSetIterator implements Iterator { + private final Iterator> it = delegate.toSet().iterator(); + private Context current; + + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public Context next() { + Map.Entry next = it.next(); + + // track the iterators cursor to handle #remove calls + current = new Context(next.getKey(), next.getValue()); + return current; + } + + @Override + public void remove() { + Context c = current; + if (c == null) { + throw new IllegalStateException(); + } + current = null; + + // delegate the removal call to the MutableContextSet, as the iterator returned by + // toSet().iterator() is immutable + delegate.remove(c.getKey(), c.getValue()); + } + } +} diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPPermissionService.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPPermissionService.java index b75fff21..e39a52af 100644 --- a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPPermissionService.java +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPPermissionService.java @@ -56,10 +56,6 @@ public interface LPPermissionService { LPSubjectCollection getDefaultSubjects(); - default LPSubjectData getDefaultData() { - return getDefaults().getSubjectData(); - } - LPSubject getDefaults(); Predicate getIdentifierValidityPredicate(); diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPSubject.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPSubject.java index 087fedf5..1101f5f1 100644 --- a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPSubject.java +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPSubject.java @@ -50,10 +50,6 @@ public interface LPSubject { return getService().newSubjectReference(getParentCollection().getIdentifier(), getIdentifier()); } - default LPSubjectData getDefaultData() { - return getDefaults().getSubjectData(); - } - default LPSubject getDefaults() { return getService().getDefaultSubjects().loadSubject(getIdentifier()).join(); } diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReference.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReference.java index 7f0aaf8d..3c87d3ac 100644 --- a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReference.java +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReference.java @@ -153,7 +153,7 @@ public final class SubjectReference implements org.spongepowered.api.service.per @Override public String toString() { return "SubjectReference(" + - "collectionIdentifier=" + this.collectionIdentifier + ", " + - "subjectIdentifier=" + this.subjectIdentifier + ")"; + "collection=" + this.collectionIdentifier + ", " + + "subject=" + this.subjectIdentifier + ")"; } } diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReferenceFactory.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReferenceFactory.java index 7c98b6b3..619bee07 100644 --- a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReferenceFactory.java +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReferenceFactory.java @@ -71,7 +71,7 @@ public final class SubjectReferenceFactory { if (reference instanceof SubjectReference) { return ((SubjectReference) reference); } else { - return SubjectReferenceFactory.obtain(service, reference.getCollectionIdentifier(), reference.getSubjectIdentifier()); + return obtain(service, reference.getCollectionIdentifier(), reference.getSubjectIdentifier()); } } 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 7c94a62d..2f0544c1 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java @@ -132,7 +132,7 @@ import java.util.stream.Stream; id = "luckperms", name = "LuckPerms", version = VersionData.VERSION, - authors = {"Luck"}, + authors = "Luck", description = "A permissions plugin", url = "https://github.com/lucko/LuckPerms" ) diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeSenderFactory.java b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeSenderFactory.java index 966d2857..6a285476 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeSenderFactory.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeSenderFactory.java @@ -29,7 +29,7 @@ import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.common.commands.sender.SenderFactory; import me.lucko.luckperms.common.constants.Constants; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; -import me.lucko.luckperms.sponge.service.model.CompatibilityUtil; +import me.lucko.luckperms.sponge.service.CompatibilityUtil; import net.kyori.text.Component; import net.kyori.text.serializer.ComponentSerializers; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionClear.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionClear.java index 61eb5fe7..24900010 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionClear.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionClear.java @@ -54,7 +54,7 @@ public class OptionClear extends SubCommand { CommandUtils.sendPluginMessage(sender, "&aCleared options matching contexts &bANY&a."); } else { subjectData.clearOptions(contextSet); - CommandUtils.sendPluginMessage(sender, "&aCleared options matching contexts &b" + SpongeUtils.contextToString(contextSet)); + CommandUtils.sendPluginMessage(sender, "&aCleared options matching contexts &b" + SpongeCommandUtils.contextToString(contextSet)); } return CommandResult.SUCCESS; } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionInfo.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionInfo.java index 5e79d830..2d9e11c5 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionInfo.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionInfo.java @@ -61,7 +61,7 @@ public class OptionInfo extends SubCommand { } for (Map.Entry> e : options.entrySet()) { - CommandUtils.sendPluginMessage(sender, "&3>> &bContext: " + SpongeUtils.contextToString(e.getKey()) + "\n" + SpongeUtils.optionsToString(e.getValue())); + CommandUtils.sendPluginMessage(sender, "&3>> &bContext: " + SpongeCommandUtils.contextToString(e.getKey()) + "\n" + SpongeCommandUtils.optionsToString(e.getValue())); } } else { @@ -72,7 +72,7 @@ public class OptionInfo extends SubCommand { } CommandUtils.sendPluginMessage(sender, "&aShowing options matching contexts &b" + - SpongeUtils.contextToString(contextSet) + "&a.\n" + SpongeUtils.optionsToString(options)); + SpongeCommandUtils.contextToString(contextSet) + "&a.\n" + SpongeCommandUtils.optionsToString(options)); } return CommandResult.SUCCESS; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionSet.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionSet.java index 64392421..24d995c1 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionSet.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionSet.java @@ -53,7 +53,7 @@ public class OptionSet extends SubCommand { ImmutableContextSet contextSet = ArgumentUtils.handleContextSponge(2, args); if (subjectData.setOption(contextSet, key, value).join()) { - CommandUtils.sendPluginMessage(sender, "&aSet &f\"" + key + "&f\"&a to &f\"" + value + "&f\"&a in context " + SpongeUtils.contextToString(contextSet)); + CommandUtils.sendPluginMessage(sender, "&aSet &f\"" + key + "&f\"&a to &f\"" + value + "&f\"&a in context " + SpongeCommandUtils.contextToString(contextSet)); } else { CommandUtils.sendPluginMessage(sender, "Unable to set option. Does the Subject already have it set?"); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionUnset.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionUnset.java index bca0978e..59531f48 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionUnset.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/OptionUnset.java @@ -52,7 +52,7 @@ public class OptionUnset extends SubCommand { ImmutableContextSet contextSet = ArgumentUtils.handleContextSponge(1, args); if (subjectData.unsetOption(contextSet, key).join()) { - CommandUtils.sendPluginMessage(sender, "&aUnset &f\"" + key + "&f\"&a in context " + SpongeUtils.contextToString(contextSet)); + CommandUtils.sendPluginMessage(sender, "&aUnset &f\"" + key + "&f\"&a in context " + SpongeCommandUtils.contextToString(contextSet)); } else { CommandUtils.sendPluginMessage(sender, "Unable to unset option. Are you sure the Subject has it set?"); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentAdd.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentAdd.java index 82988a4c..48ea04d2 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentAdd.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentAdd.java @@ -71,7 +71,7 @@ public class ParentAdd extends SubCommand { if (subjectData.addParent(contextSet, subject.toReference()).join()) { CommandUtils.sendPluginMessage(sender, "&aAdded parent &b" + subject.getParentCollection().getIdentifier() + - "&a/&b" + subject.getIdentifier() + "&a in context " + SpongeUtils.contextToString(contextSet)); + "&a/&b" + subject.getIdentifier() + "&a in context " + SpongeCommandUtils.contextToString(contextSet)); } else { CommandUtils.sendPluginMessage(sender, "Unable to add parent. Does the Subject already have it added?"); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentClear.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentClear.java index 95838f3c..7389d8c2 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentClear.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentClear.java @@ -54,7 +54,7 @@ public class ParentClear extends SubCommand { CommandUtils.sendPluginMessage(sender, "&aCleared parents matching contexts &bANY&a."); } else { subjectData.clearParents(contextSet); - CommandUtils.sendPluginMessage(sender, "&aCleared parents matching contexts &b" + SpongeUtils.contextToString(contextSet)); + CommandUtils.sendPluginMessage(sender, "&aCleared parents matching contexts &b" + SpongeCommandUtils.contextToString(contextSet)); } return CommandResult.SUCCESS; } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentInfo.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentInfo.java index 1601a7f5..f96c2f83 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentInfo.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentInfo.java @@ -62,7 +62,7 @@ public class ParentInfo extends SubCommand { } for (Map.Entry> e : parents.entrySet()) { - CommandUtils.sendPluginMessage(sender, "&3>> &bContext: " + SpongeUtils.contextToString(e.getKey()) + "\n" + SpongeUtils.parentsToString(e.getValue())); + CommandUtils.sendPluginMessage(sender, "&3>> &bContext: " + SpongeCommandUtils.contextToString(e.getKey()) + "\n" + SpongeCommandUtils.parentsToString(e.getValue())); } } else { @@ -73,7 +73,7 @@ public class ParentInfo extends SubCommand { } CommandUtils.sendPluginMessage(sender, "&aShowing parents matching contexts &b" + - SpongeUtils.contextToString(contextSet) + "&a.\n" + SpongeUtils.parentsToString(parents)); + SpongeCommandUtils.contextToString(contextSet) + "&a.\n" + SpongeCommandUtils.parentsToString(parents)); } return CommandResult.SUCCESS; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentRemove.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentRemove.java index 4f1cc2ce..d8d3ff0a 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentRemove.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentRemove.java @@ -71,7 +71,7 @@ public class ParentRemove extends SubCommand { if (subjectData.removeParent(contextSet, subject.toReference()).join()) { CommandUtils.sendPluginMessage(sender, "&aRemoved parent &b" + subject.getParentCollection().getIdentifier() + - "&a/&b" + subject.getIdentifier() + "&a in context " + SpongeUtils.contextToString(contextSet)); + "&a/&b" + subject.getIdentifier() + "&a in context " + SpongeCommandUtils.contextToString(contextSet)); } else { CommandUtils.sendPluginMessage(sender, "Unable to remove parent. Are you sure the Subject has it added?"); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionClear.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionClear.java index 64d77a27..86f77914 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionClear.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionClear.java @@ -54,7 +54,7 @@ public class PermissionClear extends SubCommand { CommandUtils.sendPluginMessage(sender, "&aCleared permissions matching contexts &bANY&a."); } else { subjectData.clearPermissions(contextSet); - CommandUtils.sendPluginMessage(sender, "&aCleared permissions matching contexts &b" + SpongeUtils.contextToString(contextSet)); + CommandUtils.sendPluginMessage(sender, "&aCleared permissions matching contexts &b" + SpongeCommandUtils.contextToString(contextSet)); } return CommandResult.SUCCESS; } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionInfo.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionInfo.java index 69db8be5..812c7280 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionInfo.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionInfo.java @@ -61,7 +61,7 @@ public class PermissionInfo extends SubCommand { } for (Map.Entry> e : permissions.entrySet()) { - CommandUtils.sendPluginMessage(sender, "&3>> &bContext: " + SpongeUtils.contextToString(e.getKey()) + "\n" + SpongeUtils.nodesToString(e.getValue())); + CommandUtils.sendPluginMessage(sender, "&3>> &bContext: " + SpongeCommandUtils.contextToString(e.getKey()) + "\n" + SpongeCommandUtils.nodesToString(e.getValue())); } } else { @@ -72,7 +72,7 @@ public class PermissionInfo extends SubCommand { } CommandUtils.sendPluginMessage(sender, "&aShowing permissions matching contexts &b" + - SpongeUtils.contextToString(contextSet) + "&a.\n" + SpongeUtils.nodesToString(permissions)); + SpongeCommandUtils.contextToString(contextSet) + "&a.\n" + SpongeCommandUtils.nodesToString(permissions)); } return CommandResult.SUCCESS; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionSet.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionSet.java index 0be9356e..ee3b70c4 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionSet.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/PermissionSet.java @@ -50,11 +50,11 @@ public class PermissionSet extends SubCommand { @Override public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List args, String label) throws CommandException { String node = args.get(0); - Tristate tristate = SpongeUtils.parseTristate(1, args); + Tristate tristate = SpongeCommandUtils.parseTristate(1, args); ImmutableContextSet contextSet = ArgumentUtils.handleContextSponge(2, args); if (subjectData.setPermission(contextSet, node, tristate).join()) { - CommandUtils.sendPluginMessage(sender, "&aSet &b" + node + "&a to &b" + tristate.toString().toLowerCase() + "&a in context " + SpongeUtils.contextToString(contextSet)); + CommandUtils.sendPluginMessage(sender, "&aSet &b" + node + "&a to &b" + tristate.toString().toLowerCase() + "&a in context " + SpongeCommandUtils.contextToString(contextSet)); } else { CommandUtils.sendPluginMessage(sender, "Unable to set permission. Does the Subject already have it set?"); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeUtils.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeCommandUtils.java similarity index 99% rename from sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeUtils.java rename to sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeCommandUtils.java index adb9832e..5dbb76db 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeUtils.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeCommandUtils.java @@ -37,7 +37,7 @@ import java.util.List; import java.util.Map; @UtilityClass -public class SpongeUtils { +public class SpongeCommandUtils { public static Tristate parseTristate(int index, List args) throws ArgumentUtils.ArgumentException { String s = args.get(index).toLowerCase(); diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/ProxiedContextCalculator.java b/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/ProxiedContextCalculator.java index 1ce3ca30..36baeb5c 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/ProxiedContextCalculator.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/ProxiedContextCalculator.java @@ -29,12 +29,11 @@ import lombok.AllArgsConstructor; import me.lucko.luckperms.api.context.ContextCalculator; import me.lucko.luckperms.api.context.MutableContextSet; -import me.lucko.luckperms.sponge.service.model.CompatibilityUtil; +import me.lucko.luckperms.sponge.service.context.DelegatingMutableContextSet; import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.permission.Subject; -import java.util.HashSet; import java.util.Set; @AllArgsConstructor @@ -43,26 +42,9 @@ public class ProxiedContextCalculator implements ContextCalculator { @Override public MutableContextSet giveApplicableContext(Subject subject, MutableContextSet accumulator) { - Set contexts = new NonNullContextHashSet(); - try { - delegate.accumulateContexts(subject, contexts); - accumulator.addAll(CompatibilityUtil.convertContexts(contexts)); - } catch (Exception e) { - throw new RuntimeException("Exception thrown by delegate Sponge calculator: " + delegate.getClass().getName(), e); - } - + Set contexts = new DelegatingMutableContextSet(accumulator); + delegate.accumulateContexts(subject, contexts); return accumulator; } - private final class NonNullContextHashSet extends HashSet { - @Override - public boolean add(Context context) { - if (context == null) { - throw new NullPointerException("context"); - } - - return super.add(context); - } - } - } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/SpongeMigrationUtils.java b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/SpongeMigrationUtils.java index ae7cf69d..47190eec 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/SpongeMigrationUtils.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/SpongeMigrationUtils.java @@ -32,7 +32,7 @@ import me.lucko.luckperms.common.commands.impl.migration.MigrationUtils; import me.lucko.luckperms.common.model.Group; import me.lucko.luckperms.common.model.PermissionHolder; import me.lucko.luckperms.common.node.NodeFactory; -import me.lucko.luckperms.sponge.service.model.CompatibilityUtil; +import me.lucko.luckperms.sponge.service.CompatibilityUtil; 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 f75d62a6..930cc688 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 @@ -47,6 +47,7 @@ import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.service.permission.PermissionService; import org.spongepowered.api.service.permission.Subject; +import java.util.Map; import java.util.Optional; public class SpongeGroup extends Group { @@ -62,7 +63,7 @@ public class SpongeGroup extends Group { return this.spongeData; } - public static class GroupSubject implements LPSubject { + public class GroupSubject implements LPSubject { private final SpongeGroup parent; private final LPSpongePlugin plugin; @@ -123,12 +124,16 @@ public class SpongeGroup extends Group { public ImmutableList getParents(ImmutableContextSet contexts) { ImmutableSet.Builder subjects = ImmutableSet.builder(); - for (String perm : parent.getCachedData().getPermissionData(plugin.getContextManager().formContexts(contexts)).getImmutableBacking().keySet()) { - if (!perm.startsWith("group.")) { + for (Map.Entry entry : parent.getCachedData().getPermissionData(plugin.getContextManager().formContexts(contexts)).getImmutableBacking().entrySet()) { + if (!entry.getValue()) { continue; } - String groupName = perm.substring("group.".length()); + if (!entry.getKey().startsWith("group.")) { + continue; + } + + String groupName = entry.getKey().substring("group.".length()); if (plugin.getGroupManager().isLoaded(groupName)) { subjects.add(plugin.getService().getGroupSubjects().loadSubject(groupName).join().toReference()); } 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 5b41e581..9b082459 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 @@ -48,6 +48,7 @@ import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.service.permission.PermissionService; import org.spongepowered.api.service.permission.Subject; +import java.util.Map; import java.util.Optional; import java.util.UUID; import java.util.function.Function; @@ -132,12 +133,16 @@ public class SpongeUser extends User { public ImmutableList getParents(ImmutableContextSet contexts) { ImmutableSet.Builder subjects = ImmutableSet.builder(); - for (String perm : parent.getCachedData().getPermissionData(plugin.getContextManager().formContexts(contexts)).getImmutableBacking().keySet()) { - if (!perm.startsWith("group.")) { + for (Map.Entry entry : parent.getCachedData().getPermissionData(plugin.getContextManager().formContexts(contexts)).getImmutableBacking().entrySet()) { + if (!entry.getValue()) { continue; } - String groupName = perm.substring("group.".length()); + if (!entry.getKey().startsWith("group.")) { + continue; + } + + String groupName = entry.getKey().substring("group.".length()); if (plugin.getGroupManager().isLoaded(groupName)) { subjects.add(plugin.getService().getGroupSubjects().loadSubject(groupName).join().toReference()); } 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 2a5890e9..8958fefb 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 @@ -57,7 +57,6 @@ import java.util.function.Consumer; import java.util.stream.Collectors; import java.util.stream.Stream; -@SuppressWarnings({"OptionalGetWithoutIsPresent", "unused"}) @AllArgsConstructor public class LuckPermsSubjectData implements LPSubjectData { private final boolean enduring; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/ProxyFactory.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/ProxyFactory.java index 8fa08d78..d5c65600 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/ProxyFactory.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/ProxyFactory.java @@ -45,6 +45,7 @@ public class ProxyFactory { private static final boolean IS_API_7 = isApi7(); private static boolean isApi7() { try { + //noinspection JavaReflectionMemberAccess Subject.class.getDeclaredMethod("asSubjectReference"); return true; } catch (NoSuchMethodException e) { 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 15a4a6d9..37ab15dd 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 @@ -36,7 +36,6 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSortedMap; import me.lucko.luckperms.api.Tristate; -import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.common.calculators.PermissionCalculator; import me.lucko.luckperms.common.calculators.PermissionCalculatorMetadata; @@ -76,11 +75,11 @@ public class CalculatedSubjectData implements LPSubjectData { private final Map> parents = new ConcurrentHashMap<>(); private final Map> options = new ConcurrentHashMap<>(); - private final LoadingCache permissionCache = Caffeine.newBuilder() + private final LoadingCache permissionCache = Caffeine.newBuilder() .expireAfterAccess(10, TimeUnit.MINUTES) - .build(new CacheLoader() { + .build(new CacheLoader() { @Override - public CalculatorHolder load(ContextSet contexts) { + public CalculatorHolder load(ImmutableContextSet contexts) { ImmutableList.Builder processors = ImmutableList.builder(); processors.add(new MapProcessor()); processors.add(new SpongeWildcardProcessor()); @@ -100,7 +99,7 @@ public class CalculatedSubjectData implements LPSubjectData { permissionCache.invalidateAll(); } - public Tristate getPermissionValue(ContextSet contexts, String permission) { + public Tristate getPermissionValue(ImmutableContextSet contexts, String permission) { return permissionCache.get(contexts).getCalculator().getPermissionValue(permission, CheckOrigin.INTERNAL); } @@ -301,7 +300,7 @@ public class CalculatedSubjectData implements LPSubjectData { return ImmutableMap.copyOf(map); } - private static SortedMap> getRelevantEntries(ContextSet set, Map> map) { + private static SortedMap> getRelevantEntries(ImmutableContextSet set, Map> map) { ImmutableSortedMap.Builder> perms = ImmutableSortedMap.orderedBy(ContextSetComparator.reverse()); for (Map.Entry> e : map.entrySet()) { diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/calculated/OptionLookupKey.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/OptionLookupKey.java similarity index 96% rename from sponge/src/main/java/me/lucko/luckperms/sponge/service/calculated/OptionLookupKey.java rename to sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/OptionLookupKey.java index 7b5d8d1b..28f207ca 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/calculated/OptionLookupKey.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/OptionLookupKey.java @@ -23,7 +23,7 @@ * SOFTWARE. */ -package me.lucko.luckperms.sponge.service.calculated; +package me.lucko.luckperms.sponge.service.persisted; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/calculated/PermissionLookupKey.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PermissionLookupKey.java similarity index 96% rename from sponge/src/main/java/me/lucko/luckperms/sponge/service/calculated/PermissionLookupKey.java rename to sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PermissionLookupKey.java index 3a38dbc2..1eaab3e1 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/calculated/PermissionLookupKey.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PermissionLookupKey.java @@ -23,7 +23,7 @@ * SOFTWARE. */ -package me.lucko.luckperms.sponge.service.calculated; +package me.lucko.luckperms.sponge.service.persisted; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; 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 3a122221..a6839219 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 @@ -39,8 +39,6 @@ import me.lucko.luckperms.common.verbose.CheckOrigin; import me.lucko.luckperms.sponge.service.LuckPermsService; import me.lucko.luckperms.sponge.service.ProxyFactory; import me.lucko.luckperms.sponge.service.calculated.CalculatedSubjectData; -import me.lucko.luckperms.sponge.service.calculated.OptionLookupKey; -import me.lucko.luckperms.sponge.service.calculated.PermissionLookupKey; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.SubjectReference; import me.lucko.luckperms.sponge.service.storage.SubjectStorageModel;