Deprecate old chat in packets

This commit is contained in:
Kieran Wallbanks 2021-03-03 14:46:17 +00:00
parent 316ecbbf5a
commit 6095523d8a
21 changed files with 246 additions and 66 deletions

View File

@ -3,6 +3,10 @@ package net.minestom.server.network;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience; import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.chat.ChatColor; import net.minestom.server.chat.ChatColor;
import net.minestom.server.chat.ColoredText; import net.minestom.server.chat.ColoredText;
@ -43,7 +47,7 @@ public final class ConnectionManager implements ForwardingAudience {
private static final long KEEP_ALIVE_DELAY = 10_000; private static final long KEEP_ALIVE_DELAY = 10_000;
private static final long KEEP_ALIVE_KICK = 30_000; private static final long KEEP_ALIVE_KICK = 30_000;
private static final ColoredText TIMEOUT_TEXT = ColoredText.of(ChatColor.RED + "Timeout"); private static final Component TIMEOUT_TEXT = Component.text("Timeout", NamedTextColor.RED);
private final Queue<Player> waitingPlayers = new ConcurrentLinkedQueue<>(); private final Queue<Player> waitingPlayers = new ConcurrentLinkedQueue<>();
private final Set<Player> players = new CopyOnWriteArraySet<>(); private final Set<Player> players = new CopyOnWriteArraySet<>();
@ -60,7 +64,7 @@ public final class ConnectionManager implements ForwardingAudience {
// The consumers to call once a player connect, mostly used to init events // The consumers to call once a player connect, mostly used to init events
private final List<Consumer<Player>> playerInitializations = new CopyOnWriteArrayList<>(); private final List<Consumer<Player>> playerInitializations = new CopyOnWriteArrayList<>();
private JsonMessage shutdownText = ColoredText.of(ChatColor.RED, "The server is shutting down."); private Component shutdownText = Component.text("The server is shutting down.", NamedTextColor.RED);
/** /**
* Gets the {@link Player} linked to a {@link PlayerConnection}. * Gets the {@link Player} linked to a {@link PlayerConnection}.
@ -146,7 +150,10 @@ public final class ConnectionManager implements ForwardingAudience {
* *
* @param jsonMessage the message to send, probably a {@link net.minestom.server.chat.ColoredText} or {@link net.minestom.server.chat.RichMessage} * @param jsonMessage the message to send, probably a {@link net.minestom.server.chat.ColoredText} or {@link net.minestom.server.chat.RichMessage}
* @param condition the condition to receive the message * @param condition the condition to receive the message
*
* @deprecated Use {@link Audience#sendMessage(Component)} on {@link #audiences(PlayerValidator)}
*/ */
@Deprecated
public void broadcastMessage(@NotNull JsonMessage jsonMessage, @Nullable PlayerValidator condition) { public void broadcastMessage(@NotNull JsonMessage jsonMessage, @Nullable PlayerValidator condition) {
final Collection<Player> recipients = getRecipients(condition); final Collection<Player> recipients = getRecipients(condition);
@ -160,7 +167,9 @@ public final class ConnectionManager implements ForwardingAudience {
* Sends a {@link JsonMessage} to all online players. * Sends a {@link JsonMessage} to all online players.
* *
* @param jsonMessage the message to send, probably a {@link net.minestom.server.chat.ColoredText} or {@link net.minestom.server.chat.RichMessage} * @param jsonMessage the message to send, probably a {@link net.minestom.server.chat.ColoredText} or {@link net.minestom.server.chat.RichMessage}
* @deprecated Use {@link #sendMessage(Component)}
*/ */
@Deprecated
public void broadcastMessage(@NotNull JsonMessage jsonMessage) { public void broadcastMessage(@NotNull JsonMessage jsonMessage) {
broadcastMessage(jsonMessage, null); broadcastMessage(jsonMessage, null);
} }
@ -315,23 +324,48 @@ public final class ConnectionManager implements ForwardingAudience {
this.playerInitializations.remove(playerInitialization); this.playerInitializations.remove(playerInitialization);
} }
/**
* Gets the kick reason when the server is shutdown using {@link MinecraftServer#stopCleanly()}.
*
* @return the kick reason in case on a shutdown
*
* @deprecated Use {@link #getShutdownText()}
*/
@Deprecated
@NotNull
public JsonMessage getShutdownTextJson() {
return JsonMessage.fromComponent(shutdownText);
}
/** /**
* Gets the kick reason when the server is shutdown using {@link MinecraftServer#stopCleanly()}. * Gets the kick reason when the server is shutdown using {@link MinecraftServer#stopCleanly()}.
* *
* @return the kick reason in case on a shutdown * @return the kick reason in case on a shutdown
*/ */
@NotNull @NotNull
public JsonMessage getShutdownText() { public Component getShutdownText() {
return shutdownText; return shutdownText;
} }
/**
* Changes the kick reason in case of a shutdown.
*
* @param shutdownText the new shutdown kick reason
* @see #getShutdownTextJson()
* @deprecated Use {@link #setShutdownText(Component)}
*/
@Deprecated
public void setShutdownText(@NotNull JsonMessage shutdownText) {
this.shutdownText = shutdownText.asComponent();
}
/** /**
* Changes the kick reason in case of a shutdown. * Changes the kick reason in case of a shutdown.
* *
* @param shutdownText the new shutdown kick reason * @param shutdownText the new shutdown kick reason
* @see #getShutdownText() * @see #getShutdownText()
*/ */
public void setShutdownText(@NotNull JsonMessage shutdownText) { public void setShutdownText(@NotNull Component shutdownText) {
this.shutdownText = shutdownText; this.shutdownText = shutdownText;
} }
@ -461,7 +495,7 @@ public final class ConnectionManager implements ForwardingAudience {
* Shutdowns the connection manager by kicking all the currently connected players. * Shutdowns the connection manager by kicking all the currently connected players.
*/ */
public void shutdown() { public void shutdown() {
DisconnectPacket disconnectPacket = new DisconnectPacket(getShutdownText()); DisconnectPacket disconnectPacket = new DisconnectPacket(MinecraftServer.getSerializationManager().serialize(shutdownText));
for (Player player : getOnlinePlayers()) { for (Player player : getOnlinePlayers()) {
final PlayerConnection playerConnection = player.getPlayerConnection(); final PlayerConnection playerConnection = player.getPlayerConnection();
if (playerConnection instanceof NettyPlayerConnection) { if (playerConnection instanceof NettyPlayerConnection) {
@ -535,4 +569,27 @@ public final class ConnectionManager implements ForwardingAudience {
public @NotNull Iterable<? extends Audience> audiences() { public @NotNull Iterable<? extends Audience> audiences() {
return this.getOnlinePlayers(); return this.getOnlinePlayers();
} }
/**
* Gets the audiences of players who match a given validator.
*
* @param validator the validator
*
* @return the audience
*/
public @NotNull Iterable<? extends Audience> audiences(@Nullable PlayerValidator validator) {
if (validator == null) {
return this.audiences();
}
List<Player> validatedPlayers = new ArrayList<>();
for (Player onlinePlayer : this.getOnlinePlayers()) {
if (validator.isValid(onlinePlayer)) {
validatedPlayers.add(onlinePlayer);
}
}
return validatedPlayers;
}
} }

View File

@ -1,8 +1,8 @@
package net.minestom.server.network.packet.client.handshake; package net.minestom.server.network.packet.client.handshake;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.chat.ChatColor;
import net.minestom.server.chat.ColoredText;
import net.minestom.server.entity.PlayerSkin; import net.minestom.server.entity.PlayerSkin;
import net.minestom.server.extras.bungee.BungeeCordProxy; import net.minestom.server.extras.bungee.BungeeCordProxy;
import net.minestom.server.network.ConnectionState; import net.minestom.server.network.ConnectionState;
@ -21,9 +21,8 @@ public class HandshakePacket implements ClientPreplayPacket {
/** /**
* Text sent if a player tries to connect with an invalid version of the client * Text sent if a player tries to connect with an invalid version of the client
*/ */
private static final ColoredText INVALID_VERSION_TEXT = ColoredText.of(ChatColor.RED, "Invalid Version, please use " + MinecraftServer.VERSION_NAME); private static final Component INVALID_VERSION_TEXT = Component.text("Invalid Version, please use " + MinecraftServer.VERSION_NAME, NamedTextColor.RED);
private static final Component INVALID_BUNGEE_FORWARDING = Component.text("If you wish to use IP forwarding, please enable it in your BungeeCord config as well!", NamedTextColor.RED);
private static final ColoredText INVALID_BUNGEE_FORWARDING = ColoredText.of(ChatColor.RED, "If you wish to use IP forwarding, please enable it in your BungeeCord config as well!");
private int protocolVersion; private int protocolVersion;
private String serverAddress; private String serverAddress;
@ -71,7 +70,7 @@ public class HandshakePacket implements ClientPreplayPacket {
nettyPlayerConnection.UNSAFE_setBungeeSkin(playerSkin); nettyPlayerConnection.UNSAFE_setBungeeSkin(playerSkin);
} else { } else {
nettyPlayerConnection.sendPacket(new LoginDisconnectPacket(INVALID_BUNGEE_FORWARDING)); nettyPlayerConnection.sendPacket(new LoginDisconnectPacket(MinecraftServer.getSerializationManager().serialize(INVALID_BUNGEE_FORWARDING)));
nettyPlayerConnection.disconnect(); nettyPlayerConnection.disconnect();
return; return;
} }
@ -95,7 +94,7 @@ public class HandshakePacket implements ClientPreplayPacket {
} }
} else { } else {
// Incorrect client version // Incorrect client version
connection.sendPacket(new LoginDisconnectPacket(INVALID_VERSION_TEXT.toString())); connection.sendPacket(new LoginDisconnectPacket(MinecraftServer.getSerializationManager().serialize(INVALID_VERSION_TEXT)));
connection.disconnect(); connection.disconnect();
} }
break; break;

View File

@ -1,8 +1,8 @@
package net.minestom.server.network.packet.client.login; package net.minestom.server.network.packet.client.login;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.chat.ChatColor;
import net.minestom.server.chat.ColoredText;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.entity.PlayerSkin; import net.minestom.server.entity.PlayerSkin;
import net.minestom.server.extras.velocity.VelocityProxy; import net.minestom.server.extras.velocity.VelocityProxy;
@ -23,7 +23,7 @@ public class LoginPluginResponsePacket implements ClientPreplayPacket {
private final static ConnectionManager CONNECTION_MANAGER = MinecraftServer.getConnectionManager(); private final static ConnectionManager CONNECTION_MANAGER = MinecraftServer.getConnectionManager();
public static final ColoredText INVALID_PROXY_RESPONSE = ColoredText.of(ChatColor.RED, "Invalid proxy response!"); public static final Component INVALID_PROXY_RESPONSE = Component.text("Invalid proxy response!", NamedTextColor.RED);
public int messageId; public int messageId;
public boolean successful; public boolean successful;
@ -80,7 +80,7 @@ public class LoginPluginResponsePacket implements ClientPreplayPacket {
Player player = CONNECTION_MANAGER.startPlayState(connection, uuid, username, true); Player player = CONNECTION_MANAGER.startPlayState(connection, uuid, username, true);
player.setSkin(playerSkin); player.setSkin(playerSkin);
} else { } else {
LoginDisconnectPacket disconnectPacket = new LoginDisconnectPacket(INVALID_PROXY_RESPONSE); LoginDisconnectPacket disconnectPacket = new LoginDisconnectPacket(MinecraftServer.getSerializationManager().serialize(INVALID_PROXY_RESPONSE));
nettyPlayerConnection.sendPacket(disconnectPacket); nettyPlayerConnection.sendPacket(disconnectPacket);
} }

View File

@ -1,5 +1,7 @@
package net.minestom.server.network.packet.client.login; package net.minestom.server.network.packet.client.login;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.chat.ChatColor; import net.minestom.server.chat.ChatColor;
import net.minestom.server.chat.ColoredText; import net.minestom.server.chat.ColoredText;
@ -22,7 +24,7 @@ import java.util.concurrent.ThreadLocalRandom;
public class LoginStartPacket implements ClientPreplayPacket { public class LoginStartPacket implements ClientPreplayPacket {
private static final ColoredText ALREADY_CONNECTED_JSON = ColoredText.of(ChatColor.RED, "You are already on this server"); private static final Component ALREADY_CONNECTED = Component.text("You are already on this server", NamedTextColor.RED);
public String username; public String username;
@ -72,7 +74,7 @@ public class LoginStartPacket implements ClientPreplayPacket {
if (MojangAuth.isEnabled() && isNettyClient) { if (MojangAuth.isEnabled() && isNettyClient) {
// Mojang auth // Mojang auth
if (CONNECTION_MANAGER.getPlayer(username) != null) { if (CONNECTION_MANAGER.getPlayer(username) != null) {
connection.sendPacket(new LoginDisconnectPacket(ALREADY_CONNECTED_JSON)); connection.sendPacket(new LoginDisconnectPacket(MinecraftServer.getSerializationManager().serialize(ALREADY_CONNECTED)));
connection.disconnect(); connection.disconnect();
return; return;
} }

View File

@ -14,6 +14,10 @@ public class LoginDisconnectPacket implements ServerPacket {
this.kickMessage = kickMessage; this.kickMessage = kickMessage;
} }
/**
* @deprecated Use {@link #LoginDisconnectPacket(String)}
*/
@Deprecated
public LoginDisconnectPacket(@NotNull JsonMessage jsonKickMessage) { public LoginDisconnectPacket(@NotNull JsonMessage jsonKickMessage) {
this(jsonKickMessage.toString()); this(jsonKickMessage.toString());
} }

View File

@ -84,8 +84,8 @@ public class AdvancementsPacket implements ServerPacket {
} }
public static class DisplayData implements Writeable { public static class DisplayData implements Writeable {
public JsonMessage title; // Only text public String title; // Only text
public JsonMessage description; // Only text public String description; // Only text
public ItemStack icon; public ItemStack icon;
public FrameType frameType; public FrameType frameType;
public int flags; public int flags;
@ -93,10 +93,19 @@ public class AdvancementsPacket implements ServerPacket {
public float x; public float x;
public float y; public float y;
/**
* @deprecated Use {@link #title}
*/
public @Deprecated JsonMessage titleJson; // Only text
/**
* @deprecated Use {@link #description}
*/
public @Deprecated JsonMessage descriptionJson; // Only text
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(title.toString()); writer.writeSizedString(titleJson != null ? titleJson.toString() : title);
writer.writeSizedString(description.toString()); writer.writeSizedString(descriptionJson != null ? descriptionJson.toString() : description);
writer.writeItemStack(icon); writer.writeItemStack(icon);
writer.writeVarInt(frameType.ordinal()); writer.writeVarInt(frameType.ordinal());
writer.writeInt(flags); writer.writeInt(flags);

View File

@ -15,12 +15,24 @@ public class BossBarPacket implements ServerPacket {
public UUID uuid; public UUID uuid;
public Action action; public Action action;
public String title; public String title; // Only text
public float health; public float health;
public int color; public int color;
public int division; public int division;
public byte flags; public byte flags;
/**
* @deprecated Use {@link #title}
*/
public @Deprecated JsonMessage titleJson;
/**
* @deprecated Use {@link #color}
*/
public @Deprecated BarColor colorOld;
/**
* @deprecated Use {@link #division}
*/
public @Deprecated BarDivision divisionOld;
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
@ -29,10 +41,10 @@ public class BossBarPacket implements ServerPacket {
switch (action) { switch (action) {
case ADD: case ADD:
writer.writeSizedString(title); writer.writeSizedString(titleJson != null ? titleJson.toString() : title);
writer.writeFloat(health); writer.writeFloat(health);
writer.writeVarInt(color); writer.writeVarInt(colorOld != null ? colorOld.ordinal() : color);
writer.writeVarInt(division); writer.writeVarInt(divisionOld != null ? divisionOld.ordinal() : division);
writer.writeByte(flags); writer.writeByte(flags);
break; break;
case REMOVE: case REMOVE:
@ -42,11 +54,11 @@ public class BossBarPacket implements ServerPacket {
writer.writeFloat(health); writer.writeFloat(health);
break; break;
case UPDATE_TITLE: case UPDATE_TITLE:
writer.writeSizedString(title); writer.writeSizedString(titleJson != null ? titleJson.toString() : title);
break; break;
case UPDATE_STYLE: case UPDATE_STYLE:
writer.writeVarInt(color); writer.writeVarInt(colorOld != null ? colorOld.ordinal() : color);
writer.writeVarInt(division); writer.writeVarInt(divisionOld != null ? divisionOld.ordinal() : division);
break; break;
case UPDATE_FLAGS: case UPDATE_FLAGS:
writer.writeByte(flags); writer.writeByte(flags);

View File

@ -3,6 +3,7 @@ package net.minestom.server.network.packet.server.play;
import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.MessageType; import net.kyori.adventure.audience.MessageType;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.minestom.server.chat.JsonMessage;
import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.binary.BinaryWriter; import net.minestom.server.utils.binary.BinaryWriter;
@ -19,10 +20,19 @@ import java.util.UUID;
public class ChatMessagePacket implements ServerPacket { public class ChatMessagePacket implements ServerPacket {
private static final UUID NULL_UUID = new UUID(0, 0); private static final UUID NULL_UUID = new UUID(0, 0);
public String jsonMessage; public String message;
public MessageType messageType; public MessageType messageType;
public UUID uuid; public UUID uuid;
/**
* @deprecated Use {@link #message}
*/
public @Deprecated JsonMessage jsonMessage;
/**
* @deprecated Use {@link #messageType}
*/
public @Deprecated Position position;
@Deprecated @Deprecated
public ChatMessagePacket(String jsonMessage, Position position, UUID uuid) { public ChatMessagePacket(String jsonMessage, Position position, UUID uuid) {
this(jsonMessage, position.asMessageType(), uuid); this(jsonMessage, position.asMessageType(), uuid);
@ -49,20 +59,20 @@ public class ChatMessagePacket implements ServerPacket {
* Constructs a new chat message packet. To send formatted messages please use the * Constructs a new chat message packet. To send formatted messages please use the
* respective {@link Audience#sendMessage(Component)} functions. * respective {@link Audience#sendMessage(Component)} functions.
* *
* @param jsonMessage the raw message payload * @param message the raw message payload
* @param messageType the message type * @param messageType the message type
* @param uuid the sender of the chat message * @param uuid the sender of the chat message
*/ */
public ChatMessagePacket(String jsonMessage, MessageType messageType, UUID uuid) { public ChatMessagePacket(String message, MessageType messageType, UUID uuid) {
this.jsonMessage = jsonMessage; this.message = message;
this.messageType = messageType; this.messageType = messageType;
this.uuid = uuid; this.uuid = uuid;
} }
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(jsonMessage); writer.writeSizedString(jsonMessage != null ? jsonMessage.toString() : message);
writer.writeByte((byte) (messageType == null ? 3 : messageType.ordinal())); writer.writeByte((byte) (position != null ? position.ordinal() : messageType == null ? 3 : messageType.ordinal()));
writer.writeUuid(uuid); writer.writeUuid(uuid);
} }

View File

@ -20,7 +20,7 @@ public class CombatEventPacket implements ServerPacket {
private int duration; private int duration;
private int opponent; private int opponent;
private int playerID; private int playerID;
private JsonMessage deathMessage; // Only text private String deathMessage;
private CombatEventPacket() { private CombatEventPacket() {
} }
@ -39,7 +39,15 @@ public class CombatEventPacket implements ServerPacket {
return packet; return packet;
} }
/**
* @deprecated Use {@link #death(Player, Entity, String)}
*/
@Deprecated
public static CombatEventPacket death(Player player, Entity killer, JsonMessage message) { public static CombatEventPacket death(Player player, Entity killer, JsonMessage message) {
return death(player, killer, message.toString());
}
public static CombatEventPacket death(Player player, Entity killer, String message) {
CombatEventPacket packet = new CombatEventPacket(); CombatEventPacket packet = new CombatEventPacket();
packet.type = EventType.DEATH; packet.type = EventType.DEATH;
packet.playerID = player.getEntityId(); packet.playerID = player.getEntityId();
@ -64,7 +72,7 @@ public class CombatEventPacket implements ServerPacket {
case DEATH: case DEATH:
writer.writeVarInt(playerID); writer.writeVarInt(playerID);
writer.writeInt(opponent); writer.writeInt(opponent);
writer.writeSizedString(deathMessage.toString()); writer.writeSizedString(deathMessage);
break; break;
} }
} }

View File

@ -9,6 +9,11 @@ import org.jetbrains.annotations.NotNull;
public class DisconnectPacket implements ServerPacket { public class DisconnectPacket implements ServerPacket {
public String message; public String message;
/**
* @deprecated Use {@link #message}
*/
@Deprecated public JsonMessage messageJson;
/** /**
* Creates a new disconnect packet with a given string. * Creates a new disconnect packet with a given string.
* @param message the message * @param message the message
@ -27,7 +32,7 @@ public class DisconnectPacket implements ServerPacket {
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(message); writer.writeSizedString(messageJson != null ? messageJson.toString() : message);
} }
@Override @Override

View File

@ -1,5 +1,6 @@
package net.minestom.server.network.packet.server.play; package net.minestom.server.network.packet.server.play;
import net.kyori.adventure.sound.Sound;
import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.sound.SoundCategory; import net.minestom.server.sound.SoundCategory;
@ -9,15 +10,20 @@ import org.jetbrains.annotations.NotNull;
public class EntitySoundEffectPacket implements ServerPacket { public class EntitySoundEffectPacket implements ServerPacket {
public int soundId; public int soundId;
public SoundCategory soundCategory; public Sound.Source soundSource;
public int entityId; public int entityId;
public float volume; public float volume;
public float pitch; public float pitch;
/**
* @deprecated Use {@link #soundSource}
*/
@Deprecated public SoundCategory soundCategory;
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writer.writeVarInt(soundId); writer.writeVarInt(soundId);
writer.writeVarInt(soundCategory.ordinal()); writer.writeVarInt(soundCategory != null ? soundCategory.ordinal() : soundSource.ordinal());
writer.writeVarInt(entityId); writer.writeVarInt(entityId);
writer.writeFloat(volume); writer.writeFloat(volume);
writer.writeFloat(pitch); writer.writeFloat(pitch);

View File

@ -63,7 +63,13 @@ public class MapDataPacket implements ServerPacket {
public int type; public int type;
public byte x, z; public byte x, z;
public byte direction; public byte direction;
public JsonMessage displayName; // Only text public String displayName;
/**
* @deprecated Use {@link #displayName}
*/
@Deprecated
public JsonMessage displayNameJson; // Only text
private void write(BinaryWriter writer) { private void write(BinaryWriter writer) {
writer.writeVarInt(type); writer.writeVarInt(type);
@ -71,10 +77,10 @@ public class MapDataPacket implements ServerPacket {
writer.writeByte(z); writer.writeByte(z);
writer.writeByte(direction); writer.writeByte(direction);
final boolean hasDisplayName = displayName != null; final boolean hasDisplayName = displayName != null || displayNameJson != null;
writer.writeBoolean(hasDisplayName); writer.writeBoolean(hasDisplayName);
if (hasDisplayName) { if (hasDisplayName) {
writer.writeSizedString(displayName.toString()); writer.writeSizedString(displayNameJson != null ? displayNameJson.toString() : displayName);
} }
} }

View File

@ -14,10 +14,15 @@ public class NamedSoundEffectPacket implements ServerPacket {
public float volume; public float volume;
public float pitch; public float pitch;
/**
* @deprecated Use {@link #soundCategory}
*/
@Deprecated public SoundCategory soundCategoryOld;
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(soundName); writer.writeSizedString(soundName);
writer.writeVarInt(soundCategory); writer.writeVarInt(soundCategoryOld != null ? soundCategoryOld.ordinal() : soundCategory);
writer.writeInt(x * 8); writer.writeInt(x * 8);
writer.writeInt(y * 8); writer.writeInt(y * 8);
writer.writeInt(z * 8); writer.writeInt(z * 8);

View File

@ -75,7 +75,12 @@ public class PlayerInfoPacket implements ServerPacket {
public List<Property> properties; public List<Property> properties;
public GameMode gameMode; public GameMode gameMode;
public int ping; public int ping;
public JsonMessage displayName; // Only text public String displayName;
/**
* @deprecated Use {@link #displayName}
*/
@Deprecated public JsonMessage displayNameJson; // Only text
public AddPlayer(UUID uuid, String name, GameMode gameMode, int ping) { public AddPlayer(UUID uuid, String name, GameMode gameMode, int ping) {
super(uuid); super(uuid);
@ -95,10 +100,10 @@ public class PlayerInfoPacket implements ServerPacket {
writer.writeVarInt(gameMode.getId()); writer.writeVarInt(gameMode.getId());
writer.writeVarInt(ping); writer.writeVarInt(ping);
final boolean hasDisplayName = displayName != null; final boolean hasDisplayName = displayName != null || displayNameJson != null;
writer.writeBoolean(hasDisplayName); writer.writeBoolean(hasDisplayName);
if (hasDisplayName) if (hasDisplayName)
writer.writeSizedString(displayName.toString()); writer.writeSizedString(displayNameJson != null ? displayNameJson.toString() : displayName);
} }
public static class Property { public static class Property {
@ -161,9 +166,22 @@ public class PlayerInfoPacket implements ServerPacket {
public static class UpdateDisplayName extends PlayerInfo { public static class UpdateDisplayName extends PlayerInfo {
public JsonMessage displayName; // Only text public String displayName;
/**
* @deprecated Use {@link #displayName}
*/
@Deprecated public JsonMessage displayNameJson; // Only text
/**
* @deprecated Use {@link #UpdateDisplayName(UUID, String)}
*/
@Deprecated
public UpdateDisplayName(UUID uuid, JsonMessage displayName) { public UpdateDisplayName(UUID uuid, JsonMessage displayName) {
this(uuid, displayName.toString());
}
public UpdateDisplayName(UUID uuid, String displayName) {
super(uuid); super(uuid);
this.displayName = displayName; this.displayName = displayName;
} }
@ -173,7 +191,7 @@ public class PlayerInfoPacket implements ServerPacket {
final boolean hasDisplayName = displayName != null; final boolean hasDisplayName = displayName != null;
writer.writeBoolean(hasDisplayName); writer.writeBoolean(hasDisplayName);
if (hasDisplayName) if (hasDisplayName)
writer.writeSizedString(displayName.toString()); writer.writeSizedString(displayNameJson != null ? displayNameJson.toString() : displayName);
} }
} }

View File

@ -3,6 +3,7 @@ package net.minestom.server.network.packet.server.play;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
import net.minestom.server.chat.JsonMessage;
import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.binary.BinaryWriter; import net.minestom.server.utils.binary.BinaryWriter;
@ -16,10 +17,20 @@ public class PlayerListHeaderAndFooterPacket implements ServerPacket {
public String header; public String header;
public String footer; public String footer;
/**
* @deprecated Use {@link #header}
*/
@Deprecated public JsonMessage headerJson;
/**
@deprecated Use {@link #footer}
*/
@Deprecated public JsonMessage footerJson;
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(Objects.requireNonNullElse(header, EMPTY_COMPONENT)); writer.writeSizedString(headerJson != null ? headerJson.toString() : Objects.requireNonNullElse(header, EMPTY_COMPONENT));
writer.writeSizedString(Objects.requireNonNullElse(footer, EMPTY_COMPONENT)); writer.writeSizedString(footerJson != null ? footerJson.toString() : Objects.requireNonNullElse(footer, EMPTY_COMPONENT));
} }
@Override @Override

View File

@ -21,19 +21,25 @@ public class ScoreboardObjectivePacket implements ServerPacket {
/** /**
* The text to be displayed for the score * The text to be displayed for the score
*/ */
public JsonMessage objectiveValue; // Only text public String objectiveValue; // Only text
/** /**
* The type how the score is displayed * The type how the score is displayed
*/ */
public Type type; public Type type;
/**
* @deprecated Use {@link #objectiveValue}
*/
@Deprecated
public JsonMessage objectiveValueJson;
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writer.writeSizedString(objectiveName); writer.writeSizedString(objectiveName);
writer.writeByte(mode); writer.writeByte(mode);
if (mode == 0 || mode == 2) { if (mode == 0 || mode == 2) {
writer.writeSizedString(objectiveValue.toString()); writer.writeSizedString(objectiveValueJson != null ? objectiveValueJson.toString() : objectiveValue);
writer.writeVarInt(type.ordinal()); writer.writeVarInt(type.ordinal());
} }
} }

View File

@ -17,6 +17,11 @@ public class SoundEffectPacket implements ServerPacket {
public float volume; public float volume;
public float pitch; public float pitch;
/**
* @deprecated Use {@link #soundCategory}
*/
@Deprecated public SoundCategory soundCategoryOld;
/** /**
* @deprecated Use variables * @deprecated Use variables
*/ */
@ -37,7 +42,7 @@ public class SoundEffectPacket implements ServerPacket {
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {
writer.writeVarInt(soundId); writer.writeVarInt(soundId);
writer.writeVarInt(soundCategory); writer.writeVarInt(soundCategoryOld != null ? soundCategoryOld.ordinal() : soundCategory);
writer.writeInt(x * 8); writer.writeInt(x * 8);
writer.writeInt(y * 8); writer.writeInt(y * 8);
writer.writeInt(z * 8); writer.writeInt(z * 8);

View File

@ -24,7 +24,7 @@ public class TabCompletePacket implements ServerPacket {
writer.writeSizedString(match.match); writer.writeSizedString(match.match);
writer.writeBoolean(match.hasTooltip); writer.writeBoolean(match.hasTooltip);
if (match.hasTooltip) if (match.hasTooltip)
writer.writeSizedString(match.tooltip.toString()); writer.writeSizedString(match.tooltipJson != null ? match.tooltipJson.toString() : match.tooltip);
} }
} }
@ -36,7 +36,12 @@ public class TabCompletePacket implements ServerPacket {
public static class Match { public static class Match {
public String match; public String match;
public boolean hasTooltip; public boolean hasTooltip;
public JsonMessage tooltip; // Only text public String tooltip;
/**
* @deprecated Use {@link #tooltip}
*/
@Deprecated public String tooltipJson;
} }
} }

View File

@ -23,7 +23,7 @@ public class TeamsPacket implements ServerPacket {
/** /**
* The display name for the team * The display name for the team
*/ */
public JsonMessage teamDisplayName; public String teamDisplayName;
/** /**
* The friendly flags to * The friendly flags to
*/ */
@ -43,16 +43,29 @@ public class TeamsPacket implements ServerPacket {
/** /**
* The prefix of the team * The prefix of the team
*/ */
public JsonMessage teamPrefix; public String teamPrefix;
/** /**
* The suffix of the team * The suffix of the team
*/ */
public JsonMessage teamSuffix; public String teamSuffix;
/** /**
* An array with all entities in the team * An array with all entities in the team
*/ */
public String[] entities; public String[] entities;
/**
* @deprecated Use {@link #teamDisplayName}
*/
@Deprecated public JsonMessage teamDisplayNameJson;
/**
@deprecated Use {@link #teamPrefix}
*/
@Deprecated public JsonMessage teamPrefixJson;
/**
@deprecated Use {@link #teamSuffix}
*/
@Deprecated public JsonMessage teamSuffixJson;
/** /**
* Writes data into the {@link BinaryWriter} * Writes data into the {@link BinaryWriter}
* *
@ -66,13 +79,13 @@ public class TeamsPacket implements ServerPacket {
switch (action) { switch (action) {
case CREATE_TEAM: case CREATE_TEAM:
case UPDATE_TEAM_INFO: case UPDATE_TEAM_INFO:
writer.writeSizedString(this.teamDisplayName.toString()); writer.writeSizedString(this.teamDisplayNameJson != null ? this.teamDisplayNameJson.toString() : this.teamDisplayName);
writer.writeByte(this.friendlyFlags); writer.writeByte(this.friendlyFlags);
writer.writeSizedString(this.nameTagVisibility.getIdentifier()); writer.writeSizedString(this.nameTagVisibility.getIdentifier());
writer.writeSizedString(this.collisionRule.getIdentifier()); writer.writeSizedString(this.collisionRule.getIdentifier());
writer.writeVarInt(this.teamColor); writer.writeVarInt(this.teamColor);
writer.writeSizedString(this.teamPrefix.toString()); writer.writeSizedString(this.teamPrefixJson != null ? this.teamPrefixJson.toString() : this.teamPrefix);
writer.writeSizedString(this.teamSuffix.toString()); writer.writeSizedString(this.teamSuffixJson != null ? this.teamSuffixJson.toString() : this.teamSuffix);
break; break;
case REMOVE_TEAM: case REMOVE_TEAM:

View File

@ -1,6 +1,5 @@
package net.minestom.server.network.packet.server.play; package net.minestom.server.network.packet.server.play;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.title.Title; import net.kyori.adventure.title.Title;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacket;

View File

@ -1,8 +1,8 @@
package net.minestom.server.network.player; package net.minestom.server.network.player;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.chat.ChatColor;
import net.minestom.server.chat.ColoredText;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.listener.manager.PacketListenerManager; import net.minestom.server.listener.manager.PacketListenerManager;
import net.minestom.server.listener.manager.ServerPacketConsumer; import net.minestom.server.listener.manager.ServerPacketConsumer;
@ -29,7 +29,7 @@ public abstract class PlayerConnection {
private boolean online; private boolean online;
// Text used to kick client sending too many packets // Text used to kick client sending too many packets
private static final ColoredText rateLimitKickMessage = ColoredText.of(ChatColor.RED + "Too Many Packets"); private static final Component rateLimitKickMessage = Component.text("Too Many Packets", NamedTextColor.RED);
//Connection Stats //Connection Stats
private final AtomicInteger packetCounter = new AtomicInteger(0); private final AtomicInteger packetCounter = new AtomicInteger(0);