Refactor contexts, expose cached data in the API & release 2.13
This commit is contained in:
@@ -49,7 +49,7 @@ public class SpongeCalculatorFactory implements CalculatorFactory {
|
||||
if (plugin.getConfiguration().isApplyingRegex()) {
|
||||
processors.add(new RegexProcessor(map));
|
||||
}
|
||||
processors.add(new DefaultsProcessor(plugin.getService(), LuckPermsService.convertContexts(contexts.getContext())));
|
||||
processors.add(new DefaultsProcessor(plugin.getService(), LuckPermsService.convertContexts(contexts.getContexts())));
|
||||
|
||||
return new PermissionCalculator(plugin, user.getName(), plugin.getConfiguration().isDebugPermissionChecks(), processors);
|
||||
}
|
||||
|
||||
@@ -22,7 +22,8 @@
|
||||
|
||||
package me.lucko.luckperms.sponge;
|
||||
|
||||
import me.lucko.luckperms.common.caching.UserData;
|
||||
import me.lucko.luckperms.api.caching.UserData;
|
||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
||||
import me.lucko.luckperms.common.constants.Message;
|
||||
import me.lucko.luckperms.common.users.User;
|
||||
import me.lucko.luckperms.common.utils.AbstractListener;
|
||||
@@ -34,9 +35,7 @@ import org.spongepowered.api.profile.GameProfile;
|
||||
import org.spongepowered.api.text.serializer.TextSerializers;
|
||||
import org.spongepowered.api.world.World;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -77,7 +76,7 @@ public class SpongeListener extends AbstractListener {
|
||||
// Attempt to pre-process some permissions for the user to save time later. Might not work, but it's better than nothing.
|
||||
Optional<Player> p = e.getCause().first(Player.class);
|
||||
if (p.isPresent()) {
|
||||
Map<String, String> context = plugin.getContextManager().giveApplicableContext(p.get(), new HashMap<>());
|
||||
MutableContextSet context = plugin.getContextManager().giveApplicableContext(p.get(), MutableContextSet.empty());
|
||||
|
||||
List<String> worlds = plugin.getGame().getServer().getWorlds().stream()
|
||||
.map(World::getName)
|
||||
@@ -88,8 +87,9 @@ public class SpongeListener extends AbstractListener {
|
||||
data.preCalculate(plugin.getService().calculateContexts(LuckPermsService.convertContexts(context)));
|
||||
|
||||
for (String world : worlds) {
|
||||
Map<String, String> modified = new HashMap<>(context);
|
||||
modified.put("world", world);
|
||||
MutableContextSet modified = MutableContextSet.fromSet(context);
|
||||
modified.removeAll("world");
|
||||
modified.add("world", world);
|
||||
data.preCalculate(plugin.getService().calculateContexts(LuckPermsService.convertContexts(modified)));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -24,6 +24,7 @@ package me.lucko.luckperms.sponge.contexts;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import me.lucko.luckperms.api.context.ContextCalculator;
|
||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
||||
import me.lucko.luckperms.sponge.service.LuckPermsService;
|
||||
import org.spongepowered.api.service.context.Context;
|
||||
import org.spongepowered.api.service.permission.Subject;
|
||||
@@ -36,12 +37,12 @@ public class SpongeCalculatorLink extends ContextCalculator<Subject> {
|
||||
private final org.spongepowered.api.service.context.ContextCalculator<Subject> calculator;
|
||||
|
||||
@Override
|
||||
public Map<String, String> giveApplicableContext(Subject subject, Map<String, String> accumulator) {
|
||||
public MutableContextSet giveApplicableContext(Subject subject, MutableContextSet accumulator) {
|
||||
Set<Context> contexts = LuckPermsService.convertContexts(accumulator);
|
||||
calculator.accumulateContexts(subject, contexts);
|
||||
|
||||
accumulator.clear();
|
||||
accumulator.putAll(LuckPermsService.convertContexts(contexts));
|
||||
accumulator.addAll(LuckPermsService.convertContexts(contexts));
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ package me.lucko.luckperms.sponge.contexts;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.lucko.luckperms.api.context.ContextCalculator;
|
||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
||||
import me.lucko.luckperms.common.commands.Util;
|
||||
import me.lucko.luckperms.sponge.LPSpongePlugin;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
@@ -39,7 +40,7 @@ public class WorldCalculator extends ContextCalculator<Subject> {
|
||||
private final LPSpongePlugin plugin;
|
||||
|
||||
@Override
|
||||
public Map<String, String> giveApplicableContext(Subject subject, Map<String, String> accumulator) {
|
||||
public MutableContextSet giveApplicableContext(Subject subject, MutableContextSet accumulator) {
|
||||
UUID uuid = Util.parseUuid(subject.getIdentifier());
|
||||
if (uuid == null) {
|
||||
return accumulator;
|
||||
@@ -50,7 +51,7 @@ public class WorldCalculator extends ContextCalculator<Subject> {
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
accumulator.put(Context.WORLD_KEY, p.get().getWorld().getName());
|
||||
accumulator.add(Context.WORLD_KEY, p.get().getWorld().getName());
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,13 +28,13 @@ import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import me.lucko.luckperms.api.LocalizedNode;
|
||||
import me.lucko.luckperms.api.Node;
|
||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
||||
import me.lucko.luckperms.common.groups.Group;
|
||||
import org.spongepowered.api.command.CommandSource;
|
||||
import org.spongepowered.api.service.context.Context;
|
||||
import org.spongepowered.api.service.permission.NodeTree;
|
||||
import org.spongepowered.api.service.permission.Subject;
|
||||
import org.spongepowered.api.service.permission.SubjectCollection;
|
||||
import org.spongepowered.api.service.permission.SubjectData;
|
||||
import org.spongepowered.api.util.Tristate;
|
||||
|
||||
import java.util.List;
|
||||
@@ -156,7 +156,7 @@ public class LuckPermsGroupSubject implements Subject {
|
||||
|
||||
@Override
|
||||
public Set<Context> getActiveContexts() {
|
||||
return SubjectData.GLOBAL_CONTEXT;
|
||||
return LuckPermsService.convertContexts(service.getPlugin().getContextManager().giveApplicableContext(this, MutableContextSet.empty()));
|
||||
}
|
||||
|
||||
private Optional<String> getChatMeta(Set<Context> contexts, boolean prefix) {
|
||||
|
||||
@@ -22,10 +22,15 @@
|
||||
|
||||
package me.lucko.luckperms.sponge.service;
|
||||
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Maps;
|
||||
import lombok.*;
|
||||
import me.lucko.luckperms.api.Contexts;
|
||||
import me.lucko.luckperms.api.context.ContextSet;
|
||||
import me.lucko.luckperms.sponge.LPSpongePlugin;
|
||||
import me.lucko.luckperms.sponge.contexts.SpongeCalculatorLink;
|
||||
import me.lucko.luckperms.sponge.service.collections.GroupCollection;
|
||||
@@ -48,28 +53,25 @@ import java.util.stream.Collectors;
|
||||
/**
|
||||
* The LuckPerms implementation of the Sponge Permission Service
|
||||
*/
|
||||
@Getter
|
||||
public class LuckPermsService implements PermissionService {
|
||||
public static final String SERVER_CONTEXT = "server";
|
||||
|
||||
@Getter
|
||||
private final LPSpongePlugin plugin;
|
||||
|
||||
@Getter
|
||||
private final SubjectStorage storage;
|
||||
|
||||
@Getter
|
||||
private final UserCollection userSubjects;
|
||||
|
||||
@Getter
|
||||
private final GroupCollection groupSubjects;
|
||||
|
||||
@Getter
|
||||
private final PersistedCollection defaultSubjects;
|
||||
|
||||
@Getter
|
||||
private final Set<PermissionDescription> descriptionSet;
|
||||
|
||||
private final Map<String, SubjectCollection> subjects;
|
||||
@Getter(value = AccessLevel.NONE)
|
||||
private final LoadingCache<String, SubjectCollection> collections = CacheBuilder.newBuilder()
|
||||
.build(new CacheLoader<String, SubjectCollection>() {
|
||||
@Override
|
||||
public SubjectCollection load(String s) {
|
||||
return new SimpleCollection(LuckPermsService.this, s);
|
||||
}
|
||||
});
|
||||
|
||||
public LuckPermsService(LPSpongePlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
@@ -81,10 +83,9 @@ public class LuckPermsService implements PermissionService {
|
||||
defaultSubjects = new PersistedCollection(this, "defaults");
|
||||
defaultSubjects.loadAll();
|
||||
|
||||
subjects = new ConcurrentHashMap<>();
|
||||
subjects.put(PermissionService.SUBJECTS_USER, userSubjects);
|
||||
subjects.put(PermissionService.SUBJECTS_GROUP, groupSubjects);
|
||||
subjects.put("defaults", defaultSubjects);
|
||||
collections.put(PermissionService.SUBJECTS_USER, userSubjects);
|
||||
collections.put(PermissionService.SUBJECTS_GROUP, groupSubjects);
|
||||
collections.put("defaults", defaultSubjects);
|
||||
|
||||
descriptionSet = ConcurrentHashMap.newKeySet();
|
||||
}
|
||||
@@ -100,16 +101,12 @@ public class LuckPermsService implements PermissionService {
|
||||
|
||||
@Override
|
||||
public SubjectCollection getSubjects(String s) {
|
||||
if (!subjects.containsKey(s)) {
|
||||
subjects.put(s, new SimpleCollection(this, s));
|
||||
}
|
||||
|
||||
return subjects.get(s);
|
||||
return collections.getUnchecked(s.toLowerCase());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, SubjectCollection> getKnownSubjects() {
|
||||
return ImmutableMap.copyOf(subjects);
|
||||
return ImmutableMap.copyOf(collections.asMap());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -150,16 +147,17 @@ public class LuckPermsService implements PermissionService {
|
||||
plugin.getConfiguration().isIncludingGlobalWorldPerms(),
|
||||
true,
|
||||
plugin.getConfiguration().isApplyingGlobalGroups(),
|
||||
plugin.getConfiguration().isApplyingGlobalWorldGroups()
|
||||
plugin.getConfiguration().isApplyingGlobalWorldGroups(),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
public static Map<String, String> convertContexts(Set<Context> contexts) {
|
||||
return contexts.stream().collect(Collectors.toMap(Context::getKey, Context::getValue));
|
||||
public static ContextSet convertContexts(Set<Context> contexts) {
|
||||
return ContextSet.fromEntries(contexts.stream().map(c -> Maps.immutableEntry(c.getKey(), c.getValue())).collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
public static Set<Context> convertContexts(Map<String, String> contexts) {
|
||||
return contexts.entrySet().stream().map(e -> new Context(e.getKey(), e.getValue())).collect(Collectors.toSet());
|
||||
public static Set<Context> convertContexts(ContextSet contexts) {
|
||||
return contexts.toSet().stream().map(e -> new Context(e.getKey(), e.getValue())).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public static Tristate convertTristate(me.lucko.luckperms.api.Tristate tristate) {
|
||||
|
||||
@@ -28,6 +28,7 @@ import com.google.common.collect.ImmutableSet;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import me.lucko.luckperms.api.Node;
|
||||
import me.lucko.luckperms.api.context.ContextSet;
|
||||
import me.lucko.luckperms.common.core.PermissionHolder;
|
||||
import me.lucko.luckperms.common.groups.Group;
|
||||
import me.lucko.luckperms.common.users.User;
|
||||
@@ -65,7 +66,7 @@ public class LuckPermsSubjectData implements SubjectData {
|
||||
Map<Set<Context>, Map<String, Boolean>> perms = new HashMap<>();
|
||||
|
||||
for (Node n : enduring ? holder.getNodes() : holder.getTransientNodes()) {
|
||||
Set<Context> contexts = LuckPermsService.convertContexts(n.getExtraContexts());
|
||||
Set<Context> contexts = LuckPermsService.convertContexts(n.getContexts());
|
||||
|
||||
if (n.isServerSpecific()) {
|
||||
contexts.add(new Context(LuckPermsService.SERVER_CONTEXT, n.getServer().get()));
|
||||
@@ -185,7 +186,7 @@ public class LuckPermsSubjectData implements SubjectData {
|
||||
continue;
|
||||
}
|
||||
|
||||
Set<Context> contexts = LuckPermsService.convertContexts(n.getExtraContexts());
|
||||
Set<Context> contexts = LuckPermsService.convertContexts(n.getContexts());
|
||||
|
||||
if (n.isServerSpecific()) {
|
||||
contexts.add(new Context(LuckPermsService.SERVER_CONTEXT, n.getServer().get()));
|
||||
@@ -225,7 +226,7 @@ public class LuckPermsSubjectData implements SubjectData {
|
||||
public boolean addParent(Set<Context> set, Subject subject) {
|
||||
if (subject instanceof LuckPermsGroupSubject) {
|
||||
LuckPermsGroupSubject permsSubject = ((LuckPermsGroupSubject) subject);
|
||||
Map<String, String> contexts = LuckPermsService.convertContexts(set);
|
||||
ContextSet contexts = LuckPermsService.convertContexts(set);
|
||||
|
||||
try {
|
||||
if (enduring) {
|
||||
@@ -249,7 +250,7 @@ public class LuckPermsSubjectData implements SubjectData {
|
||||
public boolean removeParent(Set<Context> set, Subject subject) {
|
||||
if (subject instanceof LuckPermsGroupSubject) {
|
||||
LuckPermsGroupSubject permsSubject = ((LuckPermsGroupSubject) subject);
|
||||
Map<String, String> contexts = LuckPermsService.convertContexts(set);
|
||||
ContextSet contexts = LuckPermsService.convertContexts(set);
|
||||
|
||||
try {
|
||||
if (enduring) {
|
||||
@@ -295,7 +296,7 @@ public class LuckPermsSubjectData implements SubjectData {
|
||||
|
||||
@Override
|
||||
public boolean clearParents(Set<Context> set) {
|
||||
Map<String, String> context = LuckPermsService.convertContexts(set);
|
||||
ContextSet context = LuckPermsService.convertContexts(set);
|
||||
|
||||
List<Node> toRemove = (enduring ? holder.getNodes() : holder.getTransientNodes()).stream()
|
||||
.filter(Node::isGroupNode)
|
||||
@@ -336,7 +337,7 @@ public class LuckPermsSubjectData implements SubjectData {
|
||||
continue;
|
||||
}
|
||||
|
||||
Set<Context> contexts = LuckPermsService.convertContexts(n.getExtraContexts());
|
||||
Set<Context> contexts = LuckPermsService.convertContexts(n.getContexts());
|
||||
|
||||
if (n.isServerSpecific()) {
|
||||
contexts.add(new Context(LuckPermsService.SERVER_CONTEXT, n.getServer().get()));
|
||||
@@ -384,7 +385,7 @@ public class LuckPermsSubjectData implements SubjectData {
|
||||
@Override
|
||||
public Map<String, String> getOptions(Set<Context> set) {
|
||||
ImmutableMap.Builder<String, String> options = ImmutableMap.builder();
|
||||
Map<String, String> contexts = LuckPermsService.convertContexts(set);
|
||||
ContextSet contexts = LuckPermsService.convertContexts(set);
|
||||
|
||||
int prefixPriority = Integer.MIN_VALUE;
|
||||
int suffixPriority = Integer.MIN_VALUE;
|
||||
@@ -431,7 +432,7 @@ public class LuckPermsSubjectData implements SubjectData {
|
||||
|
||||
@Override
|
||||
public boolean setOption(Set<Context> set, String key, String value) {
|
||||
Map<String, String> context = LuckPermsService.convertContexts(set);
|
||||
ContextSet context = LuckPermsService.convertContexts(set);
|
||||
|
||||
key = escapeCharacters(key);
|
||||
value = escapeCharacters(value);
|
||||
@@ -455,7 +456,7 @@ public class LuckPermsSubjectData implements SubjectData {
|
||||
|
||||
@Override
|
||||
public boolean clearOptions(Set<Context> set) {
|
||||
Map<String, String> context = LuckPermsService.convertContexts(set);
|
||||
ContextSet context = LuckPermsService.convertContexts(set);
|
||||
|
||||
List<Node> toRemove = (enduring ? holder.getNodes() : holder.getTransientNodes()).stream()
|
||||
.filter(n -> n.isMeta() || n.isPrefix() || n.isSuffix())
|
||||
|
||||
@@ -26,7 +26,8 @@ import com.google.common.collect.ImmutableList;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import me.lucko.luckperms.common.caching.MetaData;
|
||||
import me.lucko.luckperms.api.caching.MetaData;
|
||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
||||
import me.lucko.luckperms.common.users.User;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.command.CommandSource;
|
||||
@@ -36,8 +37,10 @@ import org.spongepowered.api.service.permission.Subject;
|
||||
import org.spongepowered.api.service.permission.SubjectCollection;
|
||||
import org.spongepowered.api.util.Tristate;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
@EqualsAndHashCode(of = "user")
|
||||
public class LuckPermsUserSubject implements Subject {
|
||||
@@ -171,8 +174,6 @@ public class LuckPermsUserSubject implements Subject {
|
||||
|
||||
@Override
|
||||
public Set<Context> getActiveContexts() {
|
||||
return service.getPlugin().getContextManager().giveApplicableContext(this, new HashMap<>()).entrySet().stream()
|
||||
.map(e -> new Context(e.getKey(), e.getValue()))
|
||||
.collect(Collectors.toSet());
|
||||
return LuckPermsService.convertContexts(service.getPlugin().getContextManager().giveApplicableContext(this, MutableContextSet.empty()));
|
||||
}
|
||||
}
|
||||
|
||||
+16
-13
@@ -22,6 +22,9 @@
|
||||
|
||||
package me.lucko.luckperms.sponge.service.persisted;
|
||||
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -35,7 +38,6 @@ import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -48,34 +50,35 @@ public class PersistedCollection implements SubjectCollection {
|
||||
@Getter
|
||||
private final String identifier;
|
||||
|
||||
private final Map<String, PersistedSubject> subjects = new ConcurrentHashMap<>();
|
||||
private final LoadingCache<String, PersistedSubject> subjects = CacheBuilder.newBuilder()
|
||||
.build(new CacheLoader<String, PersistedSubject>() {
|
||||
@Override
|
||||
public PersistedSubject load(String s) {
|
||||
return new PersistedSubject(s, service, PersistedCollection.this);
|
||||
}
|
||||
});
|
||||
|
||||
public void loadAll() {
|
||||
Map<String, SubjectDataHolder> holders = service.getStorage().loadAllFromFile(identifier);
|
||||
for (Map.Entry<String, SubjectDataHolder> e : holders.entrySet()) {
|
||||
PersistedSubject subject = new PersistedSubject(e.getKey(), service, this);
|
||||
PersistedSubject subject = get(e.getKey());
|
||||
subject.loadData(e.getValue());
|
||||
subjects.put(e.getKey(), subject);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Subject get(@NonNull String id) {
|
||||
if (!subjects.containsKey(id)) {
|
||||
subjects.put(id, new PersistedSubject(id, service, this));
|
||||
}
|
||||
|
||||
return subjects.get(id);
|
||||
public PersistedSubject get(@NonNull String id) {
|
||||
return subjects.getUnchecked(id.toLowerCase());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasRegistered(@NonNull String id) {
|
||||
return subjects.containsKey(id);
|
||||
return subjects.asMap().containsKey(id.toLowerCase());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Subject> getAllSubjects() {
|
||||
return subjects.values().stream().map(s -> (Subject) s).collect(Collectors.toList());
|
||||
return subjects.asMap().values().stream().map(s -> (Subject) s).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -86,7 +89,7 @@ public class PersistedCollection implements SubjectCollection {
|
||||
@Override
|
||||
public Map<Subject, Boolean> getAllWithPermission(@NonNull Set<Context> contexts, @NonNull String node) {
|
||||
Map<Subject, Boolean> m = new HashMap<>();
|
||||
for (Subject subject : subjects.values()) {
|
||||
for (Subject subject : subjects.asMap().values()) {
|
||||
Tristate ts = subject.getPermissionValue(contexts, node);
|
||||
if (ts != Tristate.UNDEFINED) {
|
||||
m.put(subject, ts.asBoolean());
|
||||
|
||||
+17
-9
@@ -25,13 +25,14 @@ package me.lucko.luckperms.sponge.service.persisted;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
||||
import me.lucko.luckperms.common.utils.BufferedRequest;
|
||||
import me.lucko.luckperms.sponge.service.LuckPermsService;
|
||||
import org.spongepowered.api.command.CommandSource;
|
||||
import org.spongepowered.api.service.context.Context;
|
||||
import org.spongepowered.api.service.permission.MemorySubjectData;
|
||||
import org.spongepowered.api.service.permission.Subject;
|
||||
import org.spongepowered.api.service.permission.SubjectCollection;
|
||||
import org.spongepowered.api.service.permission.SubjectData;
|
||||
import org.spongepowered.api.util.Tristate;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -51,6 +52,19 @@ public class PersistedSubject implements Subject {
|
||||
private final SubjectCollection containingCollection;
|
||||
private final PersistedSubjectData subjectData;
|
||||
private final MemorySubjectData transientSubjectData;
|
||||
private final BufferedRequest<Void> saveBuffer = new BufferedRequest<Void>(1000L, r -> PersistedSubject.this.service.getPlugin().doAsync(r)) {
|
||||
@Override
|
||||
protected Void perform() {
|
||||
service.getPlugin().doAsync(() -> {
|
||||
try {
|
||||
service.getStorage().saveToFile(PersistedSubject.this);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
public PersistedSubject(String identifier, LuckPermsService service, SubjectCollection containingCollection) {
|
||||
this.identifier = identifier;
|
||||
@@ -67,13 +81,7 @@ public class PersistedSubject implements Subject {
|
||||
}
|
||||
|
||||
public void save() {
|
||||
service.getPlugin().doAsync(() -> {
|
||||
try {
|
||||
service.getStorage().saveToFile(this);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
saveBuffer.request();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -178,6 +186,6 @@ public class PersistedSubject implements Subject {
|
||||
|
||||
@Override
|
||||
public Set<Context> getActiveContexts() {
|
||||
return SubjectData.GLOBAL_CONTEXT;
|
||||
return LuckPermsService.convertContexts(service.getPlugin().getContextManager().giveApplicableContext(this, MutableContextSet.empty()));
|
||||
}
|
||||
}
|
||||
|
||||
+12
-6
@@ -22,6 +22,8 @@
|
||||
|
||||
package me.lucko.luckperms.sponge.service.persisted;
|
||||
|
||||
import lombok.ToString;
|
||||
import me.lucko.luckperms.api.context.ContextSet;
|
||||
import org.spongepowered.api.service.context.Context;
|
||||
import org.spongepowered.api.service.permission.MemorySubjectData;
|
||||
import org.spongepowered.api.service.permission.PermissionService;
|
||||
@@ -35,6 +37,7 @@ import static me.lucko.luckperms.sponge.service.LuckPermsService.convertContexts
|
||||
/**
|
||||
* Holds SubjectData in a "gson friendly" format for serialization
|
||||
*/
|
||||
@ToString
|
||||
public class SubjectDataHolder {
|
||||
private final Map<Map<String, String>, Map<String, Boolean>> permissions;
|
||||
private final Map<Map<String, String>, Map<String, String>> options;
|
||||
@@ -43,17 +46,17 @@ public class SubjectDataHolder {
|
||||
public SubjectDataHolder(Map<Set<Context>, Map<String, String>> options, Map<Set<Context>, Map<String, Boolean>> permissions, Map<Set<Context>, List<Map.Entry<String, String>>> parents) {
|
||||
this.options = new HashMap<>();
|
||||
for (Map.Entry<Set<Context>, Map<String, String>> e : options.entrySet()) {
|
||||
this.options.put(convertContexts(e.getKey()), new HashMap<>(e.getValue()));
|
||||
this.options.put(convertContexts(e.getKey()).toMap(), new HashMap<>(e.getValue()));
|
||||
}
|
||||
|
||||
this.permissions = new HashMap<>();
|
||||
for (Map.Entry<Set<Context>, Map<String, Boolean>> e : permissions.entrySet()) {
|
||||
this.permissions.put(convertContexts(e.getKey()), new HashMap<>(e.getValue()));
|
||||
this.permissions.put(convertContexts(e.getKey()).toMap(), new HashMap<>(e.getValue()));
|
||||
}
|
||||
|
||||
this.parents = new HashMap<>();
|
||||
for (Map.Entry<Set<Context>, List<Map.Entry<String, String>>> e : parents.entrySet()) {
|
||||
this.parents.put(convertContexts(e.getKey()), e.getValue().stream().map(p -> new AbstractMap.SimpleEntry<>(p.getKey(), p.getValue())).collect(Collectors.toList()));
|
||||
this.parents.put(convertContexts(e.getKey()).toMap(), e.getValue().stream().map(p -> new AbstractMap.SimpleEntry<>(p.getKey(), p.getValue())).collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,20 +80,23 @@ public class SubjectDataHolder {
|
||||
|
||||
public void copyTo(MemorySubjectData subjectData, PermissionService service) {
|
||||
for (Map.Entry<Map<String, String>, Map<String, Boolean>> e : permissions.entrySet()) {
|
||||
Set<Context> contexts = convertContexts(ContextSet.fromMap(e.getKey()));
|
||||
for (Map.Entry<String, Boolean> perm : e.getValue().entrySet()) {
|
||||
subjectData.setPermission(convertContexts(e.getKey()), perm.getKey(), Tristate.fromBoolean(perm.getValue()));
|
||||
subjectData.setPermission(contexts, perm.getKey(), Tristate.fromBoolean(perm.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<Map<String, String>, Map<String, String>> e : options.entrySet()) {
|
||||
Set<Context> contexts = convertContexts(ContextSet.fromMap(e.getKey()));
|
||||
for (Map.Entry<String, String> option : e.getValue().entrySet()) {
|
||||
subjectData.setOption(convertContexts(e.getKey()), option.getKey(), option.getValue());
|
||||
subjectData.setOption(contexts, option.getKey(), option.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<Map<String, String>, List<Map.Entry<String, String>>> e : parents.entrySet()) {
|
||||
Set<Context> contexts = convertContexts(ContextSet.fromMap(e.getKey()));
|
||||
for (Map.Entry<String, String> parent : e.getValue()) {
|
||||
subjectData.addParent(convertContexts(e.getKey()), service.getSubjects(parent.getKey()).get(parent.getValue()));
|
||||
subjectData.addParent(contexts, service.getSubjects(parent.getKey()).get(parent.getValue()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -58,7 +58,7 @@ public class SubjectStorage {
|
||||
collection.mkdirs();
|
||||
}
|
||||
|
||||
File subjectFile = new File(collection, subject.getIdentifier().toLowerCase() + ".json");
|
||||
File subjectFile = new File(collection, subject.getIdentifier() + ".json");
|
||||
saveToFile(subject, subjectFile);
|
||||
}
|
||||
|
||||
@@ -110,8 +110,8 @@ public class SubjectStorage {
|
||||
return null;
|
||||
}
|
||||
|
||||
File subject = new File(collection, subjectName.toLowerCase() + ".json");
|
||||
return new AbstractMap.SimpleEntry<>(subjectName.toLowerCase(), loadFromFile(subject).getValue());
|
||||
File subject = new File(collection, subjectName + ".json");
|
||||
return new AbstractMap.SimpleEntry<>(subjectName, loadFromFile(subject).getValue());
|
||||
}
|
||||
|
||||
public Map.Entry<String, SubjectDataHolder> loadFromFile(File file) throws IOException {
|
||||
@@ -120,7 +120,7 @@ public class SubjectStorage {
|
||||
}
|
||||
|
||||
String s = Files.toString(file, Charset.defaultCharset());
|
||||
return new AbstractMap.SimpleEntry<>(file.getName().substring(file.getName().length() - 5).toLowerCase(), loadFromString(s));
|
||||
return new AbstractMap.SimpleEntry<>(file.getName().substring(0, file.getName().length() - 5), loadFromString(s));
|
||||
}
|
||||
|
||||
public SubjectDataHolder loadFromString(String s) {
|
||||
|
||||
+16
-11
@@ -22,6 +22,9 @@
|
||||
|
||||
package me.lucko.luckperms.sponge.service.simple;
|
||||
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -35,7 +38,7 @@ import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Super simple SubjectCollection implementation
|
||||
@@ -47,25 +50,27 @@ public class SimpleCollection implements SubjectCollection {
|
||||
@Getter
|
||||
private final String identifier;
|
||||
|
||||
private final Map<String, Subject> subjects = new ConcurrentHashMap<>();
|
||||
private final LoadingCache<String, SimpleSubject> subjects = CacheBuilder.newBuilder()
|
||||
.build(new CacheLoader<String, SimpleSubject>() {
|
||||
@Override
|
||||
public SimpleSubject load(String s) {
|
||||
return new SimpleSubject(s, service, SimpleCollection.this);
|
||||
}
|
||||
});
|
||||
|
||||
@Override
|
||||
public synchronized Subject get(@NonNull String id) {
|
||||
if (!subjects.containsKey(id)) {
|
||||
subjects.put(id, new SimpleSubject(id, service, this));
|
||||
}
|
||||
|
||||
return subjects.get(id);
|
||||
public Subject get(@NonNull String id) {
|
||||
return subjects.getUnchecked(id.toLowerCase());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasRegistered(@NonNull String id) {
|
||||
return subjects.containsKey(id);
|
||||
return subjects.asMap().containsKey(id.toLowerCase());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Subject> getAllSubjects() {
|
||||
return subjects.values();
|
||||
return subjects.asMap().values().stream().map(s -> (Subject) s).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -76,7 +81,7 @@ public class SimpleCollection implements SubjectCollection {
|
||||
@Override
|
||||
public Map<Subject, Boolean> getAllWithPermission(@NonNull Set<Context> contexts, @NonNull String node) {
|
||||
Map<Subject, Boolean> m = new HashMap<>();
|
||||
for (Subject subject : subjects.values()) {
|
||||
for (Subject subject : subjects.asMap().values()) {
|
||||
Tristate ts = subject.getPermissionValue(contexts, node);
|
||||
if (ts != Tristate.UNDEFINED) {
|
||||
m.put(subject, ts.asBoolean());
|
||||
|
||||
@@ -25,9 +25,14 @@ package me.lucko.luckperms.sponge.service.simple;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
||||
import me.lucko.luckperms.sponge.service.LuckPermsService;
|
||||
import org.spongepowered.api.command.CommandSource;
|
||||
import org.spongepowered.api.service.context.Context;
|
||||
import org.spongepowered.api.service.permission.*;
|
||||
import org.spongepowered.api.service.permission.MemorySubjectData;
|
||||
import org.spongepowered.api.service.permission.Subject;
|
||||
import org.spongepowered.api.service.permission.SubjectCollection;
|
||||
import org.spongepowered.api.service.permission.SubjectData;
|
||||
import org.spongepowered.api.util.Tristate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -42,11 +47,11 @@ import java.util.Set;
|
||||
public class SimpleSubject implements Subject {
|
||||
private final String identifier;
|
||||
|
||||
private final PermissionService service;
|
||||
private final LuckPermsService service;
|
||||
private final SubjectCollection containingCollection;
|
||||
private final MemorySubjectData subjectData;
|
||||
|
||||
public SimpleSubject(String identifier, PermissionService service, SubjectCollection containingCollection) {
|
||||
public SimpleSubject(String identifier, LuckPermsService service, SubjectCollection containingCollection) {
|
||||
this.identifier = identifier;
|
||||
this.service = service;
|
||||
this.containingCollection = containingCollection;
|
||||
@@ -147,6 +152,6 @@ public class SimpleSubject implements Subject {
|
||||
|
||||
@Override
|
||||
public Set<Context> getActiveContexts() {
|
||||
return SubjectData.GLOBAL_CONTEXT;
|
||||
return LuckPermsService.convertContexts(service.getPlugin().getContextManager().giveApplicableContext(this, MutableContextSet.empty()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user