Replace direction strings with concrete enums

This commit is contained in:
md_5 2014-04-16 10:28:07 +10:00
parent bc2b4db419
commit 1a1a51b38d
9 changed files with 38 additions and 35 deletions

View File

@ -119,7 +119,7 @@ public abstract class DefinedPacket
throw new UnsupportedOperationException( "Packet must implement read method" ); 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 ); read( buf );
} }
@ -129,7 +129,7 @@ public abstract class DefinedPacket
throw new UnsupportedOperationException( "Packet must implement write method" ); 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 ); write( buf );
} }

View File

@ -20,7 +20,7 @@ public class MinecraftDecoder extends ByteToMessageDecoder
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> 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 ByteBuf copy = in.copy(); // TODO
int packetId = DefinedPacket.readVarInt( in ); int packetId = DefinedPacket.readVarInt( in );
@ -29,7 +29,7 @@ public class MinecraftDecoder extends ByteToMessageDecoder
if ( prot.hasPacket( packetId ) ) if ( prot.hasPacket( packetId ) )
{ {
packet = prot.createPacket( packetId ); packet = prot.createPacket( packetId );
packet.read( in, prot, protocolVersion ); packet.read( in, prot.getDirection(), protocolVersion );
if ( in.readableBytes() != 0 ) if ( in.readableBytes() != 0 )
{ {
throw new BadPacketException( "Did not read all bytes from packet " + packet.getClass() + " " + packetId + " Protocol " + protocol + " Direction " + prot ); throw new BadPacketException( "Did not read all bytes from packet " + packet.getClass() + " " + packetId + " Protocol " + protocol + " Direction " + prot );

View File

@ -19,8 +19,8 @@ public class MinecraftEncoder extends MessageToByteEncoder<DefinedPacket>
@Override @Override
protected void encode(ChannelHandlerContext ctx, DefinedPacket msg, ByteBuf out) throws Exception 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 ); DefinedPacket.writeVarInt( prot.getId( msg.getClass() ), out );
msg.write( out, prot, protocolVersion ); msg.write( out, prot.getDirection(), protocolVersion );
} }
} }

View File

@ -6,6 +6,7 @@ import gnu.trove.map.hash.TObjectIntHashMap;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.md_5.bungee.protocol.packet.Chat; import net.md_5.bungee.protocol.packet.Chat;
import net.md_5.bungee.protocol.packet.ClientSettings; 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 final int MAX_PACKET_ID = 0xFF;
public static List<Integer> supportedVersions = Arrays.asList( ProtocolConstants.MINECRAFT_1_7_2, ProtocolConstants.MINECRAFT_1_7_6 ); public static List<Integer> supportedVersions = Arrays.asList( ProtocolConstants.MINECRAFT_1_7_2, ProtocolConstants.MINECRAFT_1_7_6 );
/*========================================================================*/ /*========================================================================*/
public final ProtocolDirection TO_SERVER = new ProtocolDirection( "TO_SERVER" ); public final DirectionData TO_SERVER = new DirectionData( ProtocolConstants.Direction.TO_SERVER );
public final ProtocolDirection TO_CLIENT = new ProtocolDirection( "TO_CLIENT" ); public final DirectionData TO_CLIENT = new DirectionData( ProtocolConstants.Direction.TO_CLIENT );
@RequiredArgsConstructor @RequiredArgsConstructor
public class ProtocolDirection public class DirectionData
{ {
private final String name; @Getter
private final ProtocolConstants.Direction direction;
private final TObjectIntMap<Class<? extends DefinedPacket>> packetMap = new TObjectIntHashMap<>( MAX_PACKET_ID ); private final TObjectIntMap<Class<? extends DefinedPacket>> packetMap = new TObjectIntHashMap<>( MAX_PACKET_ID );
private final Class<? extends DefinedPacket>[] packetClasses = new Class[ MAX_PACKET_ID ]; private final Class<? extends DefinedPacket>[] packetClasses = new Class[ MAX_PACKET_ID ];
private final Constructor<? extends DefinedPacket>[] packetConstructors = new Constructor[ MAX_PACKET_ID ]; private final Constructor<? extends DefinedPacket>[] packetConstructors = new Constructor[ MAX_PACKET_ID ];
@ -112,12 +114,6 @@ public enum Protocol
return id < MAX_PACKET_ID && packetConstructors[id] != null; return id < MAX_PACKET_ID && packetConstructors[id] != null;
} }
@Override
public String toString()
{
return name;
}
public final DefinedPacket createPacket(int id) public final DefinedPacket createPacket(int id)
{ {
if ( id > MAX_PACKET_ID ) if ( id > MAX_PACKET_ID )

View File

@ -6,4 +6,10 @@ public class ProtocolConstants
public static int MINECRAFT_1_7_2 = 4; public static int MINECRAFT_1_7_2 = 4;
public static int MINECRAFT_1_7_6 = 5; public static int MINECRAFT_1_7_6 = 5;
public static int MINECRAFT_14_11_a = 14; public static int MINECRAFT_14_11_a = 14;
public enum Direction
{
TO_CLIENT, TO_SERVER;
}
} }

