diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/context/BungeeContextManager.java b/bungee/src/main/java/me/lucko/luckperms/bungee/context/BungeeContextManager.java index d4772301a..1529c184d 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/context/BungeeContextManager.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/context/BungeeContextManager.java @@ -25,25 +25,13 @@ package me.lucko.luckperms.bungee.context; -import com.github.benmanes.caffeine.cache.LoadingCache; import me.lucko.luckperms.bungee.LPBungeePlugin; -import me.lucko.luckperms.common.context.manager.ContextManager; -import me.lucko.luckperms.common.context.manager.InlineQueryOptionsSupplier; -import me.lucko.luckperms.common.context.manager.QueryOptionsSupplier; -import me.lucko.luckperms.common.util.CaffeineFactory; -import net.luckperms.api.context.ImmutableContextSet; -import net.luckperms.api.query.QueryOptions; +import me.lucko.luckperms.common.context.manager.InlineContextManager; import net.md_5.bungee.api.connection.ProxiedPlayer; import java.util.UUID; -import java.util.concurrent.TimeUnit; - -public class BungeeContextManager extends ContextManager { - - private final LoadingCache contextsCache = CaffeineFactory.newBuilder() - .expireAfterWrite(50, TimeUnit.MILLISECONDS) - .build(this::calculate); +public class BungeeContextManager extends InlineContextManager { public BungeeContextManager(LPBungeePlugin plugin) { super(plugin, ProxiedPlayer.class, ProxiedPlayer.class); } @@ -52,35 +40,4 @@ public class BungeeContextManager extends ContextManager(subject, this.contextsCache); - } - - // override getContext, getQueryOptions and invalidateCache to skip the QueryOptionsSupplier - @Override - public ImmutableContextSet getContext(ProxiedPlayer subject) { - return getQueryOptions(subject).context(); - } - - @Override - public QueryOptions getQueryOptions(ProxiedPlayer subject) { - return this.contextsCache.get(subject); - } - - @Override - protected void invalidateCache(ProxiedPlayer subject) { - this.contextsCache.invalidate(subject); - } - - @Override - public QueryOptions formQueryOptions(ProxiedPlayer subject, ImmutableContextSet contextSet) { - return formQueryOptions(contextSet); - } - } diff --git a/common/src/main/java/me/lucko/luckperms/common/context/manager/ContextManager.java b/common/src/main/java/me/lucko/luckperms/common/context/manager/ContextManager.java index 5b95b8f6a..4cc2d7a8b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/context/manager/ContextManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/context/manager/ContextManager.java @@ -48,7 +48,8 @@ import java.util.function.Predicate; /** * Base implementation of {@link ContextManager} which caches content lookups. * - * @param the calculator type + * @param the subject type + * @param

the player type */ public abstract class ContextManager { diff --git a/common/src/main/java/me/lucko/luckperms/common/context/manager/InlineContextManager.java b/common/src/main/java/me/lucko/luckperms/common/context/manager/InlineContextManager.java new file mode 100644 index 000000000..ced09b136 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/context/manager/InlineContextManager.java @@ -0,0 +1,90 @@ +/* + * 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.context.manager; + +import com.github.benmanes.caffeine.cache.LoadingCache; +import me.lucko.luckperms.common.plugin.LuckPermsPlugin; +import me.lucko.luckperms.common.util.CaffeineFactory; +import net.luckperms.api.context.ImmutableContextSet; +import net.luckperms.api.query.QueryOptions; + +import java.util.concurrent.TimeUnit; + +public abstract class InlineContextManager extends ContextManager { + + private final LoadingCache contextsCache = CaffeineFactory.newBuilder() + .expireAfterWrite(50, TimeUnit.MILLISECONDS) + .build(this::calculate); + + protected InlineContextManager(LuckPermsPlugin plugin, Class subjectClass, Class

playerClass) { + super(plugin, subjectClass, playerClass); + } + + @Override + public final QueryOptionsSupplier getCacheFor(S subject) { + if (subject == null) { + throw new NullPointerException("subject"); + } + + return new InlineQueryOptionsSupplier<>(subject, this.contextsCache); + } + + // override getContext, getQueryOptions and invalidateCache to skip the QueryOptionsSupplier + @Override + public final ImmutableContextSet getContext(S subject) { + return getQueryOptions(subject).context(); + } + + @Override + public final QueryOptions getQueryOptions(S subject) { + return this.contextsCache.get(subject); + } + + @Override + protected final void invalidateCache(S subject) { + this.contextsCache.invalidate(subject); + } + + @Override + public QueryOptions formQueryOptions(S subject, ImmutableContextSet contextSet) { + return formQueryOptions(contextSet); + } + + private static final class InlineQueryOptionsSupplier implements QueryOptionsSupplier { + private final T key; + private final LoadingCache cache; + + InlineQueryOptionsSupplier(T key, LoadingCache cache) { + this.key = key; + this.cache = cache; + } + + @Override + public QueryOptions getQueryOptions() { + return this.cache.get(this.key); + } + } +} diff --git a/common/src/main/java/me/lucko/luckperms/common/context/manager/InlineQueryOptionsSupplier.java b/common/src/main/java/me/lucko/luckperms/common/context/manager/InlineQueryOptionsSupplier.java deleted file mode 100644 index b4d7b6d5d..000000000 --- a/common/src/main/java/me/lucko/luckperms/common/context/manager/InlineQueryOptionsSupplier.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.context.manager; - -import com.github.benmanes.caffeine.cache.LoadingCache; -import net.luckperms.api.query.QueryOptions; - -public final class InlineQueryOptionsSupplier implements QueryOptionsSupplier { - private final T key; - private final LoadingCache cache; - - public InlineQueryOptionsSupplier(T key, LoadingCache cache) { - this.key = key; - this.cache = cache; - } - - @Override - public QueryOptions getQueryOptions() { - return this.cache.get(this.key); - } -} diff --git a/neoforge/build.gradle b/neoforge/build.gradle index 8e1adb360..4ea4651d8 100644 --- a/neoforge/build.gradle +++ b/neoforge/build.gradle @@ -26,7 +26,6 @@ neoForge { dependencies { add('shade', project(':common')) compileOnly project(':common:loader-utils') - compileOnly project(':neoforge:neoforge-api') } shadowJar { diff --git a/neoforge/loader/build.gradle b/neoforge/loader/build.gradle index 6522005e1..3d698ed58 100644 --- a/neoforge/loader/build.gradle +++ b/neoforge/loader/build.gradle @@ -47,12 +47,10 @@ configurations.implementation { dependencies { add('shade', project(':api')) add('shade', project(':common:loader-utils')) - add('shade', project(':neoforge:neoforge-api')) } build { dependsOn(":neoforge:build") - dependsOn(":neoforge:neoforge-api:build") } jar { diff --git a/neoforge/neoforge-api/build.gradle b/neoforge/neoforge-api/build.gradle deleted file mode 100644 index b231d62e4..000000000 --- a/neoforge/neoforge-api/build.gradle +++ /dev/null @@ -1,16 +0,0 @@ -plugins { - alias(libs.plugins.moddevgradle) -} - -sourceCompatibility = 17 -targetCompatibility = 21 - -neoForge { - version = project.neoForgeVersion - - validateAccessTransformers = true -} - -dependencies { - implementation project(':api') -} diff --git a/neoforge/neoforge-api/src/main/java/me/lucko/luckperms/neoforge/capabilities/UserCapability.java b/neoforge/neoforge-api/src/main/java/me/lucko/luckperms/neoforge/capabilities/UserCapability.java deleted file mode 100644 index 5fbdb85b2..000000000 --- a/neoforge/neoforge-api/src/main/java/me/lucko/luckperms/neoforge/capabilities/UserCapability.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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.neoforge.capabilities; - -import net.luckperms.api.query.QueryOptions; -import net.luckperms.api.util.Tristate; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.server.level.ServerPlayer; -import net.neoforged.neoforge.capabilities.EntityCapability; - -/** - * A NeoForge {@link EntityCapability} that attaches LuckPerms functionality onto {@link ServerPlayer}s. - */ -public interface UserCapability { - - /** - * The identifier used for the capability - */ - ResourceLocation IDENTIFIER = ResourceLocation.fromNamespaceAndPath("luckperms", "user"); - - /** - * The capability instance. - */ - EntityCapability CAPABILITY = EntityCapability.createVoid(IDENTIFIER, UserCapability.class); - - /** - * Checks for a permission. - * - * @param permission the permission - * @return the result - */ - default boolean hasPermission(String permission) { - return checkPermission(permission).asBoolean(); - } - - /** - * Runs a permission check. - * - * @param permission the permission - * @return the result - */ - Tristate checkPermission(String permission); - - /** - * Runs a permission check. - * - * @param permission the permission - * @param queryOptions the query options - * @return the result - */ - Tristate checkPermission(String permission, QueryOptions queryOptions); - - /** - * Gets the user's currently query options. - * - * @return the current query options for the user - */ - QueryOptions getQueryOptions(); - -} diff --git a/neoforge/src/main/java/me/lucko/luckperms/neoforge/LPNeoForgePlugin.java b/neoforge/src/main/java/me/lucko/luckperms/neoforge/LPNeoForgePlugin.java index 453614480..c2bf68c46 100644 --- a/neoforge/src/main/java/me/lucko/luckperms/neoforge/LPNeoForgePlugin.java +++ b/neoforge/src/main/java/me/lucko/luckperms/neoforge/LPNeoForgePlugin.java @@ -41,7 +41,6 @@ import me.lucko.luckperms.common.plugin.AbstractLuckPermsPlugin; import me.lucko.luckperms.common.sender.DummyConsoleSender; import me.lucko.luckperms.common.sender.Sender; import me.lucko.luckperms.neoforge.calculator.NeoForgeCalculatorFactory; -import me.lucko.luckperms.neoforge.capabilities.UserCapabilityListener; import me.lucko.luckperms.neoforge.context.NeoForgeContextManager; import me.lucko.luckperms.neoforge.context.NeoForgePlayerCalculator; import me.lucko.luckperms.neoforge.listeners.NeoForgeAutoOpListener; @@ -93,9 +92,6 @@ public class LPNeoForgePlugin extends AbstractLuckPermsPlugin { NeoForgePlatformListener platformListener = new NeoForgePlatformListener(this); this.bootstrap.registerListeners(platformListener); - UserCapabilityListener userCapabilityListener = new UserCapabilityListener(); - this.bootstrap.registerListeners(userCapabilityListener); - NeoForgePermissionHandlerListener permissionHandlerListener = new NeoForgePermissionHandlerListener(this); this.bootstrap.registerListeners(permissionHandlerListener); diff --git a/neoforge/src/main/java/me/lucko/luckperms/neoforge/NeoForgeSenderFactory.java b/neoforge/src/main/java/me/lucko/luckperms/neoforge/NeoForgeSenderFactory.java index 25049df2e..1dbd1521d 100644 --- a/neoforge/src/main/java/me/lucko/luckperms/neoforge/NeoForgeSenderFactory.java +++ b/neoforge/src/main/java/me/lucko/luckperms/neoforge/NeoForgeSenderFactory.java @@ -28,15 +28,15 @@ package me.lucko.luckperms.neoforge; import com.mojang.brigadier.ParseResults; import me.lucko.luckperms.common.cacheddata.result.TristateResult; import me.lucko.luckperms.common.locale.TranslationManager; +import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.query.QueryOptionsImpl; import me.lucko.luckperms.common.sender.Sender; import me.lucko.luckperms.common.sender.SenderFactory; import me.lucko.luckperms.common.verbose.VerboseCheckTarget; import me.lucko.luckperms.common.verbose.event.CheckOrigin; -import me.lucko.luckperms.neoforge.capabilities.UserCapability; -import me.lucko.luckperms.neoforge.capabilities.UserCapabilityImpl; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import net.luckperms.api.query.QueryOptions; import net.luckperms.api.util.Tristate; import net.minecraft.commands.CommandSource; import net.minecraft.commands.CommandSourceStack; @@ -72,24 +72,22 @@ public class NeoForgeSenderFactory extends SenderFactory toNativeText(TranslationManager.render(message, locale)), false); } @Override protected Tristate getPermissionValue(CommandSourceStack commandSource, String node) { - if (commandSource.getEntity() instanceof ServerPlayer) { - ServerPlayer player = (ServerPlayer) commandSource.getEntity(); - UserCapability user = UserCapabilityImpl.get(player); - return user.checkPermission(node); + if (commandSource.getEntity() instanceof ServerPlayer player) { + User user = getPlugin().getUserManager().getIfLoaded(player.getUUID()); + if (user == null) { + return Tristate.UNDEFINED; + } + + QueryOptions queryOptions = getPlugin().getContextManager().getQueryOptions(player); + return user.getCachedData().getPermissionData(queryOptions).checkPermission(node, CheckOrigin.PLATFORM_API_HAS_PERMISSION).result(); } VerboseCheckTarget target = VerboseCheckTarget.internal(commandSource.getTextName()); diff --git a/neoforge/src/main/java/me/lucko/luckperms/neoforge/capabilities/UserCapabilityImpl.java b/neoforge/src/main/java/me/lucko/luckperms/neoforge/capabilities/UserCapabilityImpl.java deleted file mode 100644 index cdaf197eb..000000000 --- a/neoforge/src/main/java/me/lucko/luckperms/neoforge/capabilities/UserCapabilityImpl.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * 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.neoforge.capabilities; - -import me.lucko.luckperms.common.cacheddata.type.PermissionCache; -import me.lucko.luckperms.common.context.manager.QueryOptionsCache; -import me.lucko.luckperms.common.locale.TranslationManager; -import me.lucko.luckperms.common.model.User; -import me.lucko.luckperms.common.verbose.event.CheckOrigin; -import me.lucko.luckperms.neoforge.context.NeoForgeContextManager; -import net.luckperms.api.query.QueryOptions; -import net.luckperms.api.util.Tristate; -import net.minecraft.server.level.ServerPlayer; -import net.minecraft.world.entity.player.Player; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.Locale; -import java.util.Optional; - -public class UserCapabilityImpl implements UserCapability { - - private static Optional getCapability(Player player) { - return Optional.ofNullable(player.getCapability(CAPABILITY)); - } - - /** - * Gets a {@link UserCapability} for a given {@link ServerPlayer}. - * - * @param player the player - * @return the capability - */ - public static @NotNull UserCapabilityImpl get(@NotNull Player player) { - return (UserCapabilityImpl) getCapability(player).orElseThrow(() -> new IllegalStateException("Capability missing for " + player.getUUID())); - } - - /** - * Gets a {@link UserCapability} for a given {@link ServerPlayer}. - * - * @param player the player - * @return the capability, or null - */ - public static @Nullable UserCapabilityImpl getNullable(@NotNull Player player) { - return (UserCapabilityImpl) getCapability(player).orElse(null); - } - - private boolean initialised = false; - private boolean invalidated = false; - - private User user; - private QueryOptionsCache queryOptionsCache; - private String language; - private Locale locale; - - public UserCapabilityImpl() { - - } - - public void initialise(UserCapabilityImpl previous) { - this.user = previous.user; - this.queryOptionsCache = previous.queryOptionsCache; - this.language = previous.language; - this.locale = previous.locale; - this.initialised = true; - } - - public void initialise(User user, ServerPlayer player, NeoForgeContextManager contextManager) { - this.user = user; - this.queryOptionsCache = new QueryOptionsCache<>(player, contextManager); - this.initialised = true; - } - - private void assertInitialised() { - if (!this.initialised) { - throw new IllegalStateException("Capability has not been initialised"); - } - if (this.invalidated) { - throw new IllegalStateException("Capability has been invalidated"); - } - } - - public void invalidate() { - this.invalidated = false; - this.user = null; - this.queryOptionsCache = null; - this.language = null; - this.locale = null; - } - - @Override - public Tristate checkPermission(String permission) { - assertInitialised(); - - if (permission == null) { - throw new NullPointerException("permission"); - } - - return checkPermission(permission, this.queryOptionsCache.getQueryOptions()); - } - - @Override - public Tristate checkPermission(String permission, QueryOptions queryOptions) { - assertInitialised(); - - if (permission == null) { - throw new NullPointerException("permission"); - } - - if (queryOptions == null) { - throw new NullPointerException("queryOptions"); - } - - PermissionCache cache = this.user.getCachedData().getPermissionData(queryOptions); - return cache.checkPermission(permission, CheckOrigin.PLATFORM_API_HAS_PERMISSION).result(); - } - - public User getUser() { - assertInitialised(); - return this.user; - } - - @Override - public QueryOptions getQueryOptions() { - return getQueryOptionsCache().getQueryOptions(); - } - - public QueryOptionsCache getQueryOptionsCache() { - assertInitialised(); - return this.queryOptionsCache; - } - - public Locale getLocale(ServerPlayer player) { - if (this.language == null || !this.language.equals(player.getLanguage())) { - this.language = player.getLanguage(); - this.locale = TranslationManager.parseLocale(this.language); - } - - return this.locale; - } -} \ No newline at end of file diff --git a/neoforge/src/main/java/me/lucko/luckperms/neoforge/capabilities/UserCapabilityListener.java b/neoforge/src/main/java/me/lucko/luckperms/neoforge/capabilities/UserCapabilityListener.java deleted file mode 100644 index c34f66473..000000000 --- a/neoforge/src/main/java/me/lucko/luckperms/neoforge/capabilities/UserCapabilityListener.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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.neoforge.capabilities; - -import net.minecraft.server.level.ServerPlayer; -import net.minecraft.world.entity.EntityType; -import net.minecraft.world.entity.player.Player; -import net.neoforged.bus.api.SubscribeEvent; -import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent; -import net.neoforged.neoforge.event.entity.player.PlayerEvent; - -public class UserCapabilityListener { - - @SubscribeEvent - public void onRegisterCapabilities(RegisterCapabilitiesEvent event) { - event.registerEntity( - UserCapability.CAPABILITY, - EntityType.PLAYER, - (player, ctx) -> { - if (!(player instanceof ServerPlayer)) { - // Don't attach to LocalPlayer - return null; - } - return new UserCapabilityImpl(); - } - ); - } - - @SubscribeEvent - public void onPlayerClone(PlayerEvent.Clone event) { - Player previousPlayer = event.getOriginal(); - Player currentPlayer = event.getEntity(); - - try { - UserCapabilityImpl previous = UserCapabilityImpl.get(previousPlayer); - UserCapabilityImpl current = UserCapabilityImpl.get(currentPlayer); - - current.initialise(previous); - previous.invalidate(); - current.getQueryOptionsCache().invalidate(); - } catch (IllegalStateException e) { - // continue on if we cannot copy original data - } - } - -} diff --git a/neoforge/src/main/java/me/lucko/luckperms/neoforge/context/NeoForgeContextManager.java b/neoforge/src/main/java/me/lucko/luckperms/neoforge/context/NeoForgeContextManager.java index 8aca67312..41632e229 100644 --- a/neoforge/src/main/java/me/lucko/luckperms/neoforge/context/NeoForgeContextManager.java +++ b/neoforge/src/main/java/me/lucko/luckperms/neoforge/context/NeoForgeContextManager.java @@ -26,10 +26,8 @@ package me.lucko.luckperms.neoforge.context; import me.lucko.luckperms.common.config.ConfigKeys; -import me.lucko.luckperms.common.context.manager.ContextManager; -import me.lucko.luckperms.common.context.manager.QueryOptionsCache; +import me.lucko.luckperms.common.context.manager.InlineContextManager; import me.lucko.luckperms.neoforge.LPNeoForgePlugin; -import me.lucko.luckperms.neoforge.capabilities.UserCapabilityImpl; import net.luckperms.api.context.ImmutableContextSet; import net.luckperms.api.query.OptionKey; import net.luckperms.api.query.QueryOptions; @@ -37,7 +35,7 @@ import net.minecraft.server.level.ServerPlayer; import java.util.UUID; -public class NeoForgeContextManager extends ContextManager { +public class NeoForgeContextManager extends InlineContextManager { public static final OptionKey INTEGRATED_SERVER_OWNER = OptionKey.of("integrated_server_owner", Boolean.class); public NeoForgeContextManager(LPNeoForgePlugin plugin) { @@ -49,15 +47,6 @@ public class NeoForgeContextManager extends ContextManager getCacheFor(ServerPlayer subject) { - if (subject == null) { - throw new NullPointerException("subject"); - } - - return UserCapabilityImpl.get(subject).getQueryOptionsCache(); - } - @Override public QueryOptions formQueryOptions(ServerPlayer subject, ImmutableContextSet contextSet) { QueryOptions.Builder builder = this.plugin.getConfiguration().get(ConfigKeys.GLOBAL_QUERY_OPTIONS).toBuilder(); @@ -67,13 +56,4 @@ public class NeoForgeContextManager extends ContextManager T getPermission(ServerPlayer player, PermissionNode node, PermissionDynamicContext... context) { - UserCapabilityImpl capability = UserCapabilityImpl.getNullable(player); - - if (capability != null) { - User user = capability.getUser(); - QueryOptions queryOptions = capability.getQueryOptionsCache().getQueryOptions(); - + User user = plugin.getUserManager().getIfLoaded(player.getUUID()); + if (user != null) { + QueryOptions queryOptions = plugin.getContextManager().getQueryOptions(player); T value = getPermissionValue(user, queryOptions, node, context); if (value != null) { return value; diff --git a/neoforge/src/main/java/me/lucko/luckperms/neoforge/util/BrigadierInjector.java b/neoforge/src/main/java/me/lucko/luckperms/neoforge/util/BrigadierInjector.java index 7cd65d6df..ed91fcd29 100644 --- a/neoforge/src/main/java/me/lucko/luckperms/neoforge/util/BrigadierInjector.java +++ b/neoforge/src/main/java/me/lucko/luckperms/neoforge/util/BrigadierInjector.java @@ -32,8 +32,7 @@ import me.lucko.luckperms.common.graph.Graph; import me.lucko.luckperms.common.graph.TraversalAlgorithm; import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.neoforge.LPNeoForgePlugin; -import me.lucko.luckperms.neoforge.capabilities.UserCapability; -import me.lucko.luckperms.neoforge.capabilities.UserCapabilityImpl; +import net.luckperms.api.query.QueryOptions; import net.luckperms.api.util.Tristate; import net.minecraft.commands.CommandSourceStack; import net.minecraft.server.level.ServerPlayer; @@ -140,22 +139,16 @@ public final class BrigadierInjector { @Override public boolean test(CommandSourceStack source) { - if (source.getEntity() instanceof ServerPlayer) { - ServerPlayer player = (ServerPlayer) source.getEntity(); - Tristate state = Tristate.UNDEFINED; - // If player is still connecting and has not been added to world then check LP user directly - if (!player.isAddedToLevel()) { - User user = this.plugin.getUserManager().getIfLoaded(player.getUUID()); - if (user == null) { - // Should never happen but just in case... - return false; - } - state = user.getCachedData().getPermissionData().checkPermission(permission); - } else { - UserCapability user = UserCapabilityImpl.get(player); - state = user.checkPermission(this.permission); + if (source.getEntity() instanceof ServerPlayer player) { + + User user = this.plugin.getUserManager().getIfLoaded(player.getUUID()); + if (user == null) { + return false; } + QueryOptions queryOptions = this.plugin.getContextManager().getQueryOptions(player); + Tristate state = user.getCachedData().getPermissionData(queryOptions).checkPermission(this.permission); + if (state != Tristate.UNDEFINED) { return state.asBoolean() && this.delegate.test(source.withPermission(4)); } diff --git a/settings.gradle b/settings.gradle index db360fbf6..5598dd55f 100644 --- a/settings.gradle +++ b/settings.gradle @@ -30,7 +30,6 @@ include ( 'fabric', 'neoforge', 'neoforge:loader', - 'neoforge:neoforge-api', 'forge', 'forge:loader', 'forge:forge-api', diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/context/SpongeContextManager.java b/sponge/src/main/java/me/lucko/luckperms/sponge/context/SpongeContextManager.java index 2fa491800..046b2f478 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/context/SpongeContextManager.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/context/SpongeContextManager.java @@ -25,31 +25,20 @@ package me.lucko.luckperms.sponge.context; -import com.github.benmanes.caffeine.cache.LoadingCache; -import me.lucko.luckperms.common.context.manager.ContextManager; -import me.lucko.luckperms.common.context.manager.InlineQueryOptionsSupplier; -import me.lucko.luckperms.common.context.manager.QueryOptionsSupplier; -import me.lucko.luckperms.common.util.CaffeineFactory; +import me.lucko.luckperms.common.context.manager.InlineContextManager; import me.lucko.luckperms.sponge.LPSpongePlugin; import me.lucko.luckperms.sponge.service.model.ContextCalculatorProxy; import me.lucko.luckperms.sponge.service.model.TemporaryCauseHolderSubject; import net.luckperms.api.context.ContextCalculator; import net.luckperms.api.context.ContextConsumer; -import net.luckperms.api.context.ImmutableContextSet; import net.luckperms.api.context.StaticContextCalculator; -import net.luckperms.api.query.QueryOptions; import org.spongepowered.api.entity.living.player.server.ServerPlayer; import org.spongepowered.api.event.Cause; import org.spongepowered.api.service.permission.Subject; import java.util.UUID; -import java.util.concurrent.TimeUnit; -public class SpongeContextManager extends ContextManager { - - private final LoadingCache contextsCache = CaffeineFactory.newBuilder() - .expireAfterWrite(50, TimeUnit.MILLISECONDS) - .build(this::calculate); +public class SpongeContextManager extends InlineContextManager { public SpongeContextManager(LPSpongePlugin plugin) { super(plugin, Subject.class, ServerPlayer.class); @@ -86,34 +75,4 @@ public class SpongeContextManager extends ContextManager public UUID getUniqueId(ServerPlayer player) { return player.uniqueId(); } - - @Override - public QueryOptionsSupplier getCacheFor(Subject subject) { - if (subject == null) { - throw new NullPointerException("subject"); - } - - return new InlineQueryOptionsSupplier<>(subject, this.contextsCache); - } - - // override getContext, getQueryOptions and invalidateCache to skip the QueryOptionsSupplier - @Override - public ImmutableContextSet getContext(Subject subject) { - return getQueryOptions(subject).context(); - } - - @Override - public QueryOptions getQueryOptions(Subject subject) { - return this.contextsCache.get(subject); - } - - @Override - protected void invalidateCache(Subject subject) { - this.contextsCache.invalidate(subject); - } - - @Override - public QueryOptions formQueryOptions(Subject subject, ImmutableContextSet contextSet) { - return formQueryOptions(contextSet); - } } diff --git a/velocity/src/main/java/me/lucko/luckperms/velocity/context/VelocityContextManager.java b/velocity/src/main/java/me/lucko/luckperms/velocity/context/VelocityContextManager.java index add4b7501..50bbc15b5 100644 --- a/velocity/src/main/java/me/lucko/luckperms/velocity/context/VelocityContextManager.java +++ b/velocity/src/main/java/me/lucko/luckperms/velocity/context/VelocityContextManager.java @@ -25,25 +25,13 @@ package me.lucko.luckperms.velocity.context; -import com.github.benmanes.caffeine.cache.LoadingCache; import com.velocitypowered.api.proxy.Player; -import me.lucko.luckperms.common.context.manager.ContextManager; -import me.lucko.luckperms.common.context.manager.QueryOptionsCache; -import me.lucko.luckperms.common.context.manager.QueryOptionsSupplier; -import me.lucko.luckperms.common.util.CaffeineFactory; +import me.lucko.luckperms.common.context.manager.InlineContextManager; import me.lucko.luckperms.velocity.LPVelocityPlugin; -import net.luckperms.api.context.ImmutableContextSet; -import net.luckperms.api.query.QueryOptions; import java.util.UUID; -import java.util.concurrent.TimeUnit; - -public class VelocityContextManager extends ContextManager { - - private final LoadingCache> subjectCaches = CaffeineFactory.newBuilder() - .expireAfterAccess(1, TimeUnit.MINUTES) - .build(key -> new QueryOptionsCache<>(key, this)); +public class VelocityContextManager extends InlineContextManager { public VelocityContextManager(LPVelocityPlugin plugin) { super(plugin, Player.class, Player.class); } @@ -52,26 +40,4 @@ public class VelocityContextManager extends ContextManager { public UUID getUniqueId(Player player) { return player.getUniqueId(); } - - @Override - public QueryOptionsSupplier getCacheFor(Player subject) { - if (subject == null) { - throw new NullPointerException("subject"); - } - - return this.subjectCaches.get(subject); - } - - @Override - protected void invalidateCache(Player subject) { - QueryOptionsCache cache = this.subjectCaches.getIfPresent(subject); - if (cache != null) { - cache.invalidate(); - } - } - - @Override - public QueryOptions formQueryOptions(Player subject, ImmutableContextSet contextSet) { - return formQueryOptions(contextSet); - } }