3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-06 00:00:47 +01:00

permission Tristate and function exchanged with adventure

Dieser Commit ist enthalten in:
Aaron 2023-06-16 01:27:27 +02:00
Ursprung 64ea90d254
Commit cc67b99c8b
13 geänderte Dateien mit 52 neuen und 191 gelöschten Zeilen

Datei anzeigen

@ -9,9 +9,9 @@ package com.velocitypowered.api.event.permission;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.velocitypowered.api.event.annotation.AwaitingEvent; import com.velocitypowered.api.event.annotation.AwaitingEvent;
import com.velocitypowered.api.permission.PermissionFunction;
import com.velocitypowered.api.permission.PermissionProvider; import com.velocitypowered.api.permission.PermissionProvider;
import com.velocitypowered.api.permission.PermissionSubject; import com.velocitypowered.api.permission.PermissionSubject;
import net.kyori.adventure.permission.PermissionChecker;
import org.checkerframework.checker.nullness.qual.Nullable; 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 * @param subject the subject
* @return the obtained permission function * @return the obtained permission function
*/ */
public PermissionFunction createFunction(PermissionSubject subject) { public PermissionChecker createChecker(PermissionSubject subject) {
return this.provider.createFunction(subject); return this.provider.createChecker(subject);
} }
public PermissionProvider getProvider() { 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.
* *
* <p>Specifying <code>null</code> will reset the provider to the default * <p>Specifying <code>null</code> will reset the provider to the default
* instance given when the event was posted.</p> * instance given when the event was posted.</p>

Datei anzeigen

@ -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);
}

Datei anzeigen

@ -7,17 +7,19 @@
package com.velocitypowered.api.permission; 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 @FunctionalInterface
public interface PermissionProvider { public interface PermissionProvider {
/** /**
* Creates a {@link PermissionFunction} for the subject. * Creates a {@link PermissionChecker} for the subject.
* *
* @param subject the subject * @param subject the subject
* @return the function * @return the function
*/ */
PermissionFunction createFunction(PermissionSubject subject); PermissionChecker createChecker(PermissionSubject subject);
} }

Datei anzeigen

@ -8,6 +8,7 @@
package com.velocitypowered.api.permission; package com.velocitypowered.api.permission;
import net.kyori.adventure.permission.PermissionChecker; import net.kyori.adventure.permission.PermissionChecker;
import net.kyori.adventure.util.TriState;
/** /**
* Represents a object that has a set of queryable permissions. * 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 * @return whether or not the subject has the permission
*/ */
default boolean hasPermission(String 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 * @param permission the permission
* @return the value the permission is set to * @return the value the permission is set to
*/ */
Tristate getPermissionValue(String permission); TriState getPermissionValue(String permission);
/** /**
* Gets the permission checker for the subject. * Gets the permission checker for the subject.
@ -38,6 +39,6 @@ public interface PermissionSubject {
* @return subject's permission checker * @return subject's permission checker
*/ */
default PermissionChecker getPermissionChecker() { default PermissionChecker getPermissionChecker() {
return permission -> getPermissionValue(permission).toAdventureTriState(); return this::getPermissionValue;
} }
} }

Datei anzeigen

@ -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.
*
* <p>Possible values:</p>
* <p></p>
* <ul>
* <li>{@link #TRUE} - a positive setting</li>
* <li>{@link #FALSE} - a negative (negated) setting</li>
* <li>{@link #UNDEFINED} - a non-existent setting</li>
* </ul>
*/
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 <code>true</code> or
* <code>false</code>, respectively.
*/
public static Tristate fromBoolean(boolean val) {
return val ? TRUE : FALSE;
}
/**
* Returns a {@link Tristate} from a nullable boolean.
*
* <p>Unlike {@link #fromBoolean(boolean)}, this method returns {@link #UNDEFINED}
* if the value is null.</p>
*
* @param val the boolean value
* @return {@link #UNDEFINED}, {@link #TRUE} or {@link #FALSE}, if the value is <code>null</code>,
* <code>true</code> or <code>false</code>, 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.
*
* <p>A value of {@link #UNDEFINED} converts to false.</p>
*
* @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();
}
}
}

Datei anzeigen

@ -28,7 +28,6 @@ import com.mojang.brigadier.tree.ArgumentCommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode; import com.mojang.brigadier.tree.LiteralCommandNode;
import com.velocitypowered.api.command.BrigadierCommand; import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.permission.Tristate;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.server.RegisteredServer; 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.TextComponent;
import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.util.TriState;
/** /**
* Implements the Velocity default {@code /glist} command. * Implements the Velocity default {@code /glist} command.
@ -60,7 +60,7 @@ public class GlistCommand {
LiteralCommandNode<CommandSource> totalNode = LiteralArgumentBuilder LiteralCommandNode<CommandSource> totalNode = LiteralArgumentBuilder
.<CommandSource>literal("glist") .<CommandSource>literal("glist")
.requires(source -> .requires(source ->
source.getPermissionValue("velocity.command.glist") == Tristate.TRUE) source.getPermissionValue("velocity.command.glist") == TriState.TRUE)
.executes(this::totalCount) .executes(this::totalCount)
.build(); .build();
ArgumentCommandNode<CommandSource, String> serverNode = RequiredArgumentBuilder ArgumentCommandNode<CommandSource, String> serverNode = RequiredArgumentBuilder

Datei anzeigen

@ -22,7 +22,6 @@ import static net.kyori.adventure.text.event.HoverEvent.showText;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.permission.Tristate;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.ServerConnection; 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.TranslatableComponent;
import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.util.TriState;
/** /**
* Implements Velocity's {@code /server} command. * Implements Velocity's {@code /server} command.
@ -161,6 +161,6 @@ public class ServerCommand implements SimpleCommand {
@Override @Override
public boolean hasPermission(final SimpleCommand.Invocation invocation) { 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;
} }
} }

Datei anzeigen

@ -24,7 +24,6 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.permission.Tristate;
import com.velocitypowered.api.plugin.PluginContainer; import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.PluginDescription; import com.velocitypowered.api.plugin.PluginDescription;
import com.velocitypowered.api.proxy.ProxyServer; 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.NamedTextColor;
import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.util.TriState;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
@ -200,7 +200,7 @@ public class VelocityCommand implements SimpleCommand {
@Override @Override
public boolean hasPermission(final CommandSource source, final String @NonNull [] args) { 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 @Override
public boolean hasPermission(final CommandSource source, final String @NonNull [] args) { 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 @Override
public boolean hasPermission(final CommandSource source, final String @NonNull [] args) { 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 @Override
public boolean hasPermission(final CommandSource source, final String @NonNull [] args) { 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 @Override
public boolean hasPermission(CommandSource source, String @NonNull [] args) { public boolean hasPermission(CommandSource source, String @NonNull [] args) {
return source.getPermissionValue("velocity.command.heap") == Tristate.TRUE; return source.getPermissionValue("velocity.command.heap") == TriState.TRUE;
} }
} }

Datei anzeigen

@ -26,7 +26,6 @@ import com.velocitypowered.api.event.connection.PostLoginEvent;
import com.velocitypowered.api.event.permission.PermissionsSetupEvent; import com.velocitypowered.api.event.permission.PermissionsSetupEvent;
import com.velocitypowered.api.event.player.GameProfileRequestEvent; import com.velocitypowered.api.event.player.GameProfileRequestEvent;
import com.velocitypowered.api.event.player.PlayerChooseInitialServerEvent; 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.crypto.IdentifiedKey;
import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.util.GameProfile; import com.velocitypowered.api.util.GameProfile;
@ -45,6 +44,8 @@ import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import net.kyori.adventure.permission.PermissionChecker;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -107,8 +108,8 @@ public class AuthSessionHandler implements MinecraftSessionHandler {
.thenAcceptAsync(event -> { .thenAcceptAsync(event -> {
if (!mcConnection.isClosed()) { if (!mcConnection.isClosed()) {
// wait for permissions to load, then set the players permission function // wait for permissions to load, then set the players permission function
final PermissionFunction function = event.createFunction(player); final PermissionChecker checker = event.createChecker(player);
if (function == null) { if (checker == null) {
logger.error( logger.error(
"A plugin permission provider {} provided an invalid permission function" "A plugin permission provider {} provided an invalid permission function"
+ " for player {}. This is a bug in the plugin, not in Velocity. Falling" + " 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(), event.getProvider().getClass().getName(),
player.getUsername()); player.getUsername());
} else { } else {
player.setPermissionFunction(function); player.setPermissionChecker(checker);
} }
completeLoginProtocolPhaseAndInitialize(player); completeLoginProtocolPhaseAndInitialize(player);
} }

Datei anzeigen

@ -35,9 +35,7 @@ import com.velocitypowered.api.event.player.PlayerResourcePackStatusEvent;
import com.velocitypowered.api.event.player.PlayerSettingsChangedEvent; import com.velocitypowered.api.event.player.PlayerSettingsChangedEvent;
import com.velocitypowered.api.event.player.ServerPreConnectEvent; import com.velocitypowered.api.event.player.ServerPreConnectEvent;
import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.network.ProtocolVersion;
import com.velocitypowered.api.permission.PermissionFunction;
import com.velocitypowered.api.permission.PermissionProvider; import com.velocitypowered.api.permission.PermissionProvider;
import com.velocitypowered.api.permission.Tristate;
import com.velocitypowered.api.proxy.ConnectionRequestBuilder; import com.velocitypowered.api.proxy.ConnectionRequestBuilder;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ServerConnection; 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.Title.Times;
import net.kyori.adventure.title.TitlePart; import net.kyori.adventure.title.TitlePart;
import net.kyori.adventure.translation.GlobalTranslator; import net.kyori.adventure.translation.GlobalTranslator;
import net.kyori.adventure.util.TriState;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@ -132,7 +131,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player,
.mapper(TranslatableComponent.class, TranslatableComponent::key) .mapper(TranslatableComponent.class, TranslatableComponent::key)
.build()) .build())
.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); 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 MinecraftConnection connection;
private final @Nullable InetSocketAddress virtualHost; private final @Nullable InetSocketAddress virtualHost;
private GameProfile profile; private GameProfile profile;
private PermissionFunction permissionFunction; private PermissionChecker permissionChecker;
private int tryIndex = 0; private int tryIndex = 0;
private long ping = -1; private long ping = -1;
private final boolean onlineMode; private final boolean onlineMode;
@ -168,7 +167,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player,
.withDynamic(Identity.NAME, this::getUsername) .withDynamic(Identity.NAME, this::getUsername)
.withDynamic(Identity.DISPLAY_NAME, () -> Component.text(this.getUsername())) .withDynamic(Identity.DISPLAY_NAME, () -> Component.text(this.getUsername()))
.withDynamic(Identity.LOCALE, this::getEffectiveLocale) .withDynamic(Identity.LOCALE, this::getEffectiveLocale)
.withStatic(PermissionChecker.POINTER, getPermissionChecker()) .withDynamic(PermissionChecker.POINTER, () -> this.permissionChecker)
.withStatic(FacetPointers.TYPE, Type.PLAYER) .withStatic(FacetPointers.TYPE, Type.PLAYER)
.build(); .build();
private @Nullable String clientBrand; private @Nullable String clientBrand;
@ -184,7 +183,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player,
this.profile = profile; this.profile = profile;
this.connection = connection; this.connection = connection;
this.virtualHost = virtualHost; this.virtualHost = virtualHost;
this.permissionFunction = PermissionFunction.ALWAYS_UNDEFINED; this.permissionChecker = PermissionChecker.always(TriState.NOT_SET);
this.connectionPhase = connection.getType().getInitialClientPhase(); this.connectionPhase = connection.getType().getInitialClientPhase();
this.knownChannels = CappedSet.create(MAX_PLUGIN_CHANNELS); this.knownChannels = CappedSet.create(MAX_PLUGIN_CHANNELS);
this.onlineMode = onlineMode; this.onlineMode = onlineMode;
@ -319,8 +318,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player,
return Optional.ofNullable(virtualHost); return Optional.ofNullable(virtualHost);
} }
void setPermissionFunction(PermissionFunction permissionFunction) { void setPermissionChecker(PermissionChecker permissionChecker) {
this.permissionFunction = permissionFunction; this.permissionChecker = permissionChecker;
} }
@Override @Override
@ -909,8 +908,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player,
} }
@Override @Override
public Tristate getPermissionValue(String permission) { public TriState getPermissionValue(String permission) {
return permissionFunction.getPermissionValue(permission); return permissionChecker.value(permission);
} }
@Override @Override

Datei anzeigen

@ -17,11 +17,7 @@
package com.velocitypowered.proxy.console; 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.event.permission.PermissionsSetupEvent;
import com.velocitypowered.api.permission.PermissionFunction;
import com.velocitypowered.api.permission.Tristate;
import com.velocitypowered.api.proxy.ConsoleCommandSource; import com.velocitypowered.api.proxy.ConsoleCommandSource;
import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.util.ClosestLocaleMatcher; 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.format.NamedTextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.translation.GlobalTranslator; import net.kyori.adventure.translation.GlobalTranslator;
import net.kyori.adventure.util.TriState;
import net.minecrell.terminalconsole.SimpleTerminalConsole; import net.minecrell.terminalconsole.SimpleTerminalConsole;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager; 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 static final Logger logger = LogManager.getLogger(VelocityConsole.class);
private final VelocityServer server; 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() private final @NotNull Pointers pointers = ConsoleCommandSource.super.pointers().toBuilder()
.withDynamic(PermissionChecker.POINTER, this::getPermissionChecker) .withDynamic(PermissionChecker.POINTER, this::getPermissionChecker)
.withDynamic(Identity.LOCALE, () -> ClosestLocaleMatcher.INSTANCE .withDynamic(Identity.LOCALE, () -> ClosestLocaleMatcher.INSTANCE
@ -78,8 +75,8 @@ public final class VelocityConsole extends SimpleTerminalConsole implements Cons
} }
@Override @Override
public @NonNull Tristate getPermissionValue(@NonNull String permission) { public @NonNull TriState getPermissionValue(@NonNull String permission) {
return this.permissionFunction.getPermissionValue(permission); return this.permissionChecker.value(permission);
} }
/** /**
@ -94,16 +91,16 @@ public final class VelocityConsole extends SimpleTerminalConsole implements Cons
* Sets up permissions for the console. * Sets up permissions for the console.
*/ */
public void setupPermissions() { 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 // we can safely block here, this is before any listeners fire
this.permissionFunction = this.server.getEventManager().fire(event).join().createFunction(this); this.permissionChecker = this.server.getEventManager().fire(event).join().createChecker(this);
if (this.permissionFunction == null) { if (this.permissionChecker == null) {
logger.error( logger.error(
"A plugin permission provider {} provided an invalid permission function" "A plugin permission provider {} provided an invalid permission function"
+ " for the console. This is a bug in the plugin, not in Velocity. Falling" + " for the console. This is a bug in the plugin, not in Velocity. Falling"
+ " back to the default permission function.", + " back to the default permission function.",
event.getProvider().getClass().getName()); event.getProvider().getClass().getName());
this.permissionFunction = ALWAYS_TRUE; this.permissionChecker = PermissionChecker.always(TriState.TRUE);
} }
} }

Datei anzeigen

@ -25,12 +25,13 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.permission.Tristate;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.proxy.event.MockEventManager; import com.velocitypowered.proxy.event.MockEventManager;
import com.velocitypowered.proxy.event.VelocityEventManager; import com.velocitypowered.proxy.event.VelocityEventManager;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import net.kyori.adventure.util.TriState;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -66,7 +67,7 @@ abstract class CommandTestSuite {
final void assertPlayerSuggestions(final String input, final String... expectedSuggestions) { final void assertPlayerSuggestions(final String input, final String... expectedSuggestions) {
final var player = mock(Player.class); 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(); final var actual = manager.offerSuggestions(player, input).join();
assertEquals(Arrays.asList(expectedSuggestions), actual); assertEquals(Arrays.asList(expectedSuggestions), actual);
} }

Datei anzeigen

@ -18,7 +18,7 @@
package com.velocitypowered.proxy.command; package com.velocitypowered.proxy.command;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.permission.Tristate; import net.kyori.adventure.util.TriState;
/** /**
* A fake {@link CommandSource}. * A fake {@link CommandSource}.
@ -28,7 +28,7 @@ public class MockCommandSource implements CommandSource {
public static final CommandSource INSTANCE = new MockCommandSource(); public static final CommandSource INSTANCE = new MockCommandSource();
@Override @Override
public Tristate getPermissionValue(final String permission) { public TriState getPermissionValue(final String permission) {
return Tristate.UNDEFINED; return TriState.NOT_SET;
} }
} }