From 1a1a51b38dc3de5553a1d3fcb2db201ae60172e7 Mon Sep 17 00:00:00 2001 From: md_5 Date: Wed, 16 Apr 2014 10:28:07 +1000 Subject: [PATCH] Replace direction strings with concrete enums --- .../net/md_5/bungee/protocol/DefinedPacket.java | 4 ++-- .../md_5/bungee/protocol/MinecraftDecoder.java | 4 ++-- .../md_5/bungee/protocol/MinecraftEncoder.java | 4 ++-- .../java/net/md_5/bungee/protocol/Protocol.java | 16 ++++++---------- .../md_5/bungee/protocol/ProtocolConstants.java | 6 ++++++ .../net/md_5/bungee/protocol/packet/Chat.java | 9 +++++---- .../bungee/protocol/packet/ClientSettings.java | 10 +++++----- .../bungee/protocol/packet/ScoreboardScore.java | 10 +++++----- .../net/md_5/bungee/protocol/packet/Team.java | 10 +++++----- 9 files changed, 38 insertions(+), 35 deletions(-) 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 a42a7011..bfce4011 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 @@ -119,7 +119,7 @@ public abstract class DefinedPacket throw new UnsupportedOperationException( "Packet must implement read method" ); } - public void read(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) + public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { read( buf ); } @@ -129,7 +129,7 @@ public abstract class DefinedPacket throw new UnsupportedOperationException( "Packet must implement write method" ); } - public void write(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) + public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { write( buf ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java index 8d8319a6..89cc6df4 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftDecoder.java @@ -20,7 +20,7 @@ public class MinecraftDecoder extends ByteToMessageDecoder @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { - Protocol.ProtocolDirection prot = ( server ) ? protocol.TO_SERVER : protocol.TO_CLIENT; + Protocol.DirectionData prot = ( server ) ? protocol.TO_SERVER : protocol.TO_CLIENT; ByteBuf copy = in.copy(); // TODO int packetId = DefinedPacket.readVarInt( in ); @@ -29,7 +29,7 @@ public class MinecraftDecoder extends ByteToMessageDecoder if ( prot.hasPacket( packetId ) ) { packet = prot.createPacket( packetId ); - packet.read( in, prot, protocolVersion ); + packet.read( in, prot.getDirection(), protocolVersion ); if ( in.readableBytes() != 0 ) { throw new BadPacketException( "Did not read all bytes from packet " + packet.getClass() + " " + packetId + " Protocol " + protocol + " Direction " + prot ); diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftEncoder.java b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftEncoder.java index 0adccdc0..242a9da1 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftEncoder.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/MinecraftEncoder.java @@ -19,8 +19,8 @@ public class MinecraftEncoder extends MessageToByteEncoder @Override protected void encode(ChannelHandlerContext ctx, DefinedPacket msg, ByteBuf out) throws Exception { - Protocol.ProtocolDirection prot = ( server ) ? protocol.TO_CLIENT : protocol.TO_SERVER; + Protocol.DirectionData prot = ( server ) ? protocol.TO_CLIENT : protocol.TO_SERVER; DefinedPacket.writeVarInt( prot.getId( msg.getClass() ), out ); - msg.write( out, prot, protocolVersion ); + msg.write( out, prot.getDirection(), protocolVersion ); } } 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 314af5fc..d6bd2746 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 @@ -6,6 +6,7 @@ import gnu.trove.map.hash.TObjectIntHashMap; import java.lang.reflect.Constructor; import java.util.Arrays; import java.util.List; +import lombok.Getter; import lombok.RequiredArgsConstructor; import net.md_5.bungee.protocol.packet.Chat; import net.md_5.bungee.protocol.packet.ClientSettings; @@ -95,14 +96,15 @@ public enum Protocol public static final int MAX_PACKET_ID = 0xFF; public static List supportedVersions = Arrays.asList( ProtocolConstants.MINECRAFT_1_7_2, ProtocolConstants.MINECRAFT_1_7_6 ); /*========================================================================*/ - public final ProtocolDirection TO_SERVER = new ProtocolDirection( "TO_SERVER" ); - public final ProtocolDirection TO_CLIENT = new ProtocolDirection( "TO_CLIENT" ); + public final DirectionData TO_SERVER = new DirectionData( ProtocolConstants.Direction.TO_SERVER ); + public final DirectionData TO_CLIENT = new DirectionData( ProtocolConstants.Direction.TO_CLIENT ); @RequiredArgsConstructor - public class ProtocolDirection + public class DirectionData { - private final String name; + @Getter + private final ProtocolConstants.Direction direction; private final TObjectIntMap> packetMap = new TObjectIntHashMap<>( MAX_PACKET_ID ); private final Class[] packetClasses = new Class[ MAX_PACKET_ID ]; private final Constructor[] packetConstructors = new Constructor[ MAX_PACKET_ID ]; @@ -112,12 +114,6 @@ public enum Protocol return id < MAX_PACKET_ID && packetConstructors[id] != null; } - @Override - public String toString() - { - return name; - } - public final DefinedPacket createPacket(int id) { if ( id > MAX_PACKET_ID ) 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 c3907aad..6e3f7843 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 @@ -6,4 +6,10 @@ public class ProtocolConstants public static int MINECRAFT_1_7_2 = 4; public static int MINECRAFT_1_7_6 = 5; public static int MINECRAFT_14_11_a = 14; + + public enum Direction + { + + TO_CLIENT, TO_SERVER; + } } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Chat.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Chat.java index ec6ad4bd..910b2057 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Chat.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Chat.java @@ -8,6 +8,7 @@ import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.Protocol; +import net.md_5.bungee.protocol.ProtocolConstants; @Data @NoArgsConstructor @@ -25,20 +26,20 @@ public class Chat extends DefinedPacket } @Override - public void read(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) + public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { message = readString( buf ); - if ( direction.toString().equals( "TO_CLIENT" ) && protocolVersion >= 7 ) + if ( direction == ProtocolConstants.Direction.TO_CLIENT && protocolVersion >= ProtocolConstants.MINECRAFT_14_11_a ) { position = buf.readByte(); } } @Override - public void write(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) + public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { writeString( message, buf ); - if ( direction.toString().equals( "TO_CLIENT" ) && protocolVersion >= 7 ) + if ( direction == ProtocolConstants.Direction.TO_CLIENT && protocolVersion >= ProtocolConstants.MINECRAFT_14_11_a ) { buf.writeByte( position ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/ClientSettings.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/ClientSettings.java index 050229da..af9c6b67 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/ClientSettings.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/ClientSettings.java @@ -7,7 +7,7 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import net.md_5.bungee.protocol.AbstractPacketHandler; -import net.md_5.bungee.protocol.Protocol; +import net.md_5.bungee.protocol.ProtocolConstants; @Data @NoArgsConstructor @@ -24,13 +24,13 @@ public class ClientSettings extends DefinedPacket private byte showCape; @Override - public void read(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) + public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { locale = readString( buf ); viewDistance = buf.readByte(); chatFlags = buf.readByte(); unknown = buf.readBoolean(); - if ( protocolVersion < 6 ) + if ( protocolVersion <= ProtocolConstants.MINECRAFT_1_7_6 ) { difficulty = buf.readByte(); } @@ -38,13 +38,13 @@ public class ClientSettings extends DefinedPacket } @Override - public void write(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) + public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { writeString( locale, buf ); buf.writeByte( viewDistance ); buf.writeByte( chatFlags ); buf.writeBoolean( unknown ); - if ( protocolVersion < 6 ) + if ( protocolVersion <= ProtocolConstants.MINECRAFT_1_7_6 ) { buf.writeByte( difficulty ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/ScoreboardScore.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/ScoreboardScore.java index 3d6ea97c..219a6306 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/ScoreboardScore.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/ScoreboardScore.java @@ -7,7 +7,7 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import net.md_5.bungee.protocol.AbstractPacketHandler; -import net.md_5.bungee.protocol.Protocol; +import net.md_5.bungee.protocol.ProtocolConstants; @Data @NoArgsConstructor @@ -25,14 +25,14 @@ public class ScoreboardScore extends DefinedPacket private int value; @Override - public void read(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) + public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { itemName = readString( buf ); action = buf.readByte(); if ( action != 1 ) { scoreName = readString( buf ); - if ( protocolVersion >= 7 ) + if ( protocolVersion >= ProtocolConstants.MINECRAFT_14_11_a ) { value = readVarInt( buf ); } else @@ -43,14 +43,14 @@ public class ScoreboardScore extends DefinedPacket } @Override - public void write(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) + public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { writeString( itemName, buf ); buf.writeByte( action ); if ( action != 1 ) { writeString( scoreName, buf ); - if ( protocolVersion >= 7 ) + if ( protocolVersion >= ProtocolConstants.MINECRAFT_14_11_a ) { writeVarInt( value, buf ); } else diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Team.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Team.java index 63af9329..ec1a0d3e 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Team.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Team.java @@ -7,7 +7,7 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import net.md_5.bungee.protocol.AbstractPacketHandler; -import net.md_5.bungee.protocol.Protocol; +import net.md_5.bungee.protocol.ProtocolConstants; @Data @NoArgsConstructor @@ -40,7 +40,7 @@ public class Team extends DefinedPacket } @Override - public void read(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) + public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { name = readString( buf ); mode = buf.readByte(); @@ -53,7 +53,7 @@ public class Team extends DefinedPacket } if ( mode == 0 || mode == 3 || mode == 4 ) { - int len = ( protocolVersion >= 7 ) ? readVarInt( buf ) : buf.readShort(); + int len = ( protocolVersion >= ProtocolConstants.MINECRAFT_14_11_a ) ? readVarInt( buf ) : buf.readShort(); players = new String[ len ]; for ( int i = 0; i < len; i++ ) { @@ -63,7 +63,7 @@ public class Team extends DefinedPacket } @Override - public void write(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) + public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { writeString( name, buf ); buf.writeByte( mode ); @@ -76,7 +76,7 @@ public class Team extends DefinedPacket } if ( mode == 0 || mode == 3 || mode == 4 ) { - if ( protocolVersion >= 7 ) + if ( protocolVersion >= ProtocolConstants.MINECRAFT_14_11_a ) { writeVarInt( players.length, buf ); } else