From d678102933c7413c9dae19fc012ea79b2f31cd4d Mon Sep 17 00:00:00 2001 From: Janmm14 Date: Sat, 9 Mar 2024 03:09:42 +0100 Subject: [PATCH] Lazily deserialize BaseComponent in packets (cherry picked from commit 8cd1505911271599ee354eae90dd5c31ad64f12d) --- .../md_5/bungee/chat/ComponentSerializer.java | 11 ++ .../md_5/bungee/protocol/DefinedPacket.java | 80 ++++++++++- .../md_5/bungee/protocol/Deserializable.java | 28 ++++ .../protocol/FunctionDeserializable.java | 22 +++ .../bungee/protocol/NoOrigDeserializable.java | 27 ++++ .../bungee/protocol/SimpleDeserializable.java | 41 ++++++ .../md_5/bungee/protocol/packet/BossBar.java | 25 +++- .../net/md_5/bungee/protocol/packet/Kick.java | 40 +++++- .../packet/PlayerListHeaderFooter.java | 60 +++++++- .../protocol/packet/PlayerListItem.java | 40 ++++-- .../protocol/packet/PlayerListItemUpdate.java | 8 +- .../protocol/packet/ScoreboardObjective.java | 52 ++++++- .../protocol/packet/ScoreboardScore.java | 39 +++++- .../bungee/protocol/packet/ServerData.java | 39 +++++- .../md_5/bungee/protocol/packet/Subtitle.java | 34 ++++- .../bungee/protocol/packet/SystemChat.java | 36 ++++- .../protocol/packet/TabCompleteResponse.java | 20 ++- .../net/md_5/bungee/protocol/packet/Team.java | 130 ++++++++++++++++-- .../md_5/bungee/protocol/packet/Title.java | 42 +++++- .../java/net/md_5/bungee/ServerConnector.java | 2 +- .../bungee/connection/DownstreamBridge.java | 6 +- 21 files changed, 706 insertions(+), 76 deletions(-) create mode 100644 protocol/src/main/java/net/md_5/bungee/protocol/Deserializable.java create mode 100644 protocol/src/main/java/net/md_5/bungee/protocol/FunctionDeserializable.java create mode 100644 protocol/src/main/java/net/md_5/bungee/protocol/NoOrigDeserializable.java create mode 100644 protocol/src/main/java/net/md_5/bungee/protocol/SimpleDeserializable.java diff --git a/chat/src/main/java/net/md_5/bungee/chat/ComponentSerializer.java b/chat/src/main/java/net/md_5/bungee/chat/ComponentSerializer.java index 1164804f7c7..9410de47eda 100644 --- a/chat/src/main/java/net/md_5/bungee/chat/ComponentSerializer.java +++ b/chat/src/main/java/net/md_5/bungee/chat/ComponentSerializer.java @@ -20,6 +20,7 @@ import net.md_5.bungee.api.chat.SelectorComponent; import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TranslatableComponent; +import net.md_5.bungee.api.chat.hover.content.Content; import net.md_5.bungee.api.chat.hover.content.Entity; import net.md_5.bungee.api.chat.hover.content.EntitySerializer; import net.md_5.bungee.api.chat.hover.content.Item; @@ -163,6 +164,16 @@ public static String toString(Object object) return gson.toJson( object ); } + public static String toString(Content content) + { + return gson.toJson( content ); + } + + public static String toString(JsonElement element) + { + return gson.toJson( element ); + } + public static String toString(BaseComponent component) { return gson.toJson( component ); diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java b/protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java index 6cd9e6a425f..3a308001a43 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java @@ -3,6 +3,7 @@ import com.google.common.base.Function; import com.google.common.base.Preconditions; import com.google.gson.JsonElement; +import com.google.gson.JsonParser; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufInputStream; import io.netty.buffer.ByteBufOutputStream; @@ -93,17 +94,37 @@ public static String readString(ByteBuf buf, int maxLen) return s; } - public static Either readEitherBaseComponent(ByteBuf buf, int protocolVersion, boolean string) + public static Either, BaseComponent>> readEitherBaseComponent(ByteBuf buf, int protocolVersion, boolean string) { return ( string ) ? Either.left( readString( buf ) ) : Either.right( readBaseComponent( buf, protocolVersion ) ); } - public static BaseComponent readBaseComponent(ByteBuf buf, int protocolVersion) + public static Deserializable, BaseComponent> readBaseComponent(ByteBuf buf, int protocolVersion) { return readBaseComponent( buf, Short.MAX_VALUE, protocolVersion ); } - public static BaseComponent readBaseComponent(ByteBuf buf, int maxStringLength, int protocolVersion) + public static Deserializable, BaseComponent> readBaseComponent(ByteBuf buf, int maxStringLength, int protocolVersion) + { + if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_3 ) + { + SpecificTag nbt = (SpecificTag) readTag( buf, protocolVersion ); + + return new FunctionDeserializable<>( Either.right( nbt ), (ov) -> ComponentSerializer.deserialize( TagUtil.toJson( ov.getRight() ) ) ); + } else + { + String string = readString( buf, maxStringLength ); + + return new FunctionDeserializable<>( Either.left( string ), (ov) -> ComponentSerializer.deserialize( ov.getLeft() ) ); + } + } + + public static BaseComponent readRawComponent(ByteBuf buf, int protocolVersion) + { + return readRawComponent( buf, Short.MAX_VALUE, protocolVersion ); + } + + public static BaseComponent readRawComponent(ByteBuf buf, int maxStringLength, int protocolVersion) { if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_3 ) { @@ -127,7 +148,7 @@ public static ComponentStyle readComponentStyle(ByteBuf buf, int protocolVersion return ComponentSerializer.deserializeStyle( json ); } - public static void writeEitherBaseComponent(Either message, ByteBuf buf, int protocolVersion) + public static void writeEitherBaseComponent(Either, BaseComponent>> message, ByteBuf buf, int protocolVersion) { if ( message.isLeft() ) { @@ -138,7 +159,52 @@ public static void writeEitherBaseComponent(Either messag } } - public static void writeBaseComponent(BaseComponent message, ByteBuf buf, int protocolVersion) + public static void writeBaseComponent(Deserializable, BaseComponent> message, ByteBuf buf, int protocolVersion) + { + if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_3 ) + { + if ( message.hasDeserialized() ) + { + BaseComponent baseComponent = message.get(); + JsonElement json = ComponentSerializer.toJson( baseComponent ); + SpecificTag nbt = TagUtil.fromJson( json ); + + writeTag( nbt, buf, protocolVersion ); + } else + { + Either eitherStrJsonElem = message.original(); + if ( eitherStrJsonElem.isLeft() ) + { + writeTag( TagUtil.fromJson( JsonParser.parseString( eitherStrJsonElem.getLeft() ) ), buf, protocolVersion ); + } else + { + writeTag( eitherStrJsonElem.getRight(), buf, protocolVersion ); + } + } + } else + { + if ( message.hasDeserialized() ) + { + String string = ComponentSerializer.toString( message.get() ); + + writeString( string, buf ); + } else + { + Either eitherStrJsonElem = message.original(); + if ( eitherStrJsonElem.isLeft() ) + { + writeString( eitherStrJsonElem.getLeft(), buf ); + } else + { + String string = ComponentSerializer.toString( TagUtil.toJson( eitherStrJsonElem.getRight() ) ); + + writeString( string, buf ); + } + } + } + } + + public static void writeRawComponent(BaseComponent message, ByteBuf buf, int protocolVersion) { if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_3 ) { @@ -398,7 +464,7 @@ public static void writeNumberFormat(NumberFormat format, ByteBuf buf, int proto writeComponentStyle( (ComponentStyle) format.getValue(), buf, protocolVersion ); break; case FIXED: - writeBaseComponent( (BaseComponent) format.getValue(), buf, protocolVersion ); + writeRawComponent( (BaseComponent) format.getValue(), buf, protocolVersion ); break; } } @@ -413,7 +479,7 @@ public static NumberFormat readNumberFormat(ByteBuf buf, int protocolVersion) case 1: return new NumberFormat( NumberFormat.Type.STYLED, readComponentStyle( buf, protocolVersion ) ); case 2: - return new NumberFormat( NumberFormat.Type.FIXED, readBaseComponent( buf, protocolVersion ) ); + return new NumberFormat( NumberFormat.Type.FIXED, readRawComponent( buf, protocolVersion ) ); default: throw new IllegalArgumentException( "Unknown number format " + format ); } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/Deserializable.java b/protocol/src/main/java/net/md_5/bungee/protocol/Deserializable.java new file mode 100644 index 00000000000..930ee79829a --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/Deserializable.java @@ -0,0 +1,28 @@ +package net.md_5.bungee.protocol; + +/** + * Represents a value that can be deserialized from another value if needed. + * @param the original value + * @param the deserialized value + */ +public interface Deserializable +{ + /** + * @return the deserialized value + */ + D get(); + + /** + * If {@link #hasDeserialized()} returns true, this method may return null. This usually hapens after code has + * edited the deserialized value and wrote it back to its original place. + * @return the original value, if available + */ + OV original(); + + /** + * If the value has been deserialized, it is adviced to no longer call {@link #original()}, as the deserialized + * value may have been modified. + * @return true if the value has been deserialized + */ + boolean hasDeserialized(); +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/FunctionDeserializable.java b/protocol/src/main/java/net/md_5/bungee/protocol/FunctionDeserializable.java new file mode 100644 index 00000000000..2e8235421f4 --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/FunctionDeserializable.java @@ -0,0 +1,22 @@ +package net.md_5.bungee.protocol; + +import java.util.function.Function; +import org.jetbrains.annotations.NotNull; + +public class FunctionDeserializable extends SimpleDeserializable +{ + private final Function function; + + public FunctionDeserializable(OV ov, Function supplier) + { + super( ov ); + this.function = supplier; + } + + @NotNull + @Override + public D deserialize() + { + return function.apply( original() ); + } +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/NoOrigDeserializable.java b/protocol/src/main/java/net/md_5/bungee/protocol/NoOrigDeserializable.java new file mode 100644 index 00000000000..10dfa7b4fea --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/NoOrigDeserializable.java @@ -0,0 +1,27 @@ +package net.md_5.bungee.protocol; + +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class NoOrigDeserializable implements Deserializable +{ + private final D value; + + @Override + public D get() + { + return value; + } + + @Override + public OV original() + { + return null; + } + + @Override + public boolean hasDeserialized() + { + return true; + } +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/SimpleDeserializable.java b/protocol/src/main/java/net/md_5/bungee/protocol/SimpleDeserializable.java new file mode 100644 index 00000000000..2281b9f94d6 --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/SimpleDeserializable.java @@ -0,0 +1,41 @@ +package net.md_5.bungee.protocol; + +import lombok.NonNull; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public abstract class SimpleDeserializable implements Deserializable +{ + private final OV original; + private D deserialized; + + /** + * Method called to get the deserialized value. Called only once unless multiple threads are calling get() at the + * same time. + * @return the deserialized value + */ + @NonNull + protected abstract D deserialize(); + + @Override + public final D get() + { + if ( !hasDeserialized() ) + { + return deserialized = deserialize(); + } + return deserialized; + } + + @Override + public final boolean hasDeserialized() + { + return deserialized != null; + } + + @Override + public final OV original() + { + return original; + } +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/BossBar.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/BossBar.java index 991a1ab9e93..6a6977a4f15 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/BossBar.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/BossBar.java @@ -8,7 +8,11 @@ import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; +import net.md_5.bungee.protocol.Deserializable; +import net.md_5.bungee.protocol.Either; +import net.md_5.bungee.protocol.NoOrigDeserializable; import net.md_5.bungee.protocol.ProtocolConstants; +import se.llbit.nbt.SpecificTag; @Data @NoArgsConstructor @@ -18,7 +22,7 @@ public class BossBar extends DefinedPacket private UUID uuid; private int action; - private BaseComponent title; + private Deserializable, BaseComponent> title; private float health; private int color; private int division; @@ -107,4 +111,23 @@ public void handle(AbstractPacketHandler handler) throws Exception { handler.handle( this ); } + + public BaseComponent getTitle() + { + if ( title == null ) + { + return null; + } + return title.get(); + } + + public void setTitle(BaseComponent title) + { + if ( title == null ) + { + this.title = null; + return; + } + this.title = new NoOrigDeserializable<>( title ); + } } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Kick.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Kick.java index 14539b4b870..07294a9ab88 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Kick.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Kick.java @@ -9,8 +9,13 @@ import net.md_5.bungee.chat.ComponentSerializer; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; +import net.md_5.bungee.protocol.Deserializable; +import net.md_5.bungee.protocol.Either; +import net.md_5.bungee.protocol.FunctionDeserializable; +import net.md_5.bungee.protocol.NoOrigDeserializable; import net.md_5.bungee.protocol.Protocol; import net.md_5.bungee.protocol.ProtocolConstants; +import se.llbit.nbt.SpecificTag; @Data @NoArgsConstructor @@ -19,17 +24,18 @@ public class Kick extends DefinedPacket { - private BaseComponent message; + private Deserializable, BaseComponent> messageRaw; @Override public void read(ByteBuf buf, Protocol protocol, ProtocolConstants.Direction direction, int protocolVersion) { if ( protocol == Protocol.LOGIN ) { - message = ComponentSerializer.deserialize( readString( buf ) ); + String json = readString( buf ); + messageRaw = new FunctionDeserializable<>( Either.left( json ), (ov) -> ComponentSerializer.deserialize( ov.getLeft() ) ); } else { - message = readBaseComponent( buf, protocolVersion ); + messageRaw = readBaseComponent( buf, protocolVersion ); } } @@ -38,10 +44,10 @@ public void write(ByteBuf buf, Protocol protocol, ProtocolConstants.Direction di { if ( protocol == Protocol.LOGIN ) { - writeString( ComponentSerializer.toString( message ), buf ); + writeString( ComponentSerializer.toString( messageRaw.get() ), buf ); } else { - writeBaseComponent( message, buf, protocolVersion ); + writeBaseComponent( messageRaw, buf, protocolVersion ); } } @@ -50,4 +56,28 @@ public void handle(AbstractPacketHandler handler) throws Exception { handler.handle( this ); } + + public Kick(BaseComponent message) + { + setMessage( message ); + } + + public BaseComponent getMessage() + { + if ( messageRaw == null ) + { + return null; + } + return messageRaw.get(); + } + + public void setMessage(BaseComponent message) + { + if ( message == null ) + { + this.messageRaw = null; + return; + } + this.messageRaw = new NoOrigDeserializable<>( message ); + } } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PlayerListHeaderFooter.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PlayerListHeaderFooter.java index 98dbae4a909..44570927950 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PlayerListHeaderFooter.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PlayerListHeaderFooter.java @@ -8,7 +8,11 @@ import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; +import net.md_5.bungee.protocol.Deserializable; +import net.md_5.bungee.protocol.Either; +import net.md_5.bungee.protocol.NoOrigDeserializable; import net.md_5.bungee.protocol.ProtocolConstants; +import se.llbit.nbt.SpecificTag; @Data @NoArgsConstructor @@ -17,21 +21,21 @@ public class PlayerListHeaderFooter extends DefinedPacket { - private BaseComponent header; - private BaseComponent footer; + private Deserializable, BaseComponent> headerRaw; + private Deserializable, BaseComponent> footerRaw; @Override public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { - header = readBaseComponent( buf, protocolVersion ); - footer = readBaseComponent( buf, protocolVersion ); + headerRaw = readBaseComponent( buf, protocolVersion ); + footerRaw = readBaseComponent( buf, protocolVersion ); } @Override public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { - writeBaseComponent( header, buf, protocolVersion ); - writeBaseComponent( footer, buf, protocolVersion ); + writeBaseComponent( headerRaw, buf, protocolVersion ); + writeBaseComponent( footerRaw, buf, protocolVersion ); } @Override @@ -39,4 +43,48 @@ public void handle(AbstractPacketHandler handler) throws Exception { handler.handle( this ); } + + public PlayerListHeaderFooter(BaseComponent header, BaseComponent footer) + { + setHeader( header ); + setFooter( footer ); + } + + public BaseComponent getHeader() + { + if ( headerRaw == null ) + { + return null; + } + return headerRaw.get(); + } + + public void setHeader(BaseComponent header) + { + if ( header == null ) + { + this.headerRaw = null; + return; + } + this.headerRaw = new NoOrigDeserializable<>( header ); + } + + public BaseComponent getFooter() + { + if ( footerRaw == null ) + { + return null; + } + return footerRaw.get(); + } + + public void setFooter(BaseComponent footer) + { + if ( footer == null ) + { + this.footerRaw = null; + return; + } + this.footerRaw = new NoOrigDeserializable<>( footer ); + } } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PlayerListItem.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PlayerListItem.java index 34a12a80edb..cc07056f1b3 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PlayerListItem.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PlayerListItem.java @@ -8,9 +8,13 @@ import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; +import net.md_5.bungee.protocol.Deserializable; +import net.md_5.bungee.protocol.Either; +import net.md_5.bungee.protocol.NoOrigDeserializable; import net.md_5.bungee.protocol.PlayerPublicKey; import net.md_5.bungee.protocol.Property; import net.md_5.bungee.protocol.ProtocolConstants; +import se.llbit.nbt.SpecificTag; @Data @NoArgsConstructor @@ -39,7 +43,7 @@ public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protoco item.ping = DefinedPacket.readVarInt( buf ); if ( buf.readBoolean() ) { - item.displayName = DefinedPacket.readBaseComponent( buf, protocolVersion ); + item.displayNameRaw = DefinedPacket.readBaseComponent( buf, protocolVersion ); } if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_19 ) { @@ -55,7 +59,7 @@ public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protoco case UPDATE_DISPLAY_NAME: if ( buf.readBoolean() ) { - item.displayName = DefinedPacket.readBaseComponent( buf, protocolVersion ); + item.displayNameRaw = DefinedPacket.readBaseComponent( buf, protocolVersion ); } } } @@ -76,10 +80,10 @@ public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protoc DefinedPacket.writeProperties( item.properties, buf ); DefinedPacket.writeVarInt( item.gamemode, buf ); DefinedPacket.writeVarInt( item.ping, buf ); - buf.writeBoolean( item.displayName != null ); - if ( item.displayName != null ) + buf.writeBoolean( item.displayNameRaw != null ); + if ( item.displayNameRaw != null ) { - DefinedPacket.writeBaseComponent( item.displayName, buf, protocolVersion ); + DefinedPacket.writeBaseComponent( item.displayNameRaw, buf, protocolVersion ); } if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_19 ) { @@ -93,10 +97,10 @@ public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protoc DefinedPacket.writeVarInt( item.ping, buf ); break; case UPDATE_DISPLAY_NAME: - buf.writeBoolean( item.displayName != null ); - if ( item.displayName != null ) + buf.writeBoolean( item.displayNameRaw != null ); + if ( item.displayNameRaw != null ) { - DefinedPacket.writeBaseComponent( item.displayName, buf, protocolVersion ); + DefinedPacket.writeBaseComponent( item.displayNameRaw, buf, protocolVersion ); } break; } @@ -143,7 +147,25 @@ public static class Item Integer ping; // ADD_PLAYER & UPDATE_DISPLAY_NAME - BaseComponent displayName; + Deserializable, BaseComponent> displayNameRaw; + public BaseComponent getDisplayName() + { + if ( displayNameRaw == null ) + { + return null; + } + return displayNameRaw.get(); + } + + public void setDisplayName(BaseComponent displayName) + { + if ( displayName == null ) + { + this.displayNameRaw = null; + return; + } + this.displayNameRaw = new NoOrigDeserializable<>( displayName ); + } } } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PlayerListItemUpdate.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PlayerListItemUpdate.java index f6708b062fb..f84d65180d9 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/PlayerListItemUpdate.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/PlayerListItemUpdate.java @@ -58,7 +58,7 @@ public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protoco case UPDATE_DISPLAY_NAME: if ( buf.readBoolean() ) { - item.displayName = DefinedPacket.readBaseComponent( buf, protocolVersion ); + item.displayNameRaw = DefinedPacket.readBaseComponent( buf, protocolVersion ); } break; } @@ -103,10 +103,10 @@ public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protoc DefinedPacket.writeVarInt( item.ping, buf ); break; case UPDATE_DISPLAY_NAME: - buf.writeBoolean( item.displayName != null ); - if ( item.displayName != null ) + buf.writeBoolean( item.displayNameRaw != null ); + if ( item.displayNameRaw != null ) { - DefinedPacket.writeBaseComponent( item.displayName, buf, protocolVersion ); + DefinedPacket.writeBaseComponent( item.displayNameRaw, buf, protocolVersion ); } break; } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/ScoreboardObjective.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/ScoreboardObjective.java index a37617fd467..2456e499559 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/ScoreboardObjective.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/ScoreboardObjective.java @@ -9,9 +9,12 @@ import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; +import net.md_5.bungee.protocol.Deserializable; import net.md_5.bungee.protocol.Either; +import net.md_5.bungee.protocol.NoOrigDeserializable; import net.md_5.bungee.protocol.NumberFormat; import net.md_5.bungee.protocol.ProtocolConstants; +import se.llbit.nbt.SpecificTag; @Data @NoArgsConstructor @@ -21,8 +24,8 @@ public class ScoreboardObjective extends DefinedPacket { private String name; - private Either value; private HealthDisplay type; + private Either, BaseComponent>> valueRaw; /** * 0 to create, 1 to remove, 2 to update display text. */ @@ -38,11 +41,11 @@ public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protoco { if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_13 ) { - value = readEitherBaseComponent( buf, protocolVersion, false ); + valueRaw = readEitherBaseComponent( buf, protocolVersion, false ); type = HealthDisplay.values()[readVarInt( buf )]; } else { - value = readEitherBaseComponent( buf, protocolVersion, true ); + valueRaw = readEitherBaseComponent( buf, protocolVersion, true ); type = HealthDisplay.fromString( readString( buf ) ); } if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_3 ) @@ -59,7 +62,7 @@ public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protoc buf.writeByte( action ); if ( action == 0 || action == 2 ) { - writeEitherBaseComponent( value, buf, protocolVersion ); + writeEitherBaseComponent( valueRaw, buf, protocolVersion ); if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_13 ) { writeVarInt( type.ordinal(), buf ); @@ -96,4 +99,45 @@ public static HealthDisplay fromString(String s) return valueOf( s.toUpperCase( Locale.ROOT ) ); } } + + public ScoreboardObjective(String name, Either value, HealthDisplay type, byte action, NumberFormat numberFormat) + { + this.name = name; + setValue( value ); + this.type = type; + this.action = action; + this.numberFormat = numberFormat; + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + public Either getValue() + { + if ( valueRaw == null ) + { + return null; + } + if ( valueRaw.isLeft() ) + { + return (Either) valueRaw; + } else + { + return Either.right( valueRaw.getRight().get() ); + } + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + public void setValue(Either value) + { + if ( value == null ) + { + valueRaw = null; + } else if ( value.isLeft() ) + { + valueRaw = (Either) value; + } else + { + valueRaw = Either.right( new NoOrigDeserializable<>( value.getRight() ) ); + } + } + } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/ScoreboardScore.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/ScoreboardScore.java index d77826a7a90..bd65ac44c53 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/ScoreboardScore.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/ScoreboardScore.java @@ -8,8 +8,12 @@ import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; +import net.md_5.bungee.protocol.Deserializable; +import net.md_5.bungee.protocol.Either; +import net.md_5.bungee.protocol.NoOrigDeserializable; import net.md_5.bungee.protocol.NumberFormat; import net.md_5.bungee.protocol.ProtocolConstants; +import se.llbit.nbt.SpecificTag; @Data @NoArgsConstructor @@ -25,7 +29,7 @@ public class ScoreboardScore extends DefinedPacket private byte action; private String scoreName; private int value; - private BaseComponent displayName; + private Deserializable, BaseComponent> displayNameRaw; private NumberFormat numberFormat; @Override @@ -46,7 +50,7 @@ public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protoco } if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_3 ) { - displayName = readNullable( (b) -> readBaseComponent( b, protocolVersion ), buf ); + displayNameRaw = readNullable( (b) -> readBaseComponent( b, protocolVersion ), buf ); numberFormat = readNullable( (b) -> readNumberFormat( b, protocolVersion ), buf ); } } @@ -66,7 +70,7 @@ public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protoc } if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_3 ) { - writeNullable( displayName, (s, b) -> DefinedPacket.writeBaseComponent( s, b, protocolVersion ), buf ); + writeNullable( displayNameRaw, (s, b) -> DefinedPacket.writeBaseComponent( s, b, protocolVersion ), buf ); writeNullable( numberFormat, (s, b) -> DefinedPacket.writeNumberFormat( s, b, protocolVersion ), buf ); } } @@ -76,4 +80,33 @@ public void handle(AbstractPacketHandler handler) throws Exception { handler.handle( this ); } + + public ScoreboardScore(String itemName, byte action, String scoreName, int value, BaseComponent displayName, NumberFormat numberFormat) + { + this.itemName = itemName; + this.action = action; + this.scoreName = scoreName; + this.value = value; + setDisplayName( displayName ); + this.numberFormat = numberFormat; + } + + public BaseComponent getDisplayName() + { + if ( displayNameRaw == null ) + { + return null; + } + return displayNameRaw.get(); + } + + public void setDisplayName(BaseComponent displayName) + { + if ( displayName == null ) + { + this.displayNameRaw = null; + return; + } + this.displayNameRaw = new NoOrigDeserializable<>( displayName ); + } } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/ServerData.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/ServerData.java index f9f485e4ab6..7bece78d5b4 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/ServerData.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/ServerData.java @@ -8,7 +8,11 @@ import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; +import net.md_5.bungee.protocol.Deserializable; +import net.md_5.bungee.protocol.Either; +import net.md_5.bungee.protocol.NoOrigDeserializable; import net.md_5.bungee.protocol.ProtocolConstants; +import se.llbit.nbt.SpecificTag; @Data @NoArgsConstructor @@ -17,7 +21,7 @@ public class ServerData extends DefinedPacket { - private BaseComponent motd; + private Deserializable, BaseComponent> motdRaw; private Object icon; private boolean preview; private boolean enforceSecure; @@ -27,7 +31,7 @@ public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protoco { if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_19_4 || buf.readBoolean() ) { - motd = readBaseComponent( buf, protocolVersion ); + motdRaw = readBaseComponent( buf, protocolVersion ); } if ( buf.readBoolean() ) { @@ -54,13 +58,13 @@ public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protoco @Override public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { - if ( motd != null ) + if ( motdRaw != null ) { if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_19_4 ) { buf.writeBoolean( true ); } - writeBaseComponent( motd, buf, protocolVersion ); + writeBaseComponent( motdRaw, buf, protocolVersion ); } else { if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_19_4 ) @@ -102,4 +106,31 @@ public void handle(AbstractPacketHandler handler) throws Exception { handler.handle( this ); } + + public ServerData(BaseComponent motd, Object icon, boolean preview, boolean enforceSecure) + { + setMotd( motd ); + this.icon = icon; + this.preview = preview; + this.enforceSecure = enforceSecure; + } + + public BaseComponent getMotd() + { + if ( motdRaw == null ) + { + return null; + } + return motdRaw.get(); + } + + public void setMotd(BaseComponent motd) + { + if ( motd == null ) + { + this.motdRaw = null; + return; + } + this.motdRaw = new NoOrigDeserializable<>( motd ); + } } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Subtitle.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Subtitle.java index 69e0b84031c..1c15703a6bf 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Subtitle.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Subtitle.java @@ -7,7 +7,11 @@ import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; +import net.md_5.bungee.protocol.Deserializable; +import net.md_5.bungee.protocol.Either; +import net.md_5.bungee.protocol.NoOrigDeserializable; import net.md_5.bungee.protocol.ProtocolConstants; +import se.llbit.nbt.SpecificTag; @Data @NoArgsConstructor @@ -15,18 +19,18 @@ public class Subtitle extends DefinedPacket { - private BaseComponent text; + private Deserializable, BaseComponent> textRaw; @Override public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { - text = readBaseComponent( buf, protocolVersion ); + textRaw = readBaseComponent( buf, protocolVersion ); } @Override public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { - writeBaseComponent( text, buf, protocolVersion ); + writeBaseComponent( textRaw, buf, protocolVersion ); } @Override @@ -34,4 +38,28 @@ public void handle(AbstractPacketHandler handler) throws Exception { handler.handle( this ); } + + public Subtitle(BaseComponent text) + { + setText( text ); + } + + public BaseComponent getText() + { + if ( textRaw == null ) + { + return null; + } + return textRaw.get(); + } + + public void setText(BaseComponent text) + { + if ( text == null ) + { + this.textRaw = null; + return; + } + this.textRaw = new NoOrigDeserializable<>( text ); + } } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/SystemChat.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/SystemChat.java index 7bad3d2b990..caf2aafa1af 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/SystemChat.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/SystemChat.java @@ -9,7 +9,11 @@ import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; +import net.md_5.bungee.protocol.Deserializable; +import net.md_5.bungee.protocol.Either; +import net.md_5.bungee.protocol.NoOrigDeserializable; import net.md_5.bungee.protocol.ProtocolConstants; +import se.llbit.nbt.SpecificTag; @Data @NoArgsConstructor @@ -18,20 +22,20 @@ public class SystemChat extends DefinedPacket { - private BaseComponent message; + private Deserializable, BaseComponent> messageRaw; private int position; @Override public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { - message = readBaseComponent( buf, 262144, protocolVersion ); + messageRaw = readBaseComponent( buf, 262144, protocolVersion ); position = ( protocolVersion >= ProtocolConstants.MINECRAFT_1_19_1 ) ? ( ( buf.readBoolean() ) ? ChatMessageType.ACTION_BAR.ordinal() : 0 ) : readVarInt( buf ); } @Override public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { - writeBaseComponent( message, buf, protocolVersion ); + writeBaseComponent( messageRaw, buf, protocolVersion ); if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_19_1 ) { buf.writeBoolean( position == ChatMessageType.ACTION_BAR.ordinal() ); @@ -46,4 +50,30 @@ public void handle(AbstractPacketHandler handler) throws Exception { handler.handle( this ); } + + public SystemChat(BaseComponent message, int position) + { + this.messageRaw = new NoOrigDeserializable<>( message ); + this.position = position; + } + + public BaseComponent getMessage() + { + if ( messageRaw == null ) + { + return null; + } + return messageRaw.get(); + } + + public void setMessage(BaseComponent message) + { + if ( message == null ) + { + this.messageRaw = null; + return; + } + this.messageRaw = new NoOrigDeserializable<>( message ); + } + } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/TabCompleteResponse.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/TabCompleteResponse.java index 2ea141bc7b5..66bbef3416e 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/TabCompleteResponse.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/TabCompleteResponse.java @@ -13,7 +13,10 @@ import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; +import net.md_5.bungee.protocol.Deserializable; +import net.md_5.bungee.protocol.Either; import net.md_5.bungee.protocol.ProtocolConstants; +import se.llbit.nbt.SpecificTag; @Data @NoArgsConstructor @@ -52,7 +55,7 @@ public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protoco for ( int i = 0; i < cnt; i++ ) { String match = readString( buf ); - BaseComponent tooltip = buf.readBoolean() ? readBaseComponent( buf, protocolVersion ) : null; + Deserializable, BaseComponent> tooltip = buf.readBoolean() ? readBaseComponent( buf, protocolVersion ) : null; matches.add( new Suggestion( range, match, ( tooltip != null ) ? new ComponentMessage( tooltip ) : null ) ); } @@ -80,7 +83,7 @@ public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protoc buf.writeBoolean( suggestion.getTooltip() != null ); if ( suggestion.getTooltip() != null ) { - writeBaseComponent( ( (ComponentMessage) suggestion.getTooltip() ).getComponent(), buf, protocolVersion ); + writeBaseComponent( ( (ComponentMessage) suggestion.getTooltip() ).getComponentRaw(), buf, protocolVersion ); } } } else @@ -99,12 +102,21 @@ public void handle(AbstractPacketHandler handler) throws Exception private static class ComponentMessage implements Message { - private final BaseComponent component; + private final Deserializable, BaseComponent> componentRaw; + + public BaseComponent getComponent() + { + if ( componentRaw == null ) + { + return null; + } + return componentRaw.get(); + } @Override public String getString() { - return component.toPlainText(); + return getComponent().toPlainText(); } } } diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Team.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Team.java index 7a14e6a65a1..0cbc4898745 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Team.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Team.java @@ -8,8 +8,11 @@ import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; +import net.md_5.bungee.protocol.Deserializable; import net.md_5.bungee.protocol.Either; +import net.md_5.bungee.protocol.NoOrigDeserializable; import net.md_5.bungee.protocol.ProtocolConstants; +import se.llbit.nbt.SpecificTag; @Data @NoArgsConstructor @@ -23,9 +26,9 @@ public class Team extends DefinedPacket * 0 - create, 1 remove, 2 info update, 3 player add, 4 player remove. */ private byte mode; - private Either displayName; - private Either prefix; - private Either suffix; + private Either, BaseComponent>> displayNameRaw; + private Either, BaseComponent>> prefixRaw; + private Either, BaseComponent>> suffixRaw; private String nameTagVisibility; private String collisionRule; private int color; @@ -52,12 +55,12 @@ public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protoco { if ( protocolVersion < ProtocolConstants.MINECRAFT_1_13 ) { - displayName = readEitherBaseComponent( buf, protocolVersion, true ); - prefix = readEitherBaseComponent( buf, protocolVersion, true ); - suffix = readEitherBaseComponent( buf, protocolVersion, true ); + displayNameRaw = readEitherBaseComponent( buf, protocolVersion, true ); + prefixRaw = readEitherBaseComponent( buf, protocolVersion, true ); + suffixRaw = readEitherBaseComponent( buf, protocolVersion, true ); } else { - displayName = readEitherBaseComponent( buf, protocolVersion, false ); + displayNameRaw = readEitherBaseComponent( buf, protocolVersion, false ); } friendlyFire = buf.readByte(); nameTagVisibility = readString( buf ); @@ -68,8 +71,8 @@ public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protoco color = ( protocolVersion >= ProtocolConstants.MINECRAFT_1_13 ) ? readVarInt( buf ) : buf.readByte(); if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_13 ) { - prefix = readEitherBaseComponent( buf, protocolVersion, false ); - suffix = readEitherBaseComponent( buf, protocolVersion, false ); + prefixRaw = readEitherBaseComponent( buf, protocolVersion, false ); + suffixRaw = readEitherBaseComponent( buf, protocolVersion, false ); } } if ( mode == 0 || mode == 3 || mode == 4 ) @@ -90,11 +93,11 @@ public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protoc buf.writeByte( mode ); if ( mode == 0 || mode == 2 ) { - writeEitherBaseComponent( displayName, buf, protocolVersion ); + writeEitherBaseComponent( displayNameRaw, buf, protocolVersion ); if ( protocolVersion < ProtocolConstants.MINECRAFT_1_13 ) { - writeEitherBaseComponent( prefix, buf, protocolVersion ); - writeEitherBaseComponent( suffix, buf, protocolVersion ); + writeEitherBaseComponent( prefixRaw, buf, protocolVersion ); + writeEitherBaseComponent( suffixRaw, buf, protocolVersion ); } buf.writeByte( friendlyFire ); writeString( nameTagVisibility, buf ); @@ -106,8 +109,8 @@ public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protoc if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_13 ) { writeVarInt( color, buf ); - writeEitherBaseComponent( prefix, buf, protocolVersion ); - writeEitherBaseComponent( suffix, buf, protocolVersion ); + writeEitherBaseComponent( prefixRaw, buf, protocolVersion ); + writeEitherBaseComponent( suffixRaw, buf, protocolVersion ); } else { buf.writeByte( color ); @@ -123,6 +126,105 @@ public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protoc } } + @SuppressWarnings("unchecked") + public Either getDisplayName() + { + if ( displayNameRaw == null ) + { + return null; + } + if ( displayNameRaw.isLeft() ) + { + return (Either) displayNameRaw; + } else + { + return Either.right( displayNameRaw.getRight().get() ); + } + } + + @SuppressWarnings("unchecked") + public void setDisplayName(Either displayName) + { + if ( displayName == null ) + { + displayNameRaw = null; + return; + } + if ( displayName.isLeft() ) + { + displayNameRaw = (Either) displayName; + } else + { + displayNameRaw = Either.right( new NoOrigDeserializable<>( displayName.getRight() ) ); + } + } + + @SuppressWarnings("unchecked") + public Either getPrefix() + { + if ( prefixRaw == null ) + { + return null; + } + if ( prefixRaw.isLeft() ) + { + return (Either) prefixRaw; + } else + { + return Either.right( prefixRaw.getRight().get() ); + } + } + + @SuppressWarnings("unchecked") + public void setPrefix(Either prefix) + { + if ( prefix == null ) + { + prefixRaw = null; + return; + } + if ( prefix.isLeft() ) + { + prefixRaw = (Either) prefix; + } else + { + prefixRaw = Either.right( new NoOrigDeserializable<>( prefix.getRight() ) ); + } + } + + @SuppressWarnings("unchecked") + public Either getSuffix() + { + if ( suffixRaw == null ) + { + return null; + } + if ( suffixRaw.isLeft() ) + { + return (Either) suffixRaw; + } else + { + return Either.right( suffixRaw.getRight().get() ); + } + } + + @SuppressWarnings("unchecked") + public void setSuffix(Either suffix) + { + if ( suffix == null ) + { + suffixRaw = null; + return; + } + if ( suffix.isLeft() ) + { + suffixRaw = (Either) suffix; + } else + { + suffixRaw = Either.right( new NoOrigDeserializable<>( suffix.getRight() ) ); + } + } + @Override public void handle(AbstractPacketHandler handler) throws Exception { diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Title.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Title.java index 125cbeb772c..fad5e2b9ca9 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Title.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Title.java @@ -7,7 +7,11 @@ import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; +import net.md_5.bungee.protocol.Deserializable; +import net.md_5.bungee.protocol.Either; +import net.md_5.bungee.protocol.NoOrigDeserializable; import net.md_5.bungee.protocol.ProtocolConstants; +import se.llbit.nbt.SpecificTag; @Data @NoArgsConstructor @@ -18,7 +22,7 @@ public class Title extends DefinedPacket private Action action; // TITLE & SUBTITLE - private BaseComponent text; + private Deserializable, BaseComponent> textRaw; // TIMES private int fadeIn; @@ -35,7 +39,7 @@ public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protoco { if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_17 ) { - text = readBaseComponent( buf, protocolVersion ); + textRaw = readBaseComponent( buf, protocolVersion ); return; } @@ -53,7 +57,7 @@ public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protoco case TITLE: case SUBTITLE: case ACTIONBAR: - text = readBaseComponent( buf, protocolVersion ); + textRaw = readBaseComponent( buf, protocolVersion ); break; case TIMES: fadeIn = buf.readInt(); @@ -68,7 +72,7 @@ public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protoc { if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_17 ) { - writeBaseComponent( text, buf, protocolVersion ); + writeBaseComponent( textRaw, buf, protocolVersion ); return; } @@ -86,7 +90,7 @@ public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protoc case TITLE: case SUBTITLE: case ACTIONBAR: - writeBaseComponent( text, buf, protocolVersion ); + writeBaseComponent( textRaw, buf, protocolVersion ); break; case TIMES: buf.writeInt( fadeIn ); @@ -112,4 +116,32 @@ public static enum Action CLEAR, RESET } + + public Title(Action action, BaseComponent text, int fadeIn, int stay, int fadeOut) + { + setText( text ); + this.fadeIn = fadeIn; + this.stay = stay; + this.fadeOut = fadeOut; + this.action = action; + } + + public BaseComponent getText() + { + if ( textRaw == null ) + { + return null; + } + return textRaw.get(); + } + + public void setText(BaseComponent text) + { + if ( text == null ) + { + this.textRaw = null; + return; + } + this.textRaw = new NoOrigDeserializable<>( text ); + } } diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java index abdbfd1dccd..61f196f9bb5 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java @@ -304,7 +304,7 @@ public static void handleLogin(ProxyServer bungee, ChannelWrapper ch, UserConnec user.unsafe().sendPacket( new ScoreboardScoreReset( score.getItemName(), null ) ); } else { - user.unsafe().sendPacket( new ScoreboardScore( score.getItemName(), (byte) 1, score.getScoreName(), score.getValue(), null, null ) ); + user.unsafe().sendPacket( new ScoreboardScore( score.getItemName(), (byte) 1, score.getScoreName(), score.getValue(), (BaseComponent) null, null ) ); } } for ( Team team : serverScoreboard.getTeams() ) diff --git a/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java index 4684bfd8d4c..057ae542851 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java @@ -267,9 +267,9 @@ public void handle(net.md_5.bungee.protocol.packet.Team team) throws Exception { if ( team.getMode() == 0 || team.getMode() == 2 ) { - t.setDisplayName( team.getDisplayName().getLeftOrCompute( ComponentSerializer::toString ) ); - t.setPrefix( team.getPrefix().getLeftOrCompute( ComponentSerializer::toString ) ); - t.setSuffix( team.getSuffix().getLeftOrCompute( ComponentSerializer::toString ) ); + t.setDisplayName( team.getDisplayNameRaw().getLeftOrCompute( d -> ComponentSerializer.toString( d.get() ) ) ); + t.setPrefix( team.getPrefixRaw().getLeftOrCompute( d -> ComponentSerializer.toString( d.get() ) ) ); + t.setSuffix( team.getSuffixRaw().getLeftOrCompute( d -> ComponentSerializer.toString( d.get() ) ) ); t.setFriendlyFire( team.getFriendlyFire() ); t.setNameTagVisibility( team.getNameTagVisibility() ); t.setCollisionRule( team.getCollisionRule() );