From aa22fe68e5fb3ed26e19022d0419e269c95afcfe Mon Sep 17 00:00:00 2001 From: md_5 Date: Wed, 12 Aug 2020 07:14:42 +1000 Subject: [PATCH] Minecraft 1.16.2 support --- .../md_5/bungee/protocol/DefinedPacket.java | 26 +++ .../net/md_5/bungee/protocol/Protocol.java | 30 ++- .../bungee/protocol/ProtocolConstants.java | 4 +- .../md_5/bungee/protocol/packet/Commands.java | 1 + .../md_5/bungee/protocol/packet/Login.java | 60 ++++-- .../md_5/bungee/protocol/packet/Respawn.java | 17 +- .../java/net/md_5/bungee/ServerConnector.java | 2 +- .../bungee/connection/DownstreamBridge.java | 7 +- .../bungee/connection/UpstreamBridge.java | 7 +- .../net/md_5/bungee/entitymap/EntityMap.java | 6 +- .../md_5/bungee/entitymap/EntityMap_1_16.java | 187 ------------------ 11 files changed, 120 insertions(+), 227 deletions(-) delete mode 100644 proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_16.java diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java b/protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java index 57155c3b..f4470588 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java @@ -1,11 +1,19 @@ package net.md_5.bungee.protocol; import com.google.common.base.Charsets; +import com.google.common.base.Preconditions; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufInputStream; +import io.netty.buffer.ByteBufOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.UUID; import lombok.RequiredArgsConstructor; +import se.llbit.nbt.NamedTag; +import se.llbit.nbt.Tag; @RequiredArgsConstructor public abstract class DefinedPacket @@ -195,6 +203,24 @@ public abstract class DefinedPacket return new UUID( input.readLong(), input.readLong() ); } + public static Tag readTag(ByteBuf input) + { + Tag tag = NamedTag.read( new DataInputStream( new ByteBufInputStream( input ) ) ); + Preconditions.checkArgument( !tag.isError(), "Error reading tag: %s", tag.error() ); + return tag; + } + + public static void writeTag(Tag tag, ByteBuf output) + { + try + { + tag.write( new DataOutputStream( new ByteBufOutputStream( output ) ) ); + } catch ( IOException ex ) + { + throw new RuntimeException( "Exception writing tag", ex ); + } + } + public void read(ByteBuf buf) { throw new UnsupportedOperationException( "Packet must implement read method" ); diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/Protocol.java b/protocol/src/main/java/net/md_5/bungee/protocol/Protocol.java index 3e1776f7..caf4a685 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/Protocol.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/Protocol.java @@ -68,7 +68,8 @@ public enum Protocol map( ProtocolConstants.MINECRAFT_1_13, 0x21 ), map( ProtocolConstants.MINECRAFT_1_14, 0x20 ), map( ProtocolConstants.MINECRAFT_1_15, 0x21 ), - map( ProtocolConstants.MINECRAFT_1_16, 0x20 ) + map( ProtocolConstants.MINECRAFT_1_16, 0x20 ), + map( ProtocolConstants.MINECRAFT_1_16_2, 0x1F ) ); TO_CLIENT.registerPacket( Login.class, @@ -76,7 +77,8 @@ public enum Protocol map( ProtocolConstants.MINECRAFT_1_9, 0x23 ), map( ProtocolConstants.MINECRAFT_1_13, 0x25 ), map( ProtocolConstants.MINECRAFT_1_15, 0x26 ), - map( ProtocolConstants.MINECRAFT_1_16, 0x25 ) + map( ProtocolConstants.MINECRAFT_1_16, 0x25 ), + map( ProtocolConstants.MINECRAFT_1_16_2, 0x24 ) ); TO_CLIENT.registerPacket( Chat.class, @@ -95,7 +97,8 @@ public enum Protocol map( ProtocolConstants.MINECRAFT_1_13, 0x38 ), map( ProtocolConstants.MINECRAFT_1_14, 0x3A ), map( ProtocolConstants.MINECRAFT_1_15, 0x3B ), - map( ProtocolConstants.MINECRAFT_1_16, 0x3A ) + map( ProtocolConstants.MINECRAFT_1_16, 0x3A ), + map( ProtocolConstants.MINECRAFT_1_16_2, 0x39 ) ); TO_CLIENT.registerPacket( BossBar.class, @@ -111,7 +114,8 @@ public enum Protocol map( ProtocolConstants.MINECRAFT_1_13, 0x30 ), map( ProtocolConstants.MINECRAFT_1_14, 0x33 ), map( ProtocolConstants.MINECRAFT_1_15, 0x34 ), - map( ProtocolConstants.MINECRAFT_1_16, 0x33 ) + map( ProtocolConstants.MINECRAFT_1_16, 0x33 ), + map( ProtocolConstants.MINECRAFT_1_16_2, 0x32 ) ); TO_CLIENT.registerPacket( TabCompleteResponse.class, @@ -119,7 +123,8 @@ public enum Protocol map( ProtocolConstants.MINECRAFT_1_9, 0x0E ), map( ProtocolConstants.MINECRAFT_1_13, 0x10 ), map( ProtocolConstants.MINECRAFT_1_15, 0x11 ), - map( ProtocolConstants.MINECRAFT_1_16, 0x10 ) + map( ProtocolConstants.MINECRAFT_1_16, 0x10 ), + map( ProtocolConstants.MINECRAFT_1_16_2, 0x0F ) ); TO_CLIENT.registerPacket( ScoreboardObjective.class, @@ -168,7 +173,8 @@ public enum Protocol map( ProtocolConstants.MINECRAFT_1_13, 0x19 ), map( ProtocolConstants.MINECRAFT_1_14, 0x18 ), map( ProtocolConstants.MINECRAFT_1_15, 0x19 ), - map( ProtocolConstants.MINECRAFT_1_16, 0x18 ) + map( ProtocolConstants.MINECRAFT_1_16, 0x18 ), + map( ProtocolConstants.MINECRAFT_1_16_2, 0x17 ) ); TO_CLIENT.registerPacket( Kick.class, @@ -177,7 +183,8 @@ public enum Protocol map( ProtocolConstants.MINECRAFT_1_13, 0x1B ), map( ProtocolConstants.MINECRAFT_1_14, 0x1A ), map( ProtocolConstants.MINECRAFT_1_15, 0x1B ), - map( ProtocolConstants.MINECRAFT_1_16, 0x1A ) + map( ProtocolConstants.MINECRAFT_1_16, 0x1A ), + map( ProtocolConstants.MINECRAFT_1_16_2, 0x19 ) ); TO_CLIENT.registerPacket( Title.class, @@ -208,18 +215,21 @@ public enum Protocol map( ProtocolConstants.MINECRAFT_1_13, 0x1C ), map( ProtocolConstants.MINECRAFT_1_14, 0x1B ), map( ProtocolConstants.MINECRAFT_1_15, 0x1C ), - map( ProtocolConstants.MINECRAFT_1_16, 0x1B ) + map( ProtocolConstants.MINECRAFT_1_16, 0x1B ), + map( ProtocolConstants.MINECRAFT_1_16_2, 0x1A ) ); TO_CLIENT.registerPacket( Commands.class, map( ProtocolConstants.MINECRAFT_1_13, 0x11 ), map( ProtocolConstants.MINECRAFT_1_15, 0x12 ), - map( ProtocolConstants.MINECRAFT_1_16, 0x11 ) + map( ProtocolConstants.MINECRAFT_1_16, 0x11 ), + map( ProtocolConstants.MINECRAFT_1_16_2, 0x10 ) ); TO_CLIENT.registerPacket( GameState.class, map( ProtocolConstants.MINECRAFT_1_15, 0x1F ), - map( ProtocolConstants.MINECRAFT_1_16, 0x1E ) + map( ProtocolConstants.MINECRAFT_1_16, 0x1E ), + map( ProtocolConstants.MINECRAFT_1_16_2, 0x1D ) ); TO_CLIENT.registerPacket( ViewDistance.class, diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/ProtocolConstants.java b/protocol/src/main/java/net/md_5/bungee/protocol/ProtocolConstants.java index 7298b86c..4ffc35e4 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/ProtocolConstants.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/ProtocolConstants.java @@ -30,6 +30,7 @@ public class ProtocolConstants public static final int MINECRAFT_1_15_2 = 578; public static final int MINECRAFT_1_16 = 735; public static final int MINECRAFT_1_16_1 = 736; + public static final int MINECRAFT_1_16_2 = 751; public static final List SUPPORTED_VERSIONS = Arrays.asList( "1.8.x", "1.9.x", @@ -65,7 +66,8 @@ public class ProtocolConstants ProtocolConstants.MINECRAFT_1_15_1, ProtocolConstants.MINECRAFT_1_15_2, ProtocolConstants.MINECRAFT_1_16, - ProtocolConstants.MINECRAFT_1_16_1 + ProtocolConstants.MINECRAFT_1_16_1, + ProtocolConstants.MINECRAFT_1_16_2 ); public enum Direction diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Commands.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Commands.java index 2b1c8377..26edbca8 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Commands.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Commands.java @@ -550,6 +550,7 @@ public class Commands extends DefinedPacket PROVIDERS.put( "minecraft:uuid", VOID ); // 1.16 PROVIDERS.put( "minecraft:test_argument", VOID ); // 1.16, debug PROVIDERS.put( "minecraft:test_class", VOID ); // 1.16, debug + PROVIDERS.put( "minecraft:angle", VOID ); // 1.16.2 } private static ArgumentType read(String key, ByteBuf buf) diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Login.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Login.java index 6ca80e2b..c518eed7 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Login.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Login.java @@ -2,11 +2,6 @@ package net.md_5.bungee.protocol.packet; import com.google.common.base.Preconditions; import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufInputStream; -import io.netty.buffer.ByteBufOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; import java.util.HashSet; import java.util.Set; import lombok.AllArgsConstructor; @@ -16,7 +11,6 @@ import lombok.NoArgsConstructor; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; import net.md_5.bungee.protocol.ProtocolConstants; -import se.llbit.nbt.NamedTag; import se.llbit.nbt.Tag; @Data @@ -27,6 +21,7 @@ public class Login extends DefinedPacket { private int entityId; + private boolean hardcore; private short gameMode; private short previousGameMode; private Set worldNames; @@ -35,7 +30,7 @@ public class Login extends DefinedPacket private String worldName; private long seed; private short difficulty; - private short maxPlayers; + private int maxPlayers; private String levelType; private int viewDistance; private boolean reducedDebugInfo; @@ -47,6 +42,10 @@ public class Login extends DefinedPacket public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { entityId = buf.readInt(); + if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_16_2 ) + { + hardcore = buf.readBoolean(); + } gameMode = buf.readUnsignedByte(); if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_16 ) { @@ -61,13 +60,18 @@ public class Login extends DefinedPacket worldNames.add( readString( buf ) ); } - dimensions = NamedTag.read( new DataInputStream( new ByteBufInputStream( buf ) ) ); - Preconditions.checkArgument( !dimensions.isError(), "Error reading dimensions: %s", dimensions.error() ); + dimensions = readTag( buf ); } if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_16 ) { - dimension = readString( buf ); + if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_16_2 ) + { + dimension = readTag( buf ); + } else + { + dimension = readString( buf ); + } worldName = readString( buf ); } else if ( protocolVersion > ProtocolConstants.MINECRAFT_1_9 ) { @@ -84,7 +88,13 @@ public class Login extends DefinedPacket { difficulty = buf.readUnsignedByte(); } - maxPlayers = buf.readUnsignedByte(); + if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_16_2 ) + { + maxPlayers = readVarInt( buf ); + } else + { + maxPlayers = buf.readUnsignedByte(); + } if ( protocolVersion < ProtocolConstants.MINECRAFT_1_16 ) { levelType = readString( buf ); @@ -112,6 +122,10 @@ public class Login extends DefinedPacket public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { buf.writeInt( entityId ); + if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_16_2 ) + { + buf.writeBoolean( hardcore ); + } buf.writeByte( gameMode ); if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_16 ) { @@ -123,18 +137,18 @@ public class Login extends DefinedPacket writeString( world, buf ); } - try - { - dimensions.write( new DataOutputStream( new ByteBufOutputStream( buf ) ) ); - } catch ( IOException ex ) - { - throw new RuntimeException( "Exception writing dimensions", ex ); - } + writeTag( dimensions, buf ); } if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_16 ) { - writeString( (String) dimension, buf ); + if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_16_2 ) + { + writeTag( (Tag) dimension, buf ); + } else + { + writeString( (String) dimension, buf ); + } writeString( worldName, buf ); } else if ( protocolVersion > ProtocolConstants.MINECRAFT_1_9 ) { @@ -151,7 +165,13 @@ public class Login extends DefinedPacket { buf.writeByte( difficulty ); } - buf.writeByte( maxPlayers ); + if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_16_2 ) + { + writeVarInt( maxPlayers, buf ); + } else + { + buf.writeByte( maxPlayers ); + } if ( protocolVersion < ProtocolConstants.MINECRAFT_1_16 ) { writeString( levelType, buf ); diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Respawn.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Respawn.java index 0d8068ea..36db3d49 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Respawn.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Respawn.java @@ -8,6 +8,7 @@ import lombok.NoArgsConstructor; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; import net.md_5.bungee.protocol.ProtocolConstants; +import se.llbit.nbt.Tag; @Data @NoArgsConstructor @@ -32,7 +33,13 @@ public class Respawn extends DefinedPacket { if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_16 ) { - dimension = readString( buf ); + if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_16_2 ) + { + dimension = readTag( buf ); + } else + { + dimension = readString( buf ); + } worldName = readString( buf ); } else { @@ -64,7 +71,13 @@ public class Respawn extends DefinedPacket { if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_16 ) { - writeString( (String) dimension, buf ); + if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_16_2 ) + { + writeTag( (Tag) dimension, buf ); + } else + { + writeString( (String) dimension, buf ); + } writeString( worldName, buf ); } else { 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 b4dd2049..15b2ba0d 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java @@ -212,7 +212,7 @@ public class ServerConnector extends PacketHandler user.setServerEntityId( login.getEntityId() ); // Set tab list size, TODO: what shall we do about packet mutability - Login modLogin = new Login( login.getEntityId(), login.getGameMode(), login.getPreviousGameMode(), login.getWorldNames(), login.getDimensions(), login.getDimension(), login.getWorldName(), login.getSeed(), login.getDifficulty(), + Login modLogin = new Login( login.getEntityId(), login.isHardcore(), login.getGameMode(), login.getPreviousGameMode(), login.getWorldNames(), login.getDimensions(), login.getDimension(), login.getWorldName(), login.getSeed(), login.getDifficulty(), (byte) user.getPendingConnection().getListener().getTabListSize(), login.getLevelType(), login.getViewDistance(), login.isReducedDebugInfo(), login.isNormalRespawn(), login.isDebug(), login.isFlat() ); user.unsafe().sendPacket( modLogin ); 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 95922b34..a575b634 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 @@ -43,6 +43,7 @@ import net.md_5.bungee.api.score.Score; import net.md_5.bungee.api.score.Scoreboard; import net.md_5.bungee.api.score.Team; import net.md_5.bungee.chat.ComponentSerializer; +import net.md_5.bungee.entitymap.EntityMap; import net.md_5.bungee.netty.ChannelWrapper; import net.md_5.bungee.netty.PacketHandler; import net.md_5.bungee.protocol.DefinedPacket; @@ -119,7 +120,11 @@ public class DownstreamBridge extends PacketHandler @Override public void handle(PacketWrapper packet) throws Exception { - con.getEntityRewrite().rewriteClientbound( packet.buf, con.getServerEntityId(), con.getClientEntityId(), con.getPendingConnection().getVersion() ); + EntityMap rewrite = con.getEntityRewrite(); + if ( rewrite != null ) + { + rewrite.rewriteClientbound( packet.buf, con.getServerEntityId(), con.getClientEntityId(), con.getPendingConnection().getVersion() ); + } con.sendPacket( packet ); } diff --git a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java index d93efd87..f150d8fa 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java @@ -19,6 +19,7 @@ import net.md_5.bungee.api.event.PlayerDisconnectEvent; import net.md_5.bungee.api.event.PluginMessageEvent; import net.md_5.bungee.api.event.SettingsChangedEvent; import net.md_5.bungee.api.event.TabCompleteEvent; +import net.md_5.bungee.entitymap.EntityMap; import net.md_5.bungee.forge.ForgeConstants; import net.md_5.bungee.netty.ChannelWrapper; import net.md_5.bungee.netty.PacketHandler; @@ -114,7 +115,11 @@ public class UpstreamBridge extends PacketHandler { if ( con.getServer() != null ) { - con.getEntityRewrite().rewriteServerbound( packet.buf, con.getClientEntityId(), con.getServerEntityId(), con.getPendingConnection().getVersion() ); + EntityMap rewrite = con.getEntityRewrite(); + if ( rewrite != null ) + { + rewrite.rewriteServerbound( packet.buf, con.getClientEntityId(), con.getServerEntityId(), con.getPendingConnection().getVersion() ); + } con.getServer().getCh().write( packet ); } } diff --git a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java index 2622b738..e20be624 100644 --- a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java +++ b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java @@ -61,11 +61,9 @@ public abstract class EntityMap case ProtocolConstants.MINECRAFT_1_15_1: case ProtocolConstants.MINECRAFT_1_15_2: return EntityMap_1_15.INSTANCE; - case ProtocolConstants.MINECRAFT_1_16: - case ProtocolConstants.MINECRAFT_1_16_1: - return EntityMap_1_16.INSTANCE; + default: + return null; } - throw new RuntimeException( "Version " + version + " has no entity map" ); } protected void addRewrite(int id, ProtocolConstants.Direction direction, boolean varint) diff --git a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_16.java b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_16.java deleted file mode 100644 index c8b06707..00000000 --- a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_16.java +++ /dev/null @@ -1,187 +0,0 @@ -package net.md_5.bungee.entitymap; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import io.netty.buffer.ByteBuf; -import java.util.UUID; -import net.md_5.bungee.BungeeCord; -import net.md_5.bungee.UserConnection; -import net.md_5.bungee.api.connection.ProxiedPlayer; -import net.md_5.bungee.protocol.DefinedPacket; -import net.md_5.bungee.protocol.ProtocolConstants; - -class EntityMap_1_16 extends EntityMap -{ - - static final EntityMap_1_16 INSTANCE = new EntityMap_1_16(); - - EntityMap_1_16() - { - addRewrite( 0x00, ProtocolConstants.Direction.TO_CLIENT, true ); // Spawn Object : PacketPlayOutSpawnEntity - addRewrite( 0x01, ProtocolConstants.Direction.TO_CLIENT, true ); // Spawn Experience Orb : PacketPlayOutSpawnEntityExperienceOrb - addRewrite( 0x02, ProtocolConstants.Direction.TO_CLIENT, true ); // Spawn Mob : PacketPlayOutSpawnEntityLiving - addRewrite( 0x03, ProtocolConstants.Direction.TO_CLIENT, true ); // Spawn Painting : PacketPlayOutSpawnEntityPainting - addRewrite( 0x04, ProtocolConstants.Direction.TO_CLIENT, true ); // Spawn Player : PacketPlayOutNamedEntitySpawn - addRewrite( 0x05, ProtocolConstants.Direction.TO_CLIENT, true ); // Animation : PacketPlayOutAnimation - addRewrite( 0x08, ProtocolConstants.Direction.TO_CLIENT, true ); // Block Break Animation : PacketPlayOutBlockBreakAnimation - addRewrite( 0x1B, ProtocolConstants.Direction.TO_CLIENT, false ); // Entity Status : PacketPlayOutEntityStatus - addRewrite( 0x28, ProtocolConstants.Direction.TO_CLIENT, true ); // Entity Relative Move : PacketPlayOutRelEntityMove - addRewrite( 0x29, ProtocolConstants.Direction.TO_CLIENT, true ); // Entity Look and Relative Move : PacketPlayOutRelEntityMoveLook - addRewrite( 0x2A, ProtocolConstants.Direction.TO_CLIENT, true ); // Entity Look : PacketPlayOutEntityLook - addRewrite( 0x2B, ProtocolConstants.Direction.TO_CLIENT, true ); // Entity : PacketPlayOutEntity - addRewrite( 0x38, ProtocolConstants.Direction.TO_CLIENT, true ); // Remove Entity Effect : PacketPlayOutRemoveEntityEffect - addRewrite( 0x3B, ProtocolConstants.Direction.TO_CLIENT, true ); // Entity Head Look : PacketPlayOutEntityHeadRotation - addRewrite( 0x3E, ProtocolConstants.Direction.TO_CLIENT, true ); // Camera : PacketPlayOutCamera - addRewrite( 0x44, ProtocolConstants.Direction.TO_CLIENT, true ); // Entity Metadata : PacketPlayOutEntityMetadata - addRewrite( 0x45, ProtocolConstants.Direction.TO_CLIENT, false ); // Attach Entity : PacketPlayOutAttachEntity - addRewrite( 0x46, ProtocolConstants.Direction.TO_CLIENT, true ); // Entity Velocity : PacketPlayOutEntityVelocity - addRewrite( 0x47, ProtocolConstants.Direction.TO_CLIENT, true ); // Entity Equipment : PacketPlayOutEntityEquipment - addRewrite( 0x4B, ProtocolConstants.Direction.TO_CLIENT, true ); // Set Passengers : PacketPlayOutMount - addRewrite( 0x55, ProtocolConstants.Direction.TO_CLIENT, true ); // Collect Item : PacketPlayOutCollect - addRewrite( 0x56, ProtocolConstants.Direction.TO_CLIENT, true ); // Entity Teleport : PacketPlayOutEntityTeleport - addRewrite( 0x58, ProtocolConstants.Direction.TO_CLIENT, true ); // Entity Properties : PacketPlayOutUpdateAttributes - addRewrite( 0x59, ProtocolConstants.Direction.TO_CLIENT, true ); // Entity Effect : PacketPlayOutEntityEffect - - addRewrite( 0x0E, ProtocolConstants.Direction.TO_SERVER, true ); // Use Entity : PacketPlayInUseEntity - addRewrite( 0x1C, ProtocolConstants.Direction.TO_SERVER, true ); // Entity Action : PacketPlayInEntityAction - } - - @Override - @SuppressFBWarnings("DLS_DEAD_LOCAL_STORE") - public void rewriteClientbound(ByteBuf packet, int oldId, int newId, int protocolVersion) - { - super.rewriteClientbound( packet, oldId, newId ); - - // Special cases - int readerIndex = packet.readerIndex(); - int packetId = DefinedPacket.readVarInt( packet ); - int packetIdLength = packet.readerIndex() - readerIndex; - int jumpIndex = packet.readerIndex(); - switch ( packetId ) - { - case 0x45 /* Attach Entity : PacketPlayOutAttachEntity */: - rewriteInt( packet, oldId, newId, readerIndex + packetIdLength + 4 ); - break; - case 0x55 /* Collect Item : PacketPlayOutCollect */: - DefinedPacket.readVarInt( packet ); - rewriteVarInt( packet, oldId, newId, packet.readerIndex() ); - break; - case 0x4B /* Set Passengers : PacketPlayOutMount */: - DefinedPacket.readVarInt( packet ); - jumpIndex = packet.readerIndex(); - // Fall through on purpose to int array of IDs - case 0x37 /* Destroy Entities : PacketPlayOutEntityDestroy */: - int count = DefinedPacket.readVarInt( packet ); - int[] ids = new int[ count ]; - for ( int i = 0; i < count; i++ ) - { - ids[i] = DefinedPacket.readVarInt( packet ); - } - packet.readerIndex( jumpIndex ); - packet.writerIndex( jumpIndex ); - DefinedPacket.writeVarInt( count, packet ); - for ( int id : ids ) - { - if ( id == oldId ) - { - id = newId; - } else if ( id == newId ) - { - id = oldId; - } - DefinedPacket.writeVarInt( id, packet ); - } - break; - case 0x00 /* Spawn Object : PacketPlayOutSpawnEntity */: - DefinedPacket.readVarInt( packet ); - DefinedPacket.readUUID( packet ); - int type = DefinedPacket.readVarInt( packet ); - - if ( type == 2 || type == 102 || type == 72 ) // arrow, fishing_bobber or spectral_arrow - { - if ( type == 2 || type == 72 ) // arrow or spectral_arrow - { - oldId = oldId + 1; - newId = newId + 1; - } - - packet.skipBytes( 26 ); // double, double, double, byte, byte - int position = packet.readerIndex(); - int readId = packet.readInt(); - if ( readId == oldId ) - { - packet.setInt( position, newId ); - } else if ( readId == newId ) - { - packet.setInt( position, oldId ); - } - } - break; - case 0x04 /* Spawn Player : PacketPlayOutNamedEntitySpawn */: - DefinedPacket.readVarInt( packet ); // Entity ID - int idLength = packet.readerIndex() - readerIndex - packetIdLength; - UUID uuid = DefinedPacket.readUUID( packet ); - ProxiedPlayer player; - if ( ( player = BungeeCord.getInstance().getPlayerByOfflineUUID( uuid ) ) != null ) - { - int previous = packet.writerIndex(); - packet.readerIndex( readerIndex ); - packet.writerIndex( readerIndex + packetIdLength + idLength ); - DefinedPacket.writeUUID( player.getUniqueId(), packet ); - packet.writerIndex( previous ); - } - break; - case 0x32 /* Combat Event : PacketPlayOutCombatEvent */: - int event = packet.readUnsignedByte(); - if ( event == 1 /* End Combat*/ ) - { - DefinedPacket.readVarInt( packet ); - rewriteInt( packet, oldId, newId, packet.readerIndex() ); - } else if ( event == 2 /* Entity Dead */ ) - { - int position = packet.readerIndex(); - rewriteVarInt( packet, oldId, newId, packet.readerIndex() ); - packet.readerIndex( position ); - DefinedPacket.readVarInt( packet ); - rewriteInt( packet, oldId, newId, packet.readerIndex() ); - } - break; - case 0x44 /* EntityMetadata : PacketPlayOutEntityMetadata */: - DefinedPacket.readVarInt( packet ); // Entity ID - rewriteMetaVarInt( packet, oldId + 1, newId + 1, 7, protocolVersion ); // fishing hook - rewriteMetaVarInt( packet, oldId, newId, 8, protocolVersion ); // fireworks (et al) - rewriteMetaVarInt( packet, oldId, newId, 16, protocolVersion ); // guardian beam - break; - case 0x50 /* Entity Sound Effect : PacketPlayOutEntitySound */: - DefinedPacket.readVarInt( packet ); - DefinedPacket.readVarInt( packet ); - rewriteVarInt( packet, oldId, newId, packet.readerIndex() ); - break; - } - packet.readerIndex( readerIndex ); - } - - @Override - public void rewriteServerbound(ByteBuf packet, int oldId, int newId) - { - super.rewriteServerbound( packet, oldId, newId ); - // Special cases - int readerIndex = packet.readerIndex(); - int packetId = DefinedPacket.readVarInt( packet ); - int packetIdLength = packet.readerIndex() - readerIndex; - - if ( packetId == 0x2C /* Spectate : PacketPlayInSpectate */ && !BungeeCord.getInstance().getConfig().isIpForward() ) - { - UUID uuid = DefinedPacket.readUUID( packet ); - ProxiedPlayer player; - if ( ( player = BungeeCord.getInstance().getPlayer( uuid ) ) != null ) - { - int previous = packet.writerIndex(); - packet.readerIndex( readerIndex ); - packet.writerIndex( readerIndex + packetIdLength ); - DefinedPacket.writeUUID( ( (UserConnection) player ).getPendingConnection().getOfflineId(), packet ); - packet.writerIndex( previous ); - } - } - packet.readerIndex( readerIndex ); - } -}