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 6cd9e6a425..1c82495ab9 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; @@ -21,6 +22,16 @@ 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.protocol.data.NumberFormat; +import net.md_5.bungee.protocol.data.PlayerPublicKey; +import net.md_5.bungee.protocol.data.Property; +import net.md_5.bungee.protocol.util.ChatDeserializable; +import net.md_5.bungee.protocol.util.ChatDeserializableJson; +import net.md_5.bungee.protocol.util.ChatDeserializableTag; +import net.md_5.bungee.protocol.util.ChatNewDeserializable; +import net.md_5.bungee.protocol.util.ChatNewDeserializableTag; +import net.md_5.bungee.protocol.util.Either; +import net.md_5.bungee.protocol.util.TagUtil; import se.llbit.nbt.ErrorTag; import se.llbit.nbt.NamedTag; import se.llbit.nbt.SpecificTag; @@ -93,17 +104,33 @@ public static String readString(ByteBuf buf, int maxLen) return s; } - public static Either readEitherBaseComponent(ByteBuf buf, int protocolVersion, boolean string) + public static Either readEitherBaseComponent(ByteBuf buf, int protocolVersion, boolean string) { - return ( string ) ? Either.left( readString( buf ) ) : Either.right( readBaseComponent( buf, protocolVersion ) ); + return string ? Either.left( readString( buf ) ) : Either.right( readBaseComponent( buf, protocolVersion ) ); } - public static BaseComponent readBaseComponent(ByteBuf buf, int protocolVersion) + public static ChatDeserializable readBaseComponent(ByteBuf buf, int protocolVersion) { return readBaseComponent( buf, Short.MAX_VALUE, protocolVersion ); } - public static BaseComponent readBaseComponent(ByteBuf buf, int maxStringLength, int protocolVersion) + public static ChatDeserializable readBaseComponent(ByteBuf buf, int maxStringLength, int protocolVersion) + { + if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_3 ) + { + return new ChatDeserializableTag( (SpecificTag) readTag( buf, protocolVersion ) ); + } else + { + return new ChatDeserializableJson( readString( buf, maxStringLength ) ); + } + } + + 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 ) { @@ -119,6 +146,13 @@ public static BaseComponent readBaseComponent(ByteBuf buf, int maxStringLength, } } + public static ChatNewDeserializable readNewBaseComponent(ByteBuf buf, int protocolVersion) + { + SpecificTag nbt = (SpecificTag) readTag( buf, protocolVersion ); + + return new ChatNewDeserializableTag( nbt ); + } + public static ComponentStyle readComponentStyle(ByteBuf buf, int protocolVersion) { SpecificTag nbt = (SpecificTag) readTag( buf, protocolVersion ); @@ -127,7 +161,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 message, ByteBuf buf, int protocolVersion) { if ( message.isLeft() ) { @@ -138,7 +172,52 @@ public static void writeEitherBaseComponent(Either messag } } - public static void writeBaseComponent(BaseComponent message, ByteBuf buf, int protocolVersion) + public static void writeBaseComponent(ChatDeserializable 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 ) { @@ -154,6 +233,19 @@ public static void writeBaseComponent(BaseComponent message, ByteBuf buf, int pr } } + public static void writeNewBaseComponent(ChatNewDeserializable message, ByteBuf buf, int protocolVersion) + { + SpecificTag nbt; + if ( message.hasDeserialized() ) + { + nbt = TagUtil.fromJson( ComponentSerializer.toJson( message.get() ) ); + } else + { + nbt = message.original(); + } + writeTag( nbt, buf, protocolVersion ); + } + public static void writeComponentStyle(ComponentStyle style, ByteBuf buf, int protocolVersion) { JsonElement json = ComponentSerializer.toJson( style ); @@ -398,7 +490,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 +505,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/ChatChain.java b/protocol/src/main/java/net/md_5/bungee/protocol/data/ChatChain.java similarity index 91% rename from protocol/src/main/java/net/md_5/bungee/protocol/ChatChain.java rename to protocol/src/main/java/net/md_5/bungee/protocol/data/ChatChain.java index 7cc79e4f73..0faf74298b 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/ChatChain.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/data/ChatChain.java @@ -1,4 +1,4 @@ -package net.md_5.bungee.protocol; +package net.md_5.bungee.protocol.data; import com.google.common.base.Preconditions; import io.netty.buffer.ByteBuf; @@ -9,6 +9,9 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; +import net.md_5.bungee.protocol.AbstractPacketHandler; +import net.md_5.bungee.protocol.DefinedPacket; +import net.md_5.bungee.protocol.ProtocolConstants; @Data @NoArgsConstructor diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/Location.java b/protocol/src/main/java/net/md_5/bungee/protocol/data/Location.java similarity index 75% rename from protocol/src/main/java/net/md_5/bungee/protocol/Location.java rename to protocol/src/main/java/net/md_5/bungee/protocol/data/Location.java index e6a9f894f6..61648026bd 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/Location.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/data/Location.java @@ -1,4 +1,4 @@ -package net.md_5.bungee.protocol; +package net.md_5.bungee.protocol.data; import lombok.Data; diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/NumberFormat.java b/protocol/src/main/java/net/md_5/bungee/protocol/data/NumberFormat.java similarity index 83% rename from protocol/src/main/java/net/md_5/bungee/protocol/NumberFormat.java rename to protocol/src/main/java/net/md_5/bungee/protocol/data/NumberFormat.java index d1cefe3bbc..1c4bb9eb80 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/NumberFormat.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/data/NumberFormat.java @@ -1,4 +1,4 @@ -package net.md_5.bungee.protocol; +package net.md_5.bungee.protocol.data; import lombok.Data; diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/PlayerPublicKey.java b/protocol/src/main/java/net/md_5/bungee/protocol/data/PlayerPublicKey.java similarity index 80% rename from protocol/src/main/java/net/md_5/bungee/protocol/PlayerPublicKey.java rename to protocol/src/main/java/net/md_5/bungee/protocol/data/PlayerPublicKey.java index 7f2cc79866..c06c9310b6 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/PlayerPublicKey.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/data/PlayerPublicKey.java @@ -1,4 +1,4 @@ -package net.md_5.bungee.protocol; +package net.md_5.bungee.protocol.data; import lombok.Data; diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/Property.java b/protocol/src/main/java/net/md_5/bungee/protocol/data/Property.java similarity index 87% rename from protocol/src/main/java/net/md_5/bungee/protocol/Property.java rename to protocol/src/main/java/net/md_5/bungee/protocol/data/Property.java index 2e0b1d73a1..c786aa2b4b 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/Property.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/data/Property.java @@ -1,4 +1,4 @@ -package net.md_5.bungee.protocol; +package net.md_5.bungee.protocol.data; import lombok.AllArgsConstructor; import lombok.Data; diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/SeenMessages.java b/protocol/src/main/java/net/md_5/bungee/protocol/data/SeenMessages.java similarity index 84% rename from protocol/src/main/java/net/md_5/bungee/protocol/SeenMessages.java rename to protocol/src/main/java/net/md_5/bungee/protocol/data/SeenMessages.java index 9f0b452a46..80332e5372 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/SeenMessages.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/data/SeenMessages.java @@ -1,4 +1,4 @@ -package net.md_5.bungee.protocol; +package net.md_5.bungee.protocol.data; import io.netty.buffer.ByteBuf; import java.util.BitSet; @@ -6,6 +6,9 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; +import net.md_5.bungee.protocol.AbstractPacketHandler; +import net.md_5.bungee.protocol.DefinedPacket; +import net.md_5.bungee.protocol.ProtocolConstants; @Data @NoArgsConstructor 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 991a1ab9e9..157c7ccd1d 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 @@ -5,10 +5,10 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -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.ProtocolConstants; +import net.md_5.bungee.protocol.util.ChatDeserializable; @Data @NoArgsConstructor @@ -18,7 +18,7 @@ public class BossBar extends DefinedPacket private UUID uuid; private int action; - private BaseComponent title; + private ChatDeserializable title; private float health; private int color; private int division; diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/ClientChat.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/ClientChat.java index 105c8276f4..2e5114d1ef 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/ClientChat.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/ClientChat.java @@ -6,10 +6,10 @@ import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import net.md_5.bungee.protocol.AbstractPacketHandler; -import net.md_5.bungee.protocol.ChatChain; import net.md_5.bungee.protocol.DefinedPacket; import net.md_5.bungee.protocol.ProtocolConstants; -import net.md_5.bungee.protocol.SeenMessages; +import net.md_5.bungee.protocol.data.ChatChain; +import net.md_5.bungee.protocol.data.SeenMessages; @Data @NoArgsConstructor diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/ClientCommand.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/ClientCommand.java index 887ff29f22..edae1bf88c 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/ClientCommand.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/ClientCommand.java @@ -9,10 +9,10 @@ import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import net.md_5.bungee.protocol.AbstractPacketHandler; -import net.md_5.bungee.protocol.ChatChain; import net.md_5.bungee.protocol.DefinedPacket; import net.md_5.bungee.protocol.ProtocolConstants; -import net.md_5.bungee.protocol.SeenMessages; +import net.md_5.bungee.protocol.data.ChatChain; +import net.md_5.bungee.protocol.data.SeenMessages; @Data @NoArgsConstructor 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 14539b4b87..7d1303189e 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 @@ -5,12 +5,13 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -import net.md_5.bungee.api.chat.BaseComponent; 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.Protocol; import net.md_5.bungee.protocol.ProtocolConstants; +import net.md_5.bungee.protocol.util.ChatDeserializable; +import net.md_5.bungee.protocol.util.ChatDeserializableJson; @Data @NoArgsConstructor @@ -19,14 +20,14 @@ public class Kick extends DefinedPacket { - private BaseComponent message; + private ChatDeserializable message; @Override public void read(ByteBuf buf, Protocol protocol, ProtocolConstants.Direction direction, int protocolVersion) { if ( protocol == Protocol.LOGIN ) { - message = ComponentSerializer.deserialize( readString( buf ) ); + message = new ChatDeserializableJson( readString( buf ) ); } else { message = readBaseComponent( buf, protocolVersion ); @@ -38,7 +39,7 @@ public void write(ByteBuf buf, Protocol protocol, ProtocolConstants.Direction di { if ( protocol == Protocol.LOGIN ) { - writeString( ComponentSerializer.toString( message ), buf ); + writeString( ComponentSerializer.toString( message.get() ), buf ); } else { writeBaseComponent( message, buf, protocolVersion ); diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Login.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Login.java index a4e804e14c..20520e2e4f 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Login.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Login.java @@ -9,8 +9,8 @@ import lombok.NoArgsConstructor; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; -import net.md_5.bungee.protocol.Location; import net.md_5.bungee.protocol.ProtocolConstants; +import net.md_5.bungee.protocol.data.Location; import se.llbit.nbt.Tag; @Data diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/LoginRequest.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/LoginRequest.java index e62a3a03e9..d9cadc25d9 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/LoginRequest.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/LoginRequest.java @@ -8,8 +8,8 @@ import lombok.NoArgsConstructor; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; -import net.md_5.bungee.protocol.PlayerPublicKey; import net.md_5.bungee.protocol.ProtocolConstants; +import net.md_5.bungee.protocol.data.PlayerPublicKey; @Data @NoArgsConstructor diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/LoginSuccess.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/LoginSuccess.java index 60ff5700c9..0ae0ef6e37 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/LoginSuccess.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/LoginSuccess.java @@ -8,8 +8,8 @@ import lombok.NoArgsConstructor; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; -import net.md_5.bungee.protocol.Property; import net.md_5.bungee.protocol.ProtocolConstants; +import net.md_5.bungee.protocol.data.Property; @Data @NoArgsConstructor 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 98dbae4a90..268eecc8ff 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 @@ -5,10 +5,10 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -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.ProtocolConstants; +import net.md_5.bungee.protocol.util.ChatDeserializable; @Data @NoArgsConstructor @@ -17,8 +17,8 @@ public class PlayerListHeaderFooter extends DefinedPacket { - private BaseComponent header; - private BaseComponent footer; + private ChatDeserializable header; + private ChatDeserializable footer; @Override public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) 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 34a12a80ed..b6d72f0649 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 @@ -5,12 +5,12 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -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.PlayerPublicKey; -import net.md_5.bungee.protocol.Property; import net.md_5.bungee.protocol.ProtocolConstants; +import net.md_5.bungee.protocol.data.PlayerPublicKey; +import net.md_5.bungee.protocol.data.Property; +import net.md_5.bungee.protocol.util.ChatDeserializable; @Data @NoArgsConstructor @@ -143,7 +143,6 @@ public static class Item Integer ping; // ADD_PLAYER & UPDATE_DISPLAY_NAME - BaseComponent displayName; - + ChatDeserializable 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 f6708b062f..60d3a85b4f 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 @@ -7,8 +7,8 @@ import lombok.NoArgsConstructor; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; -import net.md_5.bungee.protocol.PlayerPublicKey; import net.md_5.bungee.protocol.ProtocolConstants; +import net.md_5.bungee.protocol.data.PlayerPublicKey; import net.md_5.bungee.protocol.packet.PlayerListItem.Item; @Data diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Respawn.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Respawn.java index 46859e633e..704327f299 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/Respawn.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/Respawn.java @@ -7,8 +7,8 @@ import lombok.NoArgsConstructor; import net.md_5.bungee.protocol.AbstractPacketHandler; import net.md_5.bungee.protocol.DefinedPacket; -import net.md_5.bungee.protocol.Location; import net.md_5.bungee.protocol.ProtocolConstants; +import net.md_5.bungee.protocol.data.Location; import se.llbit.nbt.Tag; @Data 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 a37617fd46..ac363f6358 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 @@ -6,12 +6,12 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -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.Either; -import net.md_5.bungee.protocol.NumberFormat; import net.md_5.bungee.protocol.ProtocolConstants; +import net.md_5.bungee.protocol.data.NumberFormat; +import net.md_5.bungee.protocol.util.ChatDeserializable; +import net.md_5.bungee.protocol.util.Either; @Data @NoArgsConstructor @@ -21,7 +21,7 @@ public class ScoreboardObjective extends DefinedPacket { private String name; - private Either value; + private Either value; private HealthDisplay type; /** * 0 to create, 1 to remove, 2 to update display text. 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 d77826a7a9..6f2bcd47c4 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 @@ -5,11 +5,11 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -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.NumberFormat; import net.md_5.bungee.protocol.ProtocolConstants; +import net.md_5.bungee.protocol.data.NumberFormat; +import net.md_5.bungee.protocol.util.ChatDeserializable; @Data @NoArgsConstructor @@ -25,7 +25,7 @@ public class ScoreboardScore extends DefinedPacket private byte action; private String scoreName; private int value; - private BaseComponent displayName; + private ChatDeserializable displayName; private NumberFormat numberFormat; @Override 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 f9f485e4ab..5ab4dbeac8 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 @@ -5,10 +5,10 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -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.ProtocolConstants; +import net.md_5.bungee.protocol.util.ChatDeserializable; @Data @NoArgsConstructor @@ -17,7 +17,7 @@ public class ServerData extends DefinedPacket { - private BaseComponent motd; + private ChatDeserializable motd; private Object icon; private boolean preview; private boolean enforceSecure; diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/packet/ServerLinks.java b/protocol/src/main/java/net/md_5/bungee/protocol/packet/ServerLinks.java index 5df25c24e6..a1b4b3fb03 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/packet/ServerLinks.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/packet/ServerLinks.java @@ -5,11 +5,11 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -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.Either; import net.md_5.bungee.protocol.ProtocolConstants; +import net.md_5.bungee.protocol.util.ChatNewDeserializable; +import net.md_5.bungee.protocol.util.Either; @Data @NoArgsConstructor @@ -27,13 +27,13 @@ public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protoco links = new Link[ len ]; for ( int i = 0; i < len; i++ ) { - Either type; + Either type; if ( buf.readBoolean() ) { type = Either.left( LinkType.values()[readVarInt( buf )] ); } else { - type = Either.right( readBaseComponent( buf, protocolVersion ) ); + type = Either.right( readNewBaseComponent( buf, protocolVersion ) ); } String url = readString( buf ); @@ -47,7 +47,7 @@ public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protoc writeVarInt( links.length, buf ); for ( Link link : links ) { - Either type = link.getType(); + Either type = link.getType(); if ( type.isLeft() ) { buf.writeBoolean( true ); @@ -55,7 +55,7 @@ public void write(ByteBuf buf, ProtocolConstants.Direction direction, int protoc } else { buf.writeBoolean( false ); - writeBaseComponent( type.getRight(), buf, protocolVersion ); + writeNewBaseComponent( type.getRight(), buf, protocolVersion ); } writeString( link.getUrl(), buf ); } @@ -86,7 +86,7 @@ public enum LinkType public static class Link { - private final Either type; + private final Either type; private final String url; } } 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 69e0b84031..2a06c2ddf9 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 @@ -4,10 +4,10 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -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.ProtocolConstants; +import net.md_5.bungee.protocol.util.ChatDeserializable; @Data @NoArgsConstructor @@ -15,7 +15,7 @@ public class Subtitle extends DefinedPacket { - private BaseComponent text; + private ChatDeserializable text; @Override public void read(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) 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 7bad3d2b99..681db0206f 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 @@ -6,10 +6,10 @@ import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import net.md_5.bungee.api.ChatMessageType; -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.ProtocolConstants; +import net.md_5.bungee.protocol.util.ChatDeserializable; @Data @NoArgsConstructor @@ -18,7 +18,7 @@ public class SystemChat extends DefinedPacket { - private BaseComponent message; + private ChatDeserializable message; private int position; @Override 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 2ea141bc7b..8909a1b952 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 @@ -10,10 +10,10 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -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.ProtocolConstants; +import net.md_5.bungee.protocol.util.ChatDeserializable; @Data @NoArgsConstructor @@ -52,7 +52,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; + ChatDeserializable tooltip = buf.readBoolean() ? readBaseComponent( buf, protocolVersion ) : null; matches.add( new Suggestion( range, match, ( tooltip != null ) ? new ComponentMessage( tooltip ) : null ) ); } @@ -99,12 +99,12 @@ public void handle(AbstractPacketHandler handler) throws Exception private static class ComponentMessage implements Message { - private final BaseComponent component; + private final ChatDeserializable component; @Override public String getString() { - return component.toPlainText(); + return component.get().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 7a14e6a65a..077bc062b2 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 @@ -5,11 +5,11 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -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.Either; import net.md_5.bungee.protocol.ProtocolConstants; +import net.md_5.bungee.protocol.util.ChatDeserializable; +import net.md_5.bungee.protocol.util.Either; @Data @NoArgsConstructor @@ -23,9 +23,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 displayName; + private Either prefix; + private Either suffix; private String nameTagVisibility; private String collisionRule; private int color; 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 125cbeb772..dcb3a9dfb6 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 @@ -4,10 +4,10 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; -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.ProtocolConstants; +import net.md_5.bungee.protocol.util.ChatDeserializable; @Data @NoArgsConstructor @@ -18,7 +18,7 @@ public class Title extends DefinedPacket private Action action; // TITLE & SUBTITLE - private BaseComponent text; + private ChatDeserializable text; // TIMES private int fadeIn; diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatCapturingDeserializable.java b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatCapturingDeserializable.java new file mode 100644 index 0000000000..794b9e7d2f --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatCapturingDeserializable.java @@ -0,0 +1,48 @@ +package net.md_5.bungee.protocol.util; + +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import lombok.ToString; +import net.md_5.bungee.api.chat.BaseComponent; +import se.llbit.nbt.SpecificTag; + +@ToString +@EqualsAndHashCode +@RequiredArgsConstructor +public abstract class ChatCapturingDeserializable implements ChatDeserializable +{ + @NonNull + private final Either original; + private BaseComponent 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 BaseComponent deserialize(); + + @Override + public final BaseComponent get() + { + if ( !hasDeserialized() ) + { + return deserialized = deserialize(); + } + return deserialized; + } + + @Override + public final boolean hasDeserialized() + { + return deserialized != null; + } + + @Override + public final Either original() + { + return original; + } +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatComponentDeserializable.java b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatComponentDeserializable.java new file mode 100644 index 0000000000..864270eb2c --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatComponentDeserializable.java @@ -0,0 +1,33 @@ +package net.md_5.bungee.protocol.util; + +import lombok.EqualsAndHashCode; +import lombok.RequiredArgsConstructor; +import lombok.ToString; +import net.md_5.bungee.api.chat.BaseComponent; +import se.llbit.nbt.SpecificTag; + +@ToString +@EqualsAndHashCode +@RequiredArgsConstructor +public class ChatComponentDeserializable implements ChatDeserializable +{ + private final BaseComponent value; + + @Override + public BaseComponent get() + { + return value; + } + + @Override + public Either original() + { + return null; + } + + @Override + public boolean hasDeserialized() + { + return true; + } +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatDeserializable.java b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatDeserializable.java new file mode 100644 index 0000000000..9f7736004e --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatDeserializable.java @@ -0,0 +1,12 @@ +package net.md_5.bungee.protocol.util; + +import net.md_5.bungee.api.chat.BaseComponent; +import se.llbit.nbt.SpecificTag; + +public interface ChatDeserializable extends Deserializable, BaseComponent> +{ + default ChatNewDeserializable cloneAsNew() + { + return new ChatNewComponentDeserializable( get() ); + } +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatDeserializableJson.java b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatDeserializableJson.java new file mode 100644 index 0000000000..4dbe501694 --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatDeserializableJson.java @@ -0,0 +1,26 @@ +package net.md_5.bungee.protocol.util; + +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import lombok.ToString; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.chat.ComponentSerializer; +import org.jetbrains.annotations.NotNull; + +@ToString +@EqualsAndHashCode(callSuper = true) +public class ChatDeserializableJson extends ChatCapturingDeserializable +{ + + public ChatDeserializableJson(@NonNull String chatJson) + { + super( Either.left( chatJson ) ); + } + + @NotNull + @Override + public BaseComponent deserialize() + { + return ComponentSerializer.deserialize( original().getLeft() ); + } +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatDeserializableTag.java b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatDeserializableTag.java new file mode 100644 index 0000000000..108095ef34 --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatDeserializableTag.java @@ -0,0 +1,27 @@ +package net.md_5.bungee.protocol.util; + +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import lombok.ToString; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.chat.ComponentSerializer; +import org.jetbrains.annotations.NotNull; +import se.llbit.nbt.SpecificTag; + +@ToString +@EqualsAndHashCode(callSuper = true) +public class ChatDeserializableTag extends ChatCapturingDeserializable +{ + + public ChatDeserializableTag(@NonNull SpecificTag chatTag) + { + super( Either.right( chatTag ) ); + } + + @NotNull + @Override + public BaseComponent deserialize() + { + return ComponentSerializer.deserialize( TagUtil.toJson( original().getRight() ) ); + } +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatNewCapturingDeserializable.java b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatNewCapturingDeserializable.java new file mode 100644 index 0000000000..e3af37b4cb --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatNewCapturingDeserializable.java @@ -0,0 +1,49 @@ +package net.md_5.bungee.protocol.util; + +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import lombok.ToString; +import net.md_5.bungee.api.chat.BaseComponent; +import se.llbit.nbt.SpecificTag; + +@ToString +@EqualsAndHashCode +@RequiredArgsConstructor +public abstract class ChatNewCapturingDeserializable implements ChatNewDeserializable +{ + @NonNull + private final SpecificTag original; + private BaseComponent 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 BaseComponent deserialize(); + + @Override + public final BaseComponent get() + { + if ( !hasDeserialized() ) + { + return deserialized = deserialize(); + } + return deserialized; + } + + @Override + public final boolean hasDeserialized() + { + return deserialized != null; + } + + @Override + public final SpecificTag original() + { + return original; + } +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatNewComponentDeserializable.java b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatNewComponentDeserializable.java new file mode 100644 index 0000000000..2420b74493 --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatNewComponentDeserializable.java @@ -0,0 +1,33 @@ +package net.md_5.bungee.protocol.util; + +import lombok.EqualsAndHashCode; +import lombok.RequiredArgsConstructor; +import lombok.ToString; +import net.md_5.bungee.api.chat.BaseComponent; +import se.llbit.nbt.SpecificTag; + +@ToString +@EqualsAndHashCode +@RequiredArgsConstructor +public class ChatNewComponentDeserializable implements ChatNewDeserializable +{ + private final BaseComponent value; + + @Override + public BaseComponent get() + { + return value; + } + + @Override + public SpecificTag original() + { + return null; + } + + @Override + public boolean hasDeserialized() + { + return true; + } +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatNewDeserializable.java b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatNewDeserializable.java new file mode 100644 index 0000000000..4059f38d16 --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatNewDeserializable.java @@ -0,0 +1,8 @@ +package net.md_5.bungee.protocol.util; + +import net.md_5.bungee.api.chat.BaseComponent; +import se.llbit.nbt.SpecificTag; + +public interface ChatNewDeserializable extends Deserializable +{ +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatNewDeserializableTag.java b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatNewDeserializableTag.java new file mode 100644 index 0000000000..4316a58217 --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatNewDeserializableTag.java @@ -0,0 +1,27 @@ +package net.md_5.bungee.protocol.util; + +import lombok.EqualsAndHashCode; +import lombok.NonNull; +import lombok.ToString; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.chat.ComponentSerializer; +import org.jetbrains.annotations.NotNull; +import se.llbit.nbt.SpecificTag; + +@ToString +@EqualsAndHashCode(callSuper = true) +public class ChatNewDeserializableTag extends ChatNewCapturingDeserializable +{ + + public ChatNewDeserializableTag(@NonNull SpecificTag chatTag) + { + super( chatTag ); + } + + @NotNull + @Override + public BaseComponent deserialize() + { + return ComponentSerializer.deserialize( TagUtil.toJson( original() ) ); + } +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatNewOldAdapter.java b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatNewOldAdapter.java new file mode 100644 index 0000000000..c27d3f070b --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/util/ChatNewOldAdapter.java @@ -0,0 +1,33 @@ +package net.md_5.bungee.protocol.util; + +import net.md_5.bungee.api.chat.BaseComponent; +import se.llbit.nbt.SpecificTag; + +class ChatNewOldAdapter implements ChatDeserializable +{ + + private final ChatNewDeserializable newDeserializable; + + public ChatNewOldAdapter(ChatNewDeserializable newDeserializable) + { + this.newDeserializable = newDeserializable; + } + + @Override + public BaseComponent get() + { + return newDeserializable.get(); + } + + @Override + public Either original() + { + return Either.right( newDeserializable.original() ); + } + + @Override + public boolean hasDeserialized() + { + return newDeserializable.hasDeserialized(); + } +} diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/util/Deserializable.java b/protocol/src/main/java/net/md_5/bungee/protocol/util/Deserializable.java new file mode 100644 index 0000000000..79a2b1f54e --- /dev/null +++ b/protocol/src/main/java/net/md_5/bungee/protocol/util/Deserializable.java @@ -0,0 +1,28 @@ +package net.md_5.bungee.protocol.util; + +/** + * 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/Either.java b/protocol/src/main/java/net/md_5/bungee/protocol/util/Either.java similarity index 96% rename from protocol/src/main/java/net/md_5/bungee/protocol/Either.java rename to protocol/src/main/java/net/md_5/bungee/protocol/util/Either.java index c994c1345c..09f27ea236 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/Either.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/util/Either.java @@ -1,4 +1,4 @@ -package net.md_5.bungee.protocol; +package net.md_5.bungee.protocol.util; import java.util.function.Function; import lombok.AccessLevel; diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/TagUtil.java b/protocol/src/main/java/net/md_5/bungee/protocol/util/TagUtil.java similarity index 99% rename from protocol/src/main/java/net/md_5/bungee/protocol/TagUtil.java rename to protocol/src/main/java/net/md_5/bungee/protocol/util/TagUtil.java index 4be18dfa79..ff8a34b8c1 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/TagUtil.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/util/TagUtil.java @@ -1,4 +1,4 @@ -package net.md_5.bungee.protocol; +package net.md_5.bungee.protocol.util; import com.google.gson.JsonArray; import com.google.gson.JsonElement; diff --git a/protocol/src/test/java/net/md_5/bungee/protocol/TagUtilTest.java b/protocol/src/test/java/net/md_5/bungee/protocol/TagUtilTest.java index f0beb33c0c..5a12a46363 100644 --- a/protocol/src/test/java/net/md_5/bungee/protocol/TagUtilTest.java +++ b/protocol/src/test/java/net/md_5/bungee/protocol/TagUtilTest.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.*; import com.google.gson.Gson; import com.google.gson.JsonElement; +import net.md_5.bungee.protocol.util.TagUtil; import org.junit.jupiter.api.Test; import se.llbit.nbt.SpecificTag; diff --git a/proxy/src/main/java/net/md_5/bungee/BungeeTitle.java b/proxy/src/main/java/net/md_5/bungee/BungeeTitle.java index 9a6ef7a5fd..a9e3783cc3 100644 --- a/proxy/src/main/java/net/md_5/bungee/BungeeTitle.java +++ b/proxy/src/main/java/net/md_5/bungee/BungeeTitle.java @@ -11,6 +11,7 @@ import net.md_5.bungee.protocol.packet.Subtitle; import net.md_5.bungee.protocol.packet.Title.Action; import net.md_5.bungee.protocol.packet.TitleTimes; +import net.md_5.bungee.protocol.util.ChatComponentDeserializable; public class BungeeTitle implements Title { @@ -53,7 +54,7 @@ public Title title(BaseComponent text) title = new TitlePacketHolder<>( packet, packet ); } - title.oldPacket.setText( text ); // = newPacket + title.oldPacket.setText( new ChatComponentDeserializable( text ) ); // = newPacket return this; } @@ -71,8 +72,8 @@ public Title subTitle(BaseComponent text) subtitle = new TitlePacketHolder<>( new net.md_5.bungee.protocol.packet.Title( Action.SUBTITLE ), new Subtitle() ); } - subtitle.oldPacket.setText( text ); - subtitle.newPacket.setText( text ); + subtitle.oldPacket.setText( new ChatComponentDeserializable( text ) ); + subtitle.newPacket.setText( new ChatComponentDeserializable( text ) ); return this; } diff --git a/proxy/src/main/java/net/md_5/bungee/EncryptionUtil.java b/proxy/src/main/java/net/md_5/bungee/EncryptionUtil.java index fb497ca0cd..8a620f6ac0 100644 --- a/proxy/src/main/java/net/md_5/bungee/EncryptionUtil.java +++ b/proxy/src/main/java/net/md_5/bungee/EncryptionUtil.java @@ -28,7 +28,7 @@ import net.md_5.bungee.jni.cipher.BungeeCipher; import net.md_5.bungee.jni.cipher.JavaCipher; import net.md_5.bungee.jni.cipher.NativeCipher; -import net.md_5.bungee.protocol.PlayerPublicKey; +import net.md_5.bungee.protocol.data.PlayerPublicKey; import net.md_5.bungee.protocol.packet.EncryptionRequest; import net.md_5.bungee.protocol.packet.EncryptionResponse; 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 d735989127..22a96b0b86 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnector.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnector.java @@ -15,7 +15,6 @@ import lombok.RequiredArgsConstructor; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ProxyServer; -import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.event.ServerConnectEvent; import net.md_5.bungee.api.event.ServerConnectedEvent; @@ -36,7 +35,6 @@ import net.md_5.bungee.netty.HandlerBoss; import net.md_5.bungee.netty.PacketHandler; import net.md_5.bungee.protocol.DefinedPacket; -import net.md_5.bungee.protocol.Either; import net.md_5.bungee.protocol.PacketWrapper; import net.md_5.bungee.protocol.Protocol; import net.md_5.bungee.protocol.ProtocolConstants; @@ -60,6 +58,9 @@ import net.md_5.bungee.protocol.packet.SetCompression; import net.md_5.bungee.protocol.packet.StartConfiguration; import net.md_5.bungee.protocol.packet.ViewDistance; +import net.md_5.bungee.protocol.util.ChatComponentDeserializable; +import net.md_5.bungee.protocol.util.ChatDeserializable; +import net.md_5.bungee.protocol.util.Either; import net.md_5.bungee.util.AddressUtil; import net.md_5.bungee.util.BufUtil; import net.md_5.bungee.util.QuietException; @@ -290,11 +291,16 @@ public static void handleLogin(ProxyServer bungee, ChannelWrapper ch, UserConnec Scoreboard serverScoreboard = user.getServerSentScoreboard(); for ( Objective objective : serverScoreboard.getObjectives() ) { - user.unsafe().sendPacket( new ScoreboardObjective( - objective.getName(), - ( user.getPendingConnection().getVersion() >= ProtocolConstants.MINECRAFT_1_13 ) ? Either.right( ComponentSerializer.deserialize( objective.getValue() ) ) : Either.left( objective.getValue() ), - ScoreboardObjective.HealthDisplay.fromString( objective.getType() ), - (byte) 1, null ) + Either value; + if ( user.getPendingConnection().getVersion() >= ProtocolConstants.MINECRAFT_1_13 ) + { + value = Either.right( new ChatComponentDeserializable( ComponentSerializer.deserialize( objective.getValue() ) ) ); + } else + { + value = Either.left( objective.getValue() ); + } + ScoreboardObjective.HealthDisplay type = ScoreboardObjective.HealthDisplay.fromString( objective.getType() ); + user.unsafe().sendPacket( new ScoreboardObjective( objective.getName(), value, type, (byte) 1, null ) ); } for ( Score score : serverScoreboard.getScores() ) @@ -304,7 +310,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(), (ChatDeserializable) null, null ) ); } } for ( Team team : serverScoreboard.getTeams() ) @@ -405,11 +411,8 @@ public void handle(EncryptionRequest encryptionRequest) throws Exception public void handle(Kick kick) throws Exception { ServerInfo def = user.updateAndGetNextServer( target ); - ServerKickEvent event = new ServerKickEvent( user, target, new BaseComponent[] - { - kick.getMessage() - }, def, ServerKickEvent.State.CONNECTING ); - if ( event.getKickReason().toLowerCase( Locale.ROOT ).contains( "outdated" ) && def != null ) + ServerKickEvent event = new ServerKickEvent( user, target, kick.getMessage().get(), def, ServerKickEvent.State.CONNECTING ); + if ( event.getReason().toPlainText().toLowerCase( Locale.ROOT ).contains( "outdated" ) && def != null ) { // Pre cancel the event if we are going to try another server event.setCancelled( true ); diff --git a/proxy/src/main/java/net/md_5/bungee/UserConnection.java b/proxy/src/main/java/net/md_5/bungee/UserConnection.java index 41670a1629..a87f8200ee 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -64,6 +64,7 @@ import net.md_5.bungee.protocol.packet.StoreCookie; import net.md_5.bungee.protocol.packet.SystemChat; import net.md_5.bungee.protocol.packet.Transfer; +import net.md_5.bungee.protocol.util.ChatComponentDeserializable; import net.md_5.bungee.tab.ServerUnique; import net.md_5.bungee.tab.TabList; import net.md_5.bungee.util.CaseInsensitiveSet; @@ -435,7 +436,7 @@ public void disconnect0(final BaseComponent reason) getName(), BaseComponent.toLegacyText( reason ) } ); - ch.close( new Kick( reason ) ); + ch.close( new Kick( new ChatComponentDeserializable( reason ) ) ); if ( server != null ) { @@ -523,7 +524,7 @@ private void sendMessage(ChatMessageType position, UUID sender, BaseComponent me { net.md_5.bungee.protocol.packet.Title title = new net.md_5.bungee.protocol.packet.Title(); title.setAction( net.md_5.bungee.protocol.packet.Title.Action.ACTIONBAR ); - title.setText( message ); + title.setText( new ChatComponentDeserializable( message ) ); sendPacketQueued( title ); return; } @@ -537,7 +538,7 @@ private void sendMessage(ChatMessageType position, UUID sender, BaseComponent me position = ChatMessageType.SYSTEM; } - sendPacketQueued( new SystemChat( message, position.ordinal() ) ); + sendPacketQueued( new SystemChat( new ChatComponentDeserializable( message ), position.ordinal() ) ); } else { sendPacketQueued( new Chat( ComponentSerializer.toString( message ), (byte) position.ordinal(), sender ) ); @@ -729,8 +730,8 @@ public void setTabHeader(BaseComponent header, BaseComponent footer) footer = ChatComponentTransformer.getInstance().transform( this, true, footer ); sendPacketQueued( new PlayerListHeaderFooter( - header, - footer + new ChatComponentDeserializable( header ), + new ChatComponentDeserializable( footer ) ) ); } 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 4684bfd8d4..8ba6482696 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.getDisplayName().getLeftOrCompute( d -> ComponentSerializer.toString( d.get() ) ) ); + t.setPrefix( team.getPrefix().getLeftOrCompute( d -> ComponentSerializer.toString( d.get() ) ) ); + t.setSuffix( team.getSuffix().getLeftOrCompute( d -> ComponentSerializer.toString( d.get() ) ) ); t.setFriendlyFire( team.getFriendlyFire() ); t.setNameTagVisibility( team.getNameTagVisibility() ); t.setCollisionRule( team.getCollisionRule() ); @@ -633,16 +633,14 @@ public void handle(PluginMessage pluginMessage) throws Exception public void handle(Kick kick) throws Exception { ServerInfo def = con.updateAndGetNextServer( server.getInfo() ); - ServerKickEvent event = bungee.getPluginManager().callEvent( new ServerKickEvent( con, server.getInfo(), new BaseComponent[] - { - kick.getMessage() - }, def, ServerKickEvent.State.CONNECTED ) ); + ServerKickEvent event = bungee.getPluginManager().callEvent( new ServerKickEvent( con, server.getInfo(), + kick.getMessage().get(), def, ServerKickEvent.State.CONNECTED ) ); if ( event.isCancelled() && event.getCancelServer() != null ) { con.connectNow( event.getCancelServer(), ServerConnectEvent.Reason.KICK_REDIRECT ); } else { - con.disconnect( event.getKickReasonComponent() ); // TODO: Prefix our own stuff. + con.disconnect( event.getReason() ); // TODO: Prefix our own stuff. } server.setObsolete( true ); throw CancelSendSignal.INSTANCE; diff --git a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java index 1d9a9ee5de..50ca3ff256 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java @@ -55,9 +55,9 @@ import net.md_5.bungee.netty.cipher.CipherEncoder; import net.md_5.bungee.protocol.DefinedPacket; import net.md_5.bungee.protocol.PacketWrapper; -import net.md_5.bungee.protocol.PlayerPublicKey; import net.md_5.bungee.protocol.Protocol; import net.md_5.bungee.protocol.ProtocolConstants; +import net.md_5.bungee.protocol.data.PlayerPublicKey; import net.md_5.bungee.protocol.packet.CookieRequest; import net.md_5.bungee.protocol.packet.CookieResponse; import net.md_5.bungee.protocol.packet.EncryptionRequest; @@ -73,6 +73,7 @@ import net.md_5.bungee.protocol.packet.PluginMessage; import net.md_5.bungee.protocol.packet.StatusRequest; import net.md_5.bungee.protocol.packet.StatusResponse; +import net.md_5.bungee.protocol.util.ChatComponentDeserializable; import net.md_5.bungee.util.AllowedCharacters; import net.md_5.bungee.util.BufUtil; import net.md_5.bungee.util.QuietException; @@ -751,7 +752,7 @@ public void disconnect(BaseComponent reason) { if ( canSendKickMessage() ) { - ch.delayedClose( new Kick( reason ) ); + ch.delayedClose( new Kick( new ChatComponentDeserializable( reason ) ) ); } else { ch.close(); diff --git a/proxy/src/main/java/net/md_5/bungee/connection/LoginResult.java b/proxy/src/main/java/net/md_5/bungee/connection/LoginResult.java index 5dc8a2207f..fbfbe0ee70 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/LoginResult.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/LoginResult.java @@ -2,7 +2,7 @@ import lombok.AllArgsConstructor; import lombok.Data; -import net.md_5.bungee.protocol.Property; +import net.md_5.bungee.protocol.data.Property; @Data @AllArgsConstructor diff --git a/proxy/src/main/java/net/md_5/bungee/tab/TabList.java b/proxy/src/main/java/net/md_5/bungee/tab/TabList.java index a982655dd3..512147c296 100644 --- a/proxy/src/main/java/net/md_5/bungee/tab/TabList.java +++ b/proxy/src/main/java/net/md_5/bungee/tab/TabList.java @@ -5,7 +5,7 @@ import net.md_5.bungee.UserConnection; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.connection.LoginResult; -import net.md_5.bungee.protocol.Property; +import net.md_5.bungee.protocol.data.Property; import net.md_5.bungee.protocol.packet.PlayerListItem; import net.md_5.bungee.protocol.packet.PlayerListItemRemove; import net.md_5.bungee.protocol.packet.PlayerListItemUpdate;