From cfda905d98d8dd92feeb9bb86cebdf67a3cfb96e Mon Sep 17 00:00:00 2001 From: Thinkofdeath Date: Sun, 8 Dec 2013 13:47:49 +0000 Subject: [PATCH] Fix entity id remapping The old system only worked in ints and 1.7 changed some to varints. Since the length of the varint is variable more work is needed to remap ids --- .../main/java/net/md_5/bungee/EntityMap.java | 133 ++++++++++-------- .../bungee/connection/DownstreamBridge.java | 4 +- 2 files changed, 73 insertions(+), 64 deletions(-) diff --git a/proxy/src/main/java/net/md_5/bungee/EntityMap.java b/proxy/src/main/java/net/md_5/bungee/EntityMap.java index a988fb95..a6fb16c3 100644 --- a/proxy/src/main/java/net/md_5/bungee/EntityMap.java +++ b/proxy/src/main/java/net/md_5/bungee/EntityMap.java @@ -10,83 +10,94 @@ import net.md_5.bungee.protocol.PacketWrapper; public class EntityMap { - public final static int[][] entityIds = new int[ 256 ][]; + private final static boolean[] clientboundInts = new boolean[256]; + private final static boolean[] clientboundVarInts = new boolean[256]; static { - entityIds[0x0A] = new int[] - { - 0 - }; - entityIds[0x0D] = new int[] - { - 4 - }; - entityIds[0x12] = new int[] - { - 0 - }; - entityIds[0x1A] = new int[] - { - 0 - }; - entityIds[0x1B] = new int[] - { - 0, 4 - }; - entityIds[0x1C] = new int[] - { - 0 // TODO: Meta - }; - entityIds[0x1D] = new int[] - { - 0 - }; - entityIds[0x1E] = new int[] - { - 0 - }; - entityIds[0x20] = new int[] - { - 0 - }; + clientboundInts[0x04] = true; + clientboundInts[0x0A] = true; + clientboundVarInts[0x0B] = true; + clientboundVarInts[0x0C] = true; + clientboundInts[0x0D] = true; + clientboundVarInts[0x0E] = true; + clientboundVarInts[0x0F] = true; + clientboundVarInts[0x10] = true; + clientboundVarInts[0x11] = true; + clientboundInts[0x12] = true; + clientboundInts[0x14] = true; + clientboundInts[0x15] = true; + clientboundInts[0x16] = true; + clientboundInts[0x17] = true; + clientboundInts[0x18] = true; + clientboundInts[0x19] = true; + clientboundInts[0x1A] = true; + clientboundInts[0x1B] = true; + clientboundInts[0x1C] = true; + clientboundInts[0x1D] = true; + clientboundInts[0x1E] = true; + clientboundInts[0x20] = true; + clientboundVarInts[0x25] = true; + clientboundVarInts[0x2C] = true; } - public static void rewrite(ByteBuf packet, int oldId, int newId) + public static void rewriteClientbound(ByteBuf packet, int serverEntityId, int clientEntityId) { int readerIndex = packet.readerIndex(); int packetId = DefinedPacket.readVarInt( packet ); int packetIdLength = packet.readerIndex() - readerIndex; - int[] idArray = entityIds[packetId]; - if ( idArray != null ) + if ( clientboundInts[packetId] ) { - for ( int pos : idArray ) + int readId = packet.getInt( packetIdLength ); + if ( readId == serverEntityId ) { - int readId = packet.getInt( packetIdLength + pos ); - if ( readId == oldId ) + packet.setInt( packetIdLength, clientEntityId ); + } else if ( readId == clientEntityId ) + { + packet.setInt( packetIdLength, serverEntityId ); + } + + if ( packetId == 0x0D || packetId == 0x1B ) + { + readId = packet.getInt( packetIdLength + 4 ); + if ( readId == serverEntityId ) { - packet.setInt( packetIdLength + pos, newId ); + packet.setInt( packetIdLength + 4, clientEntityId ); + } else if ( readId == clientEntityId ) + { + packet.setInt( packetIdLength + 4, serverEntityId ); + } + } + } else if ( clientboundVarInts[packetId] ) + { + // Need to rewrite the packet because VarInts are variable length + int readId = DefinedPacket.readVarInt( packet ); + int readIdLength = packet.readerIndex() - readerIndex - packetIdLength; + if ( readId == serverEntityId || readId == clientEntityId ) + { + ByteBuf data = packet.slice(); + packet.readerIndex( readerIndex ); + packet.writerIndex( packetIdLength ); + DefinedPacket.writeVarInt( readId == serverEntityId ? clientEntityId : serverEntityId, packet ); + packet.writeBytes( data ); + data.release(); + } + } else if ( packetId == 0x13 ) + { + int count = packet.getByte( packetIdLength ); + for ( int i = 0; i < count; i++ ) + { + int readId = packet.getInt( packetIdLength + 1 + i * 4); + if ( readId == serverEntityId ) + { + packet.setInt( packetIdLength + 1 + i * 4, clientEntityId ); + } else if ( readId == clientEntityId ) + { + packet.setInt( packetIdLength + 1 + i * 4, serverEntityId ); } } } - - if ( packetId == 0x0E ) - { - DefinedPacket.readVarInt( packet ); - byte type = packet.readByte(); - if ( type == 60 || type == 90 ) - { - packet.skipBytes( 14 ); - int pos = packet.readerIndex(); - int shooterId = packet.getInt( pos ); - if ( shooterId == oldId ) - { - packet.setInt( pos, newId ); - } - } - } - packet.readerIndex( readerIndex ); } } 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 3ab31530..cf0ff3bb 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 @@ -3,10 +3,8 @@ package net.md_5.bungee.connection; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; import java.io.DataInput; -import java.util.List; import java.util.Objects; import lombok.RequiredArgsConstructor; -import net.md_5.bungee.BungeeCord; import net.md_5.bungee.EntityMap; import net.md_5.bungee.ServerConnection; import net.md_5.bungee.api.event.ServerDisconnectEvent; @@ -80,7 +78,7 @@ public class DownstreamBridge extends PacketHandler { if ( !server.isObsolete() ) { - EntityMap.rewrite( packet.buf, con.getServerEntityId(), con.getClientEntityId() ); + EntityMap.rewriteClientbound( packet.buf, con.getServerEntityId(), con.getClientEntityId() ); con.sendPacket( packet ); } }