Compiles yet again
This commit is contained in:
@@ -5,31 +5,38 @@ import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToMessageCodec;
|
||||
import java.util.List;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class MinecraftCodec extends MessageToMessageCodec<ByteBuf, DefinedPacket>
|
||||
{
|
||||
|
||||
@Setter
|
||||
private Protocol protocol;
|
||||
private boolean server;
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, DefinedPacket msg, List<Object> out) throws Exception
|
||||
{
|
||||
Protocol.ProtocolDirection prot = ( server ) ? protocol.TO_CLIENT : protocol.TO_SERVER;
|
||||
|
||||
ByteBuf buf = ctx.alloc().buffer();
|
||||
DefinedPacket.writeVarInt( protocol.getId( msg.getClass() ), buf );
|
||||
DefinedPacket.writeVarInt( prot.getId( msg.getClass() ), buf );
|
||||
msg.write( buf );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception
|
||||
{
|
||||
Protocol.ProtocolDirection prot = ( server ) ? protocol.TO_SERVER : protocol.TO_CLIENT;
|
||||
|
||||
int packetId = DefinedPacket.readVarInt( msg );
|
||||
|
||||
ByteBuf copy = msg.copy();
|
||||
DefinedPacket packet = null;
|
||||
if ( protocol.hasPacket( packetId ) )
|
||||
if ( prot.hasPacket( packetId ) )
|
||||
{
|
||||
packet = protocol.createPacket( packetId );
|
||||
packet = prot.createPacket( packetId );
|
||||
packet.read( msg );
|
||||
}
|
||||
|
||||
|
@@ -26,74 +26,56 @@ public enum Protocol
|
||||
{
|
||||
|
||||
// Undef
|
||||
SERVER_HANDSHAKE
|
||||
HANDSHAKE
|
||||
{
|
||||
|
||||
{
|
||||
registerPacket( 0x00, Handshake.class );
|
||||
TO_SERVER.registerPacket( 0x00, Handshake.class );
|
||||
}
|
||||
},
|
||||
// 0
|
||||
CLIENT_GAME
|
||||
GAME
|
||||
{
|
||||
|
||||
{
|
||||
registerPacket( 0x00, KeepAlive.class );
|
||||
registerPacket( 0x01, Login.class );
|
||||
registerPacket( 0x02, Chat.class );
|
||||
registerPacket( 0x07, Respawn.class );
|
||||
registerPacket( 0x3B, PlayerListItem.class );
|
||||
registerPacket( 0x3D, TabComplete.class );
|
||||
registerPacket( 0x3E, ScoreboardObjective.class );
|
||||
registerPacket( 0x3F, ScoreboardScore.class );
|
||||
registerPacket( 0x40, ScoreboardDisplay.class );
|
||||
registerPacket( 0x41, Team.class );
|
||||
registerPacket( 0x42, PluginMessage.class );
|
||||
registerPacket( 0x43, Kick.class );
|
||||
}
|
||||
},
|
||||
// 0
|
||||
SERVER_GAME
|
||||
{
|
||||
|
||||
{
|
||||
registerPacket( 0x00, KeepAlive.class );
|
||||
registerPacket( 0x14, TabComplete.class );
|
||||
registerPacket( 0x15, ClientSettings.class );
|
||||
registerPacket( 0x17, PluginMessage.class );
|
||||
TO_CLIENT.registerPacket( 0x00, KeepAlive.class );
|
||||
TO_CLIENT.registerPacket( 0x01, Login.class );
|
||||
TO_CLIENT.registerPacket( 0x02, Chat.class );
|
||||
TO_CLIENT.registerPacket( 0x07, Respawn.class );
|
||||
TO_CLIENT.registerPacket( 0x3B, PlayerListItem.class );
|
||||
TO_CLIENT.registerPacket( 0x3D, TabComplete.class );
|
||||
TO_CLIENT.registerPacket( 0x3E, ScoreboardObjective.class );
|
||||
TO_CLIENT.registerPacket( 0x3F, ScoreboardScore.class );
|
||||
TO_CLIENT.registerPacket( 0x40, ScoreboardDisplay.class );
|
||||
TO_CLIENT.registerPacket( 0x41, Team.class );
|
||||
TO_CLIENT.registerPacket( 0x42, PluginMessage.class );
|
||||
TO_CLIENT.registerPacket( 0x43, Kick.class );
|
||||
|
||||
|
||||
TO_SERVER.registerPacket( 0x00, KeepAlive.class );
|
||||
TO_SERVER.registerPacket( 0x14, TabComplete.class );
|
||||
TO_SERVER.registerPacket( 0x15, ClientSettings.class );
|
||||
TO_SERVER.registerPacket( 0x17, PluginMessage.class );
|
||||
}
|
||||
},
|
||||
// 1
|
||||
CLIENT_STATUS
|
||||
STATUS
|
||||
{
|
||||
|
||||
{
|
||||
}
|
||||
},
|
||||
// 1
|
||||
SERVER_STATUS
|
||||
//2
|
||||
LOGIN
|
||||
{
|
||||
|
||||
{
|
||||
}
|
||||
},
|
||||
// 2
|
||||
CLIENT_LOGIN
|
||||
{
|
||||
|
||||
{
|
||||
registerPacket( 0x00, Kick.class );
|
||||
registerPacket( 0x01, EncryptionRequest.class );
|
||||
registerPacket( 0x02, LoginSuccess.class );
|
||||
}
|
||||
},
|
||||
// 2
|
||||
SERVER_LOGIN
|
||||
{
|
||||
|
||||
{
|
||||
registerPacket( 0x00, LoginRequest.class );
|
||||
registerPacket( 0x01, EncryptionResponse.class );
|
||||
TO_CLIENT.registerPacket( 0x00, Kick.class );
|
||||
TO_CLIENT.registerPacket( 0x01, EncryptionRequest.class );
|
||||
TO_CLIENT.registerPacket( 0x02, LoginSuccess.class );
|
||||
|
||||
TO_SERVER.registerPacket( 0x00, LoginRequest.class );
|
||||
TO_SERVER.registerPacket( 0x01, EncryptionResponse.class );
|
||||
}
|
||||
};
|
||||
/*========================================================================*/
|
||||
@@ -101,57 +83,64 @@ public enum Protocol
|
||||
public static final int PROTOCOL_VERSION = 0x00;
|
||||
public static final String MINECRAFT_VERSION = "13w41a";
|
||||
/*========================================================================*/
|
||||
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 Constructor<? extends DefinedPacket>[] packetConstructors = new Constructor[ MAX_PACKET_ID ];
|
||||
public final ProtocolDirection TO_SERVER = new ProtocolDirection();
|
||||
public final ProtocolDirection TO_CLIENT = new ProtocolDirection();
|
||||
|
||||
public boolean hasPacket(int id)
|
||||
public class ProtocolDirection
|
||||
{
|
||||
return id < MAX_PACKET_ID && packetConstructors[id] != null;
|
||||
}
|
||||
|
||||
public final DefinedPacket createPacket(int id)
|
||||
{
|
||||
if ( id > 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 Constructor<? extends DefinedPacket>[] packetConstructors = new Constructor[ MAX_PACKET_ID ];
|
||||
|
||||
public boolean hasPacket(int id)
|
||||
{
|
||||
throw new BadPacketException( "Packet with id " + id + " outside of range " );
|
||||
}
|
||||
if ( packetConstructors[id] == null )
|
||||
{
|
||||
throw new BadPacketException( "No packet with id " + id );
|
||||
return id < MAX_PACKET_ID && packetConstructors[id] != null;
|
||||
}
|
||||
|
||||
try
|
||||
public final DefinedPacket createPacket(int id)
|
||||
{
|
||||
return packetClasses[id].newInstance();
|
||||
} catch ( ReflectiveOperationException ex )
|
||||
{
|
||||
throw new BadPacketException( "Could not construct packet with id " + id, ex );
|
||||
if ( id > MAX_PACKET_ID )
|
||||
{
|
||||
throw new BadPacketException( "Packet with id " + id + " outside of range " );
|
||||
}
|
||||
if ( packetConstructors[id] == null )
|
||||
{
|
||||
throw new BadPacketException( "No packet with id " + id );
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return packetClasses[id].newInstance();
|
||||
} catch ( ReflectiveOperationException ex )
|
||||
{
|
||||
throw new BadPacketException( "Could not construct packet with id " + id, ex );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected final void registerPacket(int id, Class<? extends DefinedPacket> packetClass)
|
||||
{
|
||||
try
|
||||
protected final void registerPacket(int id, Class<? extends DefinedPacket> packetClass)
|
||||
{
|
||||
packetConstructors[id] = packetClass.getDeclaredConstructor();
|
||||
} catch ( NoSuchMethodException ex )
|
||||
{
|
||||
throw new BadPacketException( "No NoArgsConstructor for packet class " + packetClass );
|
||||
try
|
||||
{
|
||||
packetConstructors[id] = packetClass.getDeclaredConstructor();
|
||||
} catch ( NoSuchMethodException ex )
|
||||
{
|
||||
throw new BadPacketException( "No NoArgsConstructor for packet class " + packetClass );
|
||||
}
|
||||
packetClasses[id] = packetClass;
|
||||
packetMap.put( packetClass, id );
|
||||
}
|
||||
packetClasses[id] = packetClass;
|
||||
packetMap.put( packetClass, id );
|
||||
}
|
||||
|
||||
protected final void unregisterPacket(int id)
|
||||
{
|
||||
packetMap.remove( packetClasses[id] );
|
||||
packetClasses[id] = null;
|
||||
packetConstructors[id] = null;
|
||||
}
|
||||
protected final void unregisterPacket(int id)
|
||||
{
|
||||
packetMap.remove( packetClasses[id] );
|
||||
packetClasses[id] = null;
|
||||
packetConstructors[id] = null;
|
||||
}
|
||||
|
||||
final int getId(Class<? extends DefinedPacket> packet)
|
||||
{
|
||||
return packetMap.get( packet );
|
||||
final int getId(Class<? extends DefinedPacket> packet)
|
||||
{
|
||||
return packetMap.get( packet );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,47 @@
|
||||
package net.md_5.bungee.protocol;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
import io.netty.handler.codec.CorruptedFrameException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Varint21FrameDecoder extends ByteToMessageDecoder
|
||||
{
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception
|
||||
{
|
||||
in.markReaderIndex();
|
||||
|
||||
final byte[] buf = new byte[ 3 ];
|
||||
for ( int i = 0; i < buf.length; i++ )
|
||||
{
|
||||
if ( !in.isReadable() )
|
||||
{
|
||||
in.resetReaderIndex();
|
||||
return;
|
||||
}
|
||||
|
||||
buf[i] = in.readByte();
|
||||
if ( buf[i] >= 0 )
|
||||
{
|
||||
int length = DefinedPacket.readVarInt( Unpooled.wrappedBuffer( buf ) );
|
||||
|
||||
if ( in.readableBytes() < length )
|
||||
{
|
||||
in.resetReaderIndex();
|
||||
return;
|
||||
} else
|
||||
{
|
||||
out.add( in.readBytes( length ) );
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new CorruptedFrameException( "length wider than 21-bit" );
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
package net.md_5.bungee.protocol;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
|
||||
@ChannelHandler.Sharable
|
||||
public class Varint21LengthFieldPrepender extends MessageToByteEncoder<ByteBuf>
|
||||
{
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception
|
||||
{
|
||||
int bodyLen = msg.readableBytes();
|
||||
int headerLen = varintSize( bodyLen );
|
||||
out.ensureWritable( headerLen + bodyLen );
|
||||
|
||||
DefinedPacket.writeVarInt( bodyLen, out );
|
||||
out.writeBytes( msg, msg.readerIndex(), bodyLen );
|
||||
}
|
||||
|
||||
private static int varintSize(int paramInt)
|
||||
{
|
||||
if ( ( paramInt & 0xFFFFFF80 ) == 0 )
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if ( ( paramInt & 0xFFFFC000 ) == 0 )
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
if ( ( paramInt & 0xFFE00000 ) == 0 )
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
if ( ( paramInt & 0xF0000000 ) == 0 )
|
||||
{
|
||||
return 4;
|
||||
}
|
||||
return 5;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user