diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitSenderFactory.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitSenderFactory.java index 198029925..a473cfd5c 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitSenderFactory.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitSenderFactory.java @@ -37,8 +37,9 @@ import me.lucko.luckperms.common.utils.TextUtils; import net.kyori.text.Component; import net.kyori.text.serializer.ComponentSerializers; -import org.bukkit.command.BlockCommandSender; import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.command.RemoteConsoleCommandSender; import org.bukkit.entity.Player; import java.util.UUID; @@ -71,13 +72,14 @@ public class BukkitSenderFactory extends SenderFactory { @Override protected void sendMessage(CommandSender sender, String s) { - // send sync if command block - if (sender instanceof BlockCommandSender) { - getPlugin().getScheduler().doSync(new BlockMessengerAgent(((BlockCommandSender) sender), s)); + // we can safely send async for players and the console + if (sender instanceof Player || sender instanceof ConsoleCommandSender || sender instanceof RemoteConsoleCommandSender) { + sender.sendMessage(s); return; } - sender.sendMessage(s); + // otherwise, send the message sync + getPlugin().getScheduler().doSync(new SyncMessengerAgent(sender, s)); } @Override @@ -123,18 +125,18 @@ public class BukkitSenderFactory extends SenderFactory { } } - private static final class BlockMessengerAgent implements Runnable { - private final BlockCommandSender block; + private static final class SyncMessengerAgent implements Runnable { + private final CommandSender sender; private final String message; - private BlockMessengerAgent(BlockCommandSender block, String message) { - this.block = block; + private SyncMessengerAgent(CommandSender sender, String message) { + this.sender = sender; this.message = message; } @Override public void run() { - this.block.sendMessage(this.message); + this.sender.sendMessage(this.message); } } diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/BukkitMigrationUtils.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/BukkitMigrationUtils.java index 8e98fc50d..0fed3435d 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/BukkitMigrationUtils.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/BukkitMigrationUtils.java @@ -25,8 +25,8 @@ package me.lucko.luckperms.bukkit.migration; -import me.lucko.luckperms.common.commands.utils.CommandUtils; import me.lucko.luckperms.common.logging.ProgressLogger; +import me.lucko.luckperms.common.utils.Uuids; import org.bukkit.Bukkit; @@ -36,7 +36,7 @@ public final class BukkitMigrationUtils { @SuppressWarnings("deprecation") public static UUID lookupUuid(ProgressLogger log, String s) { - UUID uuid = CommandUtils.parseUuid(s); + UUID uuid = Uuids.parseNullable(s); if (uuid == null) { try { uuid = Bukkit.getOfflinePlayer(s).getUniqueId(); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/log/LogRecent.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/log/LogRecent.java index a7c4188b6..ea821584d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/log/LogRecent.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/log/LogRecent.java @@ -31,7 +31,6 @@ import me.lucko.luckperms.common.commands.CommandPermission; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; import me.lucko.luckperms.common.commands.sender.Sender; -import me.lucko.luckperms.common.commands.utils.CommandUtils; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.locale.CommandSpec; import me.lucko.luckperms.common.locale.LocaleManager; @@ -40,6 +39,7 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.storage.DataConstraints; import me.lucko.luckperms.common.utils.DateUtil; import me.lucko.luckperms.common.utils.Predicates; +import me.lucko.luckperms.common.utils.Uuids; import java.util.List; import java.util.Map; @@ -72,7 +72,7 @@ public class LogRecent extends SubCommand { // User and possibly page final String target = args.get(0); - UUID uuid = CommandUtils.parseUuid(target.toLowerCase()); + UUID uuid = Uuids.parseNullable(target); if (uuid == null) { if (!plugin.getConfiguration().get(ConfigKeys.ALLOW_INVALID_USERNAMES)) { if (!DataConstraints.PLAYER_USERNAME_TEST.test(target)) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/log/LogUserHistory.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/log/LogUserHistory.java index 8bef54816..9ac2d5883 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/log/LogUserHistory.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/log/LogUserHistory.java @@ -31,7 +31,6 @@ import me.lucko.luckperms.common.commands.CommandPermission; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; import me.lucko.luckperms.common.commands.sender.Sender; -import me.lucko.luckperms.common.commands.utils.CommandUtils; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.locale.CommandSpec; import me.lucko.luckperms.common.locale.LocaleManager; @@ -40,6 +39,7 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.storage.DataConstraints; import me.lucko.luckperms.common.utils.DateUtil; import me.lucko.luckperms.common.utils.Predicates; +import me.lucko.luckperms.common.utils.Uuids; import java.util.List; import java.util.Map; @@ -67,7 +67,7 @@ public class LogUserHistory extends SubCommand { } } - UUID uuid = CommandUtils.parseUuid(target.toLowerCase()); + UUID uuid = Uuids.parseNullable(target); if (uuid == null) { if (!plugin.getConfiguration().get(ConfigKeys.ALLOW_INVALID_USERNAMES)) { if (!DataConstraints.PLAYER_USERNAME_TEST.test(target)) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/CheckCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/CheckCommand.java index 0157e37fb..f0df59293 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/CheckCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/CheckCommand.java @@ -38,6 +38,7 @@ import me.lucko.luckperms.common.locale.Message; import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.utils.Predicates; +import me.lucko.luckperms.common.utils.Uuids; import me.lucko.luckperms.common.verbose.CheckOrigin; import java.util.List; @@ -55,7 +56,7 @@ public class CheckCommand extends SingleCommand { String permission = args.get(1); User user; - UUID u = CommandUtils.parseUuid(target); + UUID u = Uuids.parseNullable(target); if (u != null) { user = plugin.getUserManager().getIfLoaded(u); } else { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/TreeCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/TreeCommand.java index 84661890a..77ee13aa6 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/TreeCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/TreeCommand.java @@ -31,7 +31,6 @@ import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SingleCommand; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.commands.utils.ArgumentUtils; -import me.lucko.luckperms.common.commands.utils.CommandUtils; import me.lucko.luckperms.common.locale.CommandSpec; import me.lucko.luckperms.common.locale.LocaleManager; import me.lucko.luckperms.common.locale.Message; @@ -40,6 +39,7 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.treeview.TreeView; import me.lucko.luckperms.common.treeview.TreeViewBuilder; import me.lucko.luckperms.common.utils.Predicates; +import me.lucko.luckperms.common.utils.Uuids; import net.kyori.text.Component; import net.kyori.text.TextComponent; @@ -73,7 +73,7 @@ public class TreeCommand extends SingleCommand { if (player != null) { User user; - UUID u = CommandUtils.parseUuid(player); + UUID u = Uuids.parseNullable(player); if (u != null) { user = plugin.getUserManager().getIfLoaded(u); } else { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserClone.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserClone.java index 022846c11..3d52177bb 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserClone.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserClone.java @@ -31,7 +31,6 @@ import me.lucko.luckperms.common.commands.CommandPermission; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; import me.lucko.luckperms.common.commands.sender.Sender; -import me.lucko.luckperms.common.commands.utils.CommandUtils; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.locale.CommandSpec; import me.lucko.luckperms.common.locale.LocaleManager; @@ -40,6 +39,7 @@ import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.storage.DataConstraints; import me.lucko.luckperms.common.utils.Predicates; +import me.lucko.luckperms.common.utils.Uuids; import java.util.List; import java.util.UUID; @@ -58,7 +58,7 @@ public class UserClone extends SubCommand { String target = args.get(0); - UUID uuid = CommandUtils.parseUuid(target.toLowerCase()); + UUID uuid = Uuids.parseNullable(target); if (uuid == null) { if (!plugin.getConfiguration().get(ConfigKeys.ALLOW_INVALID_USERNAMES)) { if (!DataConstraints.PLAYER_USERNAME_TEST.test(target)) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserMainCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserMainCommand.java index 780b99082..593de62ae 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserMainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserMainCommand.java @@ -38,7 +38,6 @@ import me.lucko.luckperms.common.commands.impl.generic.other.HolderShowTracks; import me.lucko.luckperms.common.commands.impl.generic.parent.CommandParent; import me.lucko.luckperms.common.commands.impl.generic.permission.CommandPermission; import me.lucko.luckperms.common.commands.sender.Sender; -import me.lucko.luckperms.common.commands.utils.CommandUtils; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.locale.CommandSpec; import me.lucko.luckperms.common.locale.LocaleManager; @@ -47,6 +46,7 @@ import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.references.UserIdentifier; import me.lucko.luckperms.common.storage.DataConstraints; +import me.lucko.luckperms.common.utils.Uuids; import java.util.List; import java.util.UUID; @@ -83,7 +83,7 @@ public class UserMainCommand extends MainCommand { @Override protected UserIdentifier parseTarget(String target, LuckPermsPlugin plugin, Sender sender) { - UUID uuid = CommandUtils.parseUuid(target.toLowerCase()); + UUID uuid = Uuids.parseNullable(target); if (uuid == null) { if (!plugin.getConfiguration().get(ConfigKeys.ALLOW_INVALID_USERNAMES)) { if (!DataConstraints.PLAYER_USERNAME_TEST.test(target)) { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/utils/CommandUtils.java b/common/src/main/java/me/lucko/luckperms/common/commands/utils/CommandUtils.java index 2c840909b..d6a77a3a1 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/utils/CommandUtils.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/utils/CommandUtils.java @@ -37,7 +37,6 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.UUID; import java.util.function.Supplier; import java.util.regex.Pattern; @@ -108,18 +107,6 @@ public final class CommandUtils { return lists; } - public static UUID parseUuid(String s) { - try { - return UUID.fromString(s); - } catch (IllegalArgumentException e) { - try { - return UUID.fromString(s.replaceAll("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5")); - } catch (IllegalArgumentException e1) { - return null; - } - } - } - public static String toCommaSep(Collection strings) { if (strings.isEmpty()) { return "&bNone"; diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/ConfigurateDao.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/ConfigurateDao.java index 9f2f841ed..03cafc1dd 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/ConfigurateDao.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/ConfigurateDao.java @@ -35,7 +35,6 @@ import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.common.actionlog.Log; import me.lucko.luckperms.common.bulkupdate.BulkUpdate; import me.lucko.luckperms.common.commands.CommandManager; -import me.lucko.luckperms.common.commands.utils.CommandUtils; import me.lucko.luckperms.common.contexts.ContextSetConfigurateSerializer; import me.lucko.luckperms.common.managers.group.GroupManager; import me.lucko.luckperms.common.managers.track.TrackManager; @@ -51,6 +50,7 @@ import me.lucko.luckperms.common.storage.dao.AbstractDao; import me.lucko.luckperms.common.storage.dao.legacy.LegacyJsonMigration; import me.lucko.luckperms.common.storage.dao.legacy.LegacyYamlMigration; import me.lucko.luckperms.common.utils.ImmutableCollectors; +import me.lucko.luckperms.common.utils.Uuids; import ninja.leaping.configurate.ConfigurationNode; import ninja.leaping.configurate.SimpleConfigurationNode; @@ -266,7 +266,7 @@ public abstract class ConfigurateDao extends AbstractDao { } String user = s.substring(0, s.length() - this.fileExtension.length()); - UUID uuid = CommandUtils.parseUuid(user); + UUID uuid = Uuids.parseNullable(user); if (uuid == null) { return; } diff --git a/common/src/main/java/me/lucko/luckperms/common/utils/Uuids.java b/common/src/main/java/me/lucko/luckperms/common/utils/Uuids.java new file mode 100644 index 000000000..d70796d1a --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/utils/Uuids.java @@ -0,0 +1,67 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * 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.common.utils; + +import java.util.Optional; +import java.util.UUID; +import java.util.function.Predicate; +import java.util.regex.Pattern; + +import javax.annotation.Nullable; + +/** + * Utilities for working with {@link UUID}s. + */ +public final class Uuids { + private static final Pattern UUID_PATTERN = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})"); + + public static final Predicate PREDICATE = s -> parseNullable(s) != null; + + @Nullable + private static UUID fromString(String s) { + try { + return UUID.fromString(s); + } catch (IllegalArgumentException e) { + return null; + } + } + + @Nullable + public static UUID parseNullable(String s) { + UUID uuid = fromString(s); + if (uuid == null) { + uuid = fromString(UUID_PATTERN.matcher(s).replaceAll("$1-$2-$3-$4-$5")); + } + return uuid; + } + + public static Optional parse(String s) { + return Optional.ofNullable(parseNullable(s)); + } + + private Uuids() {} + +} diff --git a/common/src/main/java/me/lucko/luckperms/common/webeditor/WebEditorUtils.java b/common/src/main/java/me/lucko/luckperms/common/webeditor/WebEditorUtils.java index 0c2264f16..5046634f8 100644 --- a/common/src/main/java/me/lucko/luckperms/common/webeditor/WebEditorUtils.java +++ b/common/src/main/java/me/lucko/luckperms/common/webeditor/WebEditorUtils.java @@ -33,7 +33,6 @@ import com.google.gson.stream.JsonWriter; import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.common.commands.sender.Sender; -import me.lucko.luckperms.common.commands.utils.CommandUtils; import me.lucko.luckperms.common.contexts.ContextSetJsonSerializer; import me.lucko.luckperms.common.locale.Message; import me.lucko.luckperms.common.model.Group; @@ -41,6 +40,7 @@ import me.lucko.luckperms.common.model.PermissionHolder; import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.node.NodeModel; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; +import me.lucko.luckperms.common.utils.Uuids; import java.io.BufferedReader; import java.io.InputStream; @@ -86,7 +86,7 @@ public final class WebEditorUtils { return holder; } else if (who.startsWith(USER_ID_PATTERN)) { String user = who.substring(USER_ID_PATTERN.length()); - UUID uuid = CommandUtils.parseUuid(user); + UUID uuid = Uuids.parseNullable(user); if (uuid == null) { Message.APPLY_EDITS_TARGET_USER_NOT_UUID.send(sender, user); return null; diff --git a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SimpleDescriptionBuilder.java b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/LPDescriptionBuilder.java similarity index 88% rename from sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SimpleDescriptionBuilder.java rename to sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/LPDescriptionBuilder.java index 71636a9a5..0eabfd793 100644 --- a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SimpleDescriptionBuilder.java +++ b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/LPDescriptionBuilder.java @@ -42,15 +42,16 @@ import java.util.Map; import java.util.Objects; import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public final class SimpleDescriptionBuilder implements PermissionDescription.Builder { +public final class LPDescriptionBuilder implements PermissionDescription.Builder { @Nonnull private final LPPermissionService service; @Nonnull private final PluginContainer container; @Nonnull private final Map roles = new HashMap<>(); - private String id = null; - private Text description = null; + @Nullable private String id = null; + @Nullable private Text description = null; - public SimpleDescriptionBuilder(@Nonnull LPPermissionService service, @Nonnull PluginContainer container) { + public LPDescriptionBuilder(@Nonnull LPPermissionService service, @Nonnull PluginContainer container) { this.service = Objects.requireNonNull(service, "service"); this.container = Objects.requireNonNull(container, "container"); } @@ -84,7 +85,7 @@ public final class SimpleDescriptionBuilder implements PermissionDescription.Bui throw new IllegalStateException("id cannot be null"); } - LPPermissionDescription d = this.service.registerPermissionDescription(this.id, this.description, this.container); + LPPermissionDescription description = this.service.registerPermissionDescription(this.id, this.description, this.container); // Set role-templates LPSubjectCollection subjects = this.service.getCollection(PermissionService.SUBJECTS_ROLE_TEMPLATE); @@ -100,14 +101,14 @@ public final class SimpleDescriptionBuilder implements PermissionDescription.Bui this.id = null; this.description = null; - return d.sponge(); + return description.sponge(); } @Override public boolean equals(Object o) { if (o == this) return true; - if (!(o instanceof SimpleDescriptionBuilder)) return false; - final SimpleDescriptionBuilder other = (SimpleDescriptionBuilder) o; + if (!(o instanceof LPDescriptionBuilder)) return false; + final LPDescriptionBuilder other = (LPDescriptionBuilder) o; return this.container.equals(other.container) && this.roles.equals(other.roles) && diff --git a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/PermissionServiceProxy.java b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/PermissionServiceProxy.java index 764d262dd..9ad491534 100644 --- a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/PermissionServiceProxy.java +++ b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/PermissionServiceProxy.java @@ -91,7 +91,7 @@ public final class PermissionServiceProxy implements PermissionService { throw new IllegalArgumentException("Couldn't find a plugin container for " + o.getClass().getSimpleName()); } - return Optional.of(new SimpleDescriptionBuilder(this.handle, container.get())); + return Optional.of(new LPDescriptionBuilder(this.handle, container.get())); } @Nonnull diff --git a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectCollectionProxy.java b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectCollectionProxy.java index ce5d52592..ad9358eea 100644 --- a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectCollectionProxy.java +++ b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectCollectionProxy.java @@ -30,7 +30,7 @@ import me.lucko.luckperms.sponge.service.CompatibilityUtil; import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; -import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory; +import me.lucko.luckperms.sponge.service.reference.SubjectReferenceFactory; import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.permission.Subject; diff --git a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectDataProxy.java b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectDataProxy.java index 05cd8ed4d..44412192b 100644 --- a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectDataProxy.java +++ b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectDataProxy.java @@ -30,8 +30,8 @@ import me.lucko.luckperms.sponge.service.CompatibilityUtil; import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectData; -import me.lucko.luckperms.sponge.service.model.SubjectReference; -import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; +import me.lucko.luckperms.sponge.service.reference.SubjectReferenceFactory; import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.permission.Subject; @@ -48,17 +48,19 @@ import javax.annotation.Nonnull; @SuppressWarnings("unchecked") public final class SubjectDataProxy implements SubjectData { private final LPPermissionService service; - private final SubjectReference ref; + private final LPSubjectReference ref; private final boolean enduring; - public SubjectDataProxy(LPPermissionService service, SubjectReference ref, boolean enduring) { + public SubjectDataProxy(LPPermissionService service, LPSubjectReference ref, boolean enduring) { this.service = service; this.ref = ref; this.enduring = enduring; } private CompletableFuture handle() { - return this.enduring ? this.ref.resolveLp().thenApply(LPSubject::getSubjectData) : this.ref.resolveLp().thenApply(LPSubject::getTransientSubjectData); + return this.enduring ? + this.ref.resolveLp().thenApply(LPSubject::getSubjectData) : + this.ref.resolveLp().thenApply(LPSubject::getTransientSubjectData); } @Nonnull diff --git a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectProxy.java b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectProxy.java index 2be20a83f..45ad93e36 100644 --- a/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectProxy.java +++ b/sponge/sponge-service-api6/src/main/java/me/lucko/luckperms/sponge/service/proxy/api6/SubjectProxy.java @@ -31,8 +31,8 @@ import me.lucko.luckperms.sponge.service.CompatibilityUtil; import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.ProxiedSubject; -import me.lucko.luckperms.sponge.service.model.SubjectReference; -import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; +import me.lucko.luckperms.sponge.service.reference.SubjectReferenceFactory; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.service.context.Context; @@ -51,9 +51,9 @@ import javax.annotation.Nonnull; @SuppressWarnings("unchecked") public final class SubjectProxy implements Subject, ProxiedSubject { private final LPPermissionService service; - private final SubjectReference ref; + private final LPSubjectReference ref; - public SubjectProxy(LPPermissionService service, SubjectReference ref) { + public SubjectProxy(LPPermissionService service, LPSubjectReference ref) { this.service = service; this.ref = ref; } @@ -62,8 +62,9 @@ public final class SubjectProxy implements Subject, ProxiedSubject { return this.ref.resolveLp(); } + @Nonnull @Override - public SubjectReference getReference() { + public LPSubjectReference asSubjectReference() { return this.ref; } @@ -157,7 +158,7 @@ public final class SubjectProxy implements Subject, ProxiedSubject { @Nonnull @Override public Set getActiveContexts() { - return handle().thenApply(handle -> CompatibilityUtil.convertContexts(handle.getActiveContextSet())).join(); + return CompatibilityUtil.convertContexts(this.service.getPlugin().getContextManager().getApplicableContext(this)); } @Override diff --git a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SimpleDescriptionBuilder.java b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/LPDescriptionBuilder.java similarity index 86% rename from sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SimpleDescriptionBuilder.java rename to sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/LPDescriptionBuilder.java index db8298b7a..fb6dcac3d 100644 --- a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SimpleDescriptionBuilder.java +++ b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/LPDescriptionBuilder.java @@ -42,15 +42,16 @@ import java.util.Map; import java.util.Objects; import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public final class SimpleDescriptionBuilder implements PermissionDescription.Builder { +public final class LPDescriptionBuilder implements PermissionDescription.Builder { @Nonnull private final LPPermissionService service; @Nonnull private final PluginContainer container; @Nonnull private final Map roles = new HashMap<>(); - private String id = null; - private Text description = null; + @Nullable private String id = null; + @Nullable private Text description = null; - public SimpleDescriptionBuilder(LPPermissionService service, PluginContainer container) { + public LPDescriptionBuilder(LPPermissionService service, PluginContainer container) { this.service = Objects.requireNonNull(service, "service"); this.container = Objects.requireNonNull(container, "container"); } @@ -64,8 +65,8 @@ public final class SimpleDescriptionBuilder implements PermissionDescription.Bui @Nonnull @Override - public PermissionDescription.Builder description(Text description) { - this.description = Objects.requireNonNull(description, "description"); + public PermissionDescription.Builder description(@Nullable Text description) { + this.description = description; return this; } @@ -84,7 +85,7 @@ public final class SimpleDescriptionBuilder implements PermissionDescription.Bui throw new IllegalStateException("id cannot be null"); } - LPPermissionDescription d = this.service.registerPermissionDescription(this.id, this.description, this.container); + LPPermissionDescription description = this.service.registerPermissionDescription(this.id, this.description, this.container); // Set role-templates LPSubjectCollection subjects = this.service.getCollection(PermissionService.SUBJECTS_ROLE_TEMPLATE); @@ -100,14 +101,14 @@ public final class SimpleDescriptionBuilder implements PermissionDescription.Bui this.id = null; this.description = null; - return d.sponge(); + return description.sponge(); } @Override public boolean equals(Object o) { if (o == this) return true; - if (!(o instanceof SimpleDescriptionBuilder)) return false; - final SimpleDescriptionBuilder other = (SimpleDescriptionBuilder) o; + if (!(o instanceof LPDescriptionBuilder)) return false; + final LPDescriptionBuilder other = (LPDescriptionBuilder) o; return this.container.equals(other.container) && this.roles.equals(other.roles) && diff --git a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/PermissionServiceProxy.java b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/PermissionServiceProxy.java index 706d5cd1f..96b92cab7 100644 --- a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/PermissionServiceProxy.java +++ b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/PermissionServiceProxy.java @@ -31,7 +31,7 @@ import me.lucko.luckperms.common.utils.ImmutableCollectors; import me.lucko.luckperms.sponge.service.model.LPPermissionDescription; import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; -import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory; +import me.lucko.luckperms.sponge.service.reference.SubjectReferenceFactory; import org.spongepowered.api.Sponge; import org.spongepowered.api.plugin.PluginContainer; @@ -44,6 +44,7 @@ import org.spongepowered.api.service.permission.SubjectReference; import java.util.Collection; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.concurrent.CompletableFuture; @@ -115,8 +116,20 @@ public final class PermissionServiceProxy implements PermissionService { @Nonnull @Override - public SubjectReference newSubjectReference(@Nonnull String s, @Nonnull String s1) { - return SubjectReferenceFactory.obtain(this.handle, s, s1); + public SubjectReference newSubjectReference(@Nonnull String collectionIdentifier, @Nonnull String subjectIdentifier) { + Objects.requireNonNull(collectionIdentifier, "collectionIdentifier"); + Objects.requireNonNull(subjectIdentifier, "subjectIdentifier"); + + // test the identifiers + String collection = collectionIdentifier.toLowerCase(); + if (collection.equals("user") && !this.handle.getUserSubjects().getIdentifierValidityPredicate().test(subjectIdentifier)) { + throw new IllegalArgumentException("Subject identifier '" + subjectIdentifier + "' does not pass the validity predicate for the user subject collection"); + } else if (collection.equals("group") && !this.handle.getGroupSubjects().getIdentifierValidityPredicate().test(subjectIdentifier)) { + throw new IllegalArgumentException("Subject identifier '" + subjectIdentifier + "' does not pass the validity predicate for the group subject collection"); + } + + // obtain a reference + return SubjectReferenceFactory.obtain(this.handle, collectionIdentifier, subjectIdentifier); } @Override @@ -126,7 +139,7 @@ public final class PermissionServiceProxy implements PermissionService { throw new IllegalArgumentException("Couldn't find a plugin container for " + o.getClass().getSimpleName()); } - return new SimpleDescriptionBuilder(this.handle, container.get()); + return new LPDescriptionBuilder(this.handle, container.get()); } @Nonnull diff --git a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectCollectionProxy.java b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectCollectionProxy.java index 608972728..848919d6b 100644 --- a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectCollectionProxy.java +++ b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectCollectionProxy.java @@ -29,7 +29,7 @@ import me.lucko.luckperms.common.utils.ImmutableCollectors; import me.lucko.luckperms.sponge.service.CompatibilityUtil; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; -import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory; +import me.lucko.luckperms.sponge.service.reference.SubjectReferenceFactory; import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.permission.Subject; @@ -38,6 +38,7 @@ import org.spongepowered.api.service.permission.SubjectReference; import java.util.Collection; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.concurrent.CompletableFuture; @@ -103,8 +104,13 @@ public final class SubjectCollectionProxy implements SubjectCollection { @Nonnull @Override - public SubjectReference newSubjectReference(@Nonnull String s) { - return SubjectReferenceFactory.obtain(this.handle.getService(), getIdentifier(), s); + public SubjectReference newSubjectReference(@Nonnull String subjectIdentifier) { + Objects.requireNonNull(subjectIdentifier, "identifier"); + if (!this.handle.getIdentifierValidityPredicate().test(subjectIdentifier)) { + throw new IllegalArgumentException("Subject identifier '" + subjectIdentifier + "' does not pass the validity predicate"); + } + + return SubjectReferenceFactory.obtain(this.handle.getService(), getIdentifier(), subjectIdentifier); } @Nonnull diff --git a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectDataProxy.java b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectDataProxy.java index 4b41cf685..3f8554978 100644 --- a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectDataProxy.java +++ b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectDataProxy.java @@ -30,8 +30,8 @@ import me.lucko.luckperms.sponge.service.CompatibilityUtil; import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectData; -import me.lucko.luckperms.sponge.service.model.SubjectReference; -import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; +import me.lucko.luckperms.sponge.service.reference.SubjectReferenceFactory; import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.permission.SubjectData; @@ -48,17 +48,19 @@ import javax.annotation.Nullable; @SuppressWarnings("unchecked") public final class SubjectDataProxy implements SubjectData { private final LPPermissionService service; - private final SubjectReference ref; + private final LPSubjectReference ref; private final boolean enduring; - public SubjectDataProxy(LPPermissionService service, SubjectReference ref, boolean enduring) { + public SubjectDataProxy(LPPermissionService service, LPSubjectReference ref, boolean enduring) { this.service = service; this.ref = ref; this.enduring = enduring; } private CompletableFuture handle() { - return this.enduring ? this.ref.resolveLp().thenApply(LPSubject::getSubjectData) : this.ref.resolveLp().thenApply(LPSubject::getTransientSubjectData); + return this.enduring ? + this.ref.resolveLp().thenApply(LPSubject::getSubjectData) : + this.ref.resolveLp().thenApply(LPSubject::getTransientSubjectData); } @Nonnull diff --git a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectProxy.java b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectProxy.java index 477a440fc..c07ac0088 100644 --- a/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectProxy.java +++ b/sponge/sponge-service-api7/src/main/java/me/lucko/luckperms/sponge/service/proxy/api7/SubjectProxy.java @@ -30,14 +30,15 @@ import me.lucko.luckperms.sponge.service.CompatibilityUtil; import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.ProxiedSubject; -import me.lucko.luckperms.sponge.service.model.SubjectReference; -import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; +import me.lucko.luckperms.sponge.service.reference.SubjectReferenceFactory; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.service.context.Context; 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.service.permission.SubjectReference; import org.spongepowered.api.util.Tristate; import java.util.List; @@ -50,26 +51,27 @@ import javax.annotation.Nonnull; @SuppressWarnings("unchecked") public final class SubjectProxy implements Subject, ProxiedSubject { private final LPPermissionService service; - private final SubjectReference ref; + private final LPSubjectReference ref; - public SubjectProxy(LPPermissionService service, SubjectReference ref) { + public SubjectProxy(LPPermissionService service, LPSubjectReference ref) { this.service = service; this.ref = ref; } - private CompletableFuture getHandle() { + private CompletableFuture handle() { return this.ref.resolveLp(); } + @Nonnull @Override - public SubjectReference getReference() { + public LPSubjectReference asSubjectReference() { return this.ref; } @Nonnull @Override public Optional getCommandSource() { - return getHandle().thenApply(LPSubject::getCommandSource).join(); + return handle().thenApply(LPSubject::getCommandSource).join(); } @Nonnull @@ -78,12 +80,6 @@ public final class SubjectProxy implements Subject, ProxiedSubject { return this.service.getCollection(this.ref.getCollectionIdentifier()).sponge(); } - @Nonnull - @Override - public org.spongepowered.api.service.permission.SubjectReference asSubjectReference() { - return this.ref; - } - @Override public boolean isSubjectDataPersisted() { return true; @@ -101,52 +97,52 @@ public final class SubjectProxy implements Subject, ProxiedSubject { @Override public boolean hasPermission(@Nonnull Set contexts, @Nonnull String permission) { - return getHandle().thenApply(handle -> handle.getPermissionValue(CompatibilityUtil.convertContexts(contexts), permission).asBoolean()).join(); + return handle().thenApply(handle -> handle.getPermissionValue(CompatibilityUtil.convertContexts(contexts), permission).asBoolean()).join(); } @Override public boolean hasPermission(@Nonnull String permission) { - return getHandle().thenApply(handle -> handle.getPermissionValue(ImmutableContextSet.empty(), permission).asBoolean()).join(); + return handle().thenApply(handle -> handle.getPermissionValue(ImmutableContextSet.empty(), permission).asBoolean()).join(); } @Nonnull @Override public Tristate getPermissionValue(@Nonnull Set contexts, @Nonnull String permission) { - return getHandle().thenApply(handle -> CompatibilityUtil.convertTristate(handle.getPermissionValue(CompatibilityUtil.convertContexts(contexts), permission))).join(); + return handle().thenApply(handle -> CompatibilityUtil.convertTristate(handle.getPermissionValue(CompatibilityUtil.convertContexts(contexts), permission))).join(); } @Override - public boolean isChildOf(@Nonnull org.spongepowered.api.service.permission.SubjectReference parent) { - return getHandle().thenApply(handle -> handle.isChildOf(ImmutableContextSet.empty(), SubjectReferenceFactory.obtain(this.service, parent))).join(); + public boolean isChildOf(@Nonnull SubjectReference parent) { + return handle().thenApply(handle -> handle.isChildOf(ImmutableContextSet.empty(), SubjectReferenceFactory.obtain(this.service, parent))).join(); } @Override - public boolean isChildOf(@Nonnull Set contexts, @Nonnull org.spongepowered.api.service.permission.SubjectReference parent) { - return getHandle().thenApply(handle -> handle.isChildOf(CompatibilityUtil.convertContexts(contexts), SubjectReferenceFactory.obtain(this.service, parent))).join(); + public boolean isChildOf(@Nonnull Set contexts, @Nonnull SubjectReference parent) { + return handle().thenApply(handle -> handle.isChildOf(CompatibilityUtil.convertContexts(contexts), SubjectReferenceFactory.obtain(this.service, parent))).join(); } @Nonnull @Override - public List getParents() { - return (List) getHandle().thenApply(handle -> handle.getParents(ImmutableContextSet.empty())).join(); + public List getParents() { + return (List) handle().thenApply(handle -> handle.getParents(ImmutableContextSet.empty())).join(); } @Nonnull @Override - public List getParents(@Nonnull Set contexts) { - return (List) getHandle().thenApply(handle -> handle.getParents(CompatibilityUtil.convertContexts(contexts))).join(); + public List getParents(@Nonnull Set contexts) { + return (List) handle().thenApply(handle -> handle.getParents(CompatibilityUtil.convertContexts(contexts))).join(); } @Nonnull @Override public Optional getOption(@Nonnull Set contexts, @Nonnull String key) { - return getHandle().thenApply(handle -> handle.getOption(CompatibilityUtil.convertContexts(contexts), key)).join(); + return handle().thenApply(handle -> handle.getOption(CompatibilityUtil.convertContexts(contexts), key)).join(); } @Nonnull @Override public Optional getOption(@Nonnull String key) { - return getHandle().thenApply(handle -> handle.getOption(ImmutableContextSet.empty(), key)).join(); + return handle().thenApply(handle -> handle.getOption(ImmutableContextSet.empty(), key)).join(); } @Override @@ -157,13 +153,13 @@ public final class SubjectProxy implements Subject, ProxiedSubject { @Nonnull @Override public Optional getFriendlyIdentifier() { - return getHandle().thenApply(LPSubject::getFriendlyIdentifier).join(); + return handle().thenApply(LPSubject::getFriendlyIdentifier).join(); } @Nonnull @Override public Set getActiveContexts() { - return getHandle().thenApply(handle -> CompatibilityUtil.convertContexts(handle.getActiveContextSet())).join(); + return CompatibilityUtil.convertContexts(this.service.getPlugin().getContextManager().getApplicableContext(this)); } @Override diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPPermissionDescription.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPPermissionDescription.java index 6603e6caa..1448a5268 100644 --- a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPPermissionDescription.java +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPPermissionDescription.java @@ -25,6 +25,8 @@ package me.lucko.luckperms.sponge.service.model; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; + import org.spongepowered.api.plugin.PluginContainer; import org.spongepowered.api.service.permission.PermissionDescription; import org.spongepowered.api.text.Text; @@ -48,7 +50,7 @@ public interface LPPermissionDescription { Optional getOwner(); - CompletableFuture> findAssignedSubjects(String collectionIdentifier); + CompletableFuture> findAssignedSubjects(String collectionIdentifier); Map getAssignedSubjects(String collectionIdentifier); diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPPermissionService.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPPermissionService.java index 8ec51f3d1..1dd2f98ba 100644 --- a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPPermissionService.java +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPPermissionService.java @@ -29,7 +29,8 @@ import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import me.lucko.luckperms.common.plugin.LuckPermsPlugin; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; +import me.lucko.luckperms.sponge.service.reference.SubjectReferenceFactory; import org.spongepowered.api.plugin.PluginContainer; import org.spongepowered.api.service.context.ContextCalculator; @@ -46,7 +47,7 @@ import java.util.function.Predicate; */ public interface LPPermissionService { - LuckPermsPlugin getPlugin(); + LuckPermsSpongePlugin getPlugin(); SubjectReferenceFactory getReferenceFactory(); @@ -75,7 +76,7 @@ public interface LPPermissionService { void registerContextCalculator(ContextCalculator calculator); // utils - ImmutableList sortSubjects(Collection s); + ImmutableList sortSubjects(Collection s); void invalidateAllCaches(LPSubject.CacheLevel cacheLevel); } diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPSubject.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPSubject.java index c8d40525c..fc214a30f 100644 --- a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPSubject.java +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPSubject.java @@ -29,6 +29,8 @@ import com.google.common.collect.ImmutableList; import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.context.ImmutableContextSet; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; +import me.lucko.luckperms.sponge.service.reference.SubjectReferenceFactory; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.service.permission.Subject; @@ -46,7 +48,7 @@ public interface LPSubject { String getIdentifier(); - default SubjectReference toReference() { + default LPSubjectReference toReference() { return SubjectReferenceFactory.obtain(getService(), this); } @@ -70,14 +72,12 @@ public interface LPSubject { Tristate getPermissionValue(ImmutableContextSet contexts, String permission); - boolean isChildOf(ImmutableContextSet contexts, SubjectReference parent); + boolean isChildOf(ImmutableContextSet contexts, LPSubjectReference parent); - ImmutableList getParents(ImmutableContextSet contexts); + ImmutableList getParents(ImmutableContextSet contexts); Optional getOption(ImmutableContextSet contexts, String key); - ImmutableContextSet getActiveContextSet(); - default void performCleanup() { } diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPSubjectCollection.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPSubjectCollection.java index 08f01edf9..ef38441eb 100644 --- a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPSubjectCollection.java +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPSubjectCollection.java @@ -30,6 +30,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import me.lucko.luckperms.api.context.ImmutableContextSet; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; import org.spongepowered.api.service.permission.SubjectCollection; @@ -63,9 +64,9 @@ public interface LPSubjectCollection { CompletableFuture> getAllIdentifiers(); - CompletableFuture> getAllWithPermission(String permission); + CompletableFuture> getAllWithPermission(String permission); - CompletableFuture> getAllWithPermission(ImmutableContextSet contexts, String permission); + CompletableFuture> getAllWithPermission(ImmutableContextSet contexts, String permission); ImmutableMap getLoadedWithPermission(String permission); diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPSubjectData.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPSubjectData.java index acb52f8ad..f4d19d0f6 100644 --- a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPSubjectData.java +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LPSubjectData.java @@ -30,6 +30,7 @@ import com.google.common.collect.ImmutableMap; import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.context.ImmutableContextSet; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; import org.spongepowered.api.service.permission.SubjectData; @@ -58,15 +59,15 @@ public interface LPSubjectData { /* parents */ - ImmutableMap> getAllParents(); + ImmutableMap> getAllParents(); - default ImmutableList getParents(ImmutableContextSet contexts) { + default ImmutableList getParents(ImmutableContextSet contexts) { return ImmutableList.copyOf(getAllParents().getOrDefault(contexts, ImmutableList.of())); } - CompletableFuture addParent(ImmutableContextSet contexts, SubjectReference parent); + CompletableFuture addParent(ImmutableContextSet contexts, LPSubjectReference parent); - CompletableFuture removeParent(ImmutableContextSet contexts, SubjectReference parent); + CompletableFuture removeParent(ImmutableContextSet contexts, LPSubjectReference parent); CompletableFuture clearParents(); diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LuckPermsSpongePlugin.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LuckPermsSpongePlugin.java new file mode 100644 index 000000000..73d4f1a70 --- /dev/null +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/LuckPermsSpongePlugin.java @@ -0,0 +1,38 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * 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.model; + +import me.lucko.luckperms.common.contexts.ContextManager; +import me.lucko.luckperms.common.plugin.LuckPermsPlugin; + +import org.spongepowered.api.service.permission.Subject; + +public interface LuckPermsSpongePlugin extends LuckPermsPlugin { + + @Override + ContextManager getContextManager(); + +} diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/ProxiedSubject.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/ProxiedSubject.java index 36c8665a9..8149c4f2c 100644 --- a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/ProxiedSubject.java +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/ProxiedSubject.java @@ -25,11 +25,18 @@ package me.lucko.luckperms.sponge.service.model; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; + +import org.spongepowered.api.service.permission.Subject; + +import javax.annotation.Nonnull; + /** - * Marks that an object is a proxied representation of a {@link org.spongepowered.api.service.permission.Subject}. + * Marks that an object is a proxied representation of a {@link Subject}. */ public interface ProxiedSubject { - SubjectReference getReference(); + @Nonnull + LPSubjectReference asSubjectReference(); } diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/reference/LPSubjectReference.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/reference/LPSubjectReference.java new file mode 100644 index 000000000..2a3aad897 --- /dev/null +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/reference/LPSubjectReference.java @@ -0,0 +1,44 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * 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.reference; + +import me.lucko.luckperms.sponge.service.model.LPSubject; + +import org.spongepowered.api.service.permission.SubjectReference; + +import java.util.concurrent.CompletableFuture; + +import javax.annotation.Nonnull; + +/** + * LuckPerms model for the Sponge {@link SubjectReference} + */ +public interface LPSubjectReference extends SubjectReference { + + @Nonnull + CompletableFuture resolveLp(); + +} diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReference.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/reference/LuckPermsSubjectReference.java similarity index 89% rename from sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReference.java rename to sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/reference/LuckPermsSubjectReference.java index d8c6abd0f..6f3cad133 100644 --- a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReference.java +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/reference/LuckPermsSubjectReference.java @@ -23,7 +23,10 @@ * SOFTWARE. */ -package me.lucko.luckperms.sponge.service.model; +package me.lucko.luckperms.sponge.service.reference; + +import me.lucko.luckperms.sponge.service.model.LPPermissionService; +import me.lucko.luckperms.sponge.service.model.LPSubject; import org.spongepowered.api.service.permission.Subject; @@ -40,7 +43,7 @@ import javax.annotation.Nonnull; * Use of this class (or interface) should have no negative impact on * performance, as {@link #resolve()} calls are cached. */ -public final class SubjectReference implements org.spongepowered.api.service.permission.SubjectReference { +final class LuckPermsSubjectReference implements LPSubjectReference { /** * The time a subject instance should be cached in this reference @@ -68,7 +71,7 @@ public final class SubjectReference implements org.spongepowered.api.service.per private long lastLookup = 0L; private WeakReference cache = null; - SubjectReference(LPPermissionService service, String collectionIdentifier, String subjectIdentifier) { + LuckPermsSubjectReference(LPPermissionService service, String collectionIdentifier, String subjectIdentifier) { this.service = Objects.requireNonNull(service); this.collectionIdentifier = Objects.requireNonNull(collectionIdentifier); this.subjectIdentifier = Objects.requireNonNull(subjectIdentifier); @@ -130,6 +133,8 @@ public final class SubjectReference implements org.spongepowered.api.service.per return s; } + @Nonnull + @Override public CompletableFuture resolveLp() { // check if there is a cached value before loading LPSubject s = tryCache(); @@ -157,9 +162,10 @@ public final class SubjectReference implements org.spongepowered.api.service.per @Override public boolean equals(Object o) { if (o == this) return true; - if (!(o instanceof SubjectReference)) return false; - final SubjectReference other = (SubjectReference) o; - return this.collectionIdentifier.equals(other.collectionIdentifier) && this.subjectIdentifier.equals(other.subjectIdentifier); + if (!(o instanceof LPSubjectReference)) return false; + final LPSubjectReference other = (LPSubjectReference) o; + return this.collectionIdentifier.equals(other.getCollectionIdentifier()) && + this.subjectIdentifier.equals(other.getSubjectIdentifier()); } @Override diff --git a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReferenceFactory.java b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/reference/SubjectReferenceFactory.java similarity index 68% rename from sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReferenceFactory.java rename to sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/reference/SubjectReferenceFactory.java index 9c6c83e55..764644f18 100644 --- a/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/model/SubjectReferenceFactory.java +++ b/sponge/sponge-service/src/main/java/me/lucko/luckperms/sponge/service/reference/SubjectReferenceFactory.java @@ -23,47 +23,52 @@ * SOFTWARE. */ -package me.lucko.luckperms.sponge.service.model; +package me.lucko.luckperms.sponge.service.reference; import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.LoadingCache; import com.google.common.base.Splitter; +import me.lucko.luckperms.sponge.service.model.LPPermissionService; +import me.lucko.luckperms.sponge.service.model.LPSubject; +import me.lucko.luckperms.sponge.service.model.ProxiedSubject; + import org.spongepowered.api.service.permission.Subject; +import org.spongepowered.api.service.permission.SubjectReference; import java.util.List; import java.util.Objects; import java.util.concurrent.TimeUnit; /** - * Caches the creation of {@link SubjectReference}s. + * Caches the creation of {@link LPSubjectReference}s. */ public final class SubjectReferenceFactory { // static util access @Deprecated - public static SubjectReference deserialize(LPPermissionService service, String serialisedReference) { + public static LPSubjectReference deserialize(LPPermissionService service, String serialisedReference) { Objects.requireNonNull(service, "service"); return service.getReferenceFactory().deserialize(serialisedReference); } - public static SubjectReference obtain(LPPermissionService service, LPSubject subject) { + public static LPSubjectReference obtain(LPPermissionService service, LPSubject subject) { Objects.requireNonNull(service, "service"); return service.getReferenceFactory().obtain(subject); } - public static SubjectReference obtain(LPPermissionService service, Subject subject) { + public static LPSubjectReference obtain(LPPermissionService service, Subject subject) { Objects.requireNonNull(service, "service"); return service.getReferenceFactory().obtain(subject); } - public static SubjectReference obtain(LPPermissionService service, org.spongepowered.api.service.permission.SubjectReference reference) { + public static LPSubjectReference obtain(LPPermissionService service, SubjectReference reference) { Objects.requireNonNull(service, "service"); return service.getReferenceFactory().obtain(reference); } - public static SubjectReference obtain(LPPermissionService service, String collectionIdentifier, String subjectIdentifier) { + public static LPSubjectReference obtain(LPPermissionService service, String collectionIdentifier, String subjectIdentifier) { Objects.requireNonNull(service, "service"); return service.getReferenceFactory().obtain(collectionIdentifier, subjectIdentifier); } @@ -82,47 +87,47 @@ public final class SubjectReferenceFactory { * * It's perfectly ok if two instances of the same SubjectReference exist. (hence the 1 hour expiry) */ - private final LoadingCache referenceCache = Caffeine.newBuilder() + private final LoadingCache referenceCache = Caffeine.newBuilder() .expireAfterAccess(1, TimeUnit.HOURS) - .build(a -> new SubjectReference(SubjectReferenceFactory.this.service, a.collectionId, a.id)); + .build(a -> new LuckPermsSubjectReference(SubjectReferenceFactory.this.service, a.collectionId, a.id)); public SubjectReferenceFactory(LPPermissionService service) { this.service = service; } @Deprecated - public SubjectReference deserialize(String serialisedReference) { + public LPSubjectReference deserialize(String serialisedReference) { Objects.requireNonNull(serialisedReference, "serialisedReference"); List parts = Splitter.on('/').limit(2).splitToList(serialisedReference); return obtain(parts.get(0), parts.get(1)); } - public SubjectReference obtain(LPSubject subject) { + public LPSubjectReference obtain(LPSubject subject) { Objects.requireNonNull(subject, "subject"); - SubjectReference ret = obtain(subject.getParentCollection().getIdentifier(), subject.getIdentifier()); - ret.fillCache(subject); + LPSubjectReference ret = obtain(subject.getParentCollection().getIdentifier(), subject.getIdentifier()); + ((LuckPermsSubjectReference) ret).fillCache(subject); return ret; } - public SubjectReference obtain(Subject subject) { + public LPSubjectReference obtain(Subject subject) { Objects.requireNonNull(subject, "subject"); if (subject instanceof ProxiedSubject) { - return ((ProxiedSubject) subject).getReference(); + return ((ProxiedSubject) subject).asSubjectReference(); } return obtain(subject.getContainingCollection().getIdentifier(), subject.getIdentifier()); } - public SubjectReference obtain(org.spongepowered.api.service.permission.SubjectReference reference) { + public LPSubjectReference obtain(SubjectReference reference) { Objects.requireNonNull(reference, "reference"); - if (reference instanceof SubjectReference) { - return ((SubjectReference) reference); + if (reference instanceof LPSubjectReference) { + return ((LPSubjectReference) reference); } else { return obtain(reference.getCollectionIdentifier(), reference.getSubjectIdentifier()); } } - public SubjectReference obtain(String collectionIdentifier, String subjectIdentifier) { + public LPSubjectReference obtain(String collectionIdentifier, String subjectIdentifier) { Objects.requireNonNull(collectionIdentifier, "collectionIdentifier"); Objects.requireNonNull(subjectIdentifier, "subjectIdentifier"); return this.referenceCache.get(new SubjectReferenceAttributes(collectionIdentifier, subjectIdentifier)); @@ -134,10 +139,12 @@ public final class SubjectReferenceFactory { private static final class SubjectReferenceAttributes { private final String collectionId; private final String id; + private final int hashCode; - public SubjectReferenceAttributes(String collectionId, String id) { - this.collectionId = collectionId; - this.id = id; + private SubjectReferenceAttributes(String collectionId, String id) { + this.collectionId = collectionId.toLowerCase(); + this.id = id.toLowerCase(); + this.hashCode = calculateHashCode(); } @Override @@ -148,14 +155,18 @@ public final class SubjectReferenceFactory { return this.collectionId.equals(other.collectionId) && this.id.equals(other.id); } - @Override - public int hashCode() { + private int calculateHashCode() { final int PRIME = 59; int result = 1; result = result * PRIME + this.collectionId.hashCode(); result = result * PRIME + this.id.hashCode(); return result; } + + @Override + public int hashCode() { + return this.hashCode; + } } } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java index 6903afa0a..ba074ad51 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/LPSpongePlugin.java @@ -57,7 +57,6 @@ import me.lucko.luckperms.common.logging.SenderLogger; import me.lucko.luckperms.common.managers.track.StandardTrackManager; import me.lucko.luckperms.common.messaging.ExtendedMessagingService; import me.lucko.luckperms.common.model.User; -import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.SchedulerAdapter; import me.lucko.luckperms.common.storage.Storage; import me.lucko.luckperms.common.storage.StorageFactory; @@ -82,6 +81,7 @@ import me.lucko.luckperms.sponge.service.LuckPermsService; import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; +import me.lucko.luckperms.sponge.service.model.LuckPermsSpongePlugin; import me.lucko.luckperms.sponge.service.persisted.PersistedCollection; import me.lucko.luckperms.sponge.tasks.ServiceCacheHousekeepingTask; import me.lucko.luckperms.sponge.utils.VersionData; @@ -136,7 +136,7 @@ import java.util.stream.Stream; description = "A permissions plugin", url = "https://github.com/lucko/LuckPerms" ) -public class LPSpongePlugin implements LuckPermsPlugin { +public class LPSpongePlugin implements LuckPermsSpongePlugin { @Inject private Logger logger; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentInfo.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentInfo.java index 2a757a9c1..2d6a61fb3 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentInfo.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/ParentInfo.java @@ -39,7 +39,7 @@ import me.lucko.luckperms.common.locale.LocaleManager; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.sponge.service.model.LPSubjectData; -import me.lucko.luckperms.sponge.service.model.SubjectReference; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; import java.util.List; import java.util.Map; @@ -54,18 +54,18 @@ public class ParentInfo extends SubCommand { ImmutableContextSet contextSet = ArgumentUtils.handleContextSponge(0, args); if (contextSet.isEmpty()) { CommandUtils.sendPluginMessage(sender, "&aShowing parents matching contexts &bANY&a."); - Map> parents = subjectData.getAllParents(); + Map> parents = subjectData.getAllParents(); if (parents.isEmpty()) { CommandUtils.sendPluginMessage(sender, "That subject does not have any parents defined."); return CommandResult.SUCCESS; } - for (Map.Entry> e : parents.entrySet()) { + for (Map.Entry> e : parents.entrySet()) { CommandUtils.sendPluginMessage(sender, "&3>> &bContext: " + SpongeCommandUtils.contextToString(e.getKey()) + "\n" + SpongeCommandUtils.parentsToString(e.getValue())); } } else { - List parents = subjectData.getParents(contextSet); + List parents = subjectData.getParents(contextSet); if (parents.isEmpty()) { CommandUtils.sendPluginMessage(sender, "That subject does not have any parents defined in those contexts."); return CommandResult.SUCCESS; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeCommandUtils.java b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeCommandUtils.java index 0cb025b1c..24ef7810d 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeCommandUtils.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/commands/SpongeCommandUtils.java @@ -29,7 +29,7 @@ import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.common.commands.utils.ArgumentUtils; import me.lucko.luckperms.common.commands.utils.CommandUtils; -import me.lucko.luckperms.sponge.service.model.SubjectReference; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; import java.util.List; import java.util.Map; @@ -73,9 +73,9 @@ public final class SpongeCommandUtils { return sb.toString(); } - public static String parentsToString(Iterable parents) { + public static String parentsToString(Iterable parents) { StringBuilder sb = new StringBuilder(); - for (SubjectReference s : parents) { + for (LPSubjectReference s : parents) { sb.append("&3> &a") .append(s.getSubjectIdentifier()) .append(" &bfrom collection &a") diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeGroupManager.java b/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeGroupManager.java index 335cc82d6..388c337d1 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeGroupManager.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeGroupManager.java @@ -45,8 +45,8 @@ import me.lucko.luckperms.sponge.service.LuckPermsService; import me.lucko.luckperms.sponge.service.ProxyFactory; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; -import me.lucko.luckperms.sponge.service.model.SubjectReference; -import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; +import me.lucko.luckperms.sponge.service.reference.SubjectReferenceFactory; import org.spongepowered.api.service.permission.PermissionService; import org.spongepowered.api.service.permission.SubjectCollection; @@ -129,9 +129,7 @@ public class SpongeGroupManager extends AbstractGroupManager implem @Override public CompletableFuture loadSubject(String identifier) { if (!DataConstraints.GROUP_NAME_TEST.test(identifier)) { - CompletableFuture fut = new CompletableFuture<>(); - fut.completeExceptionally(new IllegalArgumentException("Illegal subject identifier")); - return fut; + throw new IllegalArgumentException("Illegal subject identifier"); } LPSubject present = this.subjectLoadingCache.getIfPresent(identifier.toLowerCase()); @@ -144,16 +142,20 @@ public class SpongeGroupManager extends AbstractGroupManager implem @Override public Optional getSubject(String identifier) { + if (!DataConstraints.GROUP_NAME_TEST.test(identifier)) { + return Optional.empty(); + } + return Optional.ofNullable(getIfLoaded(identifier.toLowerCase())).map(SpongeGroup::sponge); } @Override public CompletableFuture hasRegistered(String identifier) { - if (isLoaded(identifier.toLowerCase())) { - return CompletableFuture.completedFuture(true); - } else { + if (!DataConstraints.GROUP_NAME_TEST.test(identifier)) { return CompletableFuture.completedFuture(false); } + + return CompletableFuture.completedFuture(isLoaded(identifier.toLowerCase())); } @Override @@ -161,6 +163,9 @@ public class SpongeGroupManager extends AbstractGroupManager implem return CompletableFuture.supplyAsync(() -> { ImmutableSet.Builder ret = ImmutableSet.builder(); for (String id : identifiers) { + if (!DataConstraints.GROUP_NAME_TEST.test(id)) { + continue; + } ret.add(loadSubject(id.toLowerCase()).join()); } @@ -179,9 +184,9 @@ public class SpongeGroupManager extends AbstractGroupManager implem } @Override - public CompletableFuture> getAllWithPermission(String permission) { + public CompletableFuture> getAllWithPermission(String permission) { return CompletableFuture.supplyAsync(() -> { - ImmutableMap.Builder ret = ImmutableMap.builder(); + ImmutableMap.Builder ret = ImmutableMap.builder(); List> lookup = this.plugin.getStorage().getGroupsWithPermission(permission).join(); for (HeldPermission holder : lookup) { @@ -195,9 +200,9 @@ public class SpongeGroupManager extends AbstractGroupManager implem } @Override - public CompletableFuture> getAllWithPermission(ImmutableContextSet contexts, String permission) { + public CompletableFuture> getAllWithPermission(ImmutableContextSet contexts, String permission) { return CompletableFuture.supplyAsync(() -> { - ImmutableMap.Builder ret = ImmutableMap.builder(); + ImmutableMap.Builder ret = ImmutableMap.builder(); List> lookup = this.plugin.getStorage().getGroupsWithPermission(permission).join(); for (HeldPermission holder : lookup) { diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeUserManager.java b/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeUserManager.java index 906e2b490..b2768ab72 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeUserManager.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/managers/SpongeUserManager.java @@ -39,14 +39,15 @@ import me.lucko.luckperms.common.managers.user.AbstractUserManager; import me.lucko.luckperms.common.managers.user.UserHousekeeper; import me.lucko.luckperms.common.references.UserIdentifier; import me.lucko.luckperms.common.utils.ImmutableCollectors; +import me.lucko.luckperms.common.utils.Uuids; import me.lucko.luckperms.sponge.LPSpongePlugin; import me.lucko.luckperms.sponge.model.SpongeUser; import me.lucko.luckperms.sponge.service.LuckPermsService; import me.lucko.luckperms.sponge.service.ProxyFactory; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; -import me.lucko.luckperms.sponge.service.model.SubjectReference; -import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; +import me.lucko.luckperms.sponge.service.reference.SubjectReferenceFactory; import org.spongepowered.api.service.permission.PermissionService; import org.spongepowered.api.service.permission.SubjectCollection; @@ -133,26 +134,14 @@ public class SpongeUserManager extends AbstractUserManager implement @Override public Predicate getIdentifierValidityPredicate() { - return s -> { - try { - //noinspection ResultOfMethodCallIgnored - UUID.fromString(s); - return true; - } catch (IllegalArgumentException e) { - return false; - } - }; + return Uuids.PREDICATE; } @Override public CompletableFuture loadSubject(String identifier) { - UUID uuid; - try { - uuid = UUID.fromString(identifier); - } catch (IllegalArgumentException e) { - CompletableFuture fut = new CompletableFuture<>(); - fut.completeExceptionally(e); - return fut; + UUID uuid = Uuids.parseNullable(identifier); + if (uuid == null) { + throw new IllegalArgumentException("Identifier is not a UUID: " + identifier); } LPSubject present = this.subjectLoadingCache.getIfPresent(uuid); @@ -165,32 +154,25 @@ public class SpongeUserManager extends AbstractUserManager implement @Override public Optional getSubject(String identifier) { - UUID uuid = UUID.fromString(identifier); + UUID uuid = Uuids.parseNullable(identifier); + if (uuid == null) { + return Optional.empty(); + } return Optional.ofNullable(getIfLoaded(uuid)).map(SpongeUser::sponge); } @Override public CompletableFuture hasRegistered(String identifier) { - UUID uuid = null; - IllegalArgumentException ex = null; - try { - uuid = UUID.fromString(identifier); - } catch (IllegalArgumentException e) { - ex = e; + UUID uuid = Uuids.parseNullable(identifier); + if (uuid == null) { + return CompletableFuture.completedFuture(false); } - if (uuid != null && isLoaded(UserIdentifier.of(uuid, null))) { + if (isLoaded(UserIdentifier.of(uuid, null))) { return CompletableFuture.completedFuture(true); } - if (uuid == null) { - CompletableFuture fut = new CompletableFuture<>(); - fut.completeExceptionally(ex); - return fut; - } - - UUID finalUuid = uuid; - return this.plugin.getStorage().getUniqueUsers().thenApply(set -> set.contains(finalUuid)); + return this.plugin.getStorage().getUniqueUsers().thenApply(set -> set.contains(uuid)); } @Override @@ -198,13 +180,10 @@ public class SpongeUserManager extends AbstractUserManager implement return CompletableFuture.supplyAsync(() -> { ImmutableSet.Builder ret = ImmutableSet.builder(); for (String id : identifiers) { - UUID uuid; - try { - uuid = UUID.fromString(id); - } catch (IllegalArgumentException e) { + UUID uuid = Uuids.parseNullable(id); + if (uuid == null) { continue; } - ret.add(loadSubject(uuid.toString()).join()); } @@ -230,9 +209,9 @@ public class SpongeUserManager extends AbstractUserManager implement } @Override - public CompletableFuture> getAllWithPermission(String permission) { + public CompletableFuture> getAllWithPermission(String permission) { return CompletableFuture.supplyAsync(() -> { - ImmutableMap.Builder ret = ImmutableMap.builder(); + ImmutableMap.Builder ret = ImmutableMap.builder(); List> lookup = this.plugin.getStorage().getUsersWithPermission(permission).join(); for (HeldPermission holder : lookup) { @@ -246,9 +225,9 @@ public class SpongeUserManager extends AbstractUserManager implement } @Override - public CompletableFuture> getAllWithPermission(ImmutableContextSet contexts, String permission) { + public CompletableFuture> getAllWithPermission(ImmutableContextSet contexts, String permission) { return CompletableFuture.supplyAsync(() -> { - ImmutableMap.Builder ret = ImmutableMap.builder(); + ImmutableMap.Builder ret = ImmutableMap.builder(); List> lookup = this.plugin.getStorage().getUsersWithPermission(permission).join(); for (HeldPermission holder : lookup) { diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionManager.java b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionManager.java index 041013dc9..1eddb8b26 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionManager.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionManager.java @@ -31,7 +31,6 @@ import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; import me.lucko.luckperms.common.commands.impl.migration.MigrationUtils; import me.lucko.luckperms.common.commands.sender.Sender; -import me.lucko.luckperms.common.commands.utils.CommandUtils; import me.lucko.luckperms.common.locale.CommandSpec; import me.lucko.luckperms.common.locale.LocaleManager; import me.lucko.luckperms.common.logging.ProgressLogger; @@ -40,6 +39,7 @@ import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.common.utils.SafeIterator; +import me.lucko.luckperms.common.utils.Uuids; import me.lucko.luckperms.sponge.LPSpongePlugin; import me.lucko.luckperms.sponge.service.LuckPermsService; @@ -119,7 +119,7 @@ public class MigrationPermissionManager extends SubCommand { log.log("Starting user migration."); AtomicInteger userCount = new AtomicInteger(0); SafeIterator.iterate(pmService.getUserSubjects().getAllSubjects(), pmUser -> { - UUID uuid = CommandUtils.parseUuid(pmUser.getIdentifier()); + UUID uuid = Uuids.parseNullable(pmUser.getIdentifier()); if (uuid == null) { log.logErr("Could not parse UUID for user: " + pmUser.getIdentifier()); return; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionsEx.java b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionsEx.java index 86a64ba75..d6b6a836c 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionsEx.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionsEx.java @@ -31,7 +31,6 @@ import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; import me.lucko.luckperms.common.commands.impl.migration.MigrationUtils; import me.lucko.luckperms.common.commands.sender.Sender; -import me.lucko.luckperms.common.commands.utils.CommandUtils; import me.lucko.luckperms.common.locale.CommandSpec; import me.lucko.luckperms.common.locale.LocaleManager; import me.lucko.luckperms.common.logging.ProgressLogger; @@ -41,6 +40,7 @@ import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.common.utils.SafeIterator; +import me.lucko.luckperms.common.utils.Uuids; import me.lucko.luckperms.sponge.LPSpongePlugin; import me.lucko.luckperms.sponge.service.LuckPermsService; @@ -172,7 +172,7 @@ public class MigrationPermissionsEx extends SubCommand { int userWeight = maxWeight + 5; SafeIterator.iterate(pexService.getUserSubjects().getAllSubjects(), pexUser -> { - UUID uuid = CommandUtils.parseUuid(pexUser.getIdentifier()); + UUID uuid = Uuids.parseNullable(pexUser.getIdentifier()); if (uuid == null) { log.logErr("Could not parse UUID for user: " + pexUser.getIdentifier()); return; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeGroup.java b/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeGroup.java index d4680fedd..035bc8120 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeGroup.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeGroup.java @@ -40,7 +40,7 @@ import me.lucko.luckperms.sponge.service.LuckPermsSubjectData; import me.lucko.luckperms.sponge.service.ProxyFactory; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; -import me.lucko.luckperms.sponge.service.model.SubjectReference; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.service.permission.PermissionService; @@ -123,13 +123,13 @@ public class SpongeGroup extends Group { } @Override - public boolean isChildOf(ImmutableContextSet contexts, SubjectReference parent) { + public boolean isChildOf(ImmutableContextSet contexts, LPSubjectReference parent) { return parent.getCollectionIdentifier().equals(PermissionService.SUBJECTS_GROUP) && getPermissionValue(contexts, NodeFactory.groupNode(parent.getSubjectIdentifier())).asBoolean(); } @Override - public ImmutableList getParents(ImmutableContextSet contexts) { - ImmutableSet.Builder subjects = ImmutableSet.builder(); + public ImmutableList getParents(ImmutableContextSet contexts) { + ImmutableSet.Builder subjects = ImmutableSet.builder(); for (Map.Entry entry : this.parent.getCachedData().getPermissionData(this.plugin.getContextManager().formContexts(contexts)).getImmutableBacking().entrySet()) { if (!entry.getValue()) { @@ -180,11 +180,6 @@ public class SpongeGroup extends Group { return this.plugin.getService().getDefaults().getOption(contexts, s); } - @Override - public ImmutableContextSet getActiveContextSet() { - return this.plugin.getContextManager().getApplicableContext(this.sponge()); - } - @Override public void invalidateCaches(CacheLevel cacheLevel) { // invalidate for all changes diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeUser.java b/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeUser.java index dcd4cf526..8585d735e 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeUser.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/model/SpongeUser.java @@ -40,7 +40,7 @@ import me.lucko.luckperms.sponge.service.LuckPermsSubjectData; import me.lucko.luckperms.sponge.service.ProxyFactory; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; -import me.lucko.luckperms.sponge.service.model.SubjectReference; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; import org.spongepowered.api.Sponge; import org.spongepowered.api.command.CommandSource; @@ -132,13 +132,13 @@ public class SpongeUser extends User { } @Override - public boolean isChildOf(ImmutableContextSet contexts, SubjectReference parent) { + public boolean isChildOf(ImmutableContextSet contexts, LPSubjectReference parent) { return parent.getCollectionIdentifier().equals(PermissionService.SUBJECTS_GROUP) && getPermissionValue(contexts, NodeFactory.groupNode(parent.getSubjectIdentifier())).asBoolean(); } @Override - public ImmutableList getParents(ImmutableContextSet contexts) { - ImmutableSet.Builder subjects = ImmutableSet.builder(); + public ImmutableList getParents(ImmutableContextSet contexts) { + ImmutableSet.Builder subjects = ImmutableSet.builder(); for (Map.Entry entry : this.parent.getCachedData().getPermissionData(this.plugin.getContextManager().formContexts(contexts)).getImmutableBacking().entrySet()) { if (!entry.getValue()) { @@ -189,11 +189,6 @@ public class SpongeUser extends User { return this.plugin.getService().getDefaults().getOption(contexts, s); } - @Override - public ImmutableContextSet getActiveContextSet() { - return this.plugin.getContextManager().getApplicableContext(this.sponge()); - } - @Override public void invalidateCaches(CacheLevel cacheLevel) { // invalidate for all changes diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/processors/DefaultsProcessor.java b/sponge/src/main/java/me/lucko/luckperms/sponge/processors/DefaultsProcessor.java new file mode 100644 index 000000000..d03622b7b --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/processors/DefaultsProcessor.java @@ -0,0 +1,66 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * 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.processors; + +import me.lucko.luckperms.api.Tristate; +import me.lucko.luckperms.api.context.ImmutableContextSet; +import me.lucko.luckperms.common.processors.PermissionProcessor; +import me.lucko.luckperms.sponge.service.LuckPermsService; +import me.lucko.luckperms.sponge.service.model.LPSubject; + +import java.util.Map; + +public abstract class DefaultsProcessor implements PermissionProcessor { + private final LuckPermsService service; + private final ImmutableContextSet contexts; + + public DefaultsProcessor(LuckPermsService service, ImmutableContextSet contexts) { + this.service = service; + this.contexts = contexts; + } + + protected abstract LPSubject getTypeDefaults(LuckPermsService service); + + @Override + public Tristate hasPermission(String permission) { + Tristate t = getTypeDefaults(this.service).getPermissionValue(this.contexts, permission); + if (t != Tristate.UNDEFINED) { + return t; + } + + t = this.service.getDefaults().getPermissionValue(this.contexts, permission); + if (t != Tristate.UNDEFINED) { + return t; + } + + return Tristate.UNDEFINED; + } + + @Override + public void updateBacking(Map map) { + // Do nothing, this doesn't use the backing + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/processors/GroupDefaultsProcessor.java b/sponge/src/main/java/me/lucko/luckperms/sponge/processors/GroupDefaultsProcessor.java index 820a83641..2f03b8ffd 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/processors/GroupDefaultsProcessor.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/processors/GroupDefaultsProcessor.java @@ -25,39 +25,18 @@ package me.lucko.luckperms.sponge.processors; -import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.common.processors.PermissionProcessor; import me.lucko.luckperms.sponge.service.LuckPermsService; +import me.lucko.luckperms.sponge.service.model.LPSubject; -import java.util.Map; - -public class GroupDefaultsProcessor implements PermissionProcessor { - private final LuckPermsService service; - private final ImmutableContextSet contexts; - +public class GroupDefaultsProcessor extends DefaultsProcessor implements PermissionProcessor { public GroupDefaultsProcessor(LuckPermsService service, ImmutableContextSet contexts) { - this.service = service; - this.contexts = contexts; + super(service, contexts); } @Override - public Tristate hasPermission(String permission) { - Tristate t = this.service.getGroupSubjects().getDefaults().getPermissionValue(this.contexts, permission); - if (t != Tristate.UNDEFINED) { - return t; - } - - t = this.service.getDefaults().getPermissionValue(this.contexts, permission); - if (t != Tristate.UNDEFINED) { - return t; - } - - return Tristate.UNDEFINED; - } - - @Override - public void updateBacking(Map map) { - // Do nothing, this doesn't use the backing + protected LPSubject getTypeDefaults(LuckPermsService service) { + return service.getGroupSubjects().getDefaults(); } } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/processors/UserDefaultsProcessor.java b/sponge/src/main/java/me/lucko/luckperms/sponge/processors/UserDefaultsProcessor.java index 1334e390f..cd5784544 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/processors/UserDefaultsProcessor.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/processors/UserDefaultsProcessor.java @@ -25,39 +25,18 @@ package me.lucko.luckperms.sponge.processors; -import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.common.processors.PermissionProcessor; import me.lucko.luckperms.sponge.service.LuckPermsService; +import me.lucko.luckperms.sponge.service.model.LPSubject; -import java.util.Map; - -public class UserDefaultsProcessor implements PermissionProcessor { - private final LuckPermsService service; - private final ImmutableContextSet contexts; - +public class UserDefaultsProcessor extends DefaultsProcessor implements PermissionProcessor { public UserDefaultsProcessor(LuckPermsService service, ImmutableContextSet contexts) { - this.service = service; - this.contexts = contexts; + super(service, contexts); } @Override - public Tristate hasPermission(String permission) { - Tristate t = this.service.getUserSubjects().getDefaults().getPermissionValue(this.contexts, permission); - if (t != Tristate.UNDEFINED) { - return t; - } - - t = this.service.getDefaults().getPermissionValue(this.contexts, permission); - if (t != Tristate.UNDEFINED) { - return t; - } - - return Tristate.UNDEFINED; - } - - @Override - public void updateBacking(Map map) { - // Do nothing, this doesn't use the backing + protected LPSubject getTypeDefaults(LuckPermsService service) { + return service.getUserSubjects().getDefaults(); } } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsPermissionDescription.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsPermissionDescription.java index 2a4d49bdd..078b93ee1 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsPermissionDescription.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsPermissionDescription.java @@ -29,7 +29,7 @@ import me.lucko.luckperms.sponge.service.model.LPPermissionDescription; import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; -import me.lucko.luckperms.sponge.service.model.SubjectReference; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; import org.spongepowered.api.plugin.PluginContainer; import org.spongepowered.api.service.permission.PermissionDescription; @@ -89,7 +89,7 @@ public final class LuckPermsPermissionDescription implements LPPermissionDescrip @SuppressWarnings("unchecked") @Override - public CompletableFuture> findAssignedSubjects(String id) { + public CompletableFuture> findAssignedSubjects(String id) { LPSubjectCollection collection = this.service.getCollection(id); return (CompletableFuture) collection.getAllWithPermission(this.id); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java index 6a885abbb..84e2d84ae 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsService.java @@ -42,9 +42,9 @@ import me.lucko.luckperms.sponge.service.model.LPPermissionDescription; import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; -import me.lucko.luckperms.sponge.service.model.SubjectReference; -import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory; import me.lucko.luckperms.sponge.service.persisted.PersistedCollection; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; +import me.lucko.luckperms.sponge.service.reference.SubjectReferenceFactory; import me.lucko.luckperms.sponge.service.storage.SubjectStorage; import org.spongepowered.api.plugin.PluginContainer; @@ -211,8 +211,8 @@ public class LuckPermsService implements LPPermissionService { } @Override - public ImmutableList sortSubjects(Collection s) { - List ret = new ArrayList<>(s); + public ImmutableList sortSubjects(Collection s) { + List ret = new ArrayList<>(s); ret.sort(Collections.reverseOrder((o1, o2) -> { if (o1.equals(o2)) { return 0; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java index 0aed07441..3104fa40a 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java @@ -40,7 +40,7 @@ import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.node.NodeFactory; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectData; -import me.lucko.luckperms.sponge.service.model.SubjectReference; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; import org.spongepowered.api.service.permission.PermissionService; @@ -178,11 +178,11 @@ public class LuckPermsSubjectData implements LPSubjectData { } @Override - public ImmutableMap> getAllParents() { - Map> parents = new HashMap<>(); + public ImmutableMap> getAllParents() { + Map> parents = new HashMap<>(); for (Map.Entry> e : (this.enduring ? this.holder.getEnduringNodes() : this.holder.getTransientNodes()).asMap().entrySet()) { - ImmutableList.Builder results = ImmutableList.builder(); + ImmutableList.Builder results = ImmutableList.builder(); for (Node n : e.getValue()) { if (n.isGroupNode()) { results.add(this.service.getGroupSubjects().loadSubject(n.getGroupName()).join().toReference()); @@ -191,15 +191,15 @@ public class LuckPermsSubjectData implements LPSubjectData { parents.put(e.getKey(), results); } - ImmutableMap.Builder> map = ImmutableMap.builder(); - for (Map.Entry> e : parents.entrySet()) { + ImmutableMap.Builder> map = ImmutableMap.builder(); + for (Map.Entry> e : parents.entrySet()) { map.put(e.getKey(), e.getValue().build()); } return map.build(); } @Override - public CompletableFuture addParent(ImmutableContextSet contexts, SubjectReference subject) { + public CompletableFuture addParent(ImmutableContextSet contexts, LPSubjectReference subject) { Objects.requireNonNull(contexts, "contexts"); Objects.requireNonNull(subject, "subject"); @@ -228,7 +228,7 @@ public class LuckPermsSubjectData implements LPSubjectData { } @Override - public CompletableFuture removeParent(ImmutableContextSet contexts, SubjectReference subject) { + public CompletableFuture removeParent(ImmutableContextSet contexts, LPSubjectReference subject) { Objects.requireNonNull(contexts, "contexts"); Objects.requireNonNull(subject, "subject"); diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/calculated/CalculatedSubjectData.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/calculated/CalculatedSubjectData.java index 5978a5cae..2ecd829c2 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/calculated/CalculatedSubjectData.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/calculated/CalculatedSubjectData.java @@ -45,7 +45,7 @@ import me.lucko.luckperms.sponge.processors.SpongeWildcardProcessor; import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectData; -import me.lucko.luckperms.sponge.service.model.SubjectReference; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; import java.util.HashMap; import java.util.List; @@ -70,7 +70,7 @@ public class CalculatedSubjectData implements LPSubjectData { private final String calculatorDisplayName; private final Map> permissions = new ConcurrentHashMap<>(); - private final Map> parents = new ConcurrentHashMap<>(); + private final Map> parents = new ConcurrentHashMap<>(); private final Map> options = new ConcurrentHashMap<>(); private final LoadingCache permissionCache = Caffeine.newBuilder() @@ -121,10 +121,10 @@ public class CalculatedSubjectData implements LPSubjectData { this.service.invalidateAllCaches(LPSubject.CacheLevel.PERMISSION); } - public void replaceParents(Map> map) { + public void replaceParents(Map> map) { this.parents.clear(); - for (Map.Entry> e : map.entrySet()) { - Set set = ConcurrentHashMap.newKeySet(); + for (Map.Entry> e : map.entrySet()) { + Set set = ConcurrentHashMap.newKeySet(); set.addAll(e.getValue()); this.parents.put(e.getKey(), set); } @@ -194,17 +194,17 @@ public class CalculatedSubjectData implements LPSubjectData { } @Override - public ImmutableMap> getAllParents() { - ImmutableMap.Builder> map = ImmutableMap.builder(); - for (Map.Entry> e : this.parents.entrySet()) { + public ImmutableMap> getAllParents() { + ImmutableMap.Builder> map = ImmutableMap.builder(); + for (Map.Entry> e : this.parents.entrySet()) { map.put(e.getKey(), this.service.sortSubjects(e.getValue())); } return map.build(); } @Override - public CompletableFuture addParent(ImmutableContextSet contexts, SubjectReference parent) { - Set set = this.parents.computeIfAbsent(contexts, c -> ConcurrentHashMap.newKeySet()); + public CompletableFuture addParent(ImmutableContextSet contexts, LPSubjectReference parent) { + Set set = this.parents.computeIfAbsent(contexts, c -> ConcurrentHashMap.newKeySet()); boolean b = set.add(parent); if (b) { this.service.invalidateAllCaches(LPSubject.CacheLevel.PARENT); @@ -213,8 +213,8 @@ public class CalculatedSubjectData implements LPSubjectData { } @Override - public CompletableFuture removeParent(ImmutableContextSet contexts, SubjectReference parent) { - Set set = this.parents.get(contexts); + public CompletableFuture removeParent(ImmutableContextSet contexts, LPSubjectReference parent) { + Set set = this.parents.get(contexts); boolean b = set != null && set.remove(parent); if (b) { this.service.invalidateAllCaches(LPSubject.CacheLevel.PARENT); @@ -235,7 +235,7 @@ public class CalculatedSubjectData implements LPSubjectData { @Override public CompletableFuture clearParents(ImmutableContextSet contexts) { - Set set = this.parents.get(contexts); + Set set = this.parents.get(contexts); if (set == null) { return CompletableFuture.completedFuture(false); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/legacy/SubjectDataHolder.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/legacy/SubjectDataHolder.java index 401e3faf3..54c3468b4 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/legacy/SubjectDataHolder.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/legacy/SubjectDataHolder.java @@ -27,7 +27,7 @@ package me.lucko.luckperms.sponge.service.legacy; import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.sponge.service.model.LPPermissionService; -import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory; +import me.lucko.luckperms.sponge.service.reference.SubjectReferenceFactory; import me.lucko.luckperms.sponge.service.storage.SubjectStorageModel; import java.util.List; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedCollection.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedCollection.java index 54262d39d..a6d06cee8 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedCollection.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedCollection.java @@ -40,12 +40,13 @@ import me.lucko.luckperms.sponge.service.LuckPermsService; import me.lucko.luckperms.sponge.service.ProxyFactory; import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; -import me.lucko.luckperms.sponge.service.model.SubjectReference; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; import me.lucko.luckperms.sponge.service.storage.SubjectStorageModel; import org.spongepowered.api.service.permission.SubjectCollection; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.concurrent.CompletableFuture; @@ -73,7 +74,9 @@ public class PersistedCollection implements LPSubjectCollection { Map holders = this.service.getStorage().loadAllFromFile(this.identifier); for (Map.Entry e : holders.entrySet()) { PersistedSubject subject = this.subjects.get(e.getKey().toLowerCase()); - subject.loadData(e.getValue()); + if (subject != null) { + subject.loadData(e.getValue()); + } } } @@ -104,7 +107,7 @@ public class PersistedCollection implements LPSubjectCollection { @Override public Optional getSubject(String identifier) { - return Optional.of(this.subjects.get(identifier.toLowerCase())); + return Optional.of(Objects.requireNonNull(this.subjects.get(identifier.toLowerCase()))); } @Override @@ -116,7 +119,7 @@ public class PersistedCollection implements LPSubjectCollection { public CompletableFuture> loadSubjects(Set identifiers) { ImmutableSet.Builder ret = ImmutableSet.builder(); for (String id : identifiers) { - ret.add(this.subjects.get(id.toLowerCase())); + ret.add(Objects.requireNonNull(this.subjects.get(id.toLowerCase()))); } return CompletableFuture.completedFuture(ret.build()); } @@ -132,13 +135,13 @@ public class PersistedCollection implements LPSubjectCollection { } @Override - public CompletableFuture> getAllWithPermission(String permission) { + public CompletableFuture> getAllWithPermission(String permission) { return CompletableFuture.completedFuture(getLoadedWithPermission(permission).entrySet().stream() .collect(ImmutableCollectors.toMap(e -> e.getKey().toReference(), Map.Entry::getValue))); } @Override - public CompletableFuture> getAllWithPermission(ImmutableContextSet contexts, String permission) { + public CompletableFuture> getAllWithPermission(ImmutableContextSet contexts, String permission) { return CompletableFuture.completedFuture(getLoadedWithPermission(contexts, permission).entrySet().stream() .collect(ImmutableCollectors.toMap(e -> e.getKey().toReference(), Map.Entry::getValue))); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedSubject.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedSubject.java index 902540c09..5b4989b68 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedSubject.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedSubject.java @@ -37,7 +37,7 @@ import me.lucko.luckperms.sponge.service.LuckPermsService; import me.lucko.luckperms.sponge.service.ProxyFactory; import me.lucko.luckperms.sponge.service.calculated.CalculatedSubjectData; import me.lucko.luckperms.sponge.service.model.LPSubject; -import me.lucko.luckperms.sponge.service.model.SubjectReference; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; import me.lucko.luckperms.sponge.service.storage.SubjectStorageModel; import org.spongepowered.api.command.CommandSource; @@ -66,7 +66,7 @@ public class PersistedSubject implements LPSubject { .expireAfterAccess(20, TimeUnit.MINUTES) .build(lookup -> lookupPermissionValue(lookup.getContexts(), lookup.getNode())); - private final LoadingCache> parentLookupCache = Caffeine.newBuilder() + private final LoadingCache> parentLookupCache = Caffeine.newBuilder() .expireAfterAccess(20, TimeUnit.MINUTES) .build(this::lookupParents); @@ -194,7 +194,7 @@ public class PersistedSubject implements LPSubject { } } - for (SubjectReference parent : getParents(contexts)) { + for (LPSubjectReference parent : getParents(contexts)) { res = parent.resolveLp().join().getPermissionValue(contexts, node); if (res != Tristate.UNDEFINED) { return res; @@ -214,8 +214,8 @@ public class PersistedSubject implements LPSubject { return res; } - private ImmutableList lookupParents(ImmutableContextSet contexts) { - List s = new ArrayList<>(); + private ImmutableList lookupParents(ImmutableContextSet contexts) { + List s = new ArrayList<>(); s.addAll(this.subjectData.getParents(contexts)); s.addAll(this.transientSubjectData.getParents(contexts)); @@ -253,7 +253,7 @@ public class PersistedSubject implements LPSubject { } } - for (SubjectReference parent : getParents(contexts)) { + for (LPSubjectReference parent : getParents(contexts)) { res = parent.resolveLp().join().getOption(contexts, key); if (res.isPresent()) { return res; @@ -283,7 +283,7 @@ public class PersistedSubject implements LPSubject { } @Override - public boolean isChildOf(ImmutableContextSet contexts, SubjectReference subject) { + public boolean isChildOf(ImmutableContextSet contexts, LPSubjectReference subject) { Objects.requireNonNull(contexts, "contexts"); Objects.requireNonNull(subject, "subject"); @@ -299,7 +299,7 @@ public class PersistedSubject implements LPSubject { } @Override - public ImmutableList getParents(ImmutableContextSet contexts) { + public ImmutableList getParents(ImmutableContextSet contexts) { Objects.requireNonNull(contexts, "contexts"); return this.parentLookupCache.get(contexts); } @@ -308,9 +308,4 @@ public class PersistedSubject implements LPSubject { public Optional getOption(ImmutableContextSet contexts, String key) { return this.optionLookupCache.get(OptionLookupKey.of(key, contexts)); } - - @Override - public ImmutableContextSet getActiveContextSet() { - return this.service.getPlugin().getContextManager().getApplicableContext(sponge()); - } } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedSubjectData.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedSubjectData.java index 330e98eed..96b3d4cef 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedSubjectData.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/PersistedSubjectData.java @@ -29,7 +29,7 @@ import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.sponge.service.LuckPermsService; import me.lucko.luckperms.sponge.service.calculated.CalculatedSubjectData; -import me.lucko.luckperms.sponge.service.model.SubjectReference; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; import java.util.concurrent.CompletableFuture; import java.util.function.Function; @@ -79,12 +79,12 @@ public class PersistedSubjectData extends CalculatedSubjectData implements Funct } @Override - public CompletableFuture addParent(ImmutableContextSet contexts, SubjectReference parent) { + public CompletableFuture addParent(ImmutableContextSet contexts, LPSubjectReference parent) { return super.addParent(contexts, parent).thenApply(this); } @Override - public CompletableFuture removeParent(ImmutableContextSet contexts, SubjectReference parent) { + public CompletableFuture removeParent(ImmutableContextSet contexts, LPSubjectReference parent) { return super.removeParent(contexts, parent).thenApply(this); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/storage/SubjectStorageModel.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/storage/SubjectStorageModel.java index 3124e1b83..b7bf72c4e 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/storage/SubjectStorageModel.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/storage/SubjectStorageModel.java @@ -38,8 +38,8 @@ import me.lucko.luckperms.common.contexts.ContextSetJsonSerializer; import me.lucko.luckperms.common.utils.CollationKeyCache; import me.lucko.luckperms.sponge.service.calculated.CalculatedSubjectData; import me.lucko.luckperms.sponge.service.model.LPPermissionService; -import me.lucko.luckperms.sponge.service.model.SubjectReference; -import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory; +import me.lucko.luckperms.sponge.service.reference.LPSubjectReference; +import me.lucko.luckperms.sponge.service.reference.SubjectReferenceFactory; import java.util.ArrayList; import java.util.List; @@ -52,9 +52,9 @@ public class SubjectStorageModel { private final LPPermissionService service; private final Map> permissions; private final Map> options; - private final Map> parents; + private final Map> parents; - public SubjectStorageModel(LPPermissionService service, Map> permissions, Map> options, Map> parents) { + public SubjectStorageModel(LPPermissionService service, Map> permissions, Map> options, Map> parents) { this.service = service; ImmutableMap.Builder> permissionsBuilder = ImmutableMap.builder(); @@ -69,8 +69,8 @@ public class SubjectStorageModel { } this.options = optionsBuilder.build(); - ImmutableMap.Builder> parentsBuilder = ImmutableMap.builder(); - for (Map.Entry> e : parents.entrySet()) { + ImmutableMap.Builder> parentsBuilder = ImmutableMap.builder(); + for (Map.Entry> e : parents.entrySet()) { parentsBuilder.put(e.getKey(), ImmutableList.copyOf(e.getValue())); } this.parents = parentsBuilder.build(); @@ -137,7 +137,7 @@ public class SubjectStorageModel { } this.options = optionsBuilder.build(); - ImmutableMap.Builder> parentsBuilder = ImmutableMap.builder(); + ImmutableMap.Builder> parentsBuilder = ImmutableMap.builder(); for (JsonElement e : parents) { if (!e.isJsonObject()) { continue; @@ -151,7 +151,7 @@ public class SubjectStorageModel { JsonArray data = section.get("data").getAsJsonArray(); ImmutableContextSet contextSet = ContextSetJsonSerializer.deserializeContextSet(context).makeImmutable(); - ImmutableList.Builder pars = ImmutableList.builder(); + ImmutableList.Builder pars = ImmutableList.builder(); for (JsonElement p : data) { if (!p.isJsonObject()) { continue; @@ -228,7 +228,7 @@ public class SubjectStorageModel { root.add("options", options); JsonArray parents = new JsonArray(); - for (Map.Entry> e : sortContextMap(this.parents)) { + for (Map.Entry> e : sortContextMap(this.parents)) { if (e.getValue().isEmpty()) { continue; } @@ -237,7 +237,7 @@ public class SubjectStorageModel { section.add("context", ContextSetJsonSerializer.serializeContextSet(e.getKey())); JsonArray data = new JsonArray(); - for (SubjectReference ref : e.getValue()) { + for (LPSubjectReference ref : e.getValue()) { JsonObject parent = new JsonObject(); parent.addProperty("collection", ref.getCollectionIdentifier()); parent.addProperty("subject", ref.getCollectionIdentifier());