mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-24 11:38:40 +01:00
Cache context lookups & refactor Sponge subjects
This commit is contained in:
parent
0198068a13
commit
0a4d337a53
@ -26,7 +26,6 @@ import lombok.Getter;
|
|||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import me.lucko.luckperms.api.Contexts;
|
import me.lucko.luckperms.api.Contexts;
|
||||||
import me.lucko.luckperms.api.Tristate;
|
import me.lucko.luckperms.api.Tristate;
|
||||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
|
||||||
import me.lucko.luckperms.bukkit.LPBukkitPlugin;
|
import me.lucko.luckperms.bukkit.LPBukkitPlugin;
|
||||||
import me.lucko.luckperms.common.users.User;
|
import me.lucko.luckperms.common.users.User;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
@ -67,7 +66,7 @@ public class LPPermissible extends PermissibleBase {
|
|||||||
|
|
||||||
public Contexts calculateContexts() {
|
public Contexts calculateContexts() {
|
||||||
return new Contexts(
|
return new Contexts(
|
||||||
plugin.getContextManager().giveApplicableContext(parent, new MutableContextSet()),
|
plugin.getContextManager().getApplicableContext(parent),
|
||||||
plugin.getConfiguration().isIncludingGlobalPerms(),
|
plugin.getConfiguration().isIncludingGlobalPerms(),
|
||||||
plugin.getConfiguration().isIncludingGlobalWorldPerms(),
|
plugin.getConfiguration().isIncludingGlobalWorldPerms(),
|
||||||
true,
|
true,
|
||||||
|
@ -23,7 +23,6 @@
|
|||||||
package me.lucko.luckperms.bungee;
|
package me.lucko.luckperms.bungee;
|
||||||
|
|
||||||
import me.lucko.luckperms.api.Contexts;
|
import me.lucko.luckperms.api.Contexts;
|
||||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
|
||||||
import me.lucko.luckperms.api.event.events.UserFirstLoginEvent;
|
import me.lucko.luckperms.api.event.events.UserFirstLoginEvent;
|
||||||
import me.lucko.luckperms.common.constants.Message;
|
import me.lucko.luckperms.common.constants.Message;
|
||||||
import me.lucko.luckperms.common.core.UuidCache;
|
import me.lucko.luckperms.common.core.UuidCache;
|
||||||
@ -72,7 +71,7 @@ public class BungeeListener extends AbstractListener implements Listener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Contexts contexts = new Contexts(
|
Contexts contexts = new Contexts(
|
||||||
plugin.getContextManager().giveApplicableContext(player, MutableContextSet.empty()),
|
plugin.getContextManager().getApplicableContext(player),
|
||||||
plugin.getConfiguration().isIncludingGlobalPerms(),
|
plugin.getConfiguration().isIncludingGlobalPerms(),
|
||||||
plugin.getConfiguration().isIncludingGlobalWorldPerms(),
|
plugin.getConfiguration().isIncludingGlobalWorldPerms(),
|
||||||
true,
|
true,
|
||||||
|
@ -22,19 +22,43 @@
|
|||||||
|
|
||||||
package me.lucko.luckperms.common.contexts;
|
package me.lucko.luckperms.common.contexts;
|
||||||
|
|
||||||
|
import com.google.common.cache.CacheBuilder;
|
||||||
|
import com.google.common.cache.CacheLoader;
|
||||||
|
import com.google.common.cache.LoadingCache;
|
||||||
import me.lucko.luckperms.api.context.ContextListener;
|
import me.lucko.luckperms.api.context.ContextListener;
|
||||||
|
import me.lucko.luckperms.api.context.ContextSet;
|
||||||
import me.lucko.luckperms.api.context.IContextCalculator;
|
import me.lucko.luckperms.api.context.IContextCalculator;
|
||||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
import me.lucko.luckperms.api.context.MutableContextSet;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class ContextManager<T> {
|
public class ContextManager<T> {
|
||||||
|
|
||||||
private final List<IContextCalculator<T>> calculators = new CopyOnWriteArrayList<>();
|
private final List<IContextCalculator<T>> calculators = new CopyOnWriteArrayList<>();
|
||||||
private final List<ContextListener<T>> listeners = new CopyOnWriteArrayList<>();
|
private final List<ContextListener<T>> listeners = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
|
private final LoadingCache<T, ContextSet> cache = CacheBuilder.newBuilder()
|
||||||
|
.expireAfterWrite(50L, TimeUnit.MILLISECONDS)
|
||||||
|
.build(new CacheLoader<T, ContextSet>() {
|
||||||
|
@Override
|
||||||
|
public ContextSet load(T t) {
|
||||||
|
return calculateApplicableContext(t, MutableContextSet.empty()).makeImmutable();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
private MutableContextSet calculateApplicableContext(T subject, MutableContextSet accumulator) {
|
||||||
|
for (IContextCalculator<T> calculator : calculators) {
|
||||||
|
calculator.giveApplicableContext(subject, accumulator);
|
||||||
|
}
|
||||||
|
return accumulator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ContextSet getApplicableContext(T subject) {
|
||||||
|
return cache.getUnchecked(subject);
|
||||||
|
}
|
||||||
|
|
||||||
public void registerCalculator(IContextCalculator<T> calculator) {
|
public void registerCalculator(IContextCalculator<T> calculator) {
|
||||||
listeners.forEach(calculator::addListener);
|
listeners.forEach(calculator::addListener);
|
||||||
calculators.add(calculator);
|
calculators.add(calculator);
|
||||||
@ -47,20 +71,4 @@ public class ContextManager<T> {
|
|||||||
|
|
||||||
listeners.add(listener);
|
listeners.add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public MutableContextSet giveApplicableContext(T subject, MutableContextSet accumulator) {
|
|
||||||
for (IContextCalculator<T> calculator : calculators) {
|
|
||||||
calculator.giveApplicableContext(subject, accumulator);
|
|
||||||
}
|
|
||||||
return accumulator;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isContextApplicable(T subject, Map.Entry<String, String> context) {
|
|
||||||
for (IContextCalculator<T> calculator : calculators) {
|
|
||||||
if (calculator.isContextApplicable(subject, context)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,6 @@ import me.lucko.luckperms.api.context.MutableContextSet;
|
|||||||
import me.lucko.luckperms.common.constants.Message;
|
import me.lucko.luckperms.common.constants.Message;
|
||||||
import me.lucko.luckperms.common.users.User;
|
import me.lucko.luckperms.common.users.User;
|
||||||
import me.lucko.luckperms.common.utils.AbstractListener;
|
import me.lucko.luckperms.common.utils.AbstractListener;
|
||||||
import me.lucko.luckperms.sponge.service.LuckPermsService;
|
|
||||||
import org.spongepowered.api.entity.living.player.Player;
|
import org.spongepowered.api.entity.living.player.Player;
|
||||||
import org.spongepowered.api.event.Listener;
|
import org.spongepowered.api.event.Listener;
|
||||||
import org.spongepowered.api.event.network.ClientConnectionEvent;
|
import org.spongepowered.api.event.network.ClientConnectionEvent;
|
||||||
@ -76,7 +75,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.
|
// 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);
|
Optional<Player> p = e.getCause().first(Player.class);
|
||||||
if (p.isPresent()) {
|
if (p.isPresent()) {
|
||||||
MutableContextSet context = plugin.getContextManager().giveApplicableContext(p.get(), MutableContextSet.empty());
|
MutableContextSet context = MutableContextSet.fromSet(plugin.getContextManager().getApplicableContext(p.get()));
|
||||||
|
|
||||||
List<String> worlds = plugin.getGame().getServer().getWorlds().stream()
|
List<String> worlds = plugin.getGame().getServer().getWorlds().stream()
|
||||||
.map(World::getName)
|
.map(World::getName)
|
||||||
@ -84,13 +83,13 @@ public class SpongeListener extends AbstractListener {
|
|||||||
|
|
||||||
plugin.doAsync(() -> {
|
plugin.doAsync(() -> {
|
||||||
UserData data = user.getUserData();
|
UserData data = user.getUserData();
|
||||||
data.preCalculate(plugin.getService().calculateContexts(LuckPermsService.convertContexts(context)));
|
data.preCalculate(plugin.getService().calculateContexts(context));
|
||||||
|
|
||||||
for (String world : worlds) {
|
for (String world : worlds) {
|
||||||
MutableContextSet modified = MutableContextSet.fromSet(context);
|
MutableContextSet modified = MutableContextSet.fromSet(context);
|
||||||
modified.removeAll("world");
|
modified.removeAll("world");
|
||||||
modified.add("world", world);
|
modified.add("world", world);
|
||||||
data.preCalculate(plugin.getService().calculateContexts(LuckPermsService.convertContexts(modified)));
|
data.preCalculate(plugin.getService().calculateContexts(modified));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -129,7 +129,7 @@ public class MigrationPermissionsEx extends SubCommand<Object> {
|
|||||||
contexts.removeAll("world");
|
contexts.removeAll("world");
|
||||||
|
|
||||||
for (Map.Entry<String, String> opt : e.getValue().entrySet()) {
|
for (Map.Entry<String, String> opt : e.getValue().entrySet()) {
|
||||||
if (opt.getKey().equalsIgnoreCase("prefix") || opt.getKey().equalsIgnoreCase("prefix")) {
|
if (opt.getKey().equalsIgnoreCase("prefix") || opt.getKey().equalsIgnoreCase("suffix")) {
|
||||||
try {
|
try {
|
||||||
holder.setPermission(new Node.Builder(opt.getKey().toLowerCase() + ".100." + opt.getValue()).setServerRaw(server).setWorld(world).withExtraContext(contexts).setValue(true).build());
|
holder.setPermission(new Node.Builder(opt.getKey().toLowerCase() + ".100." + opt.getValue()).setServerRaw(server).setWorld(world).withExtraContext(contexts).setValue(true).build());
|
||||||
} catch (ObjectAlreadyHasException ignored) {}
|
} catch (ObjectAlreadyHasException ignored) {}
|
||||||
|
@ -23,15 +23,14 @@
|
|||||||
package me.lucko.luckperms.sponge.service;
|
package me.lucko.luckperms.sponge.service;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import lombok.AccessLevel;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NonNull;
|
|
||||||
import me.lucko.luckperms.api.LocalizedNode;
|
import me.lucko.luckperms.api.LocalizedNode;
|
||||||
import me.lucko.luckperms.api.Node;
|
import me.lucko.luckperms.api.Node;
|
||||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
import me.lucko.luckperms.api.context.ContextSet;
|
||||||
import me.lucko.luckperms.common.groups.Group;
|
import me.lucko.luckperms.common.groups.Group;
|
||||||
import org.spongepowered.api.command.CommandSource;
|
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.NodeTree;
|
||||||
import org.spongepowered.api.service.permission.Subject;
|
import org.spongepowered.api.service.permission.Subject;
|
||||||
import org.spongepowered.api.service.permission.SubjectCollection;
|
import org.spongepowered.api.service.permission.SubjectCollection;
|
||||||
@ -40,26 +39,22 @@ import org.spongepowered.api.util.Tristate;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static me.lucko.luckperms.api.MetaUtils.unescapeCharacters;
|
import static me.lucko.luckperms.api.MetaUtils.unescapeCharacters;
|
||||||
|
|
||||||
@EqualsAndHashCode(of = "group")
|
@Getter
|
||||||
public class LuckPermsGroupSubject implements Subject {
|
@EqualsAndHashCode(of = "group", callSuper = false)
|
||||||
|
public class LuckPermsGroupSubject extends LuckPermsSubject {
|
||||||
public static LuckPermsGroupSubject wrapGroup(Group group, LuckPermsService service) {
|
public static LuckPermsGroupSubject wrapGroup(Group group, LuckPermsService service) {
|
||||||
return new LuckPermsGroupSubject(group, service);
|
return new LuckPermsGroupSubject(group, service);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Getter
|
@Getter(value = AccessLevel.NONE)
|
||||||
private Group group;
|
|
||||||
|
|
||||||
private LuckPermsService service;
|
private LuckPermsService service;
|
||||||
|
|
||||||
@Getter
|
private Group group;
|
||||||
private LuckPermsSubjectData subjectData;
|
private LuckPermsSubjectData subjectData;
|
||||||
|
|
||||||
@Getter
|
|
||||||
private LuckPermsSubjectData transientSubjectData;
|
private LuckPermsSubjectData transientSubjectData;
|
||||||
|
|
||||||
private LuckPermsGroupSubject(Group group, LuckPermsService service) {
|
private LuckPermsGroupSubject(Group group, LuckPermsService service) {
|
||||||
@ -85,37 +80,32 @@ public class LuckPermsGroupSubject implements Subject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasPermission(@NonNull Set<Context> contexts, @NonNull String node) {
|
protected Tristate getPermissionValue(ContextSet contexts, String permission) {
|
||||||
return getPermissionValue(contexts, node).asBoolean();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Tristate getPermissionValue(@NonNull Set<Context> contexts, @NonNull String node) {
|
|
||||||
Map<String, Boolean> permissions = group.getAllNodesFiltered(service.calculateContexts(contexts)).stream()
|
Map<String, Boolean> permissions = group.getAllNodesFiltered(service.calculateContexts(contexts)).stream()
|
||||||
.map(LocalizedNode::getNode)
|
.map(LocalizedNode::getNode)
|
||||||
.collect(Collectors.toMap(Node::getPermission, Node::getValue));
|
.collect(Collectors.toMap(Node::getPermission, Node::getValue));
|
||||||
|
|
||||||
Tristate t = NodeTree.of(permissions).get(node);
|
Tristate t = NodeTree.of(permissions).get(permission);
|
||||||
if (t != Tristate.UNDEFINED) {
|
if (t != Tristate.UNDEFINED) {
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
t = service.getGroupSubjects().getDefaults().getPermissionValue(contexts, node);
|
t = service.getGroupSubjects().getDefaults().getPermissionValue(LuckPermsService.convertContexts(contexts), permission);
|
||||||
if (t != Tristate.UNDEFINED) {
|
if (t != Tristate.UNDEFINED) {
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
t = service.getDefaults().getPermissionValue(contexts, node);
|
t = service.getDefaults().getPermissionValue(LuckPermsService.convertContexts(contexts), permission);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isChildOf(@NonNull Set<Context> contexts, @NonNull Subject parent) {
|
public boolean isChildOf(ContextSet contexts, Subject parent) {
|
||||||
return parent instanceof LuckPermsGroupSubject && getPermissionValue(contexts, "group." + parent.getIdentifier()).asBoolean();
|
return parent instanceof LuckPermsGroupSubject && getPermissionValue(contexts, "group." + parent.getIdentifier()).asBoolean();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Subject> getParents(@NonNull Set<Context> contexts) {
|
public List<Subject> getParents(ContextSet contexts) {
|
||||||
List<Subject> subjects = group.getAllNodesFiltered(service.calculateContexts(contexts)).stream()
|
List<Subject> subjects = group.getAllNodesFiltered(service.calculateContexts(contexts)).stream()
|
||||||
.map(LocalizedNode::getNode)
|
.map(LocalizedNode::getNode)
|
||||||
.filter(Node::isGroupNode)
|
.filter(Node::isGroupNode)
|
||||||
@ -123,43 +113,43 @@ public class LuckPermsGroupSubject implements Subject {
|
|||||||
.map(s -> service.getGroupSubjects().get(s))
|
.map(s -> service.getGroupSubjects().get(s))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
subjects.addAll(service.getGroupSubjects().getDefaults().getParents(contexts));
|
subjects.addAll(service.getGroupSubjects().getDefaults().getParents(LuckPermsService.convertContexts(contexts)));
|
||||||
subjects.addAll(service.getDefaults().getParents(contexts));
|
subjects.addAll(service.getDefaults().getParents(LuckPermsService.convertContexts(contexts)));
|
||||||
|
|
||||||
return ImmutableList.copyOf(subjects);
|
return ImmutableList.copyOf(subjects);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<String> getOption(Set<Context> set, String s) {
|
public Optional<String> getOption(ContextSet contexts, String s) {
|
||||||
Optional<String> option;
|
Optional<String> option;
|
||||||
if (s.equalsIgnoreCase("prefix")) {
|
if (s.equalsIgnoreCase("prefix")) {
|
||||||
option = getChatMeta(set, true);
|
option = getChatMeta(contexts, true);
|
||||||
|
|
||||||
} else if (s.equalsIgnoreCase("suffix")) {
|
} else if (s.equalsIgnoreCase("suffix")) {
|
||||||
option = getChatMeta(set, false);
|
option = getChatMeta(contexts, false);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
option = getMeta(set, s);
|
option = getMeta(contexts, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (option.isPresent()) {
|
if (option.isPresent()) {
|
||||||
return option;
|
return option;
|
||||||
}
|
}
|
||||||
|
|
||||||
option = service.getGroupSubjects().getDefaults().getOption(set, s);
|
option = service.getGroupSubjects().getDefaults().getOption(LuckPermsService.convertContexts(contexts), s);
|
||||||
if (option.isPresent()) {
|
if (option.isPresent()) {
|
||||||
return option;
|
return option;
|
||||||
}
|
}
|
||||||
|
|
||||||
return service.getDefaults().getOption(set, s);
|
return service.getDefaults().getOption(LuckPermsService.convertContexts(contexts), s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Context> getActiveContexts() {
|
public ContextSet getActiveContextSet() {
|
||||||
return LuckPermsService.convertContexts(service.getPlugin().getContextManager().giveApplicableContext(this, MutableContextSet.empty()));
|
return service.getPlugin().getContextManager().getApplicableContext(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<String> getChatMeta(Set<Context> contexts, boolean prefix) {
|
private Optional<String> getChatMeta(ContextSet contexts, boolean prefix) {
|
||||||
int priority = Integer.MIN_VALUE;
|
int priority = Integer.MIN_VALUE;
|
||||||
String meta = null;
|
String meta = null;
|
||||||
|
|
||||||
@ -182,7 +172,7 @@ public class LuckPermsGroupSubject implements Subject {
|
|||||||
return meta == null ? Optional.empty() : Optional.of(unescapeCharacters(meta));
|
return meta == null ? Optional.empty() : Optional.of(unescapeCharacters(meta));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<String> getMeta(Set<Context> contexts, String key) {
|
private Optional<String> getMeta(ContextSet contexts, String key) {
|
||||||
for (Node n : group.getAllNodesFiltered(service.calculateContexts(contexts))) {
|
for (Node n : group.getAllNodesFiltered(service.calculateContexts(contexts))) {
|
||||||
if (!n.getValue()) {
|
if (!n.getValue()) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -140,9 +140,9 @@ public class LuckPermsService implements PermissionService {
|
|||||||
plugin.getContextManager().registerCalculator(new SpongeCalculatorLink(contextCalculator));
|
plugin.getContextManager().registerCalculator(new SpongeCalculatorLink(contextCalculator));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Contexts calculateContexts(Set<Context> contexts) {
|
public Contexts calculateContexts(ContextSet contextSet) {
|
||||||
return new Contexts(
|
return new Contexts(
|
||||||
LuckPermsService.convertContexts(contexts),
|
contextSet,
|
||||||
plugin.getConfiguration().isIncludingGlobalPerms(),
|
plugin.getConfiguration().isIncludingGlobalPerms(),
|
||||||
plugin.getConfiguration().isIncludingGlobalWorldPerms(),
|
plugin.getConfiguration().isIncludingGlobalWorldPerms(),
|
||||||
true,
|
true,
|
||||||
|
@ -0,0 +1,103 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
|
||||||
|
*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import lombok.NonNull;
|
||||||
|
import me.lucko.luckperms.api.context.ContextSet;
|
||||||
|
import org.spongepowered.api.service.context.Context;
|
||||||
|
import org.spongepowered.api.service.permission.Subject;
|
||||||
|
import org.spongepowered.api.util.Tristate;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public abstract class LuckPermsSubject implements Subject {
|
||||||
|
|
||||||
|
protected abstract Tristate getPermissionValue(ContextSet contexts, String permission);
|
||||||
|
protected abstract boolean isChildOf(ContextSet contexts, Subject parent);
|
||||||
|
protected abstract List<Subject> getParents(ContextSet contexts);
|
||||||
|
protected abstract Optional<String> getOption(ContextSet contexts, String s);
|
||||||
|
protected abstract ContextSet getActiveContextSet();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public Tristate getPermissionValue(@NonNull Set<Context> contexts, @NonNull String permission) {
|
||||||
|
return getPermissionValue(LuckPermsService.convertContexts(contexts), permission);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean hasPermission(@NonNull Set<Context> contexts, @NonNull String permission) {
|
||||||
|
return getPermissionValue(LuckPermsService.convertContexts(contexts), permission).asBoolean();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean hasPermission(@NonNull String permission) {
|
||||||
|
return getPermissionValue(getActiveContextSet(), permission).asBoolean();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean isChildOf(@NonNull Set<Context> contexts, @NonNull Subject parent) {
|
||||||
|
return isChildOf(LuckPermsService.convertContexts(contexts), parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean isChildOf(@NonNull Subject parent) {
|
||||||
|
return isChildOf(getActiveContextSet(), parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public List<Subject> getParents(@NonNull Set<Context> contexts) {
|
||||||
|
return getParents(LuckPermsService.convertContexts(contexts));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public List<Subject> getParents() {
|
||||||
|
return getParents(getActiveContextSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public Optional<String> getOption(@NonNull Set<Context> contexts, @NonNull String s) {
|
||||||
|
return getOption(LuckPermsService.convertContexts(contexts), s);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public Optional<String> getOption(@NonNull String key) {
|
||||||
|
return getOption(getActiveContextSet(), key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public Set<Context> getActiveContexts() {
|
||||||
|
return LuckPermsService.convertContexts(getActiveContextSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -23,40 +23,35 @@
|
|||||||
package me.lucko.luckperms.sponge.service;
|
package me.lucko.luckperms.sponge.service;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import lombok.AccessLevel;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NonNull;
|
|
||||||
import me.lucko.luckperms.api.caching.MetaData;
|
import me.lucko.luckperms.api.caching.MetaData;
|
||||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
import me.lucko.luckperms.api.context.ContextSet;
|
||||||
import me.lucko.luckperms.common.users.User;
|
import me.lucko.luckperms.common.users.User;
|
||||||
import org.spongepowered.api.Sponge;
|
import org.spongepowered.api.Sponge;
|
||||||
import org.spongepowered.api.command.CommandSource;
|
import org.spongepowered.api.command.CommandSource;
|
||||||
import org.spongepowered.api.entity.living.player.Player;
|
import org.spongepowered.api.entity.living.player.Player;
|
||||||
import org.spongepowered.api.service.context.Context;
|
|
||||||
import org.spongepowered.api.service.permission.Subject;
|
import org.spongepowered.api.service.permission.Subject;
|
||||||
import org.spongepowered.api.service.permission.SubjectCollection;
|
import org.spongepowered.api.service.permission.SubjectCollection;
|
||||||
import org.spongepowered.api.util.Tristate;
|
import org.spongepowered.api.util.Tristate;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@EqualsAndHashCode(of = "user")
|
@Getter
|
||||||
public class LuckPermsUserSubject implements Subject {
|
@EqualsAndHashCode(of = "user", callSuper = false)
|
||||||
|
public class LuckPermsUserSubject extends LuckPermsSubject {
|
||||||
public static LuckPermsUserSubject wrapUser(User user, LuckPermsService service) {
|
public static LuckPermsUserSubject wrapUser(User user, LuckPermsService service) {
|
||||||
return new LuckPermsUserSubject(user, service);
|
return new LuckPermsUserSubject(user, service);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Getter
|
@Getter(value = AccessLevel.NONE)
|
||||||
private User user;
|
|
||||||
|
|
||||||
private LuckPermsService service;
|
private LuckPermsService service;
|
||||||
|
|
||||||
@Getter
|
private User user;
|
||||||
private LuckPermsSubjectData subjectData;
|
private LuckPermsSubjectData subjectData;
|
||||||
|
|
||||||
@Getter
|
|
||||||
private LuckPermsSubjectData transientSubjectData;
|
private LuckPermsSubjectData transientSubjectData;
|
||||||
|
|
||||||
private LuckPermsUserSubject(User user, LuckPermsService service) {
|
private LuckPermsUserSubject(User user, LuckPermsService service) {
|
||||||
@ -102,26 +97,19 @@ public class LuckPermsUserSubject implements Subject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasPermission(Set<Context> contexts, String permission) {
|
protected Tristate getPermissionValue(ContextSet contexts, String permission) {
|
||||||
return getPermissionValue(contexts, permission).asBoolean();
|
return !hasData() ?
|
||||||
|
Tristate.UNDEFINED :
|
||||||
|
LuckPermsService.convertTristate(user.getUserData().getPermissionData(service.calculateContexts(contexts)).getPermissionValue(permission));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Tristate getPermissionValue(@NonNull Set<Context> contexts, @NonNull String permission) {
|
protected boolean isChildOf(ContextSet contexts, Subject parent) {
|
||||||
if (hasData()) {
|
|
||||||
return LuckPermsService.convertTristate(user.getUserData().getPermissionData(service.calculateContexts(contexts)).getPermissionValue(permission));
|
|
||||||
}
|
|
||||||
|
|
||||||
return Tristate.UNDEFINED;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isChildOf(@NonNull Set<Context> contexts, @NonNull Subject parent) {
|
|
||||||
return parent instanceof LuckPermsGroupSubject && getPermissionValue(contexts, "group." + parent.getIdentifier()).asBoolean();
|
return parent instanceof LuckPermsGroupSubject && getPermissionValue(contexts, "group." + parent.getIdentifier()).asBoolean();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Subject> getParents(Set<Context> contexts) {
|
protected List<Subject> getParents(ContextSet contexts) {
|
||||||
ImmutableList.Builder<Subject> subjects = ImmutableList.builder();
|
ImmutableList.Builder<Subject> subjects = ImmutableList.builder();
|
||||||
|
|
||||||
if (hasData()) {
|
if (hasData()) {
|
||||||
@ -137,14 +125,14 @@ public class LuckPermsUserSubject implements Subject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
subjects.addAll(service.getUserSubjects().getDefaults().getParents(contexts));
|
subjects.addAll(service.getUserSubjects().getDefaults().getParents(LuckPermsService.convertContexts(contexts)));
|
||||||
subjects.addAll(service.getDefaults().getParents(contexts));
|
subjects.addAll(service.getDefaults().getParents(LuckPermsService.convertContexts(contexts)));
|
||||||
|
|
||||||
return subjects.build();
|
return subjects.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<String> getOption(Set<Context> contexts, String s) {
|
protected Optional<String> getOption(ContextSet contexts, String s) {
|
||||||
if (hasData()) {
|
if (hasData()) {
|
||||||
MetaData data = user.getUserData().getMetaData(service.calculateContexts(contexts));
|
MetaData data = user.getUserData().getMetaData(service.calculateContexts(contexts));
|
||||||
if (s.equalsIgnoreCase("prefix")) {
|
if (s.equalsIgnoreCase("prefix")) {
|
||||||
@ -164,16 +152,16 @@ public class LuckPermsUserSubject implements Subject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<String> v = service.getUserSubjects().getDefaults().getOption(contexts, s);
|
Optional<String> v = service.getUserSubjects().getDefaults().getOption(LuckPermsService.convertContexts(contexts), s);
|
||||||
if (v.isPresent()) {
|
if (v.isPresent()) {
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
return service.getDefaults().getOption(contexts, s);
|
return service.getDefaults().getOption(LuckPermsService.convertContexts(contexts), s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Context> getActiveContexts() {
|
protected ContextSet getActiveContextSet() {
|
||||||
return LuckPermsService.convertContexts(service.getPlugin().getContextManager().giveApplicableContext(this, MutableContextSet.empty()));
|
return service.getPlugin().getContextManager().getApplicableContext(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,6 @@ package me.lucko.luckperms.sponge.service.persisted;
|
|||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
|
||||||
import me.lucko.luckperms.common.utils.BufferedRequest;
|
import me.lucko.luckperms.common.utils.BufferedRequest;
|
||||||
import me.lucko.luckperms.sponge.service.LuckPermsService;
|
import me.lucko.luckperms.sponge.service.LuckPermsService;
|
||||||
import org.spongepowered.api.command.CommandSource;
|
import org.spongepowered.api.command.CommandSource;
|
||||||
@ -186,6 +185,6 @@ public class PersistedSubject implements Subject {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Context> getActiveContexts() {
|
public Set<Context> getActiveContexts() {
|
||||||
return LuckPermsService.convertContexts(service.getPlugin().getContextManager().giveApplicableContext(this, MutableContextSet.empty()));
|
return LuckPermsService.convertContexts(service.getPlugin().getContextManager().getApplicableContext(this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,6 @@ package me.lucko.luckperms.sponge.service.simple;
|
|||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
|
||||||
import me.lucko.luckperms.sponge.service.LuckPermsService;
|
import me.lucko.luckperms.sponge.service.LuckPermsService;
|
||||||
import org.spongepowered.api.command.CommandSource;
|
import org.spongepowered.api.command.CommandSource;
|
||||||
import org.spongepowered.api.service.context.Context;
|
import org.spongepowered.api.service.context.Context;
|
||||||
@ -152,6 +151,6 @@ public class SimpleSubject implements Subject {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<Context> getActiveContexts() {
|
public Set<Context> getActiveContexts() {
|
||||||
return LuckPermsService.convertContexts(service.getPlugin().getContextManager().giveApplicableContext(this, MutableContextSet.empty()));
|
return LuckPermsService.convertContexts(service.getPlugin().getContextManager().getApplicableContext(this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user