It compiles
This commit is contained in:
@@ -5,6 +5,7 @@ import net.md_5.bungee.protocol.packet.ClientSettings;
|
||||
import net.md_5.bungee.protocol.packet.ClientStatus;
|
||||
import net.md_5.bungee.protocol.packet.Login;
|
||||
import net.md_5.bungee.protocol.packet.Chat;
|
||||
import net.md_5.bungee.protocol.packet.EncryptionRequest;
|
||||
import net.md_5.bungee.protocol.packet.PlayerListItem;
|
||||
import net.md_5.bungee.protocol.packet.TabComplete;
|
||||
import net.md_5.bungee.protocol.packet.ScoreboardObjective;
|
||||
@@ -70,6 +71,10 @@ public abstract class AbstractPacketHandler
|
||||
{
|
||||
}
|
||||
|
||||
public void handle(EncryptionRequest encryptionRequest) throws Exception
|
||||
{
|
||||
}
|
||||
|
||||
public void handle(ScoreboardDisplay displayScoreboard) throws Exception
|
||||
{
|
||||
}
|
||||
|
@@ -8,7 +8,7 @@ import lombok.RequiredArgsConstructor;
|
||||
public abstract class DefinedPacket
|
||||
{
|
||||
|
||||
public void writeString(String s, ByteBuf buf)
|
||||
public static void writeString(String s, ByteBuf buf)
|
||||
{
|
||||
// TODO: Check len - use Guava?
|
||||
byte[] b = s.getBytes( Charsets.UTF_8 );
|
||||
@@ -16,7 +16,7 @@ public abstract class DefinedPacket
|
||||
buf.writeBytes( b );
|
||||
}
|
||||
|
||||
public String readString(ByteBuf buf)
|
||||
public static String readString(ByteBuf buf)
|
||||
{
|
||||
int len = readVarInt( buf );
|
||||
byte[] b = new byte[ len ];
|
||||
@@ -25,14 +25,14 @@ public abstract class DefinedPacket
|
||||
return new String( b, Charsets.UTF_8 );
|
||||
}
|
||||
|
||||
public void writeArray(byte[] b, ByteBuf buf)
|
||||
public static void writeArray(byte[] b, ByteBuf buf)
|
||||
{
|
||||
// TODO: Check len - use Guava?
|
||||
buf.writeShort( b.length );
|
||||
buf.writeBytes( b );
|
||||
}
|
||||
|
||||
public byte[] readArray(ByteBuf buf)
|
||||
public static byte[] readArray(ByteBuf buf)
|
||||
{
|
||||
// TODO: Check len - use Guava?
|
||||
short len = buf.readShort();
|
||||
@@ -41,7 +41,7 @@ public abstract class DefinedPacket
|
||||
return ret;
|
||||
}
|
||||
|
||||
public int readVarInt(ByteBuf input)
|
||||
public static int readVarInt(ByteBuf input)
|
||||
{
|
||||
int out = 0;
|
||||
int bytes = 0;
|
||||
@@ -66,7 +66,7 @@ public abstract class DefinedPacket
|
||||
return out;
|
||||
}
|
||||
|
||||
public void writeVarInt(int value, ByteBuf output)
|
||||
public static void writeVarInt(int value, ByteBuf output)
|
||||
{
|
||||
int part;
|
||||
while ( true )
|
||||
|
@@ -0,0 +1,38 @@
|
||||
package net.md_5.bungee.protocol;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToMessageCodec;
|
||||
import java.util.List;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class MinecraftCodec extends MessageToMessageCodec<ByteBuf, DefinedPacket>
|
||||
{
|
||||
|
||||
private Protocol protocol;
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, DefinedPacket msg, List<Object> out) throws Exception
|
||||
{
|
||||
ByteBuf buf = ctx.alloc().buffer();
|
||||
DefinedPacket.writeVarInt( protocol.getId( msg.getClass() ), buf );
|
||||
msg.write( buf );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception
|
||||
{
|
||||
int packetId = DefinedPacket.readVarInt( msg );
|
||||
|
||||
ByteBuf copy = msg.copy();
|
||||
DefinedPacket packet = null;
|
||||
if ( protocol.hasPacket( packetId ) )
|
||||
{
|
||||
packet = protocol.createPacket( packetId );
|
||||
packet.read( msg );
|
||||
}
|
||||
|
||||
out.add( new PacketWrapper( packet, copy ) );
|
||||
}
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
package net.md_5.bungee.protocol;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import net.md_5.bungee.protocol.DefinedPacket;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class PacketWrapper
|
||||
{
|
||||
|
||||
public final DefinedPacket packet;
|
||||
public final ByteBuf buf;
|
||||
@Setter
|
||||
private boolean released;
|
||||
|
||||
public void trySingleRelease()
|
||||
{
|
||||
if ( !released )
|
||||
{
|
||||
buf.release();
|
||||
released = true;
|
||||
}
|
||||
}
|
||||
}
|
@@ -5,6 +5,7 @@ import gnu.trove.map.hash.TObjectIntHashMap;
|
||||
import java.lang.reflect.Constructor;
|
||||
import net.md_5.bungee.protocol.packet.Chat;
|
||||
import net.md_5.bungee.protocol.packet.ClientSettings;
|
||||
import net.md_5.bungee.protocol.packet.EncryptionRequest;
|
||||
import net.md_5.bungee.protocol.packet.EncryptionResponse;
|
||||
import net.md_5.bungee.protocol.packet.Handshake;
|
||||
import net.md_5.bungee.protocol.packet.KeepAlive;
|
||||
@@ -82,7 +83,7 @@ public enum Protocol
|
||||
|
||||
{
|
||||
registerPacket( 0x00, Kick.class );
|
||||
registerPacket( 0x01, EncryptionResponse.class );
|
||||
registerPacket( 0x01, EncryptionRequest.class );
|
||||
registerPacket( 0x02, LoginSuccess.class );
|
||||
}
|
||||
},
|
||||
@@ -92,15 +93,23 @@ public enum Protocol
|
||||
|
||||
{
|
||||
registerPacket( 0x00, LoginRequest.class );
|
||||
registerPacket( 0x01, EncryptionResponse.class );
|
||||
}
|
||||
};
|
||||
/*========================================================================*/
|
||||
public static final int MAX_PACKET_ID = 0xFF;
|
||||
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 boolean hasPacket(int id)
|
||||
{
|
||||
return id < MAX_PACKET_ID && packetConstructors[id] != null;
|
||||
}
|
||||
|
||||
public final DefinedPacket createPacket(int id)
|
||||
{
|
||||
if ( id > MAX_PACKET_ID )
|
||||
|
@@ -0,0 +1,43 @@
|
||||
package net.md_5.bungee.protocol.packet;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import net.md_5.bungee.protocol.AbstractPacketHandler;
|
||||
import net.md_5.bungee.protocol.DefinedPacket;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public class EncryptionRequest extends DefinedPacket
|
||||
{
|
||||
|
||||
private String serverId;
|
||||
private byte[] publicKey;
|
||||
private byte[] verifyToken;
|
||||
|
||||
@Override
|
||||
public void read(ByteBuf buf)
|
||||
{
|
||||
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
|
||||
public void handle(AbstractPacketHandler handler) throws Exception
|
||||
{
|
||||
handler.handle( this );
|
||||
}
|
||||
}
|
@@ -16,16 +16,16 @@ public class Handshake extends DefinedPacket
|
||||
{
|
||||
|
||||
private int protocolVersion;
|
||||
private String serverAddress;
|
||||
private int serverPort;
|
||||
private String host;
|
||||
private int port;
|
||||
private int requestedProtocol;
|
||||
|
||||
@Override
|
||||
public void read(ByteBuf buf)
|
||||
{
|
||||
protocolVersion = readVarInt( buf );
|
||||
serverAddress = readString( buf );
|
||||
serverPort = readVarInt( buf );
|
||||
host = readString( buf );
|
||||
port = readVarInt( buf );
|
||||
requestedProtocol = readVarInt( buf );
|
||||
}
|
||||
|
||||
@@ -33,8 +33,8 @@ public class Handshake extends DefinedPacket
|
||||
public void write(ByteBuf buf)
|
||||
{
|
||||
writeVarInt( protocolVersion, buf );
|
||||
writeString( serverAddress, buf );
|
||||
writeVarInt( serverPort, buf );
|
||||
writeString( host, buf );
|
||||
writeVarInt( port, buf );
|
||||
writeVarInt( requestedProtocol, buf );
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user