View File

@ -8,6 +8,7 @@ import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.AbstractPacketHandler;
import net.md_5.bungee.protocol.Protocol; import net.md_5.bungee.protocol.Protocol;
import net.md_5.bungee.protocol.ProtocolConstants;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@ -25,20 +26,20 @@ public class Chat extends DefinedPacket
} }
@Override @Override
public void read(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion)
{ {
message = readString( buf ); 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(); position = buf.readByte();
} }
} }
@Override @Override
public void write(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion)
{ {
writeString( message, buf ); 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 ); buf.writeByte( position );
} }

View File

@ -7,7 +7,7 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.AbstractPacketHandler;
import net.md_5.bungee.protocol.Protocol; import net.md_5.bungee.protocol.ProtocolConstants;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@ -24,13 +24,13 @@ public class ClientSettings extends DefinedPacket
private byte showCape; private byte showCape;
@Override @Override
public void read(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion)
{ {
locale = readString( buf ); locale = readString( buf );
viewDistance = buf.readByte(); viewDistance = buf.readByte();
chatFlags = buf.readByte(); chatFlags = buf.readByte();
unknown = buf.readBoolean(); unknown = buf.readBoolean();
if ( protocolVersion < 6 ) if ( protocolVersion <= ProtocolConstants.MINECRAFT_1_7_6 )
{ {
difficulty = buf.readByte(); difficulty = buf.readByte();
} }
@ -38,13 +38,13 @@ public class ClientSettings extends DefinedPacket
} }
@Override @Override
public void write(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion)
{ {
writeString( locale, buf ); writeString( locale, buf );
buf.writeByte( viewDistance ); buf.writeByte( viewDistance );
buf.writeByte( chatFlags ); buf.writeByte( chatFlags );
buf.writeBoolean( unknown ); buf.writeBoolean( unknown );
if ( protocolVersion < 6 ) if ( protocolVersion <= ProtocolConstants.MINECRAFT_1_7_6 )
{ {
buf.writeByte( difficulty ); buf.writeByte( difficulty );
} }

View File

@ -7,7 +7,7 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.AbstractPacketHandler;
import net.md_5.bungee.protocol.Protocol; import net.md_5.bungee.protocol.ProtocolConstants;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@ -25,14 +25,14 @@ public class ScoreboardScore extends DefinedPacket
private int value; private int value;
@Override @Override
public void read(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion)
{ {
itemName = readString( buf ); itemName = readString( buf );
action = buf.readByte(); action = buf.readByte();
if ( action != 1 ) if ( action != 1 )
{ {
scoreName = readString( buf ); scoreName = readString( buf );
if ( protocolVersion >= 7 ) if ( protocolVersion >= ProtocolConstants.MINECRAFT_14_11_a )
{ {
value = readVarInt( buf ); value = readVarInt( buf );
} else } else
@ -43,14 +43,14 @@ public class ScoreboardScore extends DefinedPacket
} }
@Override @Override
public void write(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion)
{ {
writeString( itemName, buf ); writeString( itemName, buf );
buf.writeByte( action ); buf.writeByte( action );
if ( action != 1 ) if ( action != 1 )
{ {
writeString( scoreName, buf ); writeString( scoreName, buf );
if ( protocolVersion >= 7 ) if ( protocolVersion >= ProtocolConstants.MINECRAFT_14_11_a )
{ {
writeVarInt( value, buf ); writeVarInt( value, buf );
} else } else

View File

@ -7,7 +7,7 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.AbstractPacketHandler;
import net.md_5.bungee.protocol.Protocol; import net.md_5.bungee.protocol.ProtocolConstants;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@ -40,7 +40,7 @@ public class Team extends DefinedPacket
} }
@Override @Override
public void read(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion)
{ {
name = readString( buf ); name = readString( buf );
mode = buf.readByte(); mode = buf.readByte();
@ -53,7 +53,7 @@ public class Team extends DefinedPacket
} }
if ( mode == 0 || mode == 3 || mode == 4 ) 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 ]; players = new String[ len ];
for ( int i = 0; i < len; i++ ) for ( int i = 0; i < len; i++ )
{ {
@ -63,7 +63,7 @@ public class Team extends DefinedPacket
} }
@Override @Override
public void write(ByteBuf buf, Protocol.ProtocolDirection direction, int protocolVersion) public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion)
{ {
writeString( name, buf ); writeString( name, buf );
buf.writeByte( mode ); buf.writeByte( mode );
@ -76,7 +76,7 @@ public class Team extends DefinedPacket
} }
if ( mode == 0 || mode == 3 || mode == 4 ) if ( mode == 0 || mode == 3 || mode == 4 )
{ {
if ( protocolVersion >= 7 ) if ( protocolVersion >= ProtocolConstants.MINECRAFT_14_11_a )
{ {
writeVarInt( players.length, buf ); writeVarInt( players.length, buf );
} else } else