Decode ComponentStyle over network as appropriate

This commit is contained in:
md_5 2024-01-06 16:35:33 +11:00
parent 737d545fb6
commit 21c8f2815a
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11

View File

@ -19,6 +19,7 @@ import java.util.UUID;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentStyle;
import net.md_5.bungee.chat.ComponentSerializer; import net.md_5.bungee.chat.ComponentSerializer;
import se.llbit.nbt.ErrorTag; import se.llbit.nbt.ErrorTag;
import se.llbit.nbt.NamedTag; import se.llbit.nbt.NamedTag;
@ -118,6 +119,14 @@ public abstract class DefinedPacket
} }
} }
public static ComponentStyle readComponentStyle(ByteBuf buf, int protocolVersion)
{
SpecificTag nbt = (SpecificTag) readTag( buf, protocolVersion );
JsonElement json = TagUtil.toJson( nbt );
return ComponentSerializer.deserializeStyle( json );
}
public static void writeEitherBaseComponent(Either<String, BaseComponent> message, ByteBuf buf, int protocolVersion) public static void writeEitherBaseComponent(Either<String, BaseComponent> message, ByteBuf buf, int protocolVersion)
{ {
if ( message.isLeft() ) if ( message.isLeft() )
@ -145,6 +154,14 @@ public abstract class DefinedPacket
} }
} }
public static void writeComponentStyle(ComponentStyle style, ByteBuf buf, int protocolVersion)
{
JsonElement json = ComponentSerializer.toJson( style );
SpecificTag nbt = TagUtil.fromJson( json );
writeTag( nbt, buf, protocolVersion );
}
public static void writeArray(byte[] b, ByteBuf buf) public static void writeArray(byte[] b, ByteBuf buf)
{ {
if ( b.length > Short.MAX_VALUE ) if ( b.length > Short.MAX_VALUE )
@ -378,7 +395,7 @@ public abstract class DefinedPacket
case BLANK: case BLANK:
break; break;
case STYLED: case STYLED:
writeBaseComponent( (BaseComponent) format.getValue(), buf, protocolVersion ); // TODO: style writeComponentStyle( (ComponentStyle) format.getValue(), buf, protocolVersion );
break; break;
case FIXED: case FIXED:
writeBaseComponent( (BaseComponent) format.getValue(), buf, protocolVersion ); writeBaseComponent( (BaseComponent) format.getValue(), buf, protocolVersion );
@ -394,7 +411,7 @@ public abstract class DefinedPacket
case 0: case 0:
return new NumberFormat( NumberFormat.Type.BLANK, null ); return new NumberFormat( NumberFormat.Type.BLANK, null );
case 1: case 1:
return new NumberFormat( NumberFormat.Type.STYLED, readBaseComponent( buf, protocolVersion ) ); // TODO: style return new NumberFormat( NumberFormat.Type.STYLED, readComponentStyle( buf, protocolVersion ) );
case 2: case 2:
return new NumberFormat( NumberFormat.Type.FIXED, readBaseComponent( buf, protocolVersion ) ); return new NumberFormat( NumberFormat.Type.FIXED, readBaseComponent( buf, protocolVersion ) );
default: default: