From 65ae8b4c6ac2c764010b3ac82b49379ae0d88180 Mon Sep 17 00:00:00 2001 From: Thinkofdeath Date: Tue, 2 Sep 2014 13:54:52 +0100 Subject: [PATCH] Correctly send the MC|Brand packet --- .../java/net/md_5/bungee/ServerConnector.java | 19 ++++++++++-- .../bungee/connection/DownstreamBridge.java | 29 +++++++++++++++++-- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java index e0da10ee..5b13911e 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java @@ -3,6 +3,9 @@ package net.md_5.bungee; import com.google.common.base.Preconditions; import java.util.Objects; import java.util.Queue; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; import lombok.RequiredArgsConstructor; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ProxyServer; @@ -23,6 +26,7 @@ import net.md_5.bungee.netty.PacketHandler; import net.md_5.bungee.protocol.MinecraftOutput; import net.md_5.bungee.protocol.DefinedPacket; import net.md_5.bungee.protocol.Protocol; +import net.md_5.bungee.protocol.ProtocolConstants; import net.md_5.bungee.protocol.packet.EncryptionRequest; import net.md_5.bungee.protocol.packet.Handshake; import net.md_5.bungee.protocol.packet.Login; @@ -151,9 +155,18 @@ public class ServerConnector extends PacketHandler user.unsafe().sendPacket( modLogin ); - MinecraftOutput out = new MinecraftOutput(); - out.writeStringUTF8WithoutLengthHeaderBecauseDinnerboneStuffedUpTheMCBrandPacket( ProxyServer.getInstance().getName() + " (" + ProxyServer.getInstance().getVersion() + ")" ); - user.unsafe().sendPacket( new PluginMessage( "MC|Brand", out.toArray() ) ); + if ( user.getPendingConnection().getVersion() < ProtocolConstants.MINECRAFT_SNAPSHOT ) + { + MinecraftOutput out = new MinecraftOutput(); + out.writeStringUTF8WithoutLengthHeaderBecauseDinnerboneStuffedUpTheMCBrandPacket( ProxyServer.getInstance().getName() + " (" + ProxyServer.getInstance().getVersion() + ")" ); + user.unsafe().sendPacket( new PluginMessage( "MC|Brand", out.toArray() ) ); + } else + { + ByteBuf brand = ByteBufAllocator.DEFAULT.heapBuffer(); + DefinedPacket.writeString( bungee.getName() + " (" + bungee.getVersion() + ")", brand ); + user.unsafe().sendPacket( new PluginMessage( "MC|Brand", brand.array().clone() ) ); + brand.release(); + } } else { user.getTabListHandler().onServerChange(); diff --git a/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java index 92b86524..735d3d35 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java @@ -4,6 +4,10 @@ import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; import java.io.DataInput; import java.util.Objects; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.Unpooled; import lombok.RequiredArgsConstructor; import net.md_5.bungee.ServerConnection; import net.md_5.bungee.api.chat.TextComponent; @@ -23,7 +27,9 @@ import net.md_5.bungee.api.score.Team; import net.md_5.bungee.chat.ComponentSerializer; import net.md_5.bungee.netty.ChannelWrapper; import net.md_5.bungee.netty.PacketHandler; +import net.md_5.bungee.protocol.DefinedPacket; import net.md_5.bungee.protocol.PacketWrapper; +import net.md_5.bungee.protocol.ProtocolConstants; import net.md_5.bungee.protocol.packet.KeepAlive; import net.md_5.bungee.protocol.packet.PlayerListItem; import net.md_5.bungee.protocol.packet.ScoreboardObjective; @@ -219,8 +225,27 @@ public class DownstreamBridge extends PacketHandler if ( pluginMessage.getTag().equals( "MC|Brand" ) ) { - String serverBrand = new String( pluginMessage.getData(), "UTF-8" ); - pluginMessage.setData( ( bungee.getName() + " (" + bungee.getVersion() + ")" + " <- " + serverBrand ).getBytes( "UTF-8" ) ); + if ( con.getPendingConnection().getVersion() >= ProtocolConstants.MINECRAFT_SNAPSHOT ) + { + try + { + ByteBuf brand = Unpooled.wrappedBuffer( pluginMessage.getData() ); + String serverBrand = DefinedPacket.readString( brand ); + brand.release(); + brand = ByteBufAllocator.DEFAULT.heapBuffer(); + DefinedPacket.writeString( bungee.getName() + " (" + bungee.getVersion() + ")" + " <- " + serverBrand, brand ); + pluginMessage.setData( brand.array().clone() ); + brand.release(); + } catch ( Exception ignored ) { + // TODO: Remove this + // Older spigot protocol builds sent the brand incorrectly + return; + } + } else + { + String serverBrand = new String( pluginMessage.getData(), "UTF-8" ); + pluginMessage.setData( ( bungee.getName() + " (" + bungee.getVersion() + ")" + " <- " + serverBrand ).getBytes( "UTF-8" ) ); + } // changes in the packet are ignored so we need to send it manually con.unsafe().sendPacket( pluginMessage ); throw CancelSendSignal.INSTANCE;