From 2f45f0d5784d1ec4732a7549a2b52c1fb17ff95c Mon Sep 17 00:00:00 2001 From: md_5 Date: Thu, 30 May 2013 18:09:46 +1000 Subject: [PATCH] Rework protocol system --- .../java/net/md_5/bungee/protocol/Forge.java | 25 ++++ .../java/net/md_5/bungee/protocol/OpCode.java | 7 + .../net/md_5/bungee/protocol/Protocol.java | 20 +++ .../{PacketDefinitions.java => Vanilla.java} | 120 ++++++++++++++++-- .../bungee/protocol/packet/DefinedPacket.java | 58 --------- .../protocol/packet/Packet0KeepAlive.java | 2 +- .../bungee/protocol/packet/Packet1Login.java | 16 +-- .../protocol/packet/Packet2Handshake.java | 2 +- .../bungee/protocol/packet/Packet3Chat.java | 2 +- .../protocol/packet/Packet9Respawn.java | 2 +- .../packet/PacketC9PlayerListItem.java | 2 +- .../protocol/packet/PacketCCSettings.java | 2 +- .../protocol/packet/PacketCDClientStatus.java | 2 +- .../packet/PacketCEScoreboardObjective.java | 2 +- .../packet/PacketCFScoreboardScore.java | 2 +- .../packet/PacketD0DisplayScoreboard.java | 2 +- .../bungee/protocol/packet/PacketD1Team.java | 2 +- .../packet/PacketFAPluginMessage.java | 2 +- .../packet/PacketFCEncryptionResponse.java | 2 +- .../packet/PacketFDEncryptionRequest.java | 2 +- .../bungee/protocol/packet/PacketFEPing.java | 2 +- .../bungee/protocol/packet/PacketFFKick.java | 2 +- .../protocol/packet/forge/Forge1Login.java | 42 ++++++ .../protocol/{netty => skip}/BulkChunk.java | 5 +- .../protocol/{netty => skip}/ByteHeader.java | 5 +- .../protocol/{netty => skip}/Instruction.java | 5 +- .../protocol/{netty => skip}/IntHeader.java | 5 +- .../bungee/protocol/{netty => skip}/Item.java | 5 +- .../bungee/protocol/{netty => skip}/Jump.java | 5 +- .../protocol/{netty => skip}/MetaData.java | 5 +- .../{netty => skip}/OptionalMotion.java | 5 +- .../{netty => skip}/PacketReader.java | 43 ++----- .../protocol/{netty => skip}/ShortHeader.java | 5 +- .../{netty => skip}/UnsignedShortByte.java | 5 +- .../net/md_5/bungee/protocol/PacketTest.java | 2 +- .../main/java/net/md_5/bungee/BungeeCord.java | 6 +- .../java/net/md_5/bungee/ServerConnector.java | 6 +- .../bungee/connection/InitialHandler.java | 4 +- .../net/md_5/bungee/netty/PacketDecoder.java | 2 +- .../net/md_5/bungee/netty/PipelineUtils.java | 4 +- 40 files changed, 273 insertions(+), 164 deletions(-) create mode 100644 protocol/src/main/java/net/md_5/bungee/protocol/Forge.java create mode 100644 protocol/src/main/java/net/md_5/bungee/protocol/OpCode.java create mode 100644 protocol/src/main/java/net/md_5/bungee/protocol/Protocol.java rename protocol/src/main/java/net/md_5/bungee/protocol/{PacketDefinitions.java => Vanilla.java} (57%) create mode 100644 protocol/src/main/java/net/md_5/bungee/protocol/packet/forge/Forge1Login.java rename protocol/src/main/java/net/md_5/bungee/protocol/{netty => skip}/BulkChunk.java (68%) rename protocol/src/main/java/net/md_5/bungee/protocol/{netty => skip}/ByteHeader.java (75%) rename protocol/src/main/java/net/md_5/bungee/protocol/{netty => skip}/Instruction.java (91%) rename protocol/src/main/java/net/md_5/bungee/protocol/{netty => skip}/IntHeader.java (74%) rename protocol/src/main/java/net/md_5/bungee/protocol/{netty => skip}/Item.java (68%) rename protocol/src/main/java/net/md_5/bungee/protocol/{netty => skip}/Jump.java (72%) rename protocol/src/main/java/net/md_5/bungee/protocol/{netty => skip}/MetaData.java (90%) rename protocol/src/main/java/net/md_5/bungee/protocol/{netty => skip}/OptionalMotion.java (65%) rename protocol/src/main/java/net/md_5/bungee/protocol/{netty => skip}/PacketReader.java (59%) rename protocol/src/main/java/net/md_5/bungee/protocol/{netty => skip}/ShortHeader.java (75%) rename protocol/src/main/java/net/md_5/bungee/protocol/{netty => skip}/UnsignedShortByte.java (61%) diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/Forge.java b/protocol/src/main/java/net/md_5/bungee/protocol/Forge.java new file mode 100644 index 00000000..52bf7473 --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/Forge.java @@ -0,0 +1,25 @@ +package net.md_5.bungee.protocol; + +import io.netty.buffer.ByteBuf; +import lombok.Getter; +import net.md_5.bungee.protocol.packet.DefinedPacket; + +public class Forge extends Vanilla +{ + + @Getter + private static final Forge instance = new Forge(); + + @Override + public DefinedPacket read(short packetId, ByteBuf buf) + { + int start = buf.readerIndex(); + DefinedPacket packet = read( packetId, buf, this ); + if ( buf.readerIndex() == start ) + { + packet = super.read( packetId, buf ); + } + + return packet; + } +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/OpCode.java b/protocol/src/main/java/net/md_5/bungee/protocol/OpCode.java new file mode 100644 index 00000000..29d6db3e --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/OpCode.java @@ -0,0 +1,7 @@ +package net.md_5.bungee.protocol; + +public enum OpCode +{ + + BOOLEAN, BULK_CHUNK, BYTE, BYTE_INT, DOUBLE, FLOAT, INT, INT_3, INT_BYTE, ITEM, LONG, METADATA, OPTIONAL_MOTION, SHORT, SHORT_BYTE, SHORT_ITEM, STRING, USHORT_BYTE +} 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 new file mode 100644 index 00000000..82fbd8e5 --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/Protocol.java @@ -0,0 +1,20 @@ +package net.md_5.bungee.protocol; + +import io.netty.buffer.ByteBuf; +import java.lang.reflect.Constructor; +import net.md_5.bungee.protocol.packet.DefinedPacket; +import net.md_5.bungee.protocol.skip.PacketReader; + +public interface Protocol +{ + + PacketReader getSkipper(); + + DefinedPacket read(short packetId, ByteBuf buf); + + OpCode[][] getOpCodes(); + + Class[] getClasses(); + + Constructor[] getConstructors(); +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/PacketDefinitions.java b/protocol/src/main/java/net/md_5/bungee/protocol/Vanilla.java similarity index 57% rename from protocol/src/main/java/net/md_5/bungee/protocol/PacketDefinitions.java rename to protocol/src/main/java/net/md_5/bungee/protocol/Vanilla.java index 4d536b6c..4719fa8b 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/PacketDefinitions.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/Vanilla.java @@ -1,23 +1,125 @@ package net.md_5.bungee.protocol; -import static net.md_5.bungee.protocol.PacketDefinitions.OpCode.*; +import io.netty.buffer.ByteBuf; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import lombok.Getter; +import static net.md_5.bungee.protocol.OpCode.*; +import net.md_5.bungee.protocol.packet.DefinedPacket; +import net.md_5.bungee.protocol.packet.Packet0KeepAlive; +import net.md_5.bungee.protocol.packet.Packet1Login; +import net.md_5.bungee.protocol.packet.Packet2Handshake; +import net.md_5.bungee.protocol.packet.Packet3Chat; +import net.md_5.bungee.protocol.packet.Packet9Respawn; +import net.md_5.bungee.protocol.packet.PacketC9PlayerListItem; +import net.md_5.bungee.protocol.packet.PacketCCSettings; +import net.md_5.bungee.protocol.packet.PacketCDClientStatus; +import net.md_5.bungee.protocol.packet.PacketCEScoreboardObjective; +import net.md_5.bungee.protocol.packet.PacketCFScoreboardScore; +import net.md_5.bungee.protocol.packet.PacketD0DisplayScoreboard; +import net.md_5.bungee.protocol.packet.PacketD1Team; +import net.md_5.bungee.protocol.packet.PacketFAPluginMessage; +import net.md_5.bungee.protocol.packet.PacketFCEncryptionResponse; +import net.md_5.bungee.protocol.packet.PacketFDEncryptionRequest; +import net.md_5.bungee.protocol.packet.PacketFEPing; +import net.md_5.bungee.protocol.packet.PacketFFKick; +import net.md_5.bungee.protocol.skip.PacketReader; -public class PacketDefinitions +public class Vanilla implements Protocol { public static final byte PROTOCOL_VERSION = 61; public static final String GAME_VERSION = "1.5.2"; - public static final OpCode[][] opCodes = new OpCode[ 512 ][]; - public static final int VANILLA_PROTOCOL = 0; - public static final int FORGE_PROTOCOL = 256; + public static final Vanilla INSTANCE = new Vanilla(); + /*========================================================================*/ + @Getter + private final OpCode[][] opCodes = new OpCode[ 256 ][]; + @SuppressWarnings("unchecked") + @Getter + private Class[] classes = new Class[ 256 ]; + @SuppressWarnings("unchecked") + @Getter + private Constructor[] constructors = new Constructor[ 256 ]; + @Getter + private final PacketReader skipper = new PacketReader( this ); + /*========================================================================*/ - public enum OpCode + { - - BOOLEAN, BULK_CHUNK, BYTE, BYTE_INT, DOUBLE, FLOAT, INT, INT_3, INT_BYTE, ITEM, LONG, METADATA, OPTIONAL_MOTION, SHORT, SHORT_BYTE, SHORT_ITEM, STRING, USHORT_BYTE + classes[0x00] = Packet0KeepAlive.class; + classes[0x01] = Packet1Login.class; + classes[0x02] = Packet2Handshake.class; + classes[0x03] = Packet3Chat.class; + classes[0x09] = Packet9Respawn.class; + classes[0xC9] = PacketC9PlayerListItem.class; + classes[0xCC] = PacketCCSettings.class; + classes[0xCD] = PacketCDClientStatus.class; + classes[0xCE] = PacketCEScoreboardObjective.class; + classes[0xCF] = PacketCFScoreboardScore.class; + classes[0xD0] = PacketD0DisplayScoreboard.class; + classes[0xD1] = PacketD1Team.class; + classes[0xFA] = PacketFAPluginMessage.class; + classes[0xFC] = PacketFCEncryptionResponse.class; + classes[0xFD] = PacketFDEncryptionRequest.class; + classes[0xFE] = PacketFEPing.class; + classes[0xFF] = PacketFFKick.class; } - static + @Override + public DefinedPacket read(short packetId, ByteBuf buf) + { + int start = buf.readerIndex(); + DefinedPacket packet = read( packetId, buf, this ); + if ( buf.readerIndex() == start ) + { + throw new RuntimeException( "Unknown packet id " + packetId ); + } + return packet; + } + + public static DefinedPacket read(short id, ByteBuf buf, Protocol protocol) + { + DefinedPacket packet = packet( id, protocol ); + if ( packet != null ) + { + packet.read( buf ); + return packet; + } + protocol.getSkipper().tryRead( id, buf ); + return null; + } + + public static DefinedPacket packet(short id, Protocol protocol) + { + DefinedPacket ret = null; + Class clazz = protocol.getClasses()[id]; + + if ( clazz != null ) + { + try + { + Constructor constructor = protocol.getConstructors()[id]; + if ( constructor == null ) + { + constructor = clazz.getDeclaredConstructor(); + constructor.setAccessible( true ); + protocol.getConstructors()[id] = constructor; + } + + if ( constructor != null ) + { + ret = constructor.newInstance(); + } + } catch ( NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex ) + { + } + } + + return ret; + } + + { opCodes[0x04] = new OpCode[] { diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/DefinedPacket.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/DefinedPacket.java index aa831082..fa0ccd41 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/DefinedPacket.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/DefinedPacket.java @@ -1,51 +1,14 @@ package net.md_5.bungee.protocol.packet; import io.netty.buffer.ByteBuf; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import lombok.AccessLevel; import lombok.RequiredArgsConstructor; @RequiredArgsConstructor public abstract class DefinedPacket { - @SuppressWarnings("unchecked") - public static Class[] classes = new Class[ 256 ]; - @SuppressWarnings("unchecked") - private static Constructor[] consructors = new Constructor[ 256 ]; private final int id; - - public static DefinedPacket packet(ByteBuf buf) - { - DefinedPacket ret = null; - int id = buf.readUnsignedByte(); - Class clazz = classes[id]; - - if ( clazz != null ) - { - try - { - Constructor constructor = consructors[id]; - if ( constructor == null ) - { - constructor = clazz.getDeclaredConstructor(); - consructors[id] = constructor; - } - - if ( constructor != null ) - { - ret = constructor.newInstance(); - } - } catch ( NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex ) - { - } - } - - return ret; - } - public final int getId() { return id; @@ -103,25 +66,4 @@ public abstract class DefinedPacket @Override public abstract String toString(); - - static - { - classes[0x00] = Packet0KeepAlive.class; - classes[0x01] = Packet1Login.class; - classes[0x02] = Packet2Handshake.class; - classes[0x03] = Packet3Chat.class; - classes[0x09] = Packet9Respawn.class; - classes[0xC9] = PacketC9PlayerListItem.class; - classes[0xCC] = PacketCCSettings.class; - classes[0xCD] = PacketCDClientStatus.class; - classes[0xCE] = PacketCEScoreboardObjective.class; - classes[0xCF] = PacketCFScoreboardScore.class; - classes[0xD0] = PacketD0DisplayScoreboard.class; - classes[0xD1] = PacketD1Team.class; - classes[0xFA] = PacketFAPluginMessage.class; - classes[0xFC] = PacketFCEncryptionResponse.class; - classes[0xFD] = PacketFDEncryptionRequest.class; - classes[0xFE] = PacketFEPing.class; - classes[0xFF] = PacketFFKick.class; - } } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet0KeepAlive.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet0KeepAlive.java index 535c0af3..4898f105 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet0KeepAlive.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet0KeepAlive.java @@ -11,7 +11,7 @@ public class Packet0KeepAlive extends DefinedPacket private int id; - Packet0KeepAlive() + private Packet0KeepAlive() { super( 0x00 ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet1Login.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet1Login.java index 7ed5d0bc..a7b6c901 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet1Login.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet1Login.java @@ -9,15 +9,15 @@ import lombok.ToString; public class Packet1Login extends DefinedPacket { - private int entityId; - private String levelType; - private byte gameMode; - private byte dimension; - private byte difficulty; - private byte unused; - private byte maxPlayers; + protected int entityId; + protected String levelType; + protected byte gameMode; + protected int dimension; + protected byte difficulty; + protected byte unused; + protected byte maxPlayers; - Packet1Login() + protected Packet1Login() { super( 0x01 ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet2Handshake.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet2Handshake.java index d482a2b2..a7c32167 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet2Handshake.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet2Handshake.java @@ -14,7 +14,7 @@ public class Packet2Handshake extends DefinedPacket private String host; private int port; - Packet2Handshake() + private Packet2Handshake() { super( 0x02 ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet3Chat.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet3Chat.java index 8cd46e3a..9f3bb0d7 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet3Chat.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet3Chat.java @@ -11,7 +11,7 @@ public class Packet3Chat extends DefinedPacket private String message; - Packet3Chat() + private Packet3Chat() { super( 0x03 ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet9Respawn.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet9Respawn.java index c616ba96..a3bd4be2 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet9Respawn.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Packet9Respawn.java @@ -15,7 +15,7 @@ public class Packet9Respawn extends DefinedPacket private short worldHeight; private String levelType; - Packet9Respawn() + private Packet9Respawn() { super( 0x09 ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketC9PlayerListItem.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketC9PlayerListItem.java index b1f07a75..8a7714e8 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketC9PlayerListItem.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketC9PlayerListItem.java @@ -13,7 +13,7 @@ public class PacketC9PlayerListItem extends DefinedPacket private boolean online; private int ping; - PacketC9PlayerListItem() + private PacketC9PlayerListItem() { super( 0xC9 ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketCCSettings.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketCCSettings.java index 16f1b95a..cc4be70b 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketCCSettings.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketCCSettings.java @@ -15,7 +15,7 @@ public class PacketCCSettings extends DefinedPacket private byte difficulty; private boolean showCape; - PacketCCSettings() + private PacketCCSettings() { super( 0xCC ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketCDClientStatus.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketCDClientStatus.java index 1e750936..06c5b516 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketCDClientStatus.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketCDClientStatus.java @@ -11,7 +11,7 @@ public class PacketCDClientStatus extends DefinedPacket private byte payload; - PacketCDClientStatus() + private PacketCDClientStatus() { super( 0xCD ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketCEScoreboardObjective.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketCEScoreboardObjective.java index 7924c4c7..60136d58 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketCEScoreboardObjective.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketCEScoreboardObjective.java @@ -16,7 +16,7 @@ public class PacketCEScoreboardObjective extends DefinedPacket */ private byte action; - PacketCEScoreboardObjective() + private PacketCEScoreboardObjective() { super( 0xCE ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketCFScoreboardScore.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketCFScoreboardScore.java index 993249e2..801aa35c 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketCFScoreboardScore.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketCFScoreboardScore.java @@ -17,7 +17,7 @@ public class PacketCFScoreboardScore extends DefinedPacket private String scoreName; private int value; - PacketCFScoreboardScore() + private PacketCFScoreboardScore() { super( 0xCF ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketD0DisplayScoreboard.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketD0DisplayScoreboard.java index 8b6d177a..5924387d 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketD0DisplayScoreboard.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketD0DisplayScoreboard.java @@ -15,7 +15,7 @@ public class PacketD0DisplayScoreboard extends DefinedPacket private byte position; private String name; - PacketD0DisplayScoreboard() + private PacketD0DisplayScoreboard() { super( 0xD0 ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketD1Team.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketD1Team.java index 7f7e6600..39d51fae 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketD1Team.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketD1Team.java @@ -21,7 +21,7 @@ public class PacketD1Team extends DefinedPacket private short playerCount; private String[] players; - PacketD1Team() + private PacketD1Team() { super( 0xD1 ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFAPluginMessage.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFAPluginMessage.java index 43bc9292..5a28a77a 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFAPluginMessage.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFAPluginMessage.java @@ -12,7 +12,7 @@ public class PacketFAPluginMessage extends DefinedPacket private String tag; private byte[] data; - PacketFAPluginMessage() + private PacketFAPluginMessage() { super( 0xFA ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFCEncryptionResponse.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFCEncryptionResponse.java index d986b80c..5f18dd3a 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFCEncryptionResponse.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFCEncryptionResponse.java @@ -12,7 +12,7 @@ public class PacketFCEncryptionResponse extends DefinedPacket private byte[] sharedSecret; private byte[] verifyToken; - PacketFCEncryptionResponse() + private PacketFCEncryptionResponse() { super( 0xFC ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFDEncryptionRequest.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFDEncryptionRequest.java index 6ded3981..64b17ae3 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFDEncryptionRequest.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFDEncryptionRequest.java @@ -13,7 +13,7 @@ public class PacketFDEncryptionRequest extends DefinedPacket private byte[] publicKey; private byte[] verifyToken; - PacketFDEncryptionRequest() + private PacketFDEncryptionRequest() { super( 0xFD ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFEPing.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFEPing.java index 6416f406..4240959f 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFEPing.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFEPing.java @@ -11,7 +11,7 @@ public class PacketFEPing extends DefinedPacket private byte version; - PacketFEPing() + private PacketFEPing() { super( 0xFE ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFFKick.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFFKick.java index 23461921..ed79d2ac 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFFKick.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketFFKick.java @@ -11,7 +11,7 @@ public class PacketFFKick extends DefinedPacket private String message; - PacketFFKick() + private PacketFFKick() { super( 0xFF ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/forge/Forge1Login.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/forge/Forge1Login.java new file mode 100644 index 00000000..9555dac4 --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/forge/Forge1Login.java @@ -0,0 +1,42 @@ +package net.md_5.bungee.protocol.packet.forge; + +import net.md_5.bungee.protocol.packet.*; +import io.netty.buffer.ByteBuf; +import lombok.EqualsAndHashCode; +import lombok.ToString; + +@ToString +@EqualsAndHashCode(callSuper = false) +public class Forge1Login extends Packet1Login +{ + + @Override + public void read(ByteBuf buf) + { + entityId = buf.readInt(); + levelType = readString( buf ); + gameMode = buf.readByte(); + dimension = buf.readInt(); + difficulty = buf.readByte(); + unused = buf.readByte(); + maxPlayers = buf.readByte(); + } + + @Override + public void write(ByteBuf buf) + { + buf.writeInt( entityId ); + writeString( levelType, buf ); + buf.writeByte( gameMode ); + buf.writeInt(dimension ); + buf.writeByte( difficulty ); + buf.writeByte( unused ); + buf.writeByte( maxPlayers ); + } + + @Override + public void handle(PacketHandler handler) throws Exception + { + handler.handle( this ); + } +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/netty/BulkChunk.java b/protocol/src/main/java/net/md_5/bungee/protocol/skip/BulkChunk.java similarity index 68% rename from protocol/src/main/java/net/md_5/bungee/protocol/netty/BulkChunk.java rename to protocol/src/main/java/net/md_5/bungee/protocol/skip/BulkChunk.java index 54fee12d..33f03e37 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/netty/BulkChunk.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/skip/BulkChunk.java @@ -1,13 +1,12 @@ -package net.md_5.bungee.protocol.netty; +package net.md_5.bungee.protocol.skip; import io.netty.buffer.ByteBuf; -import java.io.IOException; public class BulkChunk extends Instruction { @Override - void read(ByteBuf in) throws IOException + void read(ByteBuf in) { short count = in.readShort(); int size = in.readInt(); diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/netty/ByteHeader.java b/protocol/src/main/java/net/md_5/bungee/protocol/skip/ByteHeader.java similarity index 75% rename from protocol/src/main/java/net/md_5/bungee/protocol/netty/ByteHeader.java rename to protocol/src/main/java/net/md_5/bungee/protocol/skip/ByteHeader.java index 24fd1ac4..d9f99e02 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/netty/ByteHeader.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/skip/ByteHeader.java @@ -1,7 +1,6 @@ -package net.md_5.bungee.protocol.netty; +package net.md_5.bungee.protocol.skip; import io.netty.buffer.ByteBuf; -import java.io.IOException; class ByteHeader extends Instruction { @@ -14,7 +13,7 @@ class ByteHeader extends Instruction } @Override - void read(ByteBuf in) throws IOException + void read(ByteBuf in) { byte size = in.readByte(); for ( byte b = 0; b < size; b++ ) diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/netty/Instruction.java b/protocol/src/main/java/net/md_5/bungee/protocol/skip/Instruction.java similarity index 91% rename from protocol/src/main/java/net/md_5/bungee/protocol/netty/Instruction.java rename to protocol/src/main/java/net/md_5/bungee/protocol/skip/Instruction.java index d22fdb38..801b48b9 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/netty/Instruction.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/skip/Instruction.java @@ -1,7 +1,6 @@ -package net.md_5.bungee.protocol.netty; +package net.md_5.bungee.protocol.skip; import io.netty.buffer.ByteBuf; -import java.io.IOException; abstract class Instruction { @@ -29,5 +28,5 @@ abstract class Instruction // Custom instructions static final Instruction STRING_ARRAY = new ShortHeader( STRING ); - abstract void read(ByteBuf in) throws IOException; + abstract void read(ByteBuf in); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/netty/IntHeader.java b/protocol/src/main/java/net/md_5/bungee/protocol/skip/IntHeader.java similarity index 74% rename from protocol/src/main/java/net/md_5/bungee/protocol/netty/IntHeader.java rename to protocol/src/main/java/net/md_5/bungee/protocol/skip/IntHeader.java index 2f1a011b..0b87c54e 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/netty/IntHeader.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/skip/IntHeader.java @@ -1,7 +1,6 @@ -package net.md_5.bungee.protocol.netty; +package net.md_5.bungee.protocol.skip; import io.netty.buffer.ByteBuf; -import java.io.IOException; class IntHeader extends Instruction { @@ -14,7 +13,7 @@ class IntHeader extends Instruction } @Override - void read(ByteBuf in) throws IOException + void read(ByteBuf in) { int size = in.readInt(); for ( int i = 0; i < size; i++ ) diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/netty/Item.java b/protocol/src/main/java/net/md_5/bungee/protocol/skip/Item.java similarity index 68% rename from protocol/src/main/java/net/md_5/bungee/protocol/netty/Item.java rename to protocol/src/main/java/net/md_5/bungee/protocol/skip/Item.java index 529be2f3..cb632e33 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/netty/Item.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/skip/Item.java @@ -1,13 +1,12 @@ -package net.md_5.bungee.protocol.netty; +package net.md_5.bungee.protocol.skip; import io.netty.buffer.ByteBuf; -import java.io.IOException; class Item extends Instruction { @Override - void read(ByteBuf in) throws IOException + void read(ByteBuf in) { short type = in.readShort(); if ( type >= 0 ) diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/netty/Jump.java b/protocol/src/main/java/net/md_5/bungee/protocol/skip/Jump.java similarity index 72% rename from protocol/src/main/java/net/md_5/bungee/protocol/netty/Jump.java rename to protocol/src/main/java/net/md_5/bungee/protocol/skip/Jump.java index aca0d763..d7c451ae 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/netty/Jump.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/skip/Jump.java @@ -1,7 +1,6 @@ -package net.md_5.bungee.protocol.netty; +package net.md_5.bungee.protocol.skip; import io.netty.buffer.ByteBuf; -import java.io.IOException; class Jump extends Instruction { @@ -18,7 +17,7 @@ class Jump extends Instruction } @Override - void read(ByteBuf in) throws IOException + void read(ByteBuf in) { in.skipBytes( len ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/netty/MetaData.java b/protocol/src/main/java/net/md_5/bungee/protocol/skip/MetaData.java similarity index 90% rename from protocol/src/main/java/net/md_5/bungee/protocol/netty/MetaData.java rename to protocol/src/main/java/net/md_5/bungee/protocol/skip/MetaData.java index 730f3686..929a664f 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/netty/MetaData.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/skip/MetaData.java @@ -1,13 +1,12 @@ -package net.md_5.bungee.protocol.netty; +package net.md_5.bungee.protocol.skip; import io.netty.buffer.ByteBuf; -import java.io.IOException; class MetaData extends Instruction { @Override - void read(ByteBuf in) throws IOException + void read(ByteBuf in) { int x = in.readUnsignedByte(); while ( x != 127 ) diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/netty/OptionalMotion.java b/protocol/src/main/java/net/md_5/bungee/protocol/skip/OptionalMotion.java similarity index 65% rename from protocol/src/main/java/net/md_5/bungee/protocol/netty/OptionalMotion.java rename to protocol/src/main/java/net/md_5/bungee/protocol/skip/OptionalMotion.java index 4e618360..45e040fb 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/netty/OptionalMotion.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/skip/OptionalMotion.java @@ -1,13 +1,12 @@ -package net.md_5.bungee.protocol.netty; +package net.md_5.bungee.protocol.skip; import io.netty.buffer.ByteBuf; -import java.io.IOException; class OptionalMotion extends Instruction { @Override - void read(ByteBuf in) throws IOException + void read(ByteBuf in) { int data = in.readInt(); if ( data > 0 ) diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/netty/PacketReader.java b/protocol/src/main/java/net/md_5/bungee/protocol/skip/PacketReader.java similarity index 59% rename from protocol/src/main/java/net/md_5/bungee/protocol/netty/PacketReader.java rename to protocol/src/main/java/net/md_5/bungee/protocol/skip/PacketReader.java index 0adfb864..141b718d 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/netty/PacketReader.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/skip/PacketReader.java @@ -1,24 +1,24 @@ -package net.md_5.bungee.protocol.netty; +package net.md_5.bungee.protocol.skip; import io.netty.buffer.ByteBuf; -import java.io.IOException; import java.util.ArrayList; import java.util.List; -import net.md_5.bungee.protocol.PacketDefinitions; -import net.md_5.bungee.protocol.PacketDefinitions.OpCode; +import net.md_5.bungee.protocol.OpCode; +import net.md_5.bungee.protocol.Protocol; public class PacketReader { - private static final Instruction[][] instructions = new Instruction[ PacketDefinitions.opCodes.length ][]; + private final Instruction[][] instructions; - static + public PacketReader(Protocol protocol) { + instructions = new Instruction[ protocol.getOpCodes().length ][]; for ( int i = 0; i < instructions.length; i++ ) { List output = new ArrayList<>(); - OpCode[] enums = PacketDefinitions.opCodes[i]; + OpCode[] enums = protocol.getOpCodes()[i]; if ( enums != null ) { for ( OpCode struct : enums ) @@ -59,35 +59,16 @@ public class PacketReader } } - private static void readPacket(int packetId, ByteBuf in, int protocol) throws IOException + public void tryRead(short packetId, ByteBuf in) { - Instruction[] packetDef = null; - if ( packetId + protocol < instructions.length ) - { - packetDef = instructions[packetId + protocol]; - } + Instruction[] packetDef = instructions[packetId]; - if ( packetDef == null ) + if ( packetDef != null ) { - if ( protocol == PacketDefinitions.VANILLA_PROTOCOL ) + for ( Instruction instruction : packetDef ) { - throw new IOException( "Unknown packet id " + packetId ); - } else - { - readPacket( packetId, in, PacketDefinitions.VANILLA_PROTOCOL ); - return; + instruction.read( in ); } } - - for ( Instruction instruction : packetDef ) - { - instruction.read( in ); - } - } - - public static void readPacket(ByteBuf in, int protocol) throws IOException - { - int packetId = in.readUnsignedByte(); - readPacket( packetId, in, protocol ); } } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/netty/ShortHeader.java b/protocol/src/main/java/net/md_5/bungee/protocol/skip/ShortHeader.java similarity index 75% rename from protocol/src/main/java/net/md_5/bungee/protocol/netty/ShortHeader.java rename to protocol/src/main/java/net/md_5/bungee/protocol/skip/ShortHeader.java index ece0ae3c..6db71894 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/netty/ShortHeader.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/skip/ShortHeader.java @@ -1,7 +1,6 @@ -package net.md_5.bungee.protocol.netty; +package net.md_5.bungee.protocol.skip; import io.netty.buffer.ByteBuf; -import java.io.IOException; class ShortHeader extends Instruction { @@ -14,7 +13,7 @@ class ShortHeader extends Instruction } @Override - void read(ByteBuf in) throws IOException + void read(ByteBuf in) { short size = in.readShort(); for ( short s = 0; s < size; s++ ) diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/netty/UnsignedShortByte.java b/protocol/src/main/java/net/md_5/bungee/protocol/skip/UnsignedShortByte.java similarity index 61% rename from protocol/src/main/java/net/md_5/bungee/protocol/netty/UnsignedShortByte.java rename to protocol/src/main/java/net/md_5/bungee/protocol/skip/UnsignedShortByte.java index 1dea256b..3ea6ebea 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/netty/UnsignedShortByte.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/skip/UnsignedShortByte.java @@ -1,13 +1,12 @@ -package net.md_5.bungee.protocol.netty; +package net.md_5.bungee.protocol.skip; import io.netty.buffer.ByteBuf; -import java.io.IOException; class UnsignedShortByte extends Instruction { @Override - void read(ByteBuf in) throws IOException + void read(ByteBuf in) { int size = in.readUnsignedShort(); in.skipBytes( size ); diff --git a/protocol/src/test/java/net/md_5/bungee/protocol/PacketTest.java b/protocol/src/test/java/net/md_5/bungee/protocol/PacketTest.java index 5470206c..f34d9c4b 100644 --- a/protocol/src/test/java/net/md_5/bungee/protocol/PacketTest.java +++ b/protocol/src/test/java/net/md_5/bungee/protocol/PacketTest.java @@ -30,7 +30,7 @@ public class PacketTest Assert.assertTrue( "Packet " + clazz + " does not report correct id", packet.getId() == i ); Assert.assertTrue( "Packet " + clazz + " does not have custom hash code", packet.hashCode() != System.identityHashCode( packet ) ); Assert.assertTrue( "Packet " + clazz + " does not have custom toString", packet.toString().indexOf( '@' ) == -1 ); - Assert.assertTrue( "Packet " + clazz + " does not have package private no args constructor", clazz.getDeclaredConstructor().getModifiers() == 0 ); + Assert.assertTrue( "Packet " + clazz + " does not have private no args constructor", Modifier.isPrivate( clazz.getDeclaredConstructor().getModifiers() ) ); for ( Field field : clazz.getDeclaredFields() ) { diff --git a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java index 0f23f1b5..354abd32 100644 --- a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java +++ b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java @@ -54,7 +54,7 @@ import net.md_5.bungee.netty.PipelineUtils; import net.md_5.bungee.protocol.packet.DefinedPacket; import net.md_5.bungee.protocol.packet.Packet3Chat; import net.md_5.bungee.protocol.packet.PacketFAPluginMessage; -import net.md_5.bungee.protocol.PacketDefinitions; +import net.md_5.bungee.protocol.Vanilla; import net.md_5.bungee.scheduler.BungeeThreadPool; import net.md_5.bungee.util.CaseInsensitiveMap; @@ -444,13 +444,13 @@ public class BungeeCord extends ProxyServer @Override public byte getProtocolVersion() { - return PacketDefinitions.PROTOCOL_VERSION; + return Vanilla.PROTOCOL_VERSION; } @Override public String getGameVersion() { - return PacketDefinitions.GAME_VERSION; + return Vanilla.GAME_VERSION; } @Override 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 ef6a80c3..f14938b8 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java @@ -37,7 +37,7 @@ import net.md_5.bungee.protocol.packet.PacketFCEncryptionResponse; import net.md_5.bungee.protocol.packet.PacketFDEncryptionRequest; import net.md_5.bungee.protocol.packet.PacketFFKick; import net.md_5.bungee.protocol.packet.PacketHandler; -import net.md_5.bungee.protocol.PacketDefinitions; +import net.md_5.bungee.protocol.Vanilla; @RequiredArgsConstructor public class ServerConnector extends PacketHandler @@ -144,7 +144,7 @@ public class ServerConnector extends PacketHandler login.difficulty, login.unused, (byte) user.getPendingConnection().getListener().getTabListSize(), - ch.getHandle().pipeline().get( PacketDecoder.class ).getProtocol() == PacketDefinitions.FORGE_PROTOCOL ); + ch.getHandle().pipeline().get( PacketDecoder.class ).getProtocol() == Vanilla.FORGE_PROTOCOL ); user.sendPacket( modLogin ); } else { @@ -277,7 +277,7 @@ public class ServerConnector extends PacketHandler if ( in.readByte() != 0 ) { // TODO: Using forge flag - ch.getHandle().pipeline().get( PacketDecoder.class ).setProtocol( PacketDefinitions.FORGE_PROTOCOL ); + ch.getHandle().pipeline().get( PacketDecoder.class ).setProtocol( Vanilla.FORGE_PROTOCOL ); } } diff --git a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java index acb409cb..333f8c1c 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java @@ -43,7 +43,7 @@ import net.md_5.bungee.protocol.packet.PacketFDEncryptionRequest; import net.md_5.bungee.protocol.packet.PacketFEPing; import net.md_5.bungee.protocol.packet.PacketFFKick; import net.md_5.bungee.protocol.packet.PacketHandler; -import net.md_5.bungee.protocol.PacketDefinitions; +import net.md_5.bungee.protocol.Vanilla; @RequiredArgsConstructor public class InitialHandler extends PacketHandler implements PendingConnection @@ -115,7 +115,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection Preconditions.checkState( forgeLogin == null, "Already received FORGE LOGIN" ); forgeLogin = login; - ch.getHandle().pipeline().get( PacketDecoder.class ).setProtocol( PacketDefinitions.FORGE_PROTOCOL ); + ch.getHandle().pipeline().get( PacketDecoder.class ).setProtocol( Vanilla.FORGE_PROTOCOL ); } @Override diff --git a/proxy/src/main/java/net/md_5/bungee/netty/PacketDecoder.java b/proxy/src/main/java/net/md_5/bungee/netty/PacketDecoder.java index 564b88c0..614c3704 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/PacketDecoder.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/PacketDecoder.java @@ -7,7 +7,7 @@ import io.netty.handler.codec.ReplayingDecoder; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; -import net.md_5.bungee.protocol.netty.PacketReader; +import net.md_5.bungee.protocol.skip.PacketReader; /** * This class will attempt to read a packet from {@link PacketReader}, with the diff --git a/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java b/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java index 26167488..a52a93e5 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java @@ -15,7 +15,7 @@ import net.md_5.bungee.UserConnection; import net.md_5.bungee.connection.InitialHandler; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.config.ListenerInfo; -import net.md_5.bungee.protocol.PacketDefinitions; +import net.md_5.bungee.protocol.Vanilla; public class PipelineUtils { @@ -61,7 +61,7 @@ public class PipelineUtils ch.pipeline().addLast( "outbound", new OutboundHandler() ); ch.pipeline().addLast( "timer", new ReadTimeoutHandler( BungeeCord.getInstance().config.getTimeout(), TimeUnit.MILLISECONDS ) ); - ch.pipeline().addLast( "decoder", new PacketDecoder( PacketDefinitions.VANILLA_PROTOCOL ) ); + ch.pipeline().addLast( "decoder", new PacketDecoder( Vanilla.VANILLA_PROTOCOL ) ); ch.pipeline().addLast( "packet-encoder", packetEncoder ); ch.pipeline().addLast( "array-encoder", arrayEncoder ); ch.pipeline().addLast( "handler", new HandlerBoss() );