Cleanup some of the Sponge permission holder implementation code

This commit is contained in:
Luck 2019-02-07 15:35:09 +00:00
parent 215031a1f3
commit dae64fc8c4
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
10 changed files with 120 additions and 83 deletions

View File

@ -298,7 +298,7 @@ public final class ArgumentPermissions {
throw new IllegalStateException("Unable to get a User for " + sender.getUuid() + " - " + sender.getName()); throw new IllegalStateException("Unable to get a User for " + sender.getUuid() + " - " + sender.getName());
} }
PermissionCache permissionData = user.getCachedData().getPermissionData(Contexts.of(contextSet, Contexts.global().getSettings())); PermissionCache permissionData = user.getCachedData().getPermissionData(Contexts.global().setContexts(contextSet));
return !permissionData.getPermissionValue(NodeFactory.groupNode(targetGroupName), PermissionCheckEvent.Origin.INTERNAL).result().asBoolean(); return !permissionData.getPermissionValue(NodeFactory.groupNode(targetGroupName), PermissionCheckEvent.Origin.INTERNAL).result().asBoolean();
} }

View File

@ -107,7 +107,7 @@ public class MetaSetChatMeta extends SharedSubCommand {
// determine the priority to set at // determine the priority to set at
if (priority == Integer.MIN_VALUE) { if (priority == Integer.MIN_VALUE) {
MetaAccumulator metaAccumulator = holder.accumulateMeta(null, Contexts.of(context, Contexts.global().getSettings())); MetaAccumulator metaAccumulator = holder.accumulateMeta(null, Contexts.global().setContexts(context));
metaAccumulator.complete(); metaAccumulator.complete();
priority = metaAccumulator.getChatMeta(this.type).keySet().stream().mapToInt(e -> e).max().orElse(0) + 1; priority = metaAccumulator.getChatMeta(this.type).keySet().stream().mapToInt(e -> e).max().orElse(0) + 1;

View File

@ -116,7 +116,7 @@ public class MetaSetTempChatMeta extends SharedSubCommand {
// determine the priority to set at // determine the priority to set at
if (priority == Integer.MIN_VALUE) { if (priority == Integer.MIN_VALUE) {
MetaAccumulator metaAccumulator = holder.accumulateMeta(null, Contexts.of(context, Contexts.global().getSettings())); MetaAccumulator metaAccumulator = holder.accumulateMeta(null, Contexts.global().setContexts(context));
metaAccumulator.complete(); metaAccumulator.complete();
priority = metaAccumulator.getChatMeta(this.type).keySet().stream().mapToInt(e -> e).max().orElse(0) + 1; priority = metaAccumulator.getChatMeta(this.type).keySet().stream().mapToInt(e -> e).max().orElse(0) + 1;

View File

@ -29,26 +29,34 @@ import java.util.function.Supplier;
public enum NodeMapType { public enum NodeMapType {
ENDURING, ENDURING {
TRANSIENT; @Override
public void run(Runnable enduringTask, Runnable transientTask) {
enduringTask.run();
}
@Override
public <T> T supply(Supplier<T> enduringSupplier, Supplier<T> transientSupplier) {
return enduringSupplier.get();
}
},
TRANSIENT {
@Override
public void run(Runnable enduringTask, Runnable transientTask) {
transientTask.run();
}
@Override
public <T> T supply(Supplier<T> enduringSupplier, Supplier<T> transientSupplier) {
return transientSupplier.get();
}
};
// useful methods for fluent/conditional execution // useful methods for fluent/conditional execution
public void run(Runnable ifEnduring, Runnable ifTransient) { public abstract void run(Runnable enduringTask, Runnable transientTask);
if (this == ENDURING) {
ifEnduring.run();
} else {
ifTransient.run();
}
}
public <T> T supply(Supplier<T> ifEnduring, Supplier<T> ifTransient) { public abstract <T> T supply(Supplier<T> enduringSupplier, Supplier<T> transientSupplier);
if (this == ENDURING) {
return ifEnduring.get();
} else {
return ifTransient.get();
}
}
} }

View File

@ -26,7 +26,6 @@
package me.lucko.luckperms.sponge.service.proxy.api6; package me.lucko.luckperms.sponge.service.proxy.api6;
import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.context.ContextManager;
import me.lucko.luckperms.common.context.ContextsSupplier; import me.lucko.luckperms.common.context.ContextsSupplier;
import me.lucko.luckperms.common.util.ImmutableCollectors; import me.lucko.luckperms.common.util.ImmutableCollectors;
import me.lucko.luckperms.sponge.service.CompatibilityUtil; import me.lucko.luckperms.sponge.service.CompatibilityUtil;
@ -68,8 +67,7 @@ public final class SubjectProxy implements Subject, ProxiedSubject, ProxiedServi
// lazy init // lazy init
private ContextsSupplier getContextsCache() { private ContextsSupplier getContextsCache() {
if (this.contextsSupplier == null) { if (this.contextsSupplier == null) {
ContextManager<Subject> contextManager = (ContextManager<Subject>) this.service.getPlugin().getContextManager(); this.contextsSupplier = this.service.getContextManager().getCacheFor(this);
this.contextsSupplier = contextManager.getCacheFor(this);
} }
return this.contextsSupplier; return this.contextsSupplier;
} }

View File

@ -26,7 +26,6 @@
package me.lucko.luckperms.sponge.service.proxy.api7; package me.lucko.luckperms.sponge.service.proxy.api7;
import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.context.ContextManager;
import me.lucko.luckperms.common.context.ContextsSupplier; import me.lucko.luckperms.common.context.ContextsSupplier;
import me.lucko.luckperms.sponge.service.CompatibilityUtil; import me.lucko.luckperms.sponge.service.CompatibilityUtil;
import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPPermissionService;
@ -68,8 +67,7 @@ public final class SubjectProxy implements Subject, ProxiedSubject, ProxiedServi
// lazy init // lazy init
private ContextsSupplier getContextsCache() { private ContextsSupplier getContextsCache() {
if (this.contextsSupplier == null) { if (this.contextsSupplier == null) {
ContextManager<Subject> contextManager = (ContextManager<Subject>) this.service.getPlugin().getContextManager(); this.contextsSupplier = this.service.getContextManager().getCacheFor(this);
this.contextsSupplier = contextManager.getCacheFor(this);
} }
return this.contextsSupplier; return this.contextsSupplier;
} }

View File

@ -51,9 +51,7 @@ public interface LPSubjectData {
ImmutableMap<ImmutableContextSet, ImmutableMap<String, Boolean>> getAllPermissions(); ImmutableMap<ImmutableContextSet, ImmutableMap<String, Boolean>> getAllPermissions();
default ImmutableMap<String, Boolean> getPermissions(ImmutableContextSet contexts) { ImmutableMap<String, Boolean> getPermissions(ImmutableContextSet contexts);
return ImmutableMap.copyOf(getAllPermissions().getOrDefault(contexts, ImmutableMap.of()));
}
CompletableFuture<Boolean> setPermission(ImmutableContextSet contexts, String permission, Tristate value); CompletableFuture<Boolean> setPermission(ImmutableContextSet contexts, String permission, Tristate value);
@ -65,9 +63,7 @@ public interface LPSubjectData {
ImmutableMap<ImmutableContextSet, ImmutableList<LPSubjectReference>> getAllParents(); ImmutableMap<ImmutableContextSet, ImmutableList<LPSubjectReference>> getAllParents();
default ImmutableList<LPSubjectReference> getParents(ImmutableContextSet contexts) { ImmutableList<LPSubjectReference> getParents(ImmutableContextSet contexts);
return ImmutableList.copyOf(getAllParents().getOrDefault(contexts, ImmutableList.of()));
}
CompletableFuture<Boolean> addParent(ImmutableContextSet contexts, LPSubjectReference parent); CompletableFuture<Boolean> addParent(ImmutableContextSet contexts, LPSubjectReference parent);

View File

@ -294,8 +294,7 @@ public abstract class CalculatedSubject implements LPSubject {
@Override @Override
public Tristate getPermissionValue(ImmutableContextSet contexts, String permission) { public Tristate getPermissionValue(ImmutableContextSet contexts, String permission) {
Contexts lookupContexts = Contexts.of(contexts, Contexts.global().getSettings()); return this.cachedData.getPermissionData(Contexts.global().setContexts(contexts)).getPermissionValue(permission, PermissionCheckEvent.Origin.INTERNAL).result();
return this.cachedData.getPermissionData(lookupContexts).getPermissionValue(permission, PermissionCheckEvent.Origin.INTERNAL).result();
} }
@Override @Override
@ -310,8 +309,7 @@ public abstract class CalculatedSubject implements LPSubject {
@Override @Override
public Optional<String> getOption(ImmutableContextSet contexts, String key) { public Optional<String> getOption(ImmutableContextSet contexts, String key) {
Contexts lookupContexts = Contexts.of(contexts, Contexts.global().getSettings()); return Optional.ofNullable(this.cachedData.getMetaData(Contexts.global().setContexts(contexts)).getMeta(MetaCheckEvent.Origin.PLATFORM_API).get(key));
return Optional.ofNullable(this.cachedData.getMetaData(lookupContexts).getMeta(MetaCheckEvent.Origin.PLATFORM_API).get(key));
} }
@Override @Override

View File

@ -27,6 +27,7 @@ package me.lucko.luckperms.sponge.service.model.calculated;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.context.ContextSet;
@ -120,6 +121,11 @@ public class CalculatedSubjectData implements LPSubjectData {
return map.build(); return map.build();
} }
@Override
public ImmutableMap<String, Boolean> getPermissions(ImmutableContextSet contexts) {
return ImmutableMap.copyOf(this.permissions.getOrDefault(contexts, ImmutableMap.of()));
}
public Map<String, Boolean> resolvePermissions(ContextSet filter) { public Map<String, Boolean> resolvePermissions(ContextSet filter) {
// get relevant entries // get relevant entries
SortedMap<ImmutableContextSet, Map<String, Boolean>> sorted = new TreeMap<>(ContextSetComparator.reverse()); SortedMap<ImmutableContextSet, Map<String, Boolean>> sorted = new TreeMap<>(ContextSetComparator.reverse());
@ -211,6 +217,11 @@ public class CalculatedSubjectData implements LPSubjectData {
return map.build(); return map.build();
} }
@Override
public ImmutableList<LPSubjectReference> getParents(ImmutableContextSet contexts) {
return ImmutableList.copyOf(this.parents.getOrDefault(contexts, ImmutableSet.of()));
}
public Set<LPSubjectReference> resolveParents(ContextSet filter) { public Set<LPSubjectReference> resolveParents(ContextSet filter) {
// get relevant entries // get relevant entries
SortedMap<ImmutableContextSet, Set<LPSubjectReference>> sorted = new TreeMap<>(ContextSetComparator.reverse()); SortedMap<ImmutableContextSet, Set<LPSubjectReference>> sorted = new TreeMap<>(ContextSetComparator.reverse());
@ -297,6 +308,11 @@ public class CalculatedSubjectData implements LPSubjectData {
return map.build(); return map.build();
} }
@Override
public ImmutableMap<String, String> getOptions(ImmutableContextSet contexts) {
return ImmutableMap.copyOf(this.options.getOrDefault(contexts, ImmutableMap.of()));
}
public Map<String, String> resolveOptions(ContextSet filter) { public Map<String, String> resolveOptions(ContextSet filter) {
// get relevant entries // get relevant entries
SortedMap<ImmutableContextSet, Map<String, String>> sorted = new TreeMap<>(ContextSetComparator.reverse()); SortedMap<ImmutableContextSet, Map<String, String>> sorted = new TreeMap<>(ContextSetComparator.reverse());

View File

@ -105,6 +105,15 @@ public class PermissionHolderSubjectData implements LPSubjectData {
return ret.build(); return ret.build();
} }
@Override
public ImmutableMap<String, Boolean> getPermissions(ImmutableContextSet contexts) {
ImmutableMap.Builder<String, Boolean> builder = ImmutableMap.builder();
for (Node n : this.holder.getData(this.type).immutable().get(contexts)) {
builder.put(n.getPermission(), n.getValue());
}
return builder.build();
}
@Override @Override
public CompletableFuture<Boolean> setPermission(ImmutableContextSet contexts, String permission, Tristate tristate) { public CompletableFuture<Boolean> setPermission(ImmutableContextSet contexts, String permission, Tristate tristate) {
Objects.requireNonNull(contexts, "contexts"); Objects.requireNonNull(contexts, "contexts");
@ -196,6 +205,17 @@ public class PermissionHolderSubjectData implements LPSubjectData {
return ret.build(); return ret.build();
} }
@Override
public ImmutableList<LPSubjectReference> getParents(ImmutableContextSet contexts) {
ImmutableList.Builder<LPSubjectReference> builder = ImmutableList.builder();
for (Node n : this.holder.getData(this.type).immutable().get(contexts)) {
if (n.isGroupNode()) {
builder.add(this.service.getGroupSubjects().loadSubject(n.getGroupName()).join().toReference());
}
}
return builder.build();
}
@Override @Override
public CompletableFuture<Boolean> addParent(ImmutableContextSet contexts, LPSubjectReference subject) { public CompletableFuture<Boolean> addParent(ImmutableContextSet contexts, LPSubjectReference subject) {
Objects.requireNonNull(contexts, "contexts"); Objects.requireNonNull(contexts, "contexts");
@ -292,51 +312,52 @@ public class PermissionHolderSubjectData implements LPSubjectData {
@Override @Override
public ImmutableMap<ImmutableContextSet, ImmutableMap<String, String>> getAllOptions() { public ImmutableMap<ImmutableContextSet, ImmutableMap<String, String>> getAllOptions() {
Map<ImmutableContextSet, Map<String, String>> options = new HashMap<>(); ImmutableMap.Builder<ImmutableContextSet, ImmutableMap<String, String>> ret = ImmutableMap.builder();
Map<ImmutableContextSet, Integer> minPrefixPriority = new HashMap<>(); for (Map.Entry<ImmutableContextSet, ? extends Collection<? extends Node>> entry : this.holder.getData(this.type).immutable().asMap().entrySet()) {
Map<ImmutableContextSet, Integer> minSuffixPriority = new HashMap<>(); ret.put(entry.getKey(), nodesToOptions(entry.getValue()));
}
return ret.build();
}
for (Node n : this.holder.getData(this.type).immutable().values()) { @Override
public ImmutableMap<String, String> getOptions(ImmutableContextSet contexts) {
return nodesToOptions(this.holder.getData(this.type).immutable().get(contexts));
}
private static ImmutableMap<String, String> nodesToOptions(Iterable<? extends Node> nodes) {
Map<String, String> builder = new HashMap<>();
int maxPrefixPriority = Integer.MIN_VALUE;
int maxSuffixPriority = Integer.MIN_VALUE;
for (Node n : nodes) {
if (!n.getValue()) continue; if (!n.getValue()) continue;
if (!n.isMeta() && !n.isPrefix() && !n.isSuffix()) continue; if (!n.isMeta() && !n.isPrefix() && !n.isSuffix()) continue;
ImmutableContextSet immutableContexts = n.getFullContexts().makeImmutable();
if (!options.containsKey(immutableContexts)) {
options.put(immutableContexts, new HashMap<>());
minPrefixPriority.put(immutableContexts, Integer.MIN_VALUE);
minSuffixPriority.put(immutableContexts, Integer.MIN_VALUE);
}
if (n.isPrefix()) { if (n.isPrefix()) {
Map.Entry<Integer, String> value = n.getPrefix(); Map.Entry<Integer, String> value = n.getPrefix();
if (value.getKey() > minPrefixPriority.get(immutableContexts)) { if (value.getKey() > maxPrefixPriority) {
options.get(immutableContexts).put(NodeTypes.PREFIX_KEY, value.getValue()); builder.put(NodeTypes.PREFIX_KEY, value.getValue());
minPrefixPriority.put(immutableContexts, value.getKey()); maxPrefixPriority = value.getKey();
} }
continue; continue;
} }
if (n.isSuffix()) { if (n.isSuffix()) {
Map.Entry<Integer, String> value = n.getSuffix(); Map.Entry<Integer, String> value = n.getSuffix();
if (value.getKey() > minSuffixPriority.get(immutableContexts)) { if (value.getKey() > maxSuffixPriority) {
options.get(immutableContexts).put(NodeTypes.SUFFIX_KEY, value.getValue()); builder.put(NodeTypes.SUFFIX_KEY, value.getValue());
minSuffixPriority.put(immutableContexts, value.getKey()); maxSuffixPriority = value.getKey();
} }
continue; continue;
} }
if (n.isMeta()) { if (n.isMeta()) {
Map.Entry<String, String> meta = n.getMeta(); Map.Entry<String, String> meta = n.getMeta();
options.get(immutableContexts).put(meta.getKey(), meta.getValue()); builder.put(meta.getKey(), meta.getValue());
} }
} }
ImmutableMap.Builder<ImmutableContextSet, ImmutableMap<String, String>> map = ImmutableMap.builder(); return ImmutableMap.copyOf(builder);
for (Map.Entry<ImmutableContextSet, Map<String, String>> e : options.entrySet()) {
map.put(e.getKey(), ImmutableMap.copyOf(e.getValue()));
}
return map.build();
} }
@Override @Override
@ -351,17 +372,15 @@ public class PermissionHolderSubjectData implements LPSubjectData {
ChatMetaType type = ChatMetaType.valueOf(key.toUpperCase()); ChatMetaType type = ChatMetaType.valueOf(key.toUpperCase());
// remove all prefixes/suffixes from the user // remove all prefixes/suffixes from the user
List<Node> toRemove = streamNodes() streamNodes()
.filter(type::matches) .filter(type::matches)
.filter(n -> n.getFullContexts().equals(contexts)) .filter(n -> n.getFullContexts().equals(contexts))
.collect(Collectors.toList()); .forEach(n -> this.type.run(
toRemove.forEach(n -> this.type.run(
() -> this.holder.unsetPermission(n), () -> this.holder.unsetPermission(n),
() -> this.holder.unsetTransientPermission(n) () -> this.holder.unsetTransientPermission(n)
)); ));
MetaAccumulator metaAccumulator = this.holder.accumulateMeta(null, Contexts.of(contexts, Contexts.global().getSettings())); MetaAccumulator metaAccumulator = this.holder.accumulateMeta(null, Contexts.global().setContexts(contexts));
metaAccumulator.complete(); metaAccumulator.complete();
int priority = metaAccumulator.getChatMeta(type).keySet().stream().mapToInt(e -> e).max().orElse(0); int priority = metaAccumulator.getChatMeta(type).keySet().stream().mapToInt(e -> e).max().orElse(0);
priority += 10; priority += 10;
@ -369,12 +388,10 @@ public class PermissionHolderSubjectData implements LPSubjectData {
node = NodeFactory.buildChatMetaNode(type, priority, value).withExtraContext(contexts).build(); node = NodeFactory.buildChatMetaNode(type, priority, value).withExtraContext(contexts).build();
} else { } else {
// standard remove // standard remove
List<Node> toRemove = streamNodes() streamNodes()
.filter(n -> n.isMeta() && n.getMeta().getKey().equals(key)) .filter(n -> n.isMeta() && n.getMeta().getKey().equals(key))
.filter(n -> n.getFullContexts().equals(contexts)) .filter(n -> n.getFullContexts().equals(contexts))
.collect(Collectors.toList()); .forEach(n -> this.type.run(
toRemove.forEach(n -> this.type.run(
() -> this.holder.unsetPermission(n), () -> this.holder.unsetPermission(n),
() -> this.holder.unsetTransientPermission(n) () -> this.holder.unsetTransientPermission(n)
)); ));
@ -394,7 +411,7 @@ public class PermissionHolderSubjectData implements LPSubjectData {
Objects.requireNonNull(contexts, "contexts"); Objects.requireNonNull(contexts, "contexts");
Objects.requireNonNull(key, "key"); Objects.requireNonNull(key, "key");
List<Node> toRemove = streamNodes() streamNodes()
.filter(n -> { .filter(n -> {
if (key.equalsIgnoreCase(NodeTypes.PREFIX_KEY)) { if (key.equalsIgnoreCase(NodeTypes.PREFIX_KEY)) {
return n.isPrefix(); return n.isPrefix();
@ -405,9 +422,7 @@ public class PermissionHolderSubjectData implements LPSubjectData {
} }
}) })
.filter(n -> n.getFullContexts().equals(contexts)) .filter(n -> n.getFullContexts().equals(contexts))
.collect(Collectors.toList()); .forEach(node -> this.type.run(
toRemove.forEach(node -> this.type.run(
() -> this.holder.unsetPermission(node), () -> this.holder.unsetPermission(node),
() -> this.holder.unsetTransientPermission(node) () -> this.holder.unsetTransientPermission(node)
)); ));
@ -429,7 +444,11 @@ public class PermissionHolderSubjectData implements LPSubjectData {
() -> this.holder.unsetTransientPermission(node) () -> this.holder.unsetTransientPermission(node)
)); ));
return objectSave(this.holder).thenApply(v -> !toRemove.isEmpty()); if (toRemove.isEmpty()) {
return CompletableFuture.completedFuture(false);
}
return objectSave(this.holder).thenApply(v -> true);
} }
@Override @Override
@ -443,7 +462,11 @@ public class PermissionHolderSubjectData implements LPSubjectData {
() -> this.holder.unsetTransientPermission(node) () -> this.holder.unsetTransientPermission(node)
)); ));
return objectSave(this.holder).thenApply(v -> !toRemove.isEmpty()); if (toRemove.isEmpty()) {
return CompletableFuture.completedFuture(false);
}
return objectSave(this.holder).thenApply(v -> true);
} }
private CompletableFuture<Void> objectSave(PermissionHolder t) { private CompletableFuture<Void> objectSave(PermissionHolder t) {