From f579725002dae0f7c69d88265be311fc83f1b1ac Mon Sep 17 00:00:00 2001 From: Riley Park Date: Thu, 10 Jun 2021 01:08:27 -0700 Subject: [PATCH] Provide access to Pointer values --- .../connection/client/ConnectedPlayer.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) 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 64bb15122..1f58f0089 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 @@ -94,12 +94,15 @@ import java.util.concurrent.ThreadLocalRandom; import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.bossbar.BossBar; import net.kyori.adventure.identity.Identity; +import net.kyori.adventure.permission.PermissionChecker; +import net.kyori.adventure.pointer.Pointer; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer; +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; @@ -122,6 +125,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { private final MinecraftConnection connection; private final @Nullable InetSocketAddress virtualHost; private GameProfile profile; + private PermissionChecker permissionChecker; private PermissionFunction permissionFunction; private int tryIndex = 0; private long ping = -1; @@ -149,6 +153,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { this.profile = profile; this.connection = connection; this.virtualHost = virtualHost; + this.permissionChecker = PermissionChecker.always(TriState.FALSE); this.permissionFunction = PermissionFunction.ALWAYS_UNDEFINED; this.connectionPhase = connection.getType().getInitialClientPhase(); this.knownChannels = CappedSet.create(MAX_PLUGIN_CHANNELS); @@ -249,6 +254,18 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { void setPermissionFunction(PermissionFunction permissionFunction) { this.permissionFunction = permissionFunction; + this.permissionChecker = permission -> { + final Tristate state = permissionFunction.getPermissionValue(permission); + if (state == Tristate.TRUE) { + return TriState.TRUE; + } else if (state == Tristate.UNDEFINED) { + return TriState.NOT_SET; + } else if (state == Tristate.FALSE) { + return TriState.FALSE; + } else { + throw new IllegalArgumentException(); + } + }; } @Override @@ -261,6 +278,19 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { return connection.getProtocolVersion(); } + @Override + @SuppressWarnings("unchecked") // safe casts + public Optional get(final Pointer pointer) { + if (pointer == Identity.UUID) { + return Optional.of((T) this.getUniqueId()); + } else if (pointer == Identity.NAME) { + return Optional.of((T) this.getGameProfile().getName()); + } else if (pointer == PermissionChecker.POINTER) { + return Optional.of((T) this.permissionChecker); + } + return Player.super.get(pointer); + } + @Override public void sendMessage(net.kyori.text.Component component, MessagePosition position) { Preconditions.checkNotNull(component, "component");