From cc67b99c8bcf52e1eb49621ed82a17f621daf518 Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 16 Jun 2023 01:27:27 +0200 Subject: [PATCH 1/2] permission Tristate and function exchanged with adventure --- .../permission/PermissionsSetupEvent.java | 10 +- .../api/permission/PermissionFunction.java | 38 ------- .../api/permission/PermissionProvider.java | 8 +- .../api/permission/PermissionSubject.java | 7 +- .../api/permission/Tristate.java | 102 ------------------ .../proxy/command/builtin/GlistCommand.java | 4 +- .../proxy/command/builtin/ServerCommand.java | 4 +- .../command/builtin/VelocityCommand.java | 12 +-- .../connection/client/AuthSessionHandler.java | 9 +- .../connection/client/ConnectedPlayer.java | 19 ++-- .../proxy/console/VelocityConsole.java | 19 ++-- .../proxy/command/CommandTestSuite.java | 5 +- .../proxy/command/MockCommandSource.java | 6 +- 13 files changed, 52 insertions(+), 191 deletions(-) delete mode 100644 api/src/main/java/com/velocitypowered/api/permission/PermissionFunction.java delete mode 100644 api/src/main/java/com/velocitypowered/api/permission/Tristate.java diff --git a/api/src/main/java/com/velocitypowered/api/event/permission/PermissionsSetupEvent.java b/api/src/main/java/com/velocitypowered/api/event/permission/PermissionsSetupEvent.java index 7c2d47bec..29b304529 100644 --- a/api/src/main/java/com/velocitypowered/api/event/permission/PermissionsSetupEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/permission/PermissionsSetupEvent.java @@ -9,9 +9,9 @@ package com.velocitypowered.api.event.permission; import com.google.common.base.Preconditions; import com.velocitypowered.api.event.annotation.AwaitingEvent; -import com.velocitypowered.api.permission.PermissionFunction; import com.velocitypowered.api.permission.PermissionProvider; import com.velocitypowered.api.permission.PermissionSubject; +import net.kyori.adventure.permission.PermissionChecker; import org.checkerframework.checker.nullness.qual.Nullable; /** @@ -44,13 +44,13 @@ public final class PermissionsSetupEvent { } /** - * Uses the provider function to obtain a {@link PermissionFunction} for the subject. + * Uses the provider function to obtain a {@link PermissionChecker} for the subject. * * @param subject the subject * @return the obtained permission function */ - public PermissionFunction createFunction(PermissionSubject subject) { - return this.provider.createFunction(subject); + public PermissionChecker createChecker(PermissionSubject subject) { + return this.provider.createChecker(subject); } public PermissionProvider getProvider() { @@ -58,7 +58,7 @@ public final class PermissionsSetupEvent { } /** - * Sets the {@link PermissionFunction} that should be used for the subject. + * Sets the {@link PermissionChecker} that should be used for the subject. * *

Specifying null will reset the provider to the default * instance given when the event was posted.

diff --git a/api/src/main/java/com/velocitypowered/api/permission/PermissionFunction.java b/api/src/main/java/com/velocitypowered/api/permission/PermissionFunction.java deleted file mode 100644 index aa4c9b65f..000000000 --- a/api/src/main/java/com/velocitypowered/api/permission/PermissionFunction.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2018-2021 Velocity Contributors - * - * The Velocity API is licensed under the terms of the MIT License. For more details, - * reference the LICENSE file in the api top-level directory. - */ - -package com.velocitypowered.api.permission; - -/** - * Function that calculates the permission settings for a given {@link PermissionSubject}. - */ -@FunctionalInterface -public interface PermissionFunction { - - /** - * A permission function that always returns {@link Tristate#TRUE}. - */ - PermissionFunction ALWAYS_TRUE = p -> Tristate.TRUE; - - /** - * A permission function that always returns {@link Tristate#FALSE}. - */ - PermissionFunction ALWAYS_FALSE = p -> Tristate.FALSE; - - /** - * A permission function that always returns {@link Tristate#UNDEFINED}. - */ - PermissionFunction ALWAYS_UNDEFINED = p -> Tristate.UNDEFINED; - - /** - * Gets the subjects setting for a particular permission. - * - * @param permission the permission - * @return the value the permission is set to - */ - Tristate getPermissionValue(String permission); -} diff --git a/api/src/main/java/com/velocitypowered/api/permission/PermissionProvider.java b/api/src/main/java/com/velocitypowered/api/permission/PermissionProvider.java index f7a9251f5..8a1131265 100644 --- a/api/src/main/java/com/velocitypowered/api/permission/PermissionProvider.java +++ b/api/src/main/java/com/velocitypowered/api/permission/PermissionProvider.java @@ -7,17 +7,19 @@ package com.velocitypowered.api.permission; +import net.kyori.adventure.permission.PermissionChecker; + /** - * Provides {@link PermissionFunction}s for {@link PermissionSubject}s. + * Provides {@link PermissionChecker}s for {@link PermissionSubject}s. */ @FunctionalInterface public interface PermissionProvider { /** - * Creates a {@link PermissionFunction} for the subject. + * Creates a {@link PermissionChecker} for the subject. * * @param subject the subject * @return the function */ - PermissionFunction createFunction(PermissionSubject subject); + PermissionChecker createChecker(PermissionSubject subject); } diff --git a/api/src/main/java/com/velocitypowered/api/permission/PermissionSubject.java b/api/src/main/java/com/velocitypowered/api/permission/PermissionSubject.java index 14d88a980..3907c2b3e 100644 --- a/api/src/main/java/com/velocitypowered/api/permission/PermissionSubject.java +++ b/api/src/main/java/com/velocitypowered/api/permission/PermissionSubject.java @@ -8,6 +8,7 @@ package com.velocitypowered.api.permission; import net.kyori.adventure.permission.PermissionChecker; +import net.kyori.adventure.util.TriState; /** * Represents a object that has a set of queryable permissions. @@ -21,7 +22,7 @@ public interface PermissionSubject { * @return whether or not the subject has the permission */ default boolean hasPermission(String permission) { - return getPermissionValue(permission).asBoolean(); + return getPermissionValue(permission).toBooleanOrElse(false); } /** @@ -30,7 +31,7 @@ public interface PermissionSubject { * @param permission the permission * @return the value the permission is set to */ - Tristate getPermissionValue(String permission); + TriState getPermissionValue(String permission); /** * Gets the permission checker for the subject. @@ -38,6 +39,6 @@ public interface PermissionSubject { * @return subject's permission checker */ default PermissionChecker getPermissionChecker() { - return permission -> getPermissionValue(permission).toAdventureTriState(); + return this::getPermissionValue; } } diff --git a/api/src/main/java/com/velocitypowered/api/permission/Tristate.java b/api/src/main/java/com/velocitypowered/api/permission/Tristate.java deleted file mode 100644 index 189cc2789..000000000 --- a/api/src/main/java/com/velocitypowered/api/permission/Tristate.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright (C) 2018-2022 Velocity Contributors - * - * The Velocity API is licensed under the terms of the MIT License. For more details, - * reference the LICENSE file in the api top-level directory. - */ - -package com.velocitypowered.api.permission; - -import net.kyori.adventure.util.TriState; -import org.checkerframework.checker.nullness.qual.Nullable; - -/** - * Represents three different states of a setting. - * - *

Possible values:

- *

- * - */ -public enum Tristate { - - /** - * A value indicating a positive setting. - */ - TRUE(true), - - /** - * A value indicating a negative (negated) setting. - */ - FALSE(false), - - /** - * A value indicating a non-existent setting. - */ - UNDEFINED(false); - - /** - * Returns a {@link Tristate} from a boolean. - * - * @param val the boolean value - * @return {@link #TRUE} or {@link #FALSE}, if the value is true or - * false, respectively. - */ - public static Tristate fromBoolean(boolean val) { - return val ? TRUE : FALSE; - } - - /** - * Returns a {@link Tristate} from a nullable boolean. - * - *

Unlike {@link #fromBoolean(boolean)}, this method returns {@link #UNDEFINED} - * if the value is null.

- * - * @param val the boolean value - * @return {@link #UNDEFINED}, {@link #TRUE} or {@link #FALSE}, if the value is null, - * true or false, respectively. - */ - public static Tristate fromNullableBoolean(@Nullable Boolean val) { - if (val == null) { - return UNDEFINED; - } - return val ? TRUE : FALSE; - } - - private final boolean booleanValue; - - Tristate(boolean booleanValue) { - this.booleanValue = booleanValue; - } - - /** - * Returns the value of the Tristate as a boolean. - * - *

A value of {@link #UNDEFINED} converts to false.

- * - * @return a boolean representation of the Tristate. - */ - public boolean asBoolean() { - return this.booleanValue; - } - - /** - * Returns the equivalent Adventure {@link TriState}. - * - * @return equivalent Adventure TriState - */ - public TriState toAdventureTriState() { - if (this == Tristate.TRUE) { - return TriState.TRUE; - } else if (this == Tristate.UNDEFINED) { - return TriState.NOT_SET; - } else if (this == Tristate.FALSE) { - return TriState.FALSE; - } else { - throw new IllegalArgumentException(); - } - } -} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/GlistCommand.java b/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/GlistCommand.java index 5d2f19fea..5ad86511c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/GlistCommand.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/GlistCommand.java @@ -28,7 +28,6 @@ import com.mojang.brigadier.tree.ArgumentCommandNode; import com.mojang.brigadier.tree.LiteralCommandNode; import com.velocitypowered.api.command.BrigadierCommand; import com.velocitypowered.api.command.CommandSource; -import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.server.RegisteredServer; @@ -39,6 +38,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.util.TriState; /** * Implements the Velocity default {@code /glist} command. @@ -60,7 +60,7 @@ public class GlistCommand { LiteralCommandNode totalNode = LiteralArgumentBuilder .literal("glist") .requires(source -> - source.getPermissionValue("velocity.command.glist") == Tristate.TRUE) + source.getPermissionValue("velocity.command.glist") == TriState.TRUE) .executes(this::totalCount) .build(); ArgumentCommandNode serverNode = RequiredArgumentBuilder diff --git a/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/ServerCommand.java b/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/ServerCommand.java index 1064f0794..a1bb1e20f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/ServerCommand.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/ServerCommand.java @@ -22,7 +22,6 @@ import static net.kyori.adventure.text.event.HoverEvent.showText; import com.google.common.collect.ImmutableList; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; -import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ServerConnection; @@ -37,6 +36,7 @@ import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.util.TriState; /** * Implements Velocity's {@code /server} command. @@ -161,6 +161,6 @@ public class ServerCommand implements SimpleCommand { @Override public boolean hasPermission(final SimpleCommand.Invocation invocation) { - return invocation.source().getPermissionValue("velocity.command.server") != Tristate.FALSE; + return invocation.source().getPermissionValue("velocity.command.server") != TriState.FALSE; } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/VelocityCommand.java b/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/VelocityCommand.java index 93b27243b..2228f3d7e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/VelocityCommand.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/VelocityCommand.java @@ -24,7 +24,6 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; -import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.plugin.PluginContainer; import com.velocitypowered.api.plugin.PluginDescription; import com.velocitypowered.api.proxy.ProxyServer; @@ -59,6 +58,7 @@ import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextDecoration; +import net.kyori.adventure.util.TriState; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.checkerframework.checker.nullness.qual.NonNull; @@ -200,7 +200,7 @@ public class VelocityCommand implements SimpleCommand { @Override public boolean hasPermission(final CommandSource source, final String @NonNull [] args) { - return source.getPermissionValue("velocity.command.reload") == Tristate.TRUE; + return source.getPermissionValue("velocity.command.reload") == TriState.TRUE; } } @@ -255,7 +255,7 @@ public class VelocityCommand implements SimpleCommand { @Override public boolean hasPermission(final CommandSource source, final String @NonNull [] args) { - return source.getPermissionValue("velocity.command.info") != Tristate.FALSE; + return source.getPermissionValue("velocity.command.info") != TriState.FALSE; } } @@ -336,7 +336,7 @@ public class VelocityCommand implements SimpleCommand { @Override public boolean hasPermission(final CommandSource source, final String @NonNull [] args) { - return source.getPermissionValue("velocity.command.plugins") == Tristate.TRUE; + return source.getPermissionValue("velocity.command.plugins") == TriState.TRUE; } } @@ -405,7 +405,7 @@ public class VelocityCommand implements SimpleCommand { @Override public boolean hasPermission(final CommandSource source, final String @NonNull [] args) { - return source.getPermissionValue("velocity.command.plugins") == Tristate.TRUE; + return source.getPermissionValue("velocity.command.plugins") == TriState.TRUE; } } @@ -481,7 +481,7 @@ public class VelocityCommand implements SimpleCommand { @Override public boolean hasPermission(CommandSource source, String @NonNull [] args) { - return source.getPermissionValue("velocity.command.heap") == Tristate.TRUE; + return source.getPermissionValue("velocity.command.heap") == TriState.TRUE; } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/AuthSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/AuthSessionHandler.java index f68d3bb59..bfb67de31 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/AuthSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/AuthSessionHandler.java @@ -26,7 +26,6 @@ import com.velocitypowered.api.event.connection.PostLoginEvent; import com.velocitypowered.api.event.permission.PermissionsSetupEvent; import com.velocitypowered.api.event.player.GameProfileRequestEvent; import com.velocitypowered.api.event.player.PlayerChooseInitialServerEvent; -import com.velocitypowered.api.permission.PermissionFunction; import com.velocitypowered.api.proxy.crypto.IdentifiedKey; import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.util.GameProfile; @@ -45,6 +44,8 @@ import java.util.Objects; import java.util.Optional; import java.util.UUID; import java.util.concurrent.CompletableFuture; + +import net.kyori.adventure.permission.PermissionChecker; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.apache.logging.log4j.LogManager; @@ -107,8 +108,8 @@ public class AuthSessionHandler implements MinecraftSessionHandler { .thenAcceptAsync(event -> { if (!mcConnection.isClosed()) { // wait for permissions to load, then set the players permission function - final PermissionFunction function = event.createFunction(player); - if (function == null) { + final PermissionChecker checker = event.createChecker(player); + if (checker == null) { logger.error( "A plugin permission provider {} provided an invalid permission function" + " for player {}. This is a bug in the plugin, not in Velocity. Falling" @@ -116,7 +117,7 @@ public class AuthSessionHandler implements MinecraftSessionHandler { event.getProvider().getClass().getName(), player.getUsername()); } else { - player.setPermissionFunction(function); + player.setPermissionChecker(checker); } completeLoginProtocolPhaseAndInitialize(player); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index 15a960e8d..64edce738 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -35,9 +35,7 @@ import com.velocitypowered.api.event.player.PlayerResourcePackStatusEvent; import com.velocitypowered.api.event.player.PlayerSettingsChangedEvent; import com.velocitypowered.api.event.player.ServerPreConnectEvent; import com.velocitypowered.api.network.ProtocolVersion; -import com.velocitypowered.api.permission.PermissionFunction; import com.velocitypowered.api.permission.PermissionProvider; -import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.proxy.ConnectionRequestBuilder; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ServerConnection; @@ -111,6 +109,7 @@ import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.kyori.adventure.title.Title.Times; import net.kyori.adventure.title.TitlePart; import net.kyori.adventure.translation.GlobalTranslator; +import net.kyori.adventure.util.TriState; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; @@ -132,7 +131,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, .mapper(TranslatableComponent.class, TranslatableComponent::key) .build()) .build(); - static final PermissionProvider DEFAULT_PERMISSIONS = s -> PermissionFunction.ALWAYS_UNDEFINED; + static final PermissionProvider DEFAULT_PERMISSIONS = s -> PermissionChecker.always(TriState.NOT_SET); private static final Logger logger = LogManager.getLogger(ConnectedPlayer.class); @@ -143,7 +142,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, private final MinecraftConnection connection; private final @Nullable InetSocketAddress virtualHost; private GameProfile profile; - private PermissionFunction permissionFunction; + private PermissionChecker permissionChecker; private int tryIndex = 0; private long ping = -1; private final boolean onlineMode; @@ -168,7 +167,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, .withDynamic(Identity.NAME, this::getUsername) .withDynamic(Identity.DISPLAY_NAME, () -> Component.text(this.getUsername())) .withDynamic(Identity.LOCALE, this::getEffectiveLocale) - .withStatic(PermissionChecker.POINTER, getPermissionChecker()) + .withDynamic(PermissionChecker.POINTER, () -> this.permissionChecker) .withStatic(FacetPointers.TYPE, Type.PLAYER) .build(); private @Nullable String clientBrand; @@ -184,7 +183,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, this.profile = profile; this.connection = connection; this.virtualHost = virtualHost; - this.permissionFunction = PermissionFunction.ALWAYS_UNDEFINED; + this.permissionChecker = PermissionChecker.always(TriState.NOT_SET); this.connectionPhase = connection.getType().getInitialClientPhase(); this.knownChannels = CappedSet.create(MAX_PLUGIN_CHANNELS); this.onlineMode = onlineMode; @@ -319,8 +318,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, return Optional.ofNullable(virtualHost); } - void setPermissionFunction(PermissionFunction permissionFunction) { - this.permissionFunction = permissionFunction; + void setPermissionChecker(PermissionChecker permissionChecker) { + this.permissionChecker = permissionChecker; } @Override @@ -909,8 +908,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, } @Override - public Tristate getPermissionValue(String permission) { - return permissionFunction.getPermissionValue(permission); + public TriState getPermissionValue(String permission) { + return permissionChecker.value(permission); } @Override diff --git a/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java b/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java index 08bc04c07..6450d403c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java @@ -17,11 +17,7 @@ package com.velocitypowered.proxy.console; -import static com.velocitypowered.api.permission.PermissionFunction.ALWAYS_TRUE; - import com.velocitypowered.api.event.permission.PermissionsSetupEvent; -import com.velocitypowered.api.permission.PermissionFunction; -import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.proxy.ConsoleCommandSource; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.util.ClosestLocaleMatcher; @@ -37,6 +33,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.translation.GlobalTranslator; +import net.kyori.adventure.util.TriState; import net.minecrell.terminalconsole.SimpleTerminalConsole; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; @@ -57,7 +54,7 @@ public final class VelocityConsole extends SimpleTerminalConsole implements Cons private static final Logger logger = LogManager.getLogger(VelocityConsole.class); private final VelocityServer server; - private PermissionFunction permissionFunction = ALWAYS_TRUE; + private PermissionChecker permissionChecker = PermissionChecker.always(TriState.TRUE); private final @NotNull Pointers pointers = ConsoleCommandSource.super.pointers().toBuilder() .withDynamic(PermissionChecker.POINTER, this::getPermissionChecker) .withDynamic(Identity.LOCALE, () -> ClosestLocaleMatcher.INSTANCE @@ -78,8 +75,8 @@ public final class VelocityConsole extends SimpleTerminalConsole implements Cons } @Override - public @NonNull Tristate getPermissionValue(@NonNull String permission) { - return this.permissionFunction.getPermissionValue(permission); + public @NonNull TriState getPermissionValue(@NonNull String permission) { + return this.permissionChecker.value(permission); } /** @@ -94,16 +91,16 @@ public final class VelocityConsole extends SimpleTerminalConsole implements Cons * Sets up permissions for the console. */ public void setupPermissions() { - PermissionsSetupEvent event = new PermissionsSetupEvent(this, s -> ALWAYS_TRUE); + PermissionsSetupEvent event = new PermissionsSetupEvent(this, s -> PermissionChecker.always(TriState.TRUE)); // we can safely block here, this is before any listeners fire - this.permissionFunction = this.server.getEventManager().fire(event).join().createFunction(this); - if (this.permissionFunction == null) { + this.permissionChecker = this.server.getEventManager().fire(event).join().createChecker(this); + if (this.permissionChecker == null) { logger.error( "A plugin permission provider {} provided an invalid permission function" + " for the console. This is a bug in the plugin, not in Velocity. Falling" + " back to the default permission function.", event.getProvider().getClass().getName()); - this.permissionFunction = ALWAYS_TRUE; + this.permissionChecker = PermissionChecker.always(TriState.TRUE); } } diff --git a/proxy/src/test/java/com/velocitypowered/proxy/command/CommandTestSuite.java b/proxy/src/test/java/com/velocitypowered/proxy/command/CommandTestSuite.java index c8e17d0f8..994425aa2 100644 --- a/proxy/src/test/java/com/velocitypowered/proxy/command/CommandTestSuite.java +++ b/proxy/src/test/java/com/velocitypowered/proxy/command/CommandTestSuite.java @@ -25,12 +25,13 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import com.velocitypowered.api.command.CommandSource; -import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.proxy.event.MockEventManager; import com.velocitypowered.proxy.event.VelocityEventManager; import java.util.Arrays; import java.util.Collection; + +import net.kyori.adventure.util.TriState; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -66,7 +67,7 @@ abstract class CommandTestSuite { final void assertPlayerSuggestions(final String input, final String... expectedSuggestions) { final var player = mock(Player.class); - when(player.getPermissionValue(any())).thenReturn(Tristate.UNDEFINED); + when(player.getPermissionValue(any())).thenReturn(TriState.NOT_SET); final var actual = manager.offerSuggestions(player, input).join(); assertEquals(Arrays.asList(expectedSuggestions), actual); } diff --git a/proxy/src/test/java/com/velocitypowered/proxy/command/MockCommandSource.java b/proxy/src/test/java/com/velocitypowered/proxy/command/MockCommandSource.java index 6fc48ffc0..192c85ea8 100644 --- a/proxy/src/test/java/com/velocitypowered/proxy/command/MockCommandSource.java +++ b/proxy/src/test/java/com/velocitypowered/proxy/command/MockCommandSource.java @@ -18,7 +18,7 @@ package com.velocitypowered.proxy.command; import com.velocitypowered.api.command.CommandSource; -import com.velocitypowered.api.permission.Tristate; +import net.kyori.adventure.util.TriState; /** * A fake {@link CommandSource}. @@ -28,7 +28,7 @@ public class MockCommandSource implements CommandSource { public static final CommandSource INSTANCE = new MockCommandSource(); @Override - public Tristate getPermissionValue(final String permission) { - return Tristate.UNDEFINED; + public TriState getPermissionValue(final String permission) { + return TriState.NOT_SET; } } From 48e4fa637efd2179f621bf72a19c910fdaf1861d Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 16 Jun 2023 10:25:01 +0200 Subject: [PATCH 2/2] expose PermissionChecker and use this --- .../api/permission/PermissionSubject.java | 10 +++++----- .../proxy/connection/client/ConnectedPlayer.java | 4 ++-- .../velocitypowered/proxy/console/VelocityConsole.java | 4 ++-- .../proxy/command/MockCommandSource.java | 5 +++-- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/permission/PermissionSubject.java b/api/src/main/java/com/velocitypowered/api/permission/PermissionSubject.java index 3907c2b3e..de3e96b55 100644 --- a/api/src/main/java/com/velocitypowered/api/permission/PermissionSubject.java +++ b/api/src/main/java/com/velocitypowered/api/permission/PermissionSubject.java @@ -22,7 +22,7 @@ public interface PermissionSubject { * @return whether or not the subject has the permission */ default boolean hasPermission(String permission) { - return getPermissionValue(permission).toBooleanOrElse(false); + return this.getPermissionChecker().test(permission); } /** @@ -31,14 +31,14 @@ public interface PermissionSubject { * @param permission the permission * @return the value the permission is set to */ - TriState getPermissionValue(String permission); + default TriState getPermissionValue(String permission) { + return this.getPermissionChecker().value(permission); + } /** * Gets the permission checker for the subject. * * @return subject's permission checker */ - default PermissionChecker getPermissionChecker() { - return this::getPermissionValue; - } + PermissionChecker getPermissionChecker(); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index 64edce738..72900fc3f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -908,8 +908,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player, } @Override - public TriState getPermissionValue(String permission) { - return permissionChecker.value(permission); + public PermissionChecker getPermissionChecker() { + return this.permissionChecker; } @Override diff --git a/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java b/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java index 6450d403c..0dd57c9be 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java @@ -75,8 +75,8 @@ public final class VelocityConsole extends SimpleTerminalConsole implements Cons } @Override - public @NonNull TriState getPermissionValue(@NonNull String permission) { - return this.permissionChecker.value(permission); + public PermissionChecker getPermissionChecker() { + return this.permissionChecker; } /** diff --git a/proxy/src/test/java/com/velocitypowered/proxy/command/MockCommandSource.java b/proxy/src/test/java/com/velocitypowered/proxy/command/MockCommandSource.java index 192c85ea8..b1503a5e5 100644 --- a/proxy/src/test/java/com/velocitypowered/proxy/command/MockCommandSource.java +++ b/proxy/src/test/java/com/velocitypowered/proxy/command/MockCommandSource.java @@ -18,6 +18,7 @@ package com.velocitypowered.proxy.command; import com.velocitypowered.api.command.CommandSource; +import net.kyori.adventure.permission.PermissionChecker; import net.kyori.adventure.util.TriState; /** @@ -28,7 +29,7 @@ public class MockCommandSource implements CommandSource { public static final CommandSource INSTANCE = new MockCommandSource(); @Override - public TriState getPermissionValue(final String permission) { - return TriState.NOT_SET; + public PermissionChecker getPermissionChecker() { + return PermissionChecker.always(TriState.NOT_SET); } }