#3854: Fix CustomClickAction read and write implementation

This commit is contained in:
FlorianMichael 2025-06-29 21:45:47 +10:00 committed by md_5
parent bdd32d5a58
commit 88436c44a6
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11
3 changed files with 36 additions and 7 deletions

View File

@ -48,6 +48,38 @@ public abstract class DefinedPacket
}
}
public <T> T readLengthPrefixed(Function<ByteBuf, T> reader, ByteBuf buf, int maxSize)
{
int size = readVarInt( buf );
if ( size > maxSize )
{
throw new OverflowPacketException( "Cannot read length prefixed with limit " + maxSize + " (got size of " + size + ")" );
}
return reader.apply( buf.readSlice( size ) );
}
public <T> void writeLengthPrefixed(T value, BiConsumer<T, ByteBuf> writer, ByteBuf buf, int maxSize)
{
ByteBuf tempBuffer = buf.alloc().buffer();
try
{
writer.accept( value, tempBuffer );
if ( tempBuffer.readableBytes() > maxSize )
{
throw new OverflowPacketException( "Cannot write length prefixed with limit " + maxSize + " (got size of " + tempBuffer.readableBytes() + ")" );
}
writeVarInt( tempBuffer.readableBytes(), buf );
buf.writeBytes( tempBuffer );
} finally
{
tempBuffer.release();
}
}
public static void writeString(String s, ByteBuf buf)
{
writeString( s, buf, Short.MAX_VALUE );

View File

@ -323,6 +323,8 @@ public final class TagUtil
}
return jsonLongArray;
case Tag.END:
return JsonNull.INSTANCE;
default:
throw new IllegalArgumentException( "Unknown NBT tag: " + tag );
}

View File

@ -26,19 +26,14 @@ public class CustomClickAction extends DefinedPacket
public void read(ByteBuf buf, Protocol protocol, ProtocolConstants.Direction direction, int protocolVersion)
{
id = readString( buf );
data = readNullable( (buf0) -> (TypedTag) readTag( buf0, protocolVersion, new NBTLimiter( 32768L, 16 ) ), buf );
data = readLengthPrefixed( (buf0) -> (TypedTag) readTag( buf0, protocolVersion, new NBTLimiter( 32768L, 16 ) ), buf, 65536 );
}
@Override
public void write(ByteBuf buf, Protocol protocol, ProtocolConstants.Direction direction, int protocolVersion)
{
writeString( id, buf );
writeNullable( data, (data0, buf0) -> writeTag( data0, buf0, protocolVersion ), buf );
}
@Override
public void write(ByteBuf buf)
{
writeLengthPrefixed( data, (data0, buf0) -> writeTag( data0, buf0, protocolVersion ), buf, 65536 );
}
@Override