#3854: Fix CustomClickAction read and write implementation
This commit is contained in:
parent
bdd32d5a58
commit
88436c44a6
@ -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)
|
public static void writeString(String s, ByteBuf buf)
|
||||||
{
|
{
|
||||||
writeString( s, buf, Short.MAX_VALUE );
|
writeString( s, buf, Short.MAX_VALUE );
|
||||||
|
@ -323,6 +323,8 @@ public final class TagUtil
|
|||||||
}
|
}
|
||||||
|
|
||||||
return jsonLongArray;
|
return jsonLongArray;
|
||||||
|
case Tag.END:
|
||||||
|
return JsonNull.INSTANCE;
|
||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException( "Unknown NBT tag: " + tag );
|
throw new IllegalArgumentException( "Unknown NBT tag: " + tag );
|
||||||
}
|
}
|
||||||
|
@ -26,19 +26,14 @@ public class CustomClickAction extends DefinedPacket
|
|||||||
public void read(ByteBuf buf, Protocol protocol, ProtocolConstants.Direction direction, int protocolVersion)
|
public void read(ByteBuf buf, Protocol protocol, ProtocolConstants.Direction direction, int protocolVersion)
|
||||||
{
|
{
|
||||||
id = readString( buf );
|
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
|
@Override
|
||||||
public void write(ByteBuf buf, Protocol protocol, ProtocolConstants.Direction direction, int protocolVersion)
|
public void write(ByteBuf buf, Protocol protocol, ProtocolConstants.Direction direction, int protocolVersion)
|
||||||
{
|
{
|
||||||
writeString( id, buf );
|
writeString( id, buf );
|
||||||
writeNullable( data, (data0, buf0) -> writeTag( data0, buf0, protocolVersion ), buf );
|
writeLengthPrefixed( data, (data0, buf0) -> writeTag( data0, buf0, protocolVersion ), buf, 65536 );
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void write(ByteBuf buf)
|
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user