From ad4c143ce40156f862a107e61a8638ba7e202d5e Mon Sep 17 00:00:00 2001 From: md_5 Date: Thu, 30 May 2013 17:34:56 +1000 Subject: [PATCH] Finish and create passing unit tests for the integrity of all packet classes. --- .../bungee/protocol/packet/DefinedPacket.java | 16 +++- .../protocol/packet/Packet0KeepAlive.java | 9 +- .../bungee/protocol/packet/Packet1Login.java | 9 +- .../protocol/packet/Packet2Handshake.java | 41 ++++----- .../bungee/protocol/packet/Packet3Chat.java | 20 +++-- .../protocol/packet/Packet9Respawn.java | 50 +++++------ .../packet/PacketC9PlayerListItem.java | 37 ++++---- .../protocol/packet/PacketCCSettings.java | 41 ++++++--- .../protocol/packet/PacketCDClientStatus.java | 27 +++--- .../packet/PacketCEScoreboardObjective.java | 34 +++---- .../packet/PacketCFScoreboardScore.java | 43 ++++----- .../packet/PacketD0DisplayScoreboard.java | 26 ++++-- .../bungee/protocol/packet/PacketD1Team.java | 88 +++++++++++-------- .../packet/PacketFAPluginMessage.java | 27 +++--- .../packet/PacketFCEncryptionResponse.java | 28 +++--- .../packet/PacketFDEncryptionRequest.java | 33 +++---- .../bungee/protocol/packet/PacketFEPing.java | 21 +++-- .../bungee/protocol/packet/PacketFFKick.java | 19 ++-- .../bungee/protocol/packet/PacketHandler.java | 27 ------ .../net/md_5/bungee/protocol/PacketTest.java | 42 +++++++++ .../bungee/netty/DefinedPacketEncoder.java | 3 +- .../net/md_5/bungee/netty/PacketHandler.java | 13 +++ 22 files changed, 380 insertions(+), 274 deletions(-) create mode 100644 protocol/src/test/java/net/md_5/bungee/protocol/PacketTest.java create mode 100644 proxy/src/main/java/net/md_5/bungee/netty/PacketHandler.java 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 362fa0c8..aa831082 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 @@ -3,14 +3,19 @@ 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") - private static Class[] classes = new Class[ 256 ]; + 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) { @@ -31,9 +36,9 @@ public abstract class DefinedPacket if ( constructor != null ) { - ret = constructor.newInstance( buf ); + ret = constructor.newInstance(); } - } catch ( IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException ex ) + } catch ( NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex ) { } } @@ -41,6 +46,11 @@ public abstract class DefinedPacket return ret; } + public final int getId() + { + return id; + } + public void writeString(String s, ByteBuf buf) { // TODO: Check len - use Guava? 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 7ff29cca..535c0af3 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 @@ -1,20 +1,21 @@ package net.md_5.bungee.protocol.packet; import io.netty.buffer.ByteBuf; -import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; -import lombok.NoArgsConstructor; import lombok.ToString; @ToString -@NoArgsConstructor -@AllArgsConstructor @EqualsAndHashCode(callSuper = false) public class Packet0KeepAlive extends DefinedPacket { private int id; + Packet0KeepAlive() + { + super( 0x00 ); + } + @Override public void read(ByteBuf buf) { 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 cd69c3b0..7ed5d0bc 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 @@ -1,14 +1,10 @@ package net.md_5.bungee.protocol.packet; import io.netty.buffer.ByteBuf; -import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; -import lombok.NoArgsConstructor; import lombok.ToString; @ToString -@NoArgsConstructor -@AllArgsConstructor @EqualsAndHashCode(callSuper = false) public class Packet1Login extends DefinedPacket { @@ -21,6 +17,11 @@ public class Packet1Login extends DefinedPacket private byte unused; private byte maxPlayers; + Packet1Login() + { + super( 0x01 ); + } + @Override public void read(ByteBuf buf) { 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 d7cfb968..d482a2b2 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 @@ -1,39 +1,40 @@ package net.md_5.bungee.protocol.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; -import net.md_5.bungee.packet.PacketHandler; @ToString @EqualsAndHashCode(callSuper = false) public class Packet2Handshake extends DefinedPacket { - public byte procolVersion; - public String username; - public String host; - public int port; + private byte procolVersion; + private String username; + private String host; + private int port; - public Packet2Handshake(byte protocolVersion, String username, String host, int port) + Packet2Handshake() { super( 0x02 ); - writeByte( protocolVersion ); - writeString( username ); - writeString( host ); - writeInt( port ); - this.procolVersion = protocolVersion; - this.username = username; - this.host = host; - this.port = port; } - Packet2Handshake(byte[] buf) + @Override + public void read(ByteBuf buf) { - super( 0x02, buf ); - this.procolVersion = readByte(); - this.username = readUTF(); - this.host = readUTF(); - this.port = readInt(); + procolVersion = buf.readByte(); + username = readString( buf ); + host = readString( buf ); + port = buf.readInt(); + } + + @Override + public void write(ByteBuf buf) + { + buf.writeByte( procolVersion ); + writeString( username, buf ); + writeString( host, buf ); + buf.writeInt( port ); } @Override 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 66e3e30e..8cd46e3a 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 @@ -1,27 +1,31 @@ package net.md_5.bungee.protocol.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; -import net.md_5.bungee.packet.PacketHandler; @ToString @EqualsAndHashCode(callSuper = false) public class Packet3Chat extends DefinedPacket { - public String message; + private String message; - public Packet3Chat(String message) + Packet3Chat() { super( 0x03 ); - writeString( message ); - this.message = message; } - Packet3Chat(byte[] buf) + @Override + public void read(ByteBuf buf) { - super( 0x03, buf ); - this.message = readUTF(); + message = readString( buf ); + } + + @Override + public void write(ByteBuf buf) + { + writeString( message, buf ); } @Override 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 c7577695..c616ba96 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 @@ -1,45 +1,43 @@ package net.md_5.bungee.protocol.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; -import net.md_5.bungee.packet.PacketHandler; @ToString @EqualsAndHashCode(callSuper = false) public class Packet9Respawn extends DefinedPacket { - public static final Packet9Respawn DIM1_SWITCH = new Packet9Respawn( (byte) 1, (byte) 0, (byte) 0, (short) 256, "DEFAULT" ); - public static final Packet9Respawn DIM2_SWITCH = new Packet9Respawn( (byte) -1, (byte) 0, (byte) 0, (short) 256, "DEFAULT" ); - public int dimension; - public byte difficulty; - public byte gameMode; - public short worldHeight; - public String levelType; + private int dimension; + private byte difficulty; + private byte gameMode; + private short worldHeight; + private String levelType; - public Packet9Respawn(int dimension, byte difficulty, byte gameMode, short worldHeight, String levelType) + Packet9Respawn() { super( 0x09 ); - writeInt( dimension ); - writeByte( difficulty ); - writeByte( gameMode ); - writeShort( worldHeight ); - writeString( levelType ); - this.dimension = dimension; - this.difficulty = difficulty; - this.gameMode = gameMode; - this.worldHeight = worldHeight; - this.levelType = levelType; } - Packet9Respawn(byte[] buf) + @Override + public void read(ByteBuf buf) { - super( 0x09, buf ); - this.dimension = readInt(); - this.difficulty = readByte(); - this.gameMode = readByte(); - this.worldHeight = readShort(); - this.levelType = readUTF(); + dimension = buf.readInt(); + difficulty = buf.readByte(); + gameMode = buf.readByte(); + worldHeight = buf.readShort(); + levelType = readString( buf ); + } + + @Override + public void write(ByteBuf buf) + { + buf.writeInt( dimension ); + buf.writeByte( difficulty ); + buf.writeByte( gameMode ); + buf.writeShort( worldHeight ); + writeString( levelType, buf ); } @Override 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 a96feec0..b1f07a75 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 @@ -1,32 +1,37 @@ package net.md_5.bungee.protocol.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; -import net.md_5.bungee.packet.PacketHandler; @ToString @EqualsAndHashCode(callSuper = false) public class PacketC9PlayerListItem extends DefinedPacket { - public String username; - public boolean online; - public int ping; + private String username; + private boolean online; + private int ping; - PacketC9PlayerListItem(byte[] buf) - { - super( 0xC9, buf ); - username = readUTF(); - online = readBoolean(); - ping = readShort(); - } - - public PacketC9PlayerListItem(String username, boolean online, int ping) + PacketC9PlayerListItem() { super( 0xC9 ); - writeString( username ); - writeBoolean( online ); - writeShort( ping ); + } + + @Override + public void read(ByteBuf buf) + { + username = readString( buf ); + online = buf.readBoolean(); + ping = buf.readInt(); + } + + @Override + public void write(ByteBuf buf) + { + writeString( username, buf ); + buf.writeBoolean( online ); + buf.writeInt( ping ); } @Override 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 a924454b..16f1b95a 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 @@ -1,28 +1,43 @@ package net.md_5.bungee.protocol.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; -import net.md_5.bungee.packet.PacketHandler; @ToString @EqualsAndHashCode(callSuper = false) public class PacketCCSettings extends DefinedPacket { - public String locale; - public byte viewDistance; - public byte chatFlags; - public byte difficulty; - public boolean showCape; + private String locale; + private byte viewDistance; + private byte chatFlags; + private byte difficulty; + private boolean showCape; - public PacketCCSettings(byte[] buf) + PacketCCSettings() { - super( 0xCC, buf ); - locale = readUTF(); - viewDistance = readByte(); - chatFlags = readByte(); - difficulty = readByte(); - showCape = readBoolean(); + super( 0xCC ); + } + + @Override + public void read(ByteBuf buf) + { + locale = readString( buf ); + viewDistance = buf.readByte(); + chatFlags = buf.readByte(); + difficulty = buf.readByte(); + showCape = buf.readBoolean(); + } + + @Override + public void write(ByteBuf buf) + { + locale = readString( buf ); + buf.writeByte( viewDistance ); + buf.writeByte( chatFlags ); + buf.writeByte( difficulty ); + buf.writeBoolean( showCape ); } @Override 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 f5c02312..1e750936 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 @@ -1,34 +1,31 @@ package net.md_5.bungee.protocol.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; -import net.md_5.bungee.packet.PacketHandler; @ToString @EqualsAndHashCode(callSuper = false) public class PacketCDClientStatus extends DefinedPacket { - /** - * Represents the packet the client sends to the server when it is ready to - * login. - */ - public static PacketCDClientStatus CLIENT_LOGIN = new PacketCDClientStatus( (byte) 0 ); + private byte payload; - /** - * Sent from the client to the server upon respawn, - * - * @param payload 0 if initial spawn, 1 if respawn after death. - */ - public PacketCDClientStatus(byte payload) + PacketCDClientStatus() { super( 0xCD ); - writeByte( payload ); } - PacketCDClientStatus(byte[] buf) + @Override + public void read(ByteBuf buf) { - super( 0xCD, buf ); + payload = buf.readByte(); + } + + @Override + public void write(ByteBuf buf) + { + buf.writeByte( payload ); } @Override 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 b5d71a39..7924c4c7 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 @@ -1,38 +1,40 @@ package net.md_5.bungee.protocol.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; -import net.md_5.bungee.packet.PacketHandler; @ToString @EqualsAndHashCode(callSuper = false) public class PacketCEScoreboardObjective extends DefinedPacket { - public String name; - public String text; + private String name; + private String text; /** * 0 to create, 1 to remove. */ - public byte action; + private byte action; - public PacketCEScoreboardObjective(String name, String text, byte status) + PacketCEScoreboardObjective() { super( 0xCE ); - writeString( name ); - writeString( text ); - writeByte( status ); - this.name = name; - this.text = text; - this.action = status; } - PacketCEScoreboardObjective(byte[] buf) + @Override + public void read(ByteBuf buf) { - super( 0xCE, buf ); - this.name = readUTF(); - this.text = readUTF(); - this.action = readByte(); + name = readString( buf ); + text = readString( buf ); + action = buf.readByte(); + } + + @Override + public void write(ByteBuf buf) + { + writeString( name, buf ); + writeString( text, buf ); + buf.writeByte( action ); } @Override 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 60f1a4b4..993249e2 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 @@ -1,48 +1,49 @@ package net.md_5.bungee.protocol.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; -import net.md_5.bungee.packet.PacketHandler; @ToString @EqualsAndHashCode(callSuper = false) public class PacketCFScoreboardScore extends DefinedPacket { - public String itemName; + private String itemName; /** * 0 = create / update, 1 = remove. */ - public byte action; - public String scoreName; - public int value; + private byte action; + private String scoreName; + private int value; - public PacketCFScoreboardScore(byte[] buf) + PacketCFScoreboardScore() { - super( 0xCF, buf ); - itemName = readUTF(); - action = readByte(); + super( 0xCF ); + } + + @Override + public void read(ByteBuf buf) + { + itemName = readString( buf ); + action = buf.readByte(); if ( action == 0 ) { - scoreName = readUTF(); - value = readInt(); + scoreName = readString( buf ); + value = buf.readInt(); } } - public PacketCFScoreboardScore(String itemName, byte action, String scoreName, int value) + @Override + public void write(ByteBuf buf) { - super( 0xCF ); - writeString( itemName ); - writeByte( action ); + writeString( itemName, buf ); + buf.writeByte( action ); if ( action == 0 ) { - writeString( scoreName ); - writeInt( value ); + writeString( scoreName, buf ); + buf.writeInt( value ); } - this.itemName = itemName; - this.action = action; - this.scoreName = scoreName; - this.value = value; } @Override 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 3aa0caaf..8b6d177a 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 @@ -1,8 +1,8 @@ package net.md_5.bungee.protocol.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; -import net.md_5.bungee.packet.PacketHandler; @ToString @EqualsAndHashCode(callSuper = false) @@ -12,14 +12,26 @@ public class PacketD0DisplayScoreboard extends DefinedPacket /** * 0 = list, 1 = side, 2 = below. */ - public byte position; - public String name; + private byte position; + private String name; - public PacketD0DisplayScoreboard(byte[] buf) + PacketD0DisplayScoreboard() { - super( 0xD0, buf ); - position = readByte(); - name = readUTF(); + super( 0xD0 ); + } + + @Override + public void read(ByteBuf buf) + { + position = buf.readByte(); + name = readString( buf ); + } + + @Override + public void write(ByteBuf buf) + { + buf.writeByte( position ); + writeString( name, buf ); } @Override 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 e6a25022..7f7e6600 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 @@ -1,59 +1,73 @@ package net.md_5.bungee.protocol.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; -import net.md_5.bungee.packet.PacketHandler; @ToString @EqualsAndHashCode(callSuper = false) public class PacketD1Team extends DefinedPacket { - public String name; + private String name; /** * 0 - create, 1 remove, 2 info update, 3 player add, 4 player remove. */ - public byte mode; - public String displayName; - public String prefix; - public String suffix; - public byte friendlyFire; - public short playerCount; - public String[] players; + private byte mode; + private String displayName; + private String prefix; + private String suffix; + private boolean friendlyFire; + private short playerCount; + private String[] players; - public PacketD1Team(byte[] buf) - { - super( 0xD1, buf ); - name = readUTF(); - mode = readByte(); - if ( mode == 0 || mode == 2 ) - { - displayName = readUTF(); - prefix = readUTF(); - suffix = readUTF(); - friendlyFire = readByte(); - } - if ( mode == 0 || mode == 3 || mode == 4 ) - { - players = new String[ readShort() ]; - for ( int i = 0; i < players.length; i++ ) - { - players[i] = readUTF(); - } - } - } - - public PacketD1Team() + PacketD1Team() { super( 0xD1 ); } - public static PacketD1Team destroy(String name) + @Override + public void read(ByteBuf buf) { - PacketD1Team packet = new PacketD1Team(); - packet.writeString( name ); - packet.writeByte( 1 ); - return packet; + name = readString( buf ); + mode = buf.readByte(); + if ( mode == 0 || mode == 2 ) + { + displayName = readString( buf ); + prefix = readString( buf ); + suffix = readString( buf ); + friendlyFire = buf.readBoolean(); + } + if ( mode == 0 || mode == 3 || mode == 4 ) + { + players = new String[ buf.readShort() ]; + for ( int i = 0; i < players.length; i++ ) + { + players[i] = readString( buf ); + } + } + } + + @Override + public void write(ByteBuf buf) + { + writeString( name, buf ); + buf.writeByte( mode ); + if ( mode == 0 || mode == 2 ) + { + writeString( displayName, buf ); + writeString( prefix, buf ); + writeString( suffix, buf ); + buf.writeBoolean( friendlyFire ); + } + if ( mode == 0 || mode == 3 || mode == 4 ) + { + buf.writeShort( players.length ); + for ( int i = 0; i < players.length; i++ ) + { + writeString( players[i], buf ); + } + } } @Override 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 cd79608b..43bc9292 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 @@ -1,31 +1,34 @@ package net.md_5.bungee.protocol.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; -import net.md_5.bungee.packet.PacketHandler; @ToString @EqualsAndHashCode(callSuper = false) public class PacketFAPluginMessage extends DefinedPacket { - public String tag; - public byte[] data; + private String tag; + private byte[] data; - public PacketFAPluginMessage(String tag, byte[] data) + PacketFAPluginMessage() { super( 0xFA ); - writeString( tag ); - writeArray( data ); - this.tag = tag; - this.data = data; } - PacketFAPluginMessage(byte[] buf) + @Override + public void read(ByteBuf buf) { - super( 0xFA, buf ); - this.tag = readUTF(); - this.data = readArray(); + tag = readString( buf ); + data = readArray( buf ); + } + + @Override + public void write(ByteBuf buf) + { + writeString( tag, buf ); + writeArray( data, buf ); } @Override 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 5a3316a8..d986b80c 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 @@ -1,38 +1,34 @@ package net.md_5.bungee.protocol.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; -import net.md_5.bungee.packet.PacketHandler; @ToString @EqualsAndHashCode(callSuper = false) public class PacketFCEncryptionResponse extends DefinedPacket { - public byte[] sharedSecret; - public byte[] verifyToken; + private byte[] sharedSecret; + private byte[] verifyToken; - public PacketFCEncryptionResponse() + PacketFCEncryptionResponse() { super( 0xFC ); - writeArray( new byte[ 0 ] ); - writeArray( new byte[ 0 ] ); } - public PacketFCEncryptionResponse(byte[] sharedSecret, byte[] verifyToken) + @Override + public void read(ByteBuf buf) { - super( 0xFC ); - writeArray( sharedSecret ); - writeArray( verifyToken ); - this.sharedSecret = sharedSecret; - this.verifyToken = verifyToken; + sharedSecret = readArray( buf ); + verifyToken = readArray( buf ); } - PacketFCEncryptionResponse(byte[] buf) + @Override + public void write(ByteBuf buf) { - super( 0xFC, buf ); - this.sharedSecret = readArray(); - this.verifyToken = readArray(); + writeArray( sharedSecret, buf ); + writeArray( verifyToken, buf ); } @Override 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 49655f02..6ded3981 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 @@ -3,34 +3,35 @@ package net.md_5.bungee.protocol.packet; import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; -import net.md_5.bungee.packet.PacketHandler; @ToString @EqualsAndHashCode(callSuper = false) public class PacketFDEncryptionRequest extends DefinedPacket { - public String serverId; - public byte[] publicKey; - public byte[] verifyToken; + private String serverId; + private byte[] publicKey; + private byte[] verifyToken; - public PacketFDEncryptionRequest(String serverId, byte[] publicKey, byte[] verifyToken) + PacketFDEncryptionRequest() { super( 0xFD ); - writeString( serverId ); - writeArray( publicKey ); - writeArray( verifyToken ); - this.serverId = serverId; - this.publicKey = publicKey; - this.verifyToken = verifyToken; } - PacketFDEncryptionRequest(byte[] buf) + @Override + public void read(ByteBuf buf) { - super( 0xFD, buf ); - serverId = readUTF(); - publicKey = readArray(); - verifyToken = readArray(); + serverId = readString( buf ); + publicKey = readArray( buf ); + verifyToken = readArray( buf ); + } + + @Override + public void write(ByteBuf buf) + { + writeString( serverId, buf ); + writeArray( publicKey, buf ); + writeArray( verifyToken, buf ); } @Override 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 8fcaeb5e..6416f406 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 @@ -1,20 +1,31 @@ package net.md_5.bungee.protocol.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; -import net.md_5.bungee.packet.PacketHandler; @ToString @EqualsAndHashCode(callSuper = false) public class PacketFEPing extends DefinedPacket { - public byte version; + private byte version; - PacketFEPing(byte[] buffer) + PacketFEPing() { - super( 0xFE, buffer ); - version = readByte(); + super( 0xFE ); + } + + @Override + public void read(ByteBuf buf) + { + version = buf.readByte(); + } + + @Override + public void write(ByteBuf buf) + { + buf.writeByte( version ); } @Override 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 0ed8a361..23461921 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 @@ -1,26 +1,31 @@ package net.md_5.bungee.protocol.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; -import net.md_5.bungee.packet.PacketHandler; @ToString @EqualsAndHashCode(callSuper = false) public class PacketFFKick extends DefinedPacket { - public String message; + private String message; - public PacketFFKick(String message) + PacketFFKick() { super( 0xFF ); - writeString( message ); } - PacketFFKick(byte[] buf) + @Override + public void read(ByteBuf buf) { - super( 0xFF, buf ); - this.message = readUTF(); + message = readString( buf ); + } + + @Override + public void write(ByteBuf buf) + { + writeString( message, buf ); } @Override diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketHandler.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketHandler.java index 5666f0c4..b635d9ff 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketHandler.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PacketHandler.java @@ -1,38 +1,11 @@ package net.md_5.bungee.protocol.packet; -import net.md_5.bungee.protocol.packet.Packet1Login; -import net.md_5.bungee.protocol.packet.Packet3Chat; -import net.md_5.bungee.protocol.packet.PacketD0DisplayScoreboard; -import net.md_5.bungee.protocol.packet.PacketFEPing; -import net.md_5.bungee.protocol.packet.PacketFFKick; -import net.md_5.bungee.protocol.packet.PacketC9PlayerListItem; -import net.md_5.bungee.protocol.packet.PacketFAPluginMessage; -import net.md_5.bungee.protocol.packet.Packet2Handshake; -import net.md_5.bungee.protocol.packet.PacketFCEncryptionResponse; -import net.md_5.bungee.protocol.packet.PacketFDEncryptionRequest; -import net.md_5.bungee.protocol.packet.PacketCDClientStatus; -import net.md_5.bungee.protocol.packet.PacketCCSettings; -import net.md_5.bungee.protocol.packet.PacketCFScoreboardScore; -import net.md_5.bungee.protocol.packet.PacketCEScoreboardObjective; -import net.md_5.bungee.protocol.packet.Packet9Respawn; -import net.md_5.bungee.protocol.packet.PacketD1Team; -import net.md_5.bungee.protocol.packet.Packet0KeepAlive; -import net.md_5.bungee.netty.ChannelWrapper; - public abstract class PacketHandler { @Override public abstract String toString(); - public void connected(ChannelWrapper channel) throws Exception - { - } - - public void disconnected(ChannelWrapper channel) throws Exception - { - } - public void exception(Throwable t) throws Exception { } 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 new file mode 100644 index 00000000..5470206c --- /dev/null +++ b/protocol/src/test/java/net/md_5/bungee/protocol/PacketTest.java @@ -0,0 +1,42 @@ +package net.md_5.bungee.protocol; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import net.md_5.bungee.protocol.packet.DefinedPacket; +import org.junit.Assert; +import org.junit.Test; + +public class PacketTest +{ + + @Test + public void testPackets() throws NoSuchMethodException + { + for ( short i = 0; i < 256; i++ ) + { + ByteBuf buf = Unpooled.wrappedBuffer( new byte[] + { + (byte) i + } ); + Class clazz = DefinedPacket.classes[i]; + if ( clazz != null ) + { + Assert.assertTrue( "Packet " + clazz + " is not public", Modifier.isPublic( clazz.getModifiers() ) ); + DefinedPacket packet = DefinedPacket.packet( buf ); + Assert.assertTrue( "Could not create packet with id " + i + " and class " + clazz, packet != null ); + Assert.assertTrue( "Packet with id " + i + " does not have correct class (expected " + clazz + " but got " + packet.getClass(), packet.getClass() == clazz ); + 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 ); + + for ( Field field : clazz.getDeclaredFields() ) + { + Assert.assertTrue( "Packet " + clazz + " has non private field " + field, Modifier.isPrivate( field.getModifiers() ) ); + } + } + } + } +} diff --git a/proxy/src/main/java/net/md_5/bungee/netty/DefinedPacketEncoder.java b/proxy/src/main/java/net/md_5/bungee/netty/DefinedPacketEncoder.java index ff426543..cfa34503 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/DefinedPacketEncoder.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/DefinedPacketEncoder.java @@ -13,6 +13,7 @@ public class DefinedPacketEncoder extends MessageToByteEncoder @Override protected void encode(ChannelHandlerContext ctx, DefinedPacket msg, ByteBuf out) throws Exception { - out.writeBytes( msg.getPacket() ); + out.writeByte( msg.getId() ); + msg.write( out ); } } diff --git a/proxy/src/main/java/net/md_5/bungee/netty/PacketHandler.java b/proxy/src/main/java/net/md_5/bungee/netty/PacketHandler.java new file mode 100644 index 00000000..4dca13f8 --- /dev/null +++ b/proxy/src/main/java/net/md_5/bungee/netty/PacketHandler.java @@ -0,0 +1,13 @@ +package net.md_5.bungee.netty; + +public abstract class PacketHandler extends net.md_5.bungee.protocol.packet.PacketHandler +{ + + public void connected(ChannelWrapper channel) throws Exception + { + } + + public void disconnected(ChannelWrapper channel) throws Exception + { + } +}