diff --git a/src/main/java/net/minestom/server/crypto/ChatSession.java b/src/main/java/net/minestom/server/crypto/ChatSession.java new file mode 100644 index 000000000..1cdcd253b --- /dev/null +++ b/src/main/java/net/minestom/server/crypto/ChatSession.java @@ -0,0 +1,18 @@ +package net.minestom.server.crypto; + +import net.minestom.server.network.NetworkBuffer; +import org.jetbrains.annotations.NotNull; + +import java.util.UUID; + +public record ChatSession(@NotNull UUID sessionId, @NotNull PlayerPublicKey publicKey) implements NetworkBuffer.Writer { + public ChatSession(@NotNull NetworkBuffer reader) { + this(reader.read(NetworkBuffer.UUID), new PlayerPublicKey(reader)); + } + + @Override + public void write(@NotNull NetworkBuffer writer) { + writer.write(NetworkBuffer.UUID, sessionId); + writer.write(publicKey); + } +} diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index ef948715e..bb42e2516 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -1927,7 +1927,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, List.of(new PlayerInfoUpdatePacket.Property("textures", skin.textures(), skin.signature())) : List.of(); return new PlayerInfoUpdatePacket.Entry(getUuid(), getUsername(), prop, - true, getLatency(), getGameMode(), displayName); + true, getLatency(), getGameMode(), displayName, null); } /** diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientChatSessionUpdatePacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientChatSessionUpdatePacket.java index 9f9ac3d0f..a18b33e9a 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientChatSessionUpdatePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientChatSessionUpdatePacket.java @@ -1,22 +1,17 @@ package net.minestom.server.network.packet.client.play; -import net.minestom.server.crypto.PlayerPublicKey; +import net.minestom.server.crypto.ChatSession; import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import java.util.UUID; - -public record ClientChatSessionUpdatePacket(@NotNull UUID sessionId, - @Nullable PlayerPublicKey publicKey) implements ClientPacket { +public record ClientChatSessionUpdatePacket(@NotNull ChatSession chatSession) implements ClientPacket { public ClientChatSessionUpdatePacket(@NotNull NetworkBuffer reader) { - this(reader.read(NetworkBuffer.UUID), new PlayerPublicKey(reader)); + this(new ChatSession(reader)); } @Override public void write(@NotNull NetworkBuffer writer) { - writer.write(NetworkBuffer.UUID, sessionId); - writer.writeOptional(publicKey); + writer.write(chatSession); } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PlayerChatMessagePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PlayerChatMessagePacket.java index 143867aef..309eac8df 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PlayerChatMessagePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PlayerChatMessagePacket.java @@ -1,7 +1,8 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; -import net.minestom.server.crypto.MessageSignature; +import net.minestom.server.crypto.FilterMask; +import net.minestom.server.crypto.SignedMessageBody; import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; @@ -9,6 +10,7 @@ import net.minestom.server.network.packet.server.ServerPacketIdentifier; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.UUID; @@ -19,26 +21,30 @@ import static net.minestom.server.network.NetworkBuffer.*; /** * Represents an outgoing chat message packet. */ -public record PlayerChatMessagePacket(@NotNull Component signedContent, @Nullable Component unsignedContent, - int type, @NotNull UUID uuid, - @NotNull Component displayName, @Nullable Component teamDisplayName, - @NotNull MessageSignature signature) implements ComponentHoldingServerPacket { +public record PlayerChatMessagePacket(UUID sender, int index, byte @Nullable [] signature, + SignedMessageBody.@NotNull Packed messageBody, + @Nullable Component unsignedContent, FilterMask filterMask, + int msgTypeId, Component msgTypeName, + @Nullable Component msgTypeTarget) implements ComponentHoldingServerPacket { public PlayerChatMessagePacket(@NotNull NetworkBuffer reader) { - this(reader.read(COMPONENT), reader.readOptional(COMPONENT), - reader.read(VAR_INT), reader.read(NetworkBuffer.UUID), - reader.read(COMPONENT), reader.readOptional(COMPONENT), - new MessageSignature(reader)); + this(reader.read(UUID), reader.read(VAR_INT), reader.readOptional(r -> r.readBytes(256)), + new SignedMessageBody.Packed(reader), + reader.readOptional(COMPONENT), new FilterMask(reader), + reader.read(VAR_INT), reader.read(COMPONENT), + reader.readOptional(COMPONENT)); } @Override public void write(@NotNull NetworkBuffer writer) { - writer.write(COMPONENT, signedContent); + writer.write(UUID, sender); + writer.write(VAR_INT, index); + writer.writeOptional(RAW_BYTES, signature); + writer.write(messageBody); writer.writeOptional(COMPONENT, unsignedContent); - writer.write(VAR_INT, type); - writer.write(UUID, uuid); - writer.write(COMPONENT, displayName); - writer.writeOptional(COMPONENT, teamDisplayName); - writer.write(signature); + writer.write(filterMask); + writer.write(VAR_INT, msgTypeId); + writer.write(COMPONENT, msgTypeName); + writer.writeOptional(COMPONENT, msgTypeTarget); } @Override @@ -48,12 +54,19 @@ public record PlayerChatMessagePacket(@NotNull Component signedContent, @Nullabl @Override public @NotNull Collection components() { - return List.of(signedContent); + final ArrayList list = new ArrayList<>(); + list.add(msgTypeName); + if (unsignedContent != null) list.add(unsignedContent); + if (msgTypeTarget != null) list.add(msgTypeTarget); + return List.copyOf(list); } @Override public @NotNull ServerPacket copyWithOperator(@NotNull UnaryOperator operator) { - return new PlayerChatMessagePacket(signedContent, unsignedContent, type, - uuid, displayName, teamDisplayName, signature); + return new PlayerChatMessagePacket(sender, index, signature, + messageBody, + operator.apply(unsignedContent), filterMask, + msgTypeId, operator.apply(msgTypeName), + operator.apply(msgTypeTarget)); } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoUpdatePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoUpdatePacket.java index afed22153..9fc93b090 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoUpdatePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoUpdatePacket.java @@ -1,6 +1,7 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; +import net.minestom.server.crypto.ChatSession; import net.minestom.server.entity.GameMode; import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; @@ -43,7 +44,7 @@ public record PlayerInfoUpdatePacket(@NotNull EnumSet<@NotNull Action> actions, public record Entry(UUID uuid, String username, List properties, boolean listed, int latency, GameMode gameMode, - @Nullable Component displayName) { + @Nullable Component displayName, @Nullable ChatSession chatSession) { public Entry { properties = List.copyOf(properties); } @@ -73,7 +74,7 @@ public record PlayerInfoUpdatePacket(@NotNull EnumSet<@NotNull Action> actions, writer.write(STRING, entry.username); writer.writeCollection(entry.properties); }), - INITIALIZE_CHAT(null), + INITIALIZE_CHAT((writer, entry) -> writer.writeOptional(entry.chatSession)), UPDATE_GAME_MODE((writer, entry) -> writer.write(VAR_INT, entry.gameMode.ordinal())), UPDATE_LISTED((writer, entry) -> writer.write(BOOLEAN, entry.listed)), UPDATE_LATENCY((writer, entry) -> writer.write(VAR_INT, entry.latency)),