Proper context implementation - WIP
This commit is contained in:
@@ -30,9 +30,13 @@ import me.lucko.luckperms.api.implementation.ApiProvider;
|
||||
import me.lucko.luckperms.api.sponge.LuckPermsService;
|
||||
import me.lucko.luckperms.commands.ConsecutiveExecutor;
|
||||
import me.lucko.luckperms.commands.Sender;
|
||||
import me.lucko.luckperms.config.LPConfiguration;
|
||||
import me.lucko.luckperms.constants.Message;
|
||||
import me.lucko.luckperms.constants.Permission;
|
||||
import me.lucko.luckperms.core.LPConfiguration;
|
||||
import me.lucko.luckperms.contexts.ContextManager;
|
||||
import me.lucko.luckperms.contexts.ContextUpdateTask;
|
||||
import me.lucko.luckperms.contexts.ServerCalculator;
|
||||
import me.lucko.luckperms.contexts.WorldCalculator;
|
||||
import me.lucko.luckperms.core.UuidCache;
|
||||
import me.lucko.luckperms.data.Importer;
|
||||
import me.lucko.luckperms.groups.GroupManager;
|
||||
@@ -42,7 +46,6 @@ import me.lucko.luckperms.storage.Datastore;
|
||||
import me.lucko.luckperms.storage.StorageFactory;
|
||||
import me.lucko.luckperms.tracks.TrackManager;
|
||||
import me.lucko.luckperms.users.SpongeUserManager;
|
||||
import me.lucko.luckperms.users.UserManager;
|
||||
import me.lucko.luckperms.utils.LocaleManager;
|
||||
import me.lucko.luckperms.utils.LogFactory;
|
||||
import org.slf4j.Logger;
|
||||
@@ -86,7 +89,7 @@ public class LPSpongePlugin implements LuckPermsPlugin {
|
||||
|
||||
private final Set<UUID> ignoringLogs = ConcurrentHashMap.newKeySet();
|
||||
private LPConfiguration configuration;
|
||||
private UserManager userManager;
|
||||
private SpongeUserManager userManager;
|
||||
private GroupManager groupManager;
|
||||
private TrackManager trackManager;
|
||||
private Datastore datastore;
|
||||
@@ -97,6 +100,7 @@ public class LPSpongePlugin implements LuckPermsPlugin {
|
||||
private ConsecutiveExecutor consecutiveExecutor;
|
||||
private LuckPermsService service;
|
||||
private LocaleManager localeManager;
|
||||
private ContextManager<Player> contextManager;
|
||||
|
||||
@Listener
|
||||
public void onEnable(GamePreInitializationEvent event) {
|
||||
@@ -135,6 +139,11 @@ public class LPSpongePlugin implements LuckPermsPlugin {
|
||||
importer = new Importer(commandManager);
|
||||
consecutiveExecutor = new ConsecutiveExecutor(commandManager);
|
||||
|
||||
contextManager = new ContextManager<>();
|
||||
contextManager.registerCalculator(new ServerCalculator<>(getConfiguration().getServer()));
|
||||
contextManager.registerCalculator(new WorldCalculator());
|
||||
contextManager.registerListener(userManager);
|
||||
|
||||
getLog().info("Registering PermissionService...");
|
||||
Sponge.getServiceManager().setProvider(this, PermissionService.class, (service = new LuckPermsService(this)));
|
||||
|
||||
@@ -155,6 +164,7 @@ public class LPSpongePlugin implements LuckPermsPlugin {
|
||||
scheduler.createTaskBuilder().intervalTicks(1L).execute(SpongeSenderFactory.get(this)).submit(this);
|
||||
scheduler.createTaskBuilder().async().intervalTicks(60L).execute(new ExpireTemporaryTask(this)).submit(this);
|
||||
scheduler.createTaskBuilder().async().intervalTicks(20L).execute(consecutiveExecutor).submit(this);
|
||||
scheduler.createTaskBuilder().async().intervalTicks(600L).execute(new ContextUpdateTask(service.getUserSubjects())).submit(this);
|
||||
|
||||
getLog().info("Successfully loaded.");
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
package me.lucko.luckperms;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
import me.lucko.luckperms.core.AbstractConfiguration;
|
||||
import me.lucko.luckperms.config.AbstractConfiguration;
|
||||
import ninja.leaping.configurate.ConfigurationNode;
|
||||
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
|
||||
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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.api.sponge;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import me.lucko.luckperms.api.Tristate;
|
||||
import me.lucko.luckperms.calculators.PermissionCalculator;
|
||||
import me.lucko.luckperms.calculators.PermissionProcessor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class ContextData {
|
||||
private final LuckPermsUserSubject parent;
|
||||
|
||||
@Getter
|
||||
private final Map<String, String> context;
|
||||
|
||||
@Getter
|
||||
private final Map<String, Boolean> permissionCache = new ConcurrentHashMap<>();
|
||||
|
||||
private final PermissionCalculator calculator;
|
||||
|
||||
public ContextData(LuckPermsUserSubject parent, Map<String, String> context, LuckPermsService service) {
|
||||
this.parent = parent;
|
||||
this.context = context;
|
||||
|
||||
List<PermissionProcessor> processors = new ArrayList<>(5);
|
||||
processors.add(new PermissionCalculator.MapProcessor(permissionCache));
|
||||
if (service.getPlugin().getConfiguration().isApplyingWildcards()) {
|
||||
processors.add(new LuckPermsUserSubject.SpongeWildcardProcessor(permissionCache));
|
||||
processors.add(new PermissionCalculator.WildcardProcessor(permissionCache));
|
||||
}
|
||||
if (service.getPlugin().getConfiguration().isApplyingRegex()) {
|
||||
processors.add(new PermissionCalculator.RegexProcessor(permissionCache));
|
||||
}
|
||||
processors.add(new LuckPermsUserSubject.SpongeDefaultsProcessor(service));
|
||||
|
||||
calculator = new PermissionCalculator(service.getPlugin(), parent.getUser().getName(), service.getPlugin().getConfiguration().isDebugPermissionChecks(), processors);
|
||||
}
|
||||
|
||||
public void invalidateCache() {
|
||||
calculator.invalidateCache();
|
||||
}
|
||||
|
||||
public Tristate getPermissionValue(@NonNull String permission) {
|
||||
me.lucko.luckperms.api.Tristate t = calculator.getPermissionValue(permission);
|
||||
if (t != me.lucko.luckperms.api.Tristate.UNDEFINED) {
|
||||
return Tristate.fromBoolean(t.asBoolean());
|
||||
} else {
|
||||
return Tristate.UNDEFINED;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,6 +29,7 @@ import me.lucko.luckperms.LPSpongePlugin;
|
||||
import me.lucko.luckperms.api.sponge.collections.GroupCollection;
|
||||
import me.lucko.luckperms.api.sponge.collections.UserCollection;
|
||||
import me.lucko.luckperms.api.sponge.simple.SimpleCollection;
|
||||
import me.lucko.luckperms.contexts.SpongeCalculatorLink;
|
||||
import org.spongepowered.api.plugin.PluginContainer;
|
||||
import org.spongepowered.api.service.context.ContextCalculator;
|
||||
import org.spongepowered.api.service.permission.*;
|
||||
@@ -54,7 +55,6 @@ public class LuckPermsService implements PermissionService {
|
||||
private final Set<PermissionDescription> descriptionSet;
|
||||
|
||||
private final Map<String, SubjectCollection> subjects;
|
||||
private final Set<ContextCalculator<Subject>> contextCalculators; // TODO actually use context calculators, idk...
|
||||
|
||||
public LuckPermsService(LPSpongePlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
@@ -67,7 +67,6 @@ public class LuckPermsService implements PermissionService {
|
||||
subjects.put(PermissionService.SUBJECTS_GROUP, groupSubjects);
|
||||
|
||||
descriptionSet = ConcurrentHashMap.newKeySet();
|
||||
contextCalculators = ConcurrentHashMap.newKeySet();
|
||||
}
|
||||
|
||||
public SubjectData getDefaultData() {
|
||||
@@ -121,7 +120,7 @@ public class LuckPermsService implements PermissionService {
|
||||
|
||||
@Override
|
||||
public void registerContextCalculator(@NonNull ContextCalculator<Subject> contextCalculator) {
|
||||
contextCalculators.add(contextCalculator);
|
||||
plugin.getContextManager().registerCalculator(new SpongeCalculatorLink(contextCalculator));
|
||||
}
|
||||
|
||||
@RequiredArgsConstructor
|
||||
|
||||
@@ -30,12 +30,12 @@ import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import me.lucko.luckperms.api.Node;
|
||||
import me.lucko.luckperms.api.data.Callback;
|
||||
import me.lucko.luckperms.contexts.Contexts;
|
||||
import me.lucko.luckperms.core.PermissionHolder;
|
||||
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
|
||||
import me.lucko.luckperms.exceptions.ObjectLacksException;
|
||||
import me.lucko.luckperms.groups.Group;
|
||||
import me.lucko.luckperms.users.User;
|
||||
import me.lucko.luckperms.utils.Contexts;
|
||||
import org.spongepowered.api.command.CommandSource;
|
||||
import org.spongepowered.api.service.context.Context;
|
||||
import org.spongepowered.api.service.permission.Subject;
|
||||
@@ -124,7 +124,7 @@ public class LuckPermsSubject implements Subject {
|
||||
context.put(c.getKey(), c.getValue());
|
||||
}
|
||||
|
||||
switch (holder.inheritsPermission(new me.lucko.luckperms.utils.Node.Builder(node).withExtraContext(context).build())) {
|
||||
switch (holder.inheritsPermission(new me.lucko.luckperms.core.Node.Builder(node).withExtraContext(context).build())) {
|
||||
case UNDEFINED:
|
||||
return Tristate.UNDEFINED;
|
||||
case TRUE:
|
||||
@@ -277,7 +277,7 @@ public class LuckPermsSubject implements Subject {
|
||||
public boolean setPermission(Set<Context> set, String s, Tristate tristate) {
|
||||
if (tristate == Tristate.UNDEFINED) {
|
||||
// Unset
|
||||
Node.Builder builder = new me.lucko.luckperms.utils.Node.Builder(s);
|
||||
Node.Builder builder = new me.lucko.luckperms.core.Node.Builder(s);
|
||||
|
||||
for (Context ct : set) {
|
||||
builder.withExtraContext(ct.getKey(), ct.getValue());
|
||||
@@ -290,7 +290,7 @@ public class LuckPermsSubject implements Subject {
|
||||
return true;
|
||||
}
|
||||
|
||||
Node.Builder builder = new me.lucko.luckperms.utils.Node.Builder(s)
|
||||
Node.Builder builder = new me.lucko.luckperms.core.Node.Builder(s)
|
||||
.setValue(tristate.asBoolean());
|
||||
|
||||
for (Context ct : set) {
|
||||
@@ -384,7 +384,7 @@ public class LuckPermsSubject implements Subject {
|
||||
Map<String, String> contexts = set.stream().collect(Collectors.toMap(Context::getKey, Context::getValue));
|
||||
|
||||
try {
|
||||
holder.setPermission(new me.lucko.luckperms.utils.Node.Builder("group." + permsSubject.getIdentifier())
|
||||
holder.setPermission(new me.lucko.luckperms.core.Node.Builder("group." + permsSubject.getIdentifier())
|
||||
.withExtraContext(contexts)
|
||||
.build());
|
||||
} catch (ObjectAlreadyHasException ignored) {}
|
||||
@@ -403,7 +403,7 @@ public class LuckPermsSubject implements Subject {
|
||||
Map<String, String> contexts = set.stream().collect(Collectors.toMap(Context::getKey, Context::getValue));
|
||||
|
||||
try {
|
||||
holder.unsetPermission(new me.lucko.luckperms.utils.Node.Builder("group." + permsSubject.getIdentifier())
|
||||
holder.unsetPermission(new me.lucko.luckperms.core.Node.Builder("group." + permsSubject.getIdentifier())
|
||||
.withExtraContext(contexts)
|
||||
.build());
|
||||
} catch (ObjectLacksException ignored) {}
|
||||
@@ -514,7 +514,7 @@ public class LuckPermsSubject implements Subject {
|
||||
value = escapeCharacters(value);
|
||||
|
||||
try {
|
||||
holder.setPermission(new me.lucko.luckperms.utils.Node.Builder("meta." + key + "." + value)
|
||||
holder.setPermission(new me.lucko.luckperms.core.Node.Builder("meta." + key + "." + value)
|
||||
.withExtraContext(context)
|
||||
.build()
|
||||
);
|
||||
@@ -613,7 +613,7 @@ public class LuckPermsSubject implements Subject {
|
||||
if (tristate == Tristate.UNDEFINED) {
|
||||
// Unset
|
||||
|
||||
Node.Builder builder = new me.lucko.luckperms.utils.Node.Builder(s);
|
||||
Node.Builder builder = new me.lucko.luckperms.core.Node.Builder(s);
|
||||
|
||||
for (Context ct : set) {
|
||||
builder.withExtraContext(ct.getKey(), ct.getValue());
|
||||
@@ -625,7 +625,7 @@ public class LuckPermsSubject implements Subject {
|
||||
return true;
|
||||
}
|
||||
|
||||
Node.Builder builder = new me.lucko.luckperms.utils.Node.Builder(s)
|
||||
Node.Builder builder = new me.lucko.luckperms.core.Node.Builder(s)
|
||||
.setValue(tristate.asBoolean());
|
||||
|
||||
for (Context ct : set) {
|
||||
@@ -709,7 +709,7 @@ public class LuckPermsSubject implements Subject {
|
||||
Map<String, String> contexts = set.stream().collect(Collectors.toMap(Context::getKey, Context::getValue));
|
||||
|
||||
try {
|
||||
holder.setTransientPermission(new me.lucko.luckperms.utils.Node.Builder("group." + permsSubject.getIdentifier())
|
||||
holder.setTransientPermission(new me.lucko.luckperms.core.Node.Builder("group." + permsSubject.getIdentifier())
|
||||
.withExtraContext(contexts)
|
||||
.build());
|
||||
} catch (ObjectAlreadyHasException ignored) {}
|
||||
@@ -727,7 +727,7 @@ public class LuckPermsSubject implements Subject {
|
||||
Map<String, String> contexts = set.stream().collect(Collectors.toMap(Context::getKey, Context::getValue));
|
||||
|
||||
try {
|
||||
holder.unsetTransientPermission(new me.lucko.luckperms.utils.Node.Builder("group." + permsSubject.getIdentifier())
|
||||
holder.unsetTransientPermission(new me.lucko.luckperms.core.Node.Builder("group." + permsSubject.getIdentifier())
|
||||
.withExtraContext(contexts)
|
||||
.build());
|
||||
} catch (ObjectLacksException ignored) {}
|
||||
@@ -827,7 +827,7 @@ public class LuckPermsSubject implements Subject {
|
||||
value = escapeCharacters(value);
|
||||
|
||||
try {
|
||||
holder.setTransientPermission(new me.lucko.luckperms.utils.Node.Builder("meta." + key + "." + value)
|
||||
holder.setTransientPermission(new me.lucko.luckperms.core.Node.Builder("meta." + key + "." + value)
|
||||
.withExtraContext(context)
|
||||
.build()
|
||||
);
|
||||
|
||||
@@ -25,17 +25,21 @@ package me.lucko.luckperms.api.sponge;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import me.lucko.luckperms.api.event.events.UserPermissionRefreshEvent;
|
||||
import me.lucko.luckperms.api.implementation.internal.UserLink;
|
||||
import me.lucko.luckperms.calculators.PermissionProcessor;
|
||||
import me.lucko.luckperms.contexts.Contexts;
|
||||
import me.lucko.luckperms.users.User;
|
||||
import me.lucko.luckperms.utils.PermissionCalculator;
|
||||
import me.lucko.luckperms.utils.PermissionProcessor;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.command.CommandSource;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
import org.spongepowered.api.service.context.Context;
|
||||
import org.spongepowered.api.service.permission.SubjectData;
|
||||
import org.spongepowered.api.util.Tristate;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class LuckPermsUserSubject extends LuckPermsSubject {
|
||||
public static LuckPermsUserSubject wrapUser(User user, LuckPermsService service) {
|
||||
@@ -45,37 +49,22 @@ public class LuckPermsUserSubject extends LuckPermsSubject {
|
||||
@Getter
|
||||
private final User user;
|
||||
|
||||
private final PermissionCalculator calculator;
|
||||
|
||||
@Getter
|
||||
private final Map<String, Boolean> permissionCache = new ConcurrentHashMap<>();
|
||||
private final Map<Map<String, String>, ContextData> contextData;
|
||||
|
||||
private LuckPermsUserSubject(User user, LuckPermsService service) {
|
||||
super(user, service);
|
||||
this.user = user;
|
||||
|
||||
List<PermissionProcessor> processors = new ArrayList<>(5);
|
||||
processors.add(new PermissionCalculator.MapProcessor(permissionCache));
|
||||
if (service.getPlugin().getConfiguration().isApplyingWildcards()) {
|
||||
processors.add(new SpongeWildcardProcessor(permissionCache));
|
||||
processors.add(new PermissionCalculator.WildcardProcessor(permissionCache));
|
||||
}
|
||||
if (service.getPlugin().getConfiguration().isApplyingRegex()) {
|
||||
processors.add(new PermissionCalculator.RegexProcessor(permissionCache));
|
||||
}
|
||||
processors.add(new SpongeDefaultsProcessor(service));
|
||||
|
||||
calculator = new PermissionCalculator(service.getPlugin(), user.getName(), service.getPlugin().getConfiguration().isDebugPermissionChecks(), processors);
|
||||
contextData = new ConcurrentHashMap<>();
|
||||
}
|
||||
|
||||
public void invalidateCache() {
|
||||
calculator.invalidateCache();
|
||||
}
|
||||
|
||||
// TODO don't ignore context
|
||||
@Override
|
||||
public Tristate getPermissionValue(@NonNull Set<Context> contexts, @NonNull String permission) {
|
||||
me.lucko.luckperms.api.Tristate t = calculator.getPermissionValue(permission);
|
||||
Map<String, String> context = contexts.stream().collect(Collectors.toMap(Context::getKey, Context::getValue));
|
||||
ContextData cd = contextData.computeIfAbsent(context, this::calculatePermissions);
|
||||
|
||||
me.lucko.luckperms.api.Tristate t = cd.getPermissionValue(permission);
|
||||
if (t != me.lucko.luckperms.api.Tristate.UNDEFINED) {
|
||||
return Tristate.fromBoolean(t.asBoolean());
|
||||
} else {
|
||||
@@ -83,6 +72,57 @@ public class LuckPermsUserSubject extends LuckPermsSubject {
|
||||
}
|
||||
}
|
||||
|
||||
public ContextData calculatePermissions(Map<String, String> context) {
|
||||
Map<String, Boolean> toApply = user.exportNodes(
|
||||
new Contexts(
|
||||
context,
|
||||
service.getPlugin().getConfiguration().isIncludingGlobalPerms(),
|
||||
service.getPlugin().getConfiguration().isIncludingGlobalWorldPerms(),
|
||||
true,
|
||||
service.getPlugin().getConfiguration().isApplyingGlobalGroups(),
|
||||
service.getPlugin().getConfiguration().isApplyingGlobalWorldGroups()
|
||||
),
|
||||
Collections.emptyList()
|
||||
);
|
||||
|
||||
ContextData existing = contextData.get(context);
|
||||
if (existing == null) {
|
||||
existing = new ContextData(this, context, service);
|
||||
contextData.put(context, existing);
|
||||
}
|
||||
|
||||
boolean different = false;
|
||||
if (toApply.size() != existing.getPermissionCache().size()) {
|
||||
different = true;
|
||||
} else {
|
||||
for (Map.Entry<String, Boolean> e : existing.getPermissionCache().entrySet()) {
|
||||
if (toApply.containsKey(e.getKey()) && toApply.get(e.getKey()) == e.getValue()) {
|
||||
continue;
|
||||
}
|
||||
different = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!different) return existing;
|
||||
|
||||
existing.getPermissionCache().clear();
|
||||
existing.invalidateCache();
|
||||
existing.getPermissionCache().putAll(toApply);
|
||||
service.getPlugin().getApiProvider().fireEventAsync(new UserPermissionRefreshEvent(new UserLink(user)));
|
||||
return existing;
|
||||
}
|
||||
|
||||
public void calculatePermissions(Set<Context> contexts) {
|
||||
Map<String, String> context = contexts.stream().collect(Collectors.toMap(Context::getKey, Context::getValue));
|
||||
calculatePermissions(context);
|
||||
}
|
||||
|
||||
public void calculateActivePermissions() {
|
||||
calculatePermissions(getActiveContexts());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getIdentifier() {
|
||||
return service.getPlugin().getUuidCache().getExternalUUID(user.getUuid()).toString();
|
||||
@@ -100,8 +140,22 @@ public class LuckPermsUserSubject extends LuckPermsSubject {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Context> getActiveContexts() {
|
||||
final UUID uuid = service.getPlugin().getUuidCache().getExternalUUID(user.getUuid());
|
||||
Optional<Player> player = Sponge.getServer().getPlayer(uuid);
|
||||
|
||||
if (!player.isPresent()) {
|
||||
return SubjectData.GLOBAL_CONTEXT;
|
||||
}
|
||||
|
||||
Map<String, String> context = new HashMap<>();
|
||||
service.getPlugin().getContextManager().giveApplicableContext(player.get(), context);
|
||||
return context.entrySet().stream().map(e -> new Context(e.getKey(), e.getValue())).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@AllArgsConstructor
|
||||
private static class SpongeWildcardProcessor implements PermissionProcessor {
|
||||
static class SpongeWildcardProcessor implements PermissionProcessor {
|
||||
|
||||
@Getter
|
||||
private final Map<String, Boolean> map;
|
||||
@@ -152,7 +206,7 @@ public class LuckPermsUserSubject extends LuckPermsSubject {
|
||||
}
|
||||
|
||||
@AllArgsConstructor
|
||||
private static class SpongeDefaultsProcessor implements PermissionProcessor {
|
||||
static class SpongeDefaultsProcessor implements PermissionProcessor {
|
||||
private final LuckPermsService service;
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.contexts;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import me.lucko.luckperms.api.sponge.LuckPermsUserSubject;
|
||||
import me.lucko.luckperms.api.sponge.collections.UserCollection;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class ContextUpdateTask implements Runnable {
|
||||
private final UserCollection userCollection;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (LuckPermsUserSubject subject : userCollection.getUsers().values()) {
|
||||
Set<Map<String, String>> contexts = new HashSet<>(subject.getContextData().keySet());
|
||||
contexts.forEach(subject::calculatePermissions);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.contexts;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import me.lucko.luckperms.api.context.ContextCalculator;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
import org.spongepowered.api.service.context.Context;
|
||||
import org.spongepowered.api.service.permission.Subject;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class SpongeCalculatorLink extends ContextCalculator<Player> {
|
||||
private final org.spongepowered.api.service.context.ContextCalculator<Subject> calculator;
|
||||
|
||||
@Override
|
||||
public Map<String, String> giveApplicableContext(Player subject, Map<String, String> accumulator) {
|
||||
Set<Context> contexts = accumulator.entrySet().stream().map(e -> new Context(e.getKey(), e.getValue())).collect(Collectors.toSet());
|
||||
calculator.accumulateContexts(subject, contexts);
|
||||
|
||||
contexts.forEach(c -> accumulator.put(c.getKey(), c.getValue()));
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isContextApplicable(Player subject, Map.Entry<String, String> context) {
|
||||
Context c = new Context(context.getKey(), context.getValue());
|
||||
return calculator.matches(c, subject);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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.contexts;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.lucko.luckperms.api.context.ContextCalculator;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
import org.spongepowered.api.service.context.Context;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class WorldCalculator extends ContextCalculator<Player> {
|
||||
|
||||
@Override
|
||||
public Map<String, String> giveApplicableContext(Player subject, Map<String, String> accumulator) {
|
||||
accumulator.put(Context.WORLD_KEY, subject.getWorld().getName());
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isContextApplicable(Player subject, Map.Entry<String, String> context) {
|
||||
return context.getKey().equals(Context.WORLD_KEY) && subject.getWorld().getName().equals(context.getValue());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,14 +23,9 @@
|
||||
package me.lucko.luckperms.users;
|
||||
|
||||
import me.lucko.luckperms.LPSpongePlugin;
|
||||
import me.lucko.luckperms.api.event.events.UserPermissionRefreshEvent;
|
||||
import me.lucko.luckperms.api.implementation.internal.UserLink;
|
||||
import me.lucko.luckperms.api.sponge.LuckPermsUserSubject;
|
||||
import me.lucko.luckperms.api.sponge.collections.UserCollection;
|
||||
import me.lucko.luckperms.utils.Contexts;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
class SpongeUser extends User {
|
||||
@@ -53,47 +48,7 @@ class SpongeUser extends User {
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate the permissions that should be applied. This is done async, who cares about how long it takes or how often it's done.
|
||||
Map<String, Boolean> toApply = exportNodes(
|
||||
new Contexts(
|
||||
plugin.getConfiguration().getServer(),
|
||||
null, // TODO per world perms
|
||||
null,
|
||||
plugin.getConfiguration().isIncludingGlobalPerms(),
|
||||
plugin.getConfiguration().isIncludingGlobalWorldPerms(),
|
||||
true,
|
||||
plugin.getConfiguration().isApplyingGlobalGroups(),
|
||||
plugin.getConfiguration().isApplyingGlobalWorldGroups()
|
||||
),
|
||||
Collections.emptyList()
|
||||
);
|
||||
|
||||
try {
|
||||
LuckPermsUserSubject us = uc.getUsers().get(getUuid());
|
||||
Map<String, Boolean> existing = us.getPermissionCache();
|
||||
|
||||
boolean different = false;
|
||||
if (toApply.size() != existing.size()) {
|
||||
different = true;
|
||||
} else {
|
||||
for (Map.Entry<String, Boolean> e : existing.entrySet()) {
|
||||
if (toApply.containsKey(e.getKey()) && toApply.get(e.getKey()) == e.getValue()) {
|
||||
continue;
|
||||
}
|
||||
different = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!different) return;
|
||||
|
||||
existing.clear();
|
||||
us.invalidateCache();
|
||||
existing.putAll(toApply);
|
||||
|
||||
plugin.getApiProvider().fireEventAsync(new UserPermissionRefreshEvent(new UserLink(this)));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
LuckPermsUserSubject us = uc.getUsers().get(getUuid());
|
||||
us.calculateActivePermissions();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,10 +23,13 @@
|
||||
package me.lucko.luckperms.users;
|
||||
|
||||
import me.lucko.luckperms.LPSpongePlugin;
|
||||
import me.lucko.luckperms.api.context.ContextListener;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SpongeUserManager extends UserManager {
|
||||
public class SpongeUserManager extends UserManager implements ContextListener<Player> {
|
||||
private final LPSpongePlugin plugin;
|
||||
|
||||
public SpongeUserManager(LPSpongePlugin plugin) {
|
||||
@@ -58,4 +61,14 @@ public class SpongeUserManager extends UserManager {
|
||||
.map(p -> plugin.getUuidCache().getUUID(p.getUniqueId()))
|
||||
.forEach(u -> plugin.getDatastore().loadUser(u, "null"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onContextChange(Player subject, Map.Entry<String, String> before, Map.Entry<String, String> current) throws Exception {
|
||||
UUID internal = plugin.getUuidCache().getUUID(subject.getUniqueId());
|
||||
|
||||
User user = get(internal);
|
||||
if (user != null) {
|
||||
plugin.doAsync(user::refreshPermissions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user