From f579725002dae0f7c69d88265be311fc83f1b1ac Mon Sep 17 00:00:00 2001 From: Riley Park Date: Thu, 10 Jun 2021 01:08:27 -0700 Subject: [PATCH 01/18] 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"); From 3a1517f3a4975a4492adda474ea8b5b29b9c3db3 Mon Sep 17 00:00:00 2001 From: lexikiq Date: Fri, 11 Jun 2021 19:39:40 -0400 Subject: [PATCH 02/18] Expand Adventure Support Adds support for assorted Adventure interfaces --- .../api/command/CommandSource.java | 22 ++++++++++- .../com/velocitypowered/api/proxy/Player.java | 37 ++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java index c37105075..75ad2ddd2 100644 --- a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java +++ b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java @@ -8,18 +8,23 @@ package com.velocitypowered.api.command; import com.velocitypowered.api.permission.PermissionSubject; +import com.velocitypowered.api.permission.Tristate; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.identity.Identified; import net.kyori.adventure.identity.Identity; +import net.kyori.adventure.permission.PermissionChecker; +import net.kyori.adventure.pointer.Pointers; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.legacytext3.LegacyText3ComponentSerializer; +import net.kyori.adventure.util.TriState; import org.checkerframework.checker.nullness.qual.NonNull; +import org.jetbrains.annotations.NotNull; /** * Represents something that can be used to run a {@link Command}. */ -public interface CommandSource extends Audience, PermissionSubject { +public interface CommandSource extends Audience, PermissionSubject, PermissionChecker { /** * Sends the specified {@code component} to the invoker. @@ -36,4 +41,19 @@ public interface CommandSource extends Audience, PermissionSubject { @NonNull MessageType type) { this.sendMessage(LegacyText3ComponentSerializer.get().serialize(message)); } + + @Override + default @NotNull Pointers pointers() { + return Pointers.builder().withStatic(PermissionChecker.POINTER, this).build(); + } + + @Override + default @NotNull TriState value(String permission) { + Tristate state = getPermissionValue(permission); + if (state == Tristate.TRUE) + return TriState.TRUE; + if (state == Tristate.UNDEFINED) + return TriState.NOT_SET; + return TriState.FALSE; + } } diff --git a/api/src/main/java/com/velocitypowered/api/proxy/Player.java b/api/src/main/java/com/velocitypowered/api/proxy/Player.java index 51d966044..3c72d70df 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/Player.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/Player.java @@ -23,16 +23,25 @@ import com.velocitypowered.api.util.title.Title; import java.util.List; import java.util.Optional; import java.util.UUID; +import java.util.function.UnaryOperator; import net.kyori.adventure.identity.Identified; import net.kyori.adventure.identity.Identity; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.key.Keyed; +import net.kyori.adventure.pointer.Pointers; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.ComponentLike; +import net.kyori.adventure.text.event.HoverEvent; +import net.kyori.adventure.text.event.HoverEventSource; import org.checkerframework.checker.nullness.qual.Nullable; +import org.jetbrains.annotations.NotNull; /** * Represents a player who is connected to the proxy. */ public interface Player extends CommandSource, Identified, InboundConnection, - ChannelMessageSource, ChannelMessageSink { + ChannelMessageSource, ChannelMessageSink, HoverEventSource, + ComponentLike, Keyed { /** * Returns the player's current username. @@ -275,4 +284,30 @@ public interface Player extends CommandSource, Identified, InboundConnection, */ @Override boolean sendPluginMessage(ChannelIdentifier identifier, byte[] data); + + @Override + default @NotNull Key key() { + return Key.key("player"); + } + + @Override + default @NotNull Component asComponent() { + return Component.text(getUsername()).hoverEvent(this); + } + + @Override + default @NotNull HoverEvent asHoverEvent( + @NotNull UnaryOperator op) { + return HoverEvent.showEntity(op.apply(HoverEvent.ShowEntity.of(this, getUniqueId(), + Component.text(getUsername())))); + } + + @Override + default @NotNull Pointers pointers() { + return CommandSource.super.pointers().toBuilder() + .withDynamic(Identity.NAME, this::getUsername) + .withDynamic(Identity.DISPLAY_NAME, this::asComponent) + .withDynamic(Identity.UUID, this::getUniqueId) + .build(); + } } From ba4ba2022eba18f87e6a9da46cada147c6725bd6 Mon Sep 17 00:00:00 2001 From: lexikiq Date: Fri, 11 Jun 2021 19:53:30 -0400 Subject: [PATCH 03/18] Obey Checkstyle rules --- .../java/com/velocitypowered/api/command/CommandSource.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java index 75ad2ddd2..2f465fda7 100644 --- a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java +++ b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java @@ -50,10 +50,12 @@ public interface CommandSource extends Audience, PermissionSubject, PermissionCh @Override default @NotNull TriState value(String permission) { Tristate state = getPermissionValue(permission); - if (state == Tristate.TRUE) + if (state == Tristate.TRUE) { return TriState.TRUE; - if (state == Tristate.UNDEFINED) + } + if (state == Tristate.UNDEFINED) { return TriState.NOT_SET; + } return TriState.FALSE; } } From 9491046935f4dee57adf60271a4735ed56975119 Mon Sep 17 00:00:00 2001 From: lexikiq Date: Fri, 11 Jun 2021 20:06:28 -0400 Subject: [PATCH 04/18] Improve Player#asComponent --- api/src/main/java/com/velocitypowered/api/proxy/Player.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/com/velocitypowered/api/proxy/Player.java b/api/src/main/java/com/velocitypowered/api/proxy/Player.java index 3c72d70df..be83ee4b6 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/Player.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/Player.java @@ -31,6 +31,7 @@ import net.kyori.adventure.key.Keyed; import net.kyori.adventure.pointer.Pointers; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.ComponentLike; +import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.event.HoverEventSource; import org.checkerframework.checker.nullness.qual.Nullable; @@ -292,7 +293,9 @@ public interface Player extends CommandSource, Identified, InboundConnection, @Override default @NotNull Component asComponent() { - return Component.text(getUsername()).hoverEvent(this); + return Component.text(getUsername()).hoverEvent(this) + .clickEvent(ClickEvent.suggestCommand("/tell " + getUsername())) + .insertion(getUsername()); } @Override From f59516165f76fbfa8e82313a49c6bef726630549 Mon Sep 17 00:00:00 2001 From: lexikiq Date: Fri, 11 Jun 2021 21:06:59 -0400 Subject: [PATCH 05/18] Rebase onto feature/adventure-pointers --- .../velocitypowered/api/command/CommandSource.java | 11 ++++++++--- .../java/com/velocitypowered/api/proxy/Player.java | 10 ---------- .../proxy/connection/client/ConnectedPlayer.java | 4 ++-- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java index 2f465fda7..75748fb0c 100644 --- a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java +++ b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java @@ -14,12 +14,13 @@ import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.identity.Identified; import net.kyori.adventure.identity.Identity; import net.kyori.adventure.permission.PermissionChecker; -import net.kyori.adventure.pointer.Pointers; +import net.kyori.adventure.pointer.Pointer; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.legacytext3.LegacyText3ComponentSerializer; import net.kyori.adventure.util.TriState; import org.checkerframework.checker.nullness.qual.NonNull; import org.jetbrains.annotations.NotNull; +import java.util.Optional; /** * Represents something that can be used to run a {@link Command}. @@ -43,8 +44,12 @@ public interface CommandSource extends Audience, PermissionSubject, PermissionCh } @Override - default @NotNull Pointers pointers() { - return Pointers.builder().withStatic(PermissionChecker.POINTER, this).build(); + @SuppressWarnings("unchecked") // safe casts + default @NotNull Optional get(final @NotNull Pointer pointer) { + if (pointer == PermissionChecker.POINTER) { + return Optional.of((T) this); + } + return Audience.super.get(pointer); } @Override diff --git a/api/src/main/java/com/velocitypowered/api/proxy/Player.java b/api/src/main/java/com/velocitypowered/api/proxy/Player.java index be83ee4b6..a06db7ba0 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/Player.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/Player.java @@ -28,7 +28,6 @@ import net.kyori.adventure.identity.Identified; import net.kyori.adventure.identity.Identity; import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Keyed; -import net.kyori.adventure.pointer.Pointers; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.event.ClickEvent; @@ -304,13 +303,4 @@ public interface Player extends CommandSource, Identified, InboundConnection, return HoverEvent.showEntity(op.apply(HoverEvent.ShowEntity.of(this, getUniqueId(), Component.text(getUsername())))); } - - @Override - default @NotNull Pointers pointers() { - return CommandSource.super.pointers().toBuilder() - .withDynamic(Identity.NAME, this::getUsername) - .withDynamic(Identity.DISPLAY_NAME, this::asComponent) - .withDynamic(Identity.UUID, this::getUniqueId) - .build(); - } } 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 1f58f0089..bfbd9038a 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 @@ -285,8 +285,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { 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); + } else if (pointer == Identity.DISPLAY_NAME) { + return Optional.of((T) asComponent()); } return Player.super.get(pointer); } From 069796a2f2f98f4b45ea006c08ff5af9fe17c4ce Mon Sep 17 00:00:00 2001 From: lexikiq Date: Fri, 11 Jun 2021 21:09:41 -0400 Subject: [PATCH 06/18] Fix import order --- .../java/com/velocitypowered/api/command/CommandSource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java index 75748fb0c..dba098709 100644 --- a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java +++ b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java @@ -9,6 +9,7 @@ package com.velocitypowered.api.command; import com.velocitypowered.api.permission.PermissionSubject; import com.velocitypowered.api.permission.Tristate; +import java.util.Optional; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.identity.Identified; @@ -20,7 +21,6 @@ import net.kyori.adventure.text.serializer.legacytext3.LegacyText3ComponentSeria import net.kyori.adventure.util.TriState; import org.checkerframework.checker.nullness.qual.NonNull; import org.jetbrains.annotations.NotNull; -import java.util.Optional; /** * Represents something that can be used to run a {@link Command}. From 3c4312056b5ec21d7a07fea16e77899daba2463c Mon Sep 17 00:00:00 2001 From: lexikiq Date: Fri, 11 Jun 2021 21:12:44 -0400 Subject: [PATCH 07/18] Switch to #pointers Ensures that Pointered#getOrDefault[From] works --- .../api/command/CommandSource.java | 9 +++------ .../connection/client/ConnectedPlayer.java | 18 ++++++++---------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java index dba098709..addc666b6 100644 --- a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java +++ b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java @@ -16,6 +16,7 @@ import net.kyori.adventure.identity.Identified; import net.kyori.adventure.identity.Identity; import net.kyori.adventure.permission.PermissionChecker; import net.kyori.adventure.pointer.Pointer; +import net.kyori.adventure.pointer.Pointers; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.legacytext3.LegacyText3ComponentSerializer; import net.kyori.adventure.util.TriState; @@ -44,12 +45,8 @@ public interface CommandSource extends Audience, PermissionSubject, PermissionCh } @Override - @SuppressWarnings("unchecked") // safe casts - default @NotNull Optional get(final @NotNull Pointer pointer) { - if (pointer == PermissionChecker.POINTER) { - return Optional.of((T) this); - } - return Audience.super.get(pointer); + default @NotNull Pointers pointers() { + return Pointers.builder().withStatic(PermissionChecker.POINTER, this).build(); } @Override 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 bfbd9038a..8479ae0e0 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 @@ -96,6 +96,7 @@ 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.pointer.Pointers; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.format.NamedTextColor; @@ -108,6 +109,7 @@ import org.apache.logging.log4j.Logger; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; +import org.jetbrains.annotations.NotNull; public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { @@ -279,16 +281,12 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { } @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 == Identity.DISPLAY_NAME) { - return Optional.of((T) asComponent()); - } - return Player.super.get(pointer); + public @NotNull Pointers pointers() { + return Player.super.pointers().toBuilder() + .withDynamic(Identity.UUID, this::getUniqueId) + .withDynamic(Identity.NAME, this::getUsername) + .withDynamic(Identity.DISPLAY_NAME, this::asComponent) + .build(); } @Override From db7e3262a637de19b9c94576627352b6b404f903 Mon Sep 17 00:00:00 2001 From: lexikiq Date: Fri, 11 Jun 2021 21:25:51 -0400 Subject: [PATCH 08/18] Optimize imports --- .../java/com/velocitypowered/api/command/CommandSource.java | 2 -- .../proxy/connection/client/ConnectedPlayer.java | 1 - 2 files changed, 3 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java index addc666b6..2f465fda7 100644 --- a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java +++ b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java @@ -9,13 +9,11 @@ package com.velocitypowered.api.command; import com.velocitypowered.api.permission.PermissionSubject; import com.velocitypowered.api.permission.Tristate; -import java.util.Optional; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.identity.Identified; import net.kyori.adventure.identity.Identity; import net.kyori.adventure.permission.PermissionChecker; -import net.kyori.adventure.pointer.Pointer; import net.kyori.adventure.pointer.Pointers; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.legacytext3.LegacyText3ComponentSerializer; 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 8479ae0e0..1f0540b53 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 @@ -95,7 +95,6 @@ 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.pointer.Pointers; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TranslatableComponent; From 29b409b9f19f2d012931f8874f4831f2361df8f3 Mon Sep 17 00:00:00 2001 From: lexikiq Date: Fri, 11 Jun 2021 21:33:15 -0400 Subject: [PATCH 09/18] Move #pointers to Player It uses fully API methods so it makes sense to me to use it in Player, open to criticism though --- .../java/com/velocitypowered/api/proxy/Player.java | 10 ++++++++++ .../proxy/connection/client/ConnectedPlayer.java | 11 ----------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/proxy/Player.java b/api/src/main/java/com/velocitypowered/api/proxy/Player.java index a06db7ba0..f96807eed 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/Player.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/Player.java @@ -28,6 +28,7 @@ import net.kyori.adventure.identity.Identified; import net.kyori.adventure.identity.Identity; import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Keyed; +import net.kyori.adventure.pointer.Pointers; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.event.ClickEvent; @@ -285,6 +286,15 @@ public interface Player extends CommandSource, Identified, InboundConnection, @Override boolean sendPluginMessage(ChannelIdentifier identifier, byte[] data); + @Override + default @NotNull Pointers pointers() { + return CommandSource.super.pointers().toBuilder() + .withDynamic(Identity.UUID, this::getUniqueId) + .withDynamic(Identity.NAME, this::getUsername) + .withDynamic(Identity.DISPLAY_NAME, this::asComponent) + .build(); + } + @Override default @NotNull Key key() { return Key.key("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 1f0540b53..e95f3ae8d 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 @@ -95,7 +95,6 @@ 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.Pointers; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.format.NamedTextColor; @@ -108,7 +107,6 @@ import org.apache.logging.log4j.Logger; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; -import org.jetbrains.annotations.NotNull; public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { @@ -279,15 +277,6 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { return connection.getProtocolVersion(); } - @Override - public @NotNull Pointers pointers() { - return Player.super.pointers().toBuilder() - .withDynamic(Identity.UUID, this::getUniqueId) - .withDynamic(Identity.NAME, this::getUsername) - .withDynamic(Identity.DISPLAY_NAME, this::asComponent) - .build(); - } - @Override public void sendMessage(net.kyori.text.Component component, MessagePosition position) { Preconditions.checkNotNull(component, "component"); From 19f80bd61885e85a049b2f9c490a07949fbcb698 Mon Sep 17 00:00:00 2001 From: lexikiq Date: Fri, 11 Jun 2021 21:49:40 -0400 Subject: [PATCH 10/18] Don't directly implement PermissionChecker --- .../api/command/CommandSource.java | 32 ++++++++++++------- .../connection/client/ConnectedPlayer.java | 16 ---------- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java index 2f465fda7..380cb9217 100644 --- a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java +++ b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java @@ -24,7 +24,7 @@ import org.jetbrains.annotations.NotNull; /** * Represents something that can be used to run a {@link Command}. */ -public interface CommandSource extends Audience, PermissionSubject, PermissionChecker { +public interface CommandSource extends Audience, PermissionSubject { /** * Sends the specified {@code component} to the invoker. @@ -44,18 +44,26 @@ public interface CommandSource extends Audience, PermissionSubject, PermissionCh @Override default @NotNull Pointers pointers() { - return Pointers.builder().withStatic(PermissionChecker.POINTER, this).build(); + return Pointers.builder().withStatic(PermissionChecker.POINTER, getPermissionChecker()).build(); } - @Override - default @NotNull TriState value(String permission) { - Tristate state = getPermissionValue(permission); - if (state == Tristate.TRUE) { - return TriState.TRUE; - } - if (state == Tristate.UNDEFINED) { - return TriState.NOT_SET; - } - return TriState.FALSE; + /** + * Gets the permission checker for the invoker. + * + * @return invoker's permission checker + */ + default PermissionChecker getPermissionChecker() { + return permission -> { + final Tristate state = 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(); + } + }; } } 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 e95f3ae8d..64bb15122 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,14 +94,12 @@ 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.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; @@ -124,7 +122,6 @@ 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; @@ -152,7 +149,6 @@ 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); @@ -253,18 +249,6 @@ 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 From 39d39e58bd0cdd1d42425acc646b645aa02994e5 Mon Sep 17 00:00:00 2001 From: lexikiq Date: Fri, 11 Jun 2021 22:06:37 -0400 Subject: [PATCH 11/18] Remove ComponentLike interface from Player --- api/src/main/java/com/velocitypowered/api/proxy/Player.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/proxy/Player.java b/api/src/main/java/com/velocitypowered/api/proxy/Player.java index f96807eed..611c25b3b 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/Player.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/Player.java @@ -30,7 +30,6 @@ import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Keyed; import net.kyori.adventure.pointer.Pointers; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.event.HoverEventSource; @@ -41,8 +40,7 @@ import org.jetbrains.annotations.NotNull; * Represents a player who is connected to the proxy. */ public interface Player extends CommandSource, Identified, InboundConnection, - ChannelMessageSource, ChannelMessageSink, HoverEventSource, - ComponentLike, Keyed { + ChannelMessageSource, ChannelMessageSink, HoverEventSource, Keyed { /** * Returns the player's current username. @@ -300,7 +298,6 @@ public interface Player extends CommandSource, Identified, InboundConnection, return Key.key("player"); } - @Override default @NotNull Component asComponent() { return Component.text(getUsername()).hoverEvent(this) .clickEvent(ClickEvent.suggestCommand("/tell " + getUsername())) From 56a46d051b01ced6e9d0a6cfe65216347801d936 Mon Sep 17 00:00:00 2001 From: lexikiq Date: Fri, 11 Jun 2021 22:15:12 -0400 Subject: [PATCH 12/18] Rename method + add JD --- .../main/java/com/velocitypowered/api/proxy/Player.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/proxy/Player.java b/api/src/main/java/com/velocitypowered/api/proxy/Player.java index 611c25b3b..7cfbe75c9 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/Player.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/Player.java @@ -289,7 +289,7 @@ public interface Player extends CommandSource, Identified, InboundConnection, return CommandSource.super.pointers().toBuilder() .withDynamic(Identity.UUID, this::getUniqueId) .withDynamic(Identity.NAME, this::getUsername) - .withDynamic(Identity.DISPLAY_NAME, this::asComponent) + .withDynamic(Identity.DISPLAY_NAME, this::getDisplayName) .build(); } @@ -298,7 +298,12 @@ public interface Player extends CommandSource, Identified, InboundConnection, return Key.key("player"); } - default @NotNull Component asComponent() { + /** + * Gets a {@link Component} that renders a player name similarly to vanilla. + * + * @return a Component representing this player + */ + default @NotNull Component getDisplayName() { return Component.text(getUsername()).hoverEvent(this) .clickEvent(ClickEvent.suggestCommand("/tell " + getUsername())) .insertion(getUsername()); From 4003666c2710f3e227d1576d310a066d3d60d2d4 Mon Sep 17 00:00:00 2001 From: lexikiq Date: Fri, 11 Jun 2021 23:32:49 -0400 Subject: [PATCH 13/18] Cache pointers --- .../java/com/velocitypowered/api/proxy/Player.java | 10 ---------- .../proxy/connection/client/ConnectedPlayer.java | 12 ++++++++++++ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/proxy/Player.java b/api/src/main/java/com/velocitypowered/api/proxy/Player.java index 7cfbe75c9..27fa6f299 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/Player.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/Player.java @@ -28,7 +28,6 @@ import net.kyori.adventure.identity.Identified; import net.kyori.adventure.identity.Identity; import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Keyed; -import net.kyori.adventure.pointer.Pointers; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.HoverEvent; @@ -284,15 +283,6 @@ public interface Player extends CommandSource, Identified, InboundConnection, @Override boolean sendPluginMessage(ChannelIdentifier identifier, byte[] data); - @Override - default @NotNull Pointers pointers() { - return CommandSource.super.pointers().toBuilder() - .withDynamic(Identity.UUID, this::getUniqueId) - .withDynamic(Identity.NAME, this::getUsername) - .withDynamic(Identity.DISPLAY_NAME, this::getDisplayName) - .build(); - } - @Override default @NotNull Key key() { return Key.key("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 64bb15122..94b2fd1f5 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,6 +94,7 @@ 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.pointer.Pointers; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.format.NamedTextColor; @@ -105,6 +106,7 @@ import org.apache.logging.log4j.Logger; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; +import org.jetbrains.annotations.NotNull; public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { @@ -142,6 +144,11 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { private final Queue outstandingResourcePacks = new ArrayDeque<>(); private @Nullable ResourcePackInfo pendingResourcePack; private @Nullable ResourcePackInfo appliedResourcePack; + private final @NotNull Pointers pointers = Player.super.pointers().toBuilder() + .withDynamic(Identity.UUID, this::getUniqueId) + .withDynamic(Identity.NAME, this::getUsername) + .withDynamic(Identity.DISPLAY_NAME, this::getDisplayName) + .build(); ConnectedPlayer(VelocityServer server, GameProfile profile, MinecraftConnection connection, @Nullable InetSocketAddress virtualHost, boolean onlineMode) { @@ -1069,6 +1076,11 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { return minecraftOrFmlMessage || knownChannels.contains(message.getChannel()); } + @Override + public @NotNull Pointers pointers() { + return pointers; + } + private class IdentityImpl implements Identity { @Override public @NonNull UUID uuid() { From 8356b812c34af6fb1e1ebfb652720fbcf2ccc681 Mon Sep 17 00:00:00 2001 From: lexikiq Date: Fri, 11 Jun 2021 23:44:59 -0400 Subject: [PATCH 14/18] Cache permission pointers --- .../com/velocitypowered/api/command/CommandSource.java | 7 ------- .../proxy/connection/client/ConnectedPlayer.java | 2 ++ .../velocitypowered/proxy/console/VelocityConsole.java | 10 ++++++++++ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java index 380cb9217..31392f538 100644 --- a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java +++ b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java @@ -14,12 +14,10 @@ import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.identity.Identified; import net.kyori.adventure.identity.Identity; import net.kyori.adventure.permission.PermissionChecker; -import net.kyori.adventure.pointer.Pointers; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.legacytext3.LegacyText3ComponentSerializer; import net.kyori.adventure.util.TriState; import org.checkerframework.checker.nullness.qual.NonNull; -import org.jetbrains.annotations.NotNull; /** * Represents something that can be used to run a {@link Command}. @@ -42,11 +40,6 @@ public interface CommandSource extends Audience, PermissionSubject { this.sendMessage(LegacyText3ComponentSerializer.get().serialize(message)); } - @Override - default @NotNull Pointers pointers() { - return Pointers.builder().withStatic(PermissionChecker.POINTER, getPermissionChecker()).build(); - } - /** * Gets the permission checker for the invoker. * 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 94b2fd1f5..7807c8518 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,6 +94,7 @@ 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.Pointers; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TranslatableComponent; @@ -148,6 +149,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { .withDynamic(Identity.UUID, this::getUniqueId) .withDynamic(Identity.NAME, this::getUsername) .withDynamic(Identity.DISPLAY_NAME, this::getDisplayName) + .withStatic(PermissionChecker.POINTER, getPermissionChecker()) .build(); ConnectedPlayer(VelocityServer server, GameProfile profile, MinecraftConnection connection, 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 6b465193f..28454816a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java @@ -26,6 +26,8 @@ import com.velocitypowered.api.proxy.ConsoleCommandSource; import com.velocitypowered.proxy.VelocityServer; import java.util.List; import net.kyori.adventure.identity.Identity; +import net.kyori.adventure.permission.PermissionChecker; +import net.kyori.adventure.pointer.Pointers; import net.kyori.adventure.text.Component; import net.kyori.text.TextComponent; import net.kyori.text.format.TextColor; @@ -35,6 +37,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.io.IoBuilder; import org.checkerframework.checker.nullness.qual.NonNull; +import org.jetbrains.annotations.NotNull; import org.jline.reader.Candidate; import org.jline.reader.LineReader; import org.jline.reader.LineReaderBuilder; @@ -45,6 +48,8 @@ public final class VelocityConsole extends SimpleTerminalConsole implements Cons private final VelocityServer server; private PermissionFunction permissionFunction = ALWAYS_TRUE; + private final @NotNull Pointers pointers = ConsoleCommandSource.super.pointers().toBuilder() + .withDynamic(PermissionChecker.POINTER, this::getPermissionChecker).build(); public VelocityConsole(VelocityServer server) { this.server = server; @@ -131,4 +136,9 @@ public final class VelocityConsole extends SimpleTerminalConsole implements Cons protected void shutdown() { this.server.shutdown(true); } + + @Override + public @NotNull Pointers pointers() { + return pointers; + } } From 72408d5bd896e3a2e1bf4dadfe733e1217dff1f6 Mon Sep 17 00:00:00 2001 From: lexikiq Date: Sat, 12 Jun 2021 12:12:04 -0400 Subject: [PATCH 15/18] Remove display name --- .../java/com/velocitypowered/api/proxy/Player.java | 12 ------------ .../proxy/connection/client/ConnectedPlayer.java | 1 - 2 files changed, 13 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/proxy/Player.java b/api/src/main/java/com/velocitypowered/api/proxy/Player.java index 27fa6f299..66c5b433a 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/Player.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/Player.java @@ -29,7 +29,6 @@ import net.kyori.adventure.identity.Identity; import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Keyed; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.event.HoverEventSource; import org.checkerframework.checker.nullness.qual.Nullable; @@ -288,17 +287,6 @@ public interface Player extends CommandSource, Identified, InboundConnection, return Key.key("player"); } - /** - * Gets a {@link Component} that renders a player name similarly to vanilla. - * - * @return a Component representing this player - */ - default @NotNull Component getDisplayName() { - return Component.text(getUsername()).hoverEvent(this) - .clickEvent(ClickEvent.suggestCommand("/tell " + getUsername())) - .insertion(getUsername()); - } - @Override default @NotNull HoverEvent asHoverEvent( @NotNull UnaryOperator op) { 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 7807c8518..f0f31f0ed 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 @@ -148,7 +148,6 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { private final @NotNull Pointers pointers = Player.super.pointers().toBuilder() .withDynamic(Identity.UUID, this::getUniqueId) .withDynamic(Identity.NAME, this::getUsername) - .withDynamic(Identity.DISPLAY_NAME, this::getDisplayName) .withStatic(PermissionChecker.POINTER, getPermissionChecker()) .build(); From e798f0d906d083422364a3899c1fe4e5f260f7c2 Mon Sep 17 00:00:00 2001 From: lexikiq Date: Sat, 12 Jun 2021 12:52:35 -0400 Subject: [PATCH 16/18] Move #getPermissionChecker --- .../api/command/CommandSource.java | 23 ------------------- .../api/permission/PermissionSubject.java | 23 +++++++++++++++++++ 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java index 31392f538..c37105075 100644 --- a/api/src/main/java/com/velocitypowered/api/command/CommandSource.java +++ b/api/src/main/java/com/velocitypowered/api/command/CommandSource.java @@ -8,15 +8,12 @@ package com.velocitypowered.api.command; import com.velocitypowered.api.permission.PermissionSubject; -import com.velocitypowered.api.permission.Tristate; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.identity.Identified; import net.kyori.adventure.identity.Identity; -import net.kyori.adventure.permission.PermissionChecker; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.legacytext3.LegacyText3ComponentSerializer; -import net.kyori.adventure.util.TriState; import org.checkerframework.checker.nullness.qual.NonNull; /** @@ -39,24 +36,4 @@ public interface CommandSource extends Audience, PermissionSubject { @NonNull MessageType type) { this.sendMessage(LegacyText3ComponentSerializer.get().serialize(message)); } - - /** - * Gets the permission checker for the invoker. - * - * @return invoker's permission checker - */ - default PermissionChecker getPermissionChecker() { - return permission -> { - final Tristate state = 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(); - } - }; - } } 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 ec7171e9f..2f1c036c9 100644 --- a/api/src/main/java/com/velocitypowered/api/permission/PermissionSubject.java +++ b/api/src/main/java/com/velocitypowered/api/permission/PermissionSubject.java @@ -7,6 +7,9 @@ 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. */ @@ -29,4 +32,24 @@ public interface PermissionSubject { * @return the value the permission is set to */ Tristate getPermissionValue(String permission); + + /** + * Gets the permission checker for the subject. + * + * @return subject's permission checker + */ + default PermissionChecker getPermissionChecker() { + return permission -> { + final Tristate state = 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(); + } + }; + } } From 5c424c1da7fb71652b3449cca3331457716737c5 Mon Sep 17 00:00:00 2001 From: lexikiq Date: Sat, 12 Jun 2021 20:01:42 -0400 Subject: [PATCH 17/18] Move TriState converter --- .../api/permission/PermissionSubject.java | 13 +------------ .../velocitypowered/api/permission/Tristate.java | 13 +++++++++++++ 2 files changed, 14 insertions(+), 12 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 2f1c036c9..b117f7385 100644 --- a/api/src/main/java/com/velocitypowered/api/permission/PermissionSubject.java +++ b/api/src/main/java/com/velocitypowered/api/permission/PermissionSubject.java @@ -39,17 +39,6 @@ public interface PermissionSubject { * @return subject's permission checker */ default PermissionChecker getPermissionChecker() { - return permission -> { - final Tristate state = 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(); - } - }; + return permission -> getPermissionValue(permission).toAdventureTriState(); } } diff --git a/api/src/main/java/com/velocitypowered/api/permission/Tristate.java b/api/src/main/java/com/velocitypowered/api/permission/Tristate.java index 70653eef6..cdb82c184 100644 --- a/api/src/main/java/com/velocitypowered/api/permission/Tristate.java +++ b/api/src/main/java/com/velocitypowered/api/permission/Tristate.java @@ -7,6 +7,7 @@ package com.velocitypowered.api.permission; +import net.kyori.adventure.util.TriState; import org.checkerframework.checker.nullness.qual.Nullable; /** @@ -81,4 +82,16 @@ public enum Tristate { public boolean asBoolean() { return this.booleanValue; } + + 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(); + } + } } From fe5809fdc1c8e25219f5587cbf3dca09b43b6b15 Mon Sep 17 00:00:00 2001 From: lexikiq Date: Sat, 12 Jun 2021 20:04:20 -0400 Subject: [PATCH 18/18] Javadocs --- .../java/com/velocitypowered/api/permission/Tristate.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/api/src/main/java/com/velocitypowered/api/permission/Tristate.java b/api/src/main/java/com/velocitypowered/api/permission/Tristate.java index cdb82c184..9ad1482a7 100644 --- a/api/src/main/java/com/velocitypowered/api/permission/Tristate.java +++ b/api/src/main/java/com/velocitypowered/api/permission/Tristate.java @@ -83,6 +83,11 @@ public enum Tristate { return this.booleanValue; } + /** + * Returns the equivalent Adventure {@link TriState}. + * + * @return equivalent Adventure TriState + */ public TriState toAdventureTriState() { if (this == Tristate.TRUE) { return TriState.TRUE;