From f04599ae68baca55f98db5beaf4168164e7c13ed Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sat, 29 Sep 2018 01:28:07 -0400 Subject: [PATCH] Clean up some plugin message channel code --- .../client/ClientPlaySessionHandler.java | 15 ++++----------- .../proxy/protocol/util/PluginMessageUtil.java | 10 +++++----- 2 files changed, 9 insertions(+), 16 deletions(-) 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 810899956..35b2b7efe 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 @@ -44,13 +44,8 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { @Override public void activated() { - PluginMessage message; - if (player.getProtocolVersion() >= ProtocolConstants.MINECRAFT_1_13) { - message = PluginMessageUtil.constructChannelsPacket("minecraft:register", server.getChannelRegistrar().getModernChannelIds()); - } else { - message = PluginMessageUtil.constructChannelsPacket("REGISTER", server.getChannelRegistrar().getIdsForLegacyConnections()); - } - player.getConnection().write(message); + PluginMessage register = PluginMessageUtil.constructChannelsPacket(player.getProtocolVersion(), server.getChannelRegistrar().getModernChannelIds()); + player.getConnection().write(register); } @Override @@ -213,10 +208,8 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { toRegister.addAll(server.getChannelRegistrar().getIdsForLegacyConnections()); } if (!toRegister.isEmpty()) { - String channel = player.getConnection().getProtocolVersion() >= ProtocolConstants.MINECRAFT_1_13 ? - "minecraft:register" : "REGISTER"; player.getConnectedServer().getConnection().delayedWrite(PluginMessageUtil.constructChannelsPacket( - channel, toRegister)); + player.getConnection().getProtocolVersion(), toRegister)); } // If we had plugin messages queued during login/FML handshake, send them now. @@ -267,7 +260,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { } if (actuallyRegistered.size() > 0) { - PluginMessage newRegisterPacket = PluginMessageUtil.constructChannelsPacket(packet.getChannel(), actuallyRegistered); + PluginMessage newRegisterPacket = PluginMessageUtil.constructChannelsPacket(player.getProtocolVersion(), actuallyRegistered); player.getConnectedServer().getConnection().write(newRegisterPacket); } } else if (PluginMessageUtil.isMCUnregister(packet)) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/util/PluginMessageUtil.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/util/PluginMessageUtil.java index 1ffb37dc9..45a682fd1 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/util/PluginMessageUtil.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/util/PluginMessageUtil.java @@ -2,6 +2,7 @@ package com.velocitypowered.proxy.protocol.util; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; +import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.packet.PluginMessage; import io.netty.buffer.ByteBuf; @@ -37,12 +38,11 @@ public enum PluginMessageUtil { return ImmutableList.copyOf(channels.split("\0")); } - public static PluginMessage constructChannelsPacket(String channel, Collection channels) { - Preconditions.checkNotNull(channel, "channel"); - Preconditions.checkNotNull(channel, "channels"); - + public static PluginMessage constructChannelsPacket(int protocolVersion, Collection channels) { + Preconditions.checkNotNull(channels, "channels"); + String channelName = protocolVersion >= ProtocolConstants.MINECRAFT_1_13 ? "minecraft:register" : "REGISTER"; PluginMessage message = new PluginMessage(); - message.setChannel(channel); + message.setChannel(channelName); message.setData(String.join("\0", channels).getBytes(StandardCharsets.UTF_8)); return message; }