Implement Support for MinecraftForge / FML 1.7.10
Additional implementation help provided by @jk-5 and @bloodmc.
This commit is contained in:
@@ -31,18 +31,30 @@ public abstract class DefinedPacket
|
||||
return new String( b, Charsets.UTF_8 );
|
||||
}
|
||||
|
||||
public static void writeArrayLegacy(byte[] b, ByteBuf buf)
|
||||
public static void writeArrayLegacy(byte[] b, ByteBuf buf, boolean allowExtended)
|
||||
{
|
||||
Preconditions.checkArgument( b.length <= Short.MAX_VALUE, "Cannot send array longer than Short.MAX_VALUE (got %s bytes)", b.length );
|
||||
|
||||
buf.writeShort( b.length );
|
||||
// (Integer.MAX_VALUE & 0x1FFF9A ) = 2097050 - Forge's current upper limit
|
||||
if ( allowExtended )
|
||||
{
|
||||
Preconditions.checkArgument( b.length <= ( Integer.MAX_VALUE & 0x1FFF9A ), "Cannot send array longer than 2097050 (got %s bytes)", b.length );
|
||||
} else
|
||||
{
|
||||
Preconditions.checkArgument( b.length <= Short.MAX_VALUE, "Cannot send array longer than Short.MAX_VALUE (got %s bytes)", b.length );
|
||||
}
|
||||
// Write a 2 or 3 byte number that represents the length of the packet. (3 byte "shorts" for Forge only)
|
||||
// No vanilla packet should give a 3 byte packet, this method will still retain vanilla behaviour.
|
||||
writeVarShort( buf, b.length );
|
||||
buf.writeBytes( b );
|
||||
}
|
||||
|
||||
public static byte[] readArrayLegacy(ByteBuf buf)
|
||||
{
|
||||
short len = buf.readShort();
|
||||
Preconditions.checkArgument( len <= Short.MAX_VALUE, "Cannot receive array longer than Short.MAX_VALUE (got %s bytes)", len );
|
||||
// Read in a 2 or 3 byte number that represents the length of the packet. (3 byte "shorts" for Forge only)
|
||||
// No vanilla packet should give a 3 byte packet, this method will still retain vanilla behaviour.
|
||||
int len = readVarShort( buf );
|
||||
|
||||
// (Integer.MAX_VALUE & 0x1FFF9A ) = 2097050 - Forge's current upper limit
|
||||
Preconditions.checkArgument( len <= ( Integer.MAX_VALUE & 0x1FFF9A ), "Cannot receive array longer than 2097050 (got %s bytes)", len );
|
||||
|
||||
byte[] ret = new byte[ len ];
|
||||
buf.readBytes( ret );
|
||||
@@ -83,6 +95,11 @@ public abstract class DefinedPacket
|
||||
}
|
||||
|
||||
public static int readVarInt(ByteBuf input)
|
||||
{
|
||||
return readVarInt( input, 5 );
|
||||
}
|
||||
|
||||
public static int readVarInt(ByteBuf input, int maxBytes)
|
||||
{
|
||||
int out = 0;
|
||||
int bytes = 0;
|
||||
@@ -93,7 +110,7 @@ public abstract class DefinedPacket
|
||||
|
||||
out |= ( in & 0x7F ) << ( bytes++ * 7 );
|
||||
|
||||
if ( bytes > 5 )
|
||||
if ( bytes > maxBytes )
|
||||
{
|
||||
throw new RuntimeException( "VarInt too big" );
|
||||
}
|
||||
@@ -129,6 +146,33 @@ public abstract class DefinedPacket
|
||||
}
|
||||
}
|
||||
|
||||
public static int readVarShort(ByteBuf buf)
|
||||
{
|
||||
int low = buf.readUnsignedShort();
|
||||
int high = 0;
|
||||
if ( ( low & 0x8000 ) != 0 )
|
||||
{
|
||||
low = low & 0x7FFF;
|
||||
high = buf.readUnsignedByte();
|
||||
}
|
||||
return ( ( high & 0xFF ) << 15 ) | low;
|
||||
}
|
||||
|
||||
public static void writeVarShort(ByteBuf buf, int toWrite)
|
||||
{
|
||||
int low = toWrite & 0x7FFF;
|
||||
int high = ( toWrite & 0x7F8000 ) >> 15;
|
||||
if ( high != 0 )
|
||||
{
|
||||
low = low | 0x8000;
|
||||
}
|
||||
buf.writeShort( low );
|
||||
if ( high != 0 )
|
||||
{
|
||||
buf.writeByte( high );
|
||||
}
|
||||
}
|
||||
|
||||
public static void writeUUID(UUID value, ByteBuf output)
|
||||
{
|
||||
output.writeLong( value.getMostSignificantBits() );
|
||||
|
@@ -41,8 +41,8 @@ public class EncryptionRequest extends DefinedPacket
|
||||
writeString( serverId, buf );
|
||||
if ( protocolVersion < ProtocolConstants.MINECRAFT_SNAPSHOT )
|
||||
{
|
||||
writeArrayLegacy( publicKey, buf );
|
||||
writeArrayLegacy( verifyToken, buf );
|
||||
writeArrayLegacy( publicKey, buf, false );
|
||||
writeArrayLegacy( verifyToken, buf, false );
|
||||
} else
|
||||
{
|
||||
writeArray( publicKey, buf );
|
||||
|
@@ -38,8 +38,8 @@ public class EncryptionResponse extends DefinedPacket
|
||||
{
|
||||
if ( protocolVersion < ProtocolConstants.MINECRAFT_SNAPSHOT )
|
||||
{
|
||||
writeArrayLegacy( sharedSecret, buf );
|
||||
writeArrayLegacy( verifyToken, buf );
|
||||
writeArrayLegacy( sharedSecret, buf, false );
|
||||
writeArrayLegacy( verifyToken, buf, false );
|
||||
} else
|
||||
{
|
||||
writeArray( sharedSecret, buf );
|
||||
|
@@ -24,6 +24,11 @@ public class PluginMessage extends DefinedPacket
|
||||
private String tag;
|
||||
private byte[] data;
|
||||
|
||||
/**
|
||||
* Allow this packet to be sent as an "extended" packet.
|
||||
*/
|
||||
private boolean allowExtendedPacket = false;
|
||||
|
||||
@Override
|
||||
public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion)
|
||||
{
|
||||
@@ -44,7 +49,7 @@ public class PluginMessage extends DefinedPacket
|
||||
writeString( tag, buf );
|
||||
if ( protocolVersion < ProtocolConstants.MINECRAFT_SNAPSHOT )
|
||||
{
|
||||
writeArrayLegacy( data, buf );
|
||||
writeArrayLegacy( data, buf, allowExtendedPacket );
|
||||
} else
|
||||
{
|
||||
buf.writeBytes( data );
|
||||
|
Reference in New Issue
Block a user