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());
}
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();
}

View File

@ -107,7 +107,7 @@ public class MetaSetChatMeta extends SharedSubCommand {
// determine the priority to set at
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();
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
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();
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 {
ENDURING,
TRANSIENT;
ENDURING {
@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
public void run(Runnable ifEnduring, Runnable ifTransient) {
if (this == ENDURING) {
ifEnduring.run();
} else {
ifTransient.run();
}
}
public abstract void run(Runnable enduringTask, Runnable transientTask);
public <T> T supply(Supplier<T> ifEnduring, Supplier<T> ifTransient) {
if (this == ENDURING) {
return ifEnduring.get();
} else {
return ifTransient.get();
}
}
public abstract <T> T supply(Supplier<T> enduringSupplier, Supplier<T> transientSupplier);
}

View File

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

View File

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

View File

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

View File

@ -294,8 +294,7 @@ public abstract class CalculatedSubject implements LPSubject {
@Override
public Tristate getPermissionValue(ImmutableContextSet contexts, String permission) {
Contexts lookupContexts = Contexts.of(contexts, Contexts.global().getSettings());
return this.cachedData.getPermissionData(lookupContexts).getPermissionValue(permission, PermissionCheckEvent.Origin.INTERNAL).result();
return this.cachedData.getPermissionData(Contexts.global().setContexts(contexts)).getPermissionValue(permission, PermissionCheckEvent.Origin.INTERNAL).result();
}
@Override
@ -310,8 +309,7 @@ public abstract class CalculatedSubject implements LPSubject {
@Override
public Optional<String> getOption(ImmutableContextSet contexts, String key) {
Contexts lookupContexts = Contexts.of(contexts, Contexts.global().getSettings());
return Optional.ofNullable(this.cachedData.getMetaData(lookupContexts).getMeta(MetaCheckEvent.Origin.PLATFORM_API).get(key));
return Optional.ofNullable(this.cachedData.getMetaData(Contexts.global().setContexts(contexts)).getMeta(MetaCheckEvent.Origin.PLATFORM_API).get(key));
}
@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.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.context.ContextSet;
@ -120,6 +121,11 @@ public class CalculatedSubjectData implements LPSubjectData {
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) {
// get relevant entries
SortedMap<ImmutableContextSet, Map<String, Boolean>> sorted = new TreeMap<>(ContextSetComparator.reverse());
@ -211,6 +217,11 @@ public class CalculatedSubjectData implements LPSubjectData {
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) {
// get relevant entries
SortedMap<ImmutableContextSet, Set<LPSubjectReference>> sorted = new TreeMap<>(ContextSetComparator.reverse());
@ -297,6 +308,11 @@ public class CalculatedSubjectData implements LPSubjectData {
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) {
// get relevant entries
SortedMap<ImmutableContextSet, Map<String, String>> sorted = new TreeMap<>(ContextSetComparator.reverse());

View File

@ -105,6 +105,15 @@ public class PermissionHolderSubjectData implements LPSubjectData {
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
public CompletableFuture<Boolean> setPermission(ImmutableContextSet contexts, String permission, Tristate tristate) {
Objects.requireNonNull(contexts, "contexts");
@ -196,6 +205,17 @@ public class PermissionHolderSubjectData implements LPSubjectData {
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
public CompletableFuture<Boolean> addParent(ImmutableContextSet contexts, LPSubjectReference subject) {
Objects.requireNonNull(contexts, "contexts");
@ -292,51 +312,52 @@ public class PermissionHolderSubjectData implements LPSubjectData {
@Override
public ImmutableMap<ImmutableContextSet, ImmutableMap<String, String>> getAllOptions() {
Map<ImmutableContextSet, Map<String, String>> options = new HashMap<>();
Map<ImmutableContextSet, Integer> minPrefixPriority = new HashMap<>();
Map<ImmutableContextSet, Integer> minSuffixPriority = new HashMap<>();
ImmutableMap.Builder<ImmutableContextSet, ImmutableMap<String, String>> ret = ImmutableMap.builder();
for (Map.Entry<ImmutableContextSet, ? extends Collection<? extends Node>> entry : this.holder.getData(this.type).immutable().asMap().entrySet()) {
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.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()) {
Map.Entry<Integer, String> value = n.getPrefix();
if (value.getKey() > minPrefixPriority.get(immutableContexts)) {
options.get(immutableContexts).put(NodeTypes.PREFIX_KEY, value.getValue());
minPrefixPriority.put(immutableContexts, value.getKey());
if (value.getKey() > maxPrefixPriority) {
builder.put(NodeTypes.PREFIX_KEY, value.getValue());
maxPrefixPriority = value.getKey();
}
continue;
}
if (n.isSuffix()) {
Map.Entry<Integer, String> value = n.getSuffix();
if (value.getKey() > minSuffixPriority.get(immutableContexts)) {
options.get(immutableContexts).put(NodeTypes.SUFFIX_KEY, value.getValue());
minSuffixPriority.put(immutableContexts, value.getKey());
if (value.getKey() > maxSuffixPriority) {
builder.put(NodeTypes.SUFFIX_KEY, value.getValue());
maxSuffixPriority = value.getKey();
}
continue;
}
if (n.isMeta()) {
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();
for (Map.Entry<ImmutableContextSet, Map<String, String>> e : options.entrySet()) {
map.put(e.getKey(), ImmutableMap.copyOf(e.getValue()));
}
return map.build();
return ImmutableMap.copyOf(builder);
}
@Override
@ -351,17 +372,15 @@ public class PermissionHolderSubjectData implements LPSubjectData {
ChatMetaType type = ChatMetaType.valueOf(key.toUpperCase());
// remove all prefixes/suffixes from the user
List<Node> toRemove = streamNodes()
streamNodes()
.filter(type::matches)
.filter(n -> n.getFullContexts().equals(contexts))
.collect(Collectors.toList());
.forEach(n -> this.type.run(
() -> this.holder.unsetPermission(n),
() -> this.holder.unsetTransientPermission(n)
));
toRemove.forEach(n -> this.type.run(
() -> this.holder.unsetPermission(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();
int priority = metaAccumulator.getChatMeta(type).keySet().stream().mapToInt(e -> e).max().orElse(0);
priority += 10;
@ -369,15 +388,13 @@ public class PermissionHolderSubjectData implements LPSubjectData {
node = NodeFactory.buildChatMetaNode(type, priority, value).withExtraContext(contexts).build();
} else {
// standard remove
List<Node> toRemove = streamNodes()
streamNodes()
.filter(n -> n.isMeta() && n.getMeta().getKey().equals(key))
.filter(n -> n.getFullContexts().equals(contexts))
.collect(Collectors.toList());
toRemove.forEach(n -> this.type.run(
() -> this.holder.unsetPermission(n),
() -> this.holder.unsetTransientPermission(n)
));
.forEach(n -> this.type.run(
() -> this.holder.unsetPermission(n),
() -> this.holder.unsetTransientPermission(n)
));
node = NodeFactory.buildMetaNode(key, value).withExtraContext(contexts).build();
}
@ -394,7 +411,7 @@ public class PermissionHolderSubjectData implements LPSubjectData {
Objects.requireNonNull(contexts, "contexts");
Objects.requireNonNull(key, "key");
List<Node> toRemove = streamNodes()
streamNodes()
.filter(n -> {
if (key.equalsIgnoreCase(NodeTypes.PREFIX_KEY)) {
return n.isPrefix();
@ -405,12 +422,10 @@ public class PermissionHolderSubjectData implements LPSubjectData {
}
})
.filter(n -> n.getFullContexts().equals(contexts))
.collect(Collectors.toList());
toRemove.forEach(node -> this.type.run(
() -> this.holder.unsetPermission(node),
() -> this.holder.unsetTransientPermission(node)
));
.forEach(node -> this.type.run(
() -> this.holder.unsetPermission(node),
() -> this.holder.unsetTransientPermission(node)
));
return objectSave(this.holder).thenApply(v -> true);
}
@ -429,7 +444,11 @@ public class PermissionHolderSubjectData implements LPSubjectData {
() -> 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
@ -443,7 +462,11 @@ public class PermissionHolderSubjectData implements LPSubjectData {
() -> 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) {