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
This commit is contained in:
Thinkofdeath 2013-12-08 13:47:49 +00:00
parent 85c27f30ee
commit cfda905d98
2 changed files with 73 additions and 64 deletions

View File

@ -10,83 +10,94 @@ import net.md_5.bungee.protocol.PacketWrapper;
public class EntityMap 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 static
{ {
entityIds[0x0A] = new int[] clientboundInts[0x04] = true;
{ clientboundInts[0x0A] = true;
0 clientboundVarInts[0x0B] = true;
}; clientboundVarInts[0x0C] = true;
entityIds[0x0D] = new int[] clientboundInts[0x0D] = true;
{ clientboundVarInts[0x0E] = true;
4 clientboundVarInts[0x0F] = true;
}; clientboundVarInts[0x10] = true;
entityIds[0x12] = new int[] clientboundVarInts[0x11] = true;
{ clientboundInts[0x12] = true;
0 clientboundInts[0x14] = true;
}; clientboundInts[0x15] = true;
entityIds[0x1A] = new int[] clientboundInts[0x16] = true;
{ clientboundInts[0x17] = true;
0 clientboundInts[0x18] = true;
}; clientboundInts[0x19] = true;
entityIds[0x1B] = new int[] clientboundInts[0x1A] = true;
{ clientboundInts[0x1B] = true;
0, 4 clientboundInts[0x1C] = true;
}; clientboundInts[0x1D] = true;
entityIds[0x1C] = new int[] clientboundInts[0x1E] = true;
{ clientboundInts[0x20] = true;
0 // TODO: Meta clientboundVarInts[0x25] = true;
}; clientboundVarInts[0x2C] = true;
entityIds[0x1D] = new int[]
{
0
};
entityIds[0x1E] = new int[]
{
0
};
entityIds[0x20] = new int[]
{
0
};
} }
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 readerIndex = packet.readerIndex();
int packetId = DefinedPacket.readVarInt( packet ); int packetId = DefinedPacket.readVarInt( packet );
int packetIdLength = packet.readerIndex() - readerIndex; 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 ); packet.setInt( packetIdLength, clientEntityId );
if ( readId == oldId ) } 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 ); packet.readerIndex( readerIndex );
} }
} }

View File

@ -3,10 +3,8 @@ package net.md_5.bungee.connection;
import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import java.io.DataInput; import java.io.DataInput;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.EntityMap; import net.md_5.bungee.EntityMap;
import net.md_5.bungee.ServerConnection; import net.md_5.bungee.ServerConnection;
import net.md_5.bungee.api.event.ServerDisconnectEvent; import net.md_5.bungee.api.event.ServerDisconnectEvent;
@ -80,7 +78,7 @@ public class DownstreamBridge extends PacketHandler
{ {
if ( !server.isObsolete() ) if ( !server.isObsolete() )
{ {
EntityMap.rewrite( packet.buf, con.getServerEntityId(), con.getClientEntityId() ); EntityMap.rewriteClientbound( packet.buf, con.getServerEntityId(), con.getClientEntityId() );
con.sendPacket( packet ); con.sendPacket( packet );
} }
} }