Skip to content

Commit

Permalink
Decode ComponentStyle over network as appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
md-5 committed Jan 6, 2024
1 parent 737d545 commit 21c8f28
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.function.BiConsumer;
import lombok.RequiredArgsConstructor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentStyle;
import net.md_5.bungee.chat.ComponentSerializer;
import se.llbit.nbt.ErrorTag;
import se.llbit.nbt.NamedTag;
Expand Down Expand Up @@ -118,6 +119,14 @@ public static BaseComponent readBaseComponent(ByteBuf buf, int maxStringLength,
}
}

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)
{
if ( message.isLeft() )
Expand Down Expand Up @@ -145,6 +154,14 @@ public static void writeBaseComponent(BaseComponent message, ByteBuf buf, int pr
}
}

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)
{
if ( b.length > Short.MAX_VALUE )
Expand Down Expand Up @@ -378,7 +395,7 @@ public static void writeNumberFormat(NumberFormat format, ByteBuf buf, int proto
case BLANK:
break;
case STYLED:
writeBaseComponent( (BaseComponent) format.getValue(), buf, protocolVersion ); // TODO: style
writeComponentStyle( (ComponentStyle) format.getValue(), buf, protocolVersion );
break;
case FIXED:
writeBaseComponent( (BaseComponent) format.getValue(), buf, protocolVersion );
Expand All @@ -394,7 +411,7 @@ public static NumberFormat readNumberFormat(ByteBuf buf, int protocolVersion)
case 0:
return new NumberFormat( NumberFormat.Type.BLANK, null );
case 1:
return new NumberFormat( NumberFormat.Type.STYLED, readBaseComponent( buf, protocolVersion ) ); // TODO: style
return new NumberFormat( NumberFormat.Type.STYLED, readComponentStyle( buf, protocolVersion ) );
case 2:
return new NumberFormat( NumberFormat.Type.FIXED, readBaseComponent( buf, protocolVersion ) );
default:
Expand Down

0 comments on commit 21c8f28

Please sign in to comment.