From d47b339908c546c52e5c08576e27322a48404bc0 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Tue, 9 Feb 2021 13:51:43 -0500 Subject: [PATCH 1/2] Add PlayerPluginMessageRegisterEvent --- .../PlayerPluginMessageRegisterEvent.java | 37 +++++++++++++++++++ .../client/ClientPlaySessionHandler.java | 6 ++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 api/src/main/java/com/velocitypowered/api/event/player/PlayerPluginMessageRegisterEvent.java diff --git a/api/src/main/java/com/velocitypowered/api/event/player/PlayerPluginMessageRegisterEvent.java b/api/src/main/java/com/velocitypowered/api/event/player/PlayerPluginMessageRegisterEvent.java new file mode 100644 index 000000000..0683661f3 --- /dev/null +++ b/api/src/main/java/com/velocitypowered/api/event/player/PlayerPluginMessageRegisterEvent.java @@ -0,0 +1,37 @@ +package com.velocitypowered.api.event.player; + +import com.google.common.base.Preconditions; +import com.velocitypowered.api.proxy.Player; + +import java.util.List; + +/** + * This event is fired when a client ({@link Player}) sends a plugin message through the + * register channel. + */ +public final class PlayerPluginMessageRegisterEvent { + + private final Player player; + private final List channels; + + public PlayerPluginMessageRegisterEvent(Player player, List channels) { + this.player = Preconditions.checkNotNull(player, "player"); + this.channels = Preconditions.checkNotNull(channels, "channels"); + } + + public Player getPlayer() { + return player; + } + + public List getChannels() { + return channels; + } + + @Override + public String toString() { + return "PlayerPluginMessageRegisterEvent{" + + "player=" + player + + ", channels=" + channels + + '}'; + } +} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java index 414e2520b..0df9b7594 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java @@ -8,6 +8,7 @@ import static com.velocitypowered.proxy.protocol.util.PluginMessageUtil.construc import com.velocitypowered.api.event.command.CommandExecuteEvent.CommandResult; import com.velocitypowered.api.event.connection.PluginMessageEvent; import com.velocitypowered.api.event.player.PlayerChatEvent; +import com.velocitypowered.api.event.player.PlayerPluginMessageRegisterEvent; import com.velocitypowered.api.event.player.PlayerResourcePackStatusEvent; import com.velocitypowered.api.event.player.TabCompleteEvent; import com.velocitypowered.api.network.ProtocolVersion; @@ -190,7 +191,10 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { logger.warn("A plugin message was received while the backend server was not " + "ready. Channel: {}. Packet discarded.", packet.getChannel()); } else if (PluginMessageUtil.isRegister(packet)) { - player.getKnownChannels().addAll(PluginMessageUtil.getChannels(packet)); + List channels = PluginMessageUtil.getChannels(packet); + player.getKnownChannels().addAll(channels); + server.getEventManager().fireAndForget(new PlayerPluginMessageRegisterEvent(player, + channels)); backendConn.write(packet.retain()); } else if (PluginMessageUtil.isUnregister(packet)) { player.getKnownChannels().removeAll(PluginMessageUtil.getChannels(packet)); From fb879cb498c108daa4f85f18081855d4382688cc Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Tue, 9 Feb 2021 14:13:33 -0500 Subject: [PATCH 2/2] Apply initial requested changes --- ...ent.java => PlayerChannelRegisterEvent.java} | 11 ++++++----- .../client/ClientPlaySessionHandler.java | 17 ++++++++++++++--- 2 files changed, 20 insertions(+), 8 deletions(-) rename api/src/main/java/com/velocitypowered/api/event/player/{PlayerPluginMessageRegisterEvent.java => PlayerChannelRegisterEvent.java} (66%) diff --git a/api/src/main/java/com/velocitypowered/api/event/player/PlayerPluginMessageRegisterEvent.java b/api/src/main/java/com/velocitypowered/api/event/player/PlayerChannelRegisterEvent.java similarity index 66% rename from api/src/main/java/com/velocitypowered/api/event/player/PlayerPluginMessageRegisterEvent.java rename to api/src/main/java/com/velocitypowered/api/event/player/PlayerChannelRegisterEvent.java index 0683661f3..d9c790eed 100644 --- a/api/src/main/java/com/velocitypowered/api/event/player/PlayerPluginMessageRegisterEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/player/PlayerChannelRegisterEvent.java @@ -2,6 +2,7 @@ package com.velocitypowered.api.event.player; import com.google.common.base.Preconditions; import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.messages.ChannelIdentifier; import java.util.List; @@ -9,12 +10,12 @@ import java.util.List; * This event is fired when a client ({@link Player}) sends a plugin message through the * register channel. */ -public final class PlayerPluginMessageRegisterEvent { +public final class PlayerChannelRegisterEvent { private final Player player; - private final List channels; + private final List channels; - public PlayerPluginMessageRegisterEvent(Player player, List channels) { + public PlayerChannelRegisterEvent(Player player, List channels) { this.player = Preconditions.checkNotNull(player, "player"); this.channels = Preconditions.checkNotNull(channels, "channels"); } @@ -23,13 +24,13 @@ public final class PlayerPluginMessageRegisterEvent { return player; } - public List getChannels() { + public List getChannels() { return channels; } @Override public String toString() { - return "PlayerPluginMessageRegisterEvent{" + return "PlayerChannelRegisterEvent{" + "player=" + player + ", channels=" + channels + '}'; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java index 0df9b7594..2f0375575 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java @@ -5,14 +5,17 @@ import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_16; import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_8; import static com.velocitypowered.proxy.protocol.util.PluginMessageUtil.constructChannelsPacket; +import com.google.common.collect.ImmutableList; import com.velocitypowered.api.event.command.CommandExecuteEvent.CommandResult; import com.velocitypowered.api.event.connection.PluginMessageEvent; +import com.velocitypowered.api.event.player.PlayerChannelRegisterEvent; import com.velocitypowered.api.event.player.PlayerChatEvent; -import com.velocitypowered.api.event.player.PlayerPluginMessageRegisterEvent; import com.velocitypowered.api.event.player.PlayerResourcePackStatusEvent; import com.velocitypowered.api.event.player.TabCompleteEvent; import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.proxy.messages.ChannelIdentifier; +import com.velocitypowered.api.proxy.messages.LegacyChannelIdentifier; +import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.connection.ConnectionTypes; import com.velocitypowered.proxy.connection.MinecraftConnection; @@ -193,8 +196,16 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { } else if (PluginMessageUtil.isRegister(packet)) { List channels = PluginMessageUtil.getChannels(packet); player.getKnownChannels().addAll(channels); - server.getEventManager().fireAndForget(new PlayerPluginMessageRegisterEvent(player, - channels)); + List channelIdentifiers = new ArrayList<>(); + for (String channel : channels) { + try { + channelIdentifiers.add(MinecraftChannelIdentifier.from(channel)); + } catch (IllegalArgumentException e) { + channelIdentifiers.add(new LegacyChannelIdentifier(channel)); + } + } + server.getEventManager().fireAndForget(new PlayerChannelRegisterEvent(player, + ImmutableList.copyOf(channelIdentifiers))); backendConn.write(packet.retain()); } else if (PluginMessageUtil.isUnregister(packet)) { player.getKnownChannels().removeAll(PluginMessageUtil.getChannels(packet));