diff --git a/build.gradle b/build.gradle index 3b8fe3a93..02113b535 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,6 @@ sourceCompatibility = 1.11 repositories { mavenCentral() maven { url 'https://jitpack.io' } - } @@ -33,7 +32,6 @@ dependencies { api group: 'com.google.code.gson', name: 'gson', version: '2.8.5' api 'com.github.TheMode:CommandBuilder:f893cfbfe4' - api 'com.github.Minestom:minecraft-text:69fd808e92' // https://jitpack.io/#Articdive/Jnoise/1.0-SNAPSHOT api 'com.github.Articdive:Jnoise:1.0-SNAPSHOT' @@ -41,4 +39,8 @@ dependencies { // https://mvnrepository.com/artifact/org.rocksdb/rocksdbjni api group: 'org.rocksdb', name: 'rocksdbjni', version: '6.8.1' + api 'net.kyori:text-api:3.0.3' + api 'net.kyori:text-serializer-legacy:3.0.3' + api 'net.kyori:text-serializer-gson:3.0.3' + api 'net.kyori:text-serializer-plain:3.0.3' } diff --git a/src/main/java/net/minestom/server/chat/Chat.java b/src/main/java/net/minestom/server/chat/Chat.java index 71d3b15ad..1fa07d3d1 100644 --- a/src/main/java/net/minestom/server/chat/Chat.java +++ b/src/main/java/net/minestom/server/chat/Chat.java @@ -1,7 +1,9 @@ package net.minestom.server.chat; -import club.thectm.minecraft.text.LegacyText; -import club.thectm.minecraft.text.TextObject; +import net.kyori.text.Component; +import net.kyori.text.TextComponent; +import net.kyori.text.serializer.gson.GsonComponentSerializer; +import net.kyori.text.serializer.legacy.LegacyComponentSerializer; /** * Thank for the minecraft-text library made by rbrick: @@ -11,23 +13,23 @@ public class Chat { public static final char COLOR_CHAR = (char) 0xA7; // Represent the character 'ยง' - public static TextObject legacyText(String text, char colorChar) { - return LegacyText.fromLegacy(text, colorChar); + public static String toJsonString(Component component) { + return GsonComponentSerializer.INSTANCE.serialize(component); } - public static TextObject legacyText(String text) { - return legacyText(text, COLOR_CHAR); + public static Component fromJsonString(String json) { + return GsonComponentSerializer.INSTANCE.deserialize(json); } - public static String legacyTextString(String text, char colorChar) { - return legacyText(text, colorChar).toJson().toString(); + public static String toLegacyText(Component component) { + return LegacyComponentSerializer.legacyLinking().serialize(component); } - public static String legacyTextString(String text) { - return legacyText(text).toJson().toString(); + public static TextComponent fromLegacyText(String text, char colorChar) { + return LegacyComponentSerializer.legacyLinking().deserialize(text, colorChar); } - public static String uncoloredLegacyText(String text) { - return text.replace(String.valueOf(COLOR_CHAR), ""); + public static TextComponent fromLegacyText(String text) { + return fromLegacyText(text, COLOR_CHAR); } } diff --git a/src/main/java/net/minestom/server/chat/ChatColor.java b/src/main/java/net/minestom/server/chat/ChatColor.java deleted file mode 100644 index 86bb3e05b..000000000 --- a/src/main/java/net/minestom/server/chat/ChatColor.java +++ /dev/null @@ -1,39 +0,0 @@ -package net.minestom.server.chat; - -import net.minestom.server.utils.HexUtils; - -public enum ChatColor { - - BLACK((byte) 0), - DARK_BLUE((byte) 1), - DARK_GREEN((byte) 2), - DARK_AQUA((byte) 3), - DARK_RED((byte) 4), - DARK_PURPLE((byte) 5), - GOLD((byte) 6), - GRAY((byte) 7), - DARK_GRAY((byte) 8), - BLUE((byte) 9), - GREEN((byte) 0xa), - AQUA((byte) 0xb), - RED((byte) 0xc), - LIGHT_PURPLE((byte) 0xd), - YELLOW((byte) 0xe), - WHITE((byte) 0xf); - - - private byte id; - - ChatColor(byte id) { - this.id = id; - } - - public byte getId() { - return id; - } - - @Override - public String toString() { - return Chat.COLOR_CHAR + String.valueOf(HexUtils.byteToHex(id)); - } -} diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 6c69b1d6b..a329fe503 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -1,8 +1,7 @@ package net.minestom.server.entity; -import club.thectm.minecraft.text.TextBuilder; -import club.thectm.minecraft.text.TextObject; -import com.google.gson.JsonObject; +import net.kyori.text.Component; +import net.kyori.text.TextComponent; import net.minestom.server.MinecraftServer; import net.minestom.server.bossbar.BossBar; import net.minestom.server.chat.Chat; @@ -249,21 +248,21 @@ public class Player extends LivingEntity { public void kill() { if (!isDead()) { // send death message to player - TextObject deathMessage; + Component deathMessage; if (lastDamageSource != null) { deathMessage = lastDamageSource.buildDeathScreenMessage(this); } else { // may happen if killed by the server without applying damage - deathMessage = TextBuilder.of("Killed by poor programming.").build(); + deathMessage = TextComponent.of("Killed by poor programming."); } CombatEventPacket deathPacket = CombatEventPacket.death(this, Optional.empty(), deathMessage); playerConnection.sendPacket(deathPacket); // send death message to chat - TextObject chatMessage; + Component chatMessage; if (lastDamageSource != null) { chatMessage = lastDamageSource.buildChatMessage(this); } else { // may happen if killed by the server without applying damage - chatMessage = TextBuilder.of(getUsername() + " was killed by poor programming.").build(); + chatMessage = TextComponent.of(getUsername() + " was killed by poor programming."); } MinecraftServer.getConnectionManager().getOnlinePlayers().forEach(player -> { player.sendMessage(chatMessage); @@ -399,20 +398,16 @@ public class Player extends LivingEntity { // Use legacy color formatting public void sendMessage(String message) { - sendMessage(Chat.legacyText(message)); + sendMessage(Chat.fromLegacyText(message)); } public void sendMessage(String message, char colorChar) { - sendMessage(Chat.legacyText(message, colorChar)); + sendMessage(Chat.fromLegacyText(message, colorChar)); } - public void sendMessage(JsonObject jsonObject) { - ChatMessagePacket chatMessagePacket = new ChatMessagePacket(jsonObject.toString(), ChatMessagePacket.Position.CHAT); - playerConnection.sendPacket(chatMessagePacket); - } - - public void sendMessage(TextObject textObject) { - sendMessage(textObject.toJson()); + public void sendMessage(Component textObject) { + String json = Chat.toJsonString(textObject); + playerConnection.sendPacket(new ChatMessagePacket(json, ChatMessagePacket.Position.CHAT)); } public void playSound(Sound sound, SoundCategory soundCategory, int x, int y, int z, float volume, float pitch) { @@ -452,24 +447,77 @@ public class Player extends LivingEntity { playerConnection.sendPacket(stopSoundPacket); } - public void sendHeaderFooter(String header, String footer, char colorChar) { + public void sendHeaderFooter(Component header, Component footer) { PlayerListHeaderAndFooterPacket playerListHeaderAndFooterPacket = new PlayerListHeaderAndFooterPacket(); playerListHeaderAndFooterPacket.emptyHeader = header == null; playerListHeaderAndFooterPacket.emptyFooter = footer == null; - - playerListHeaderAndFooterPacket.header = Chat.legacyText(header, colorChar).toJson().toString(); - playerListHeaderAndFooterPacket.footer = Chat.legacyText(footer, colorChar).toJson().toString(); + playerListHeaderAndFooterPacket.header = Chat.toJsonString(header); + playerListHeaderAndFooterPacket.footer = Chat.toJsonString(footer); playerConnection.sendPacket(playerListHeaderAndFooterPacket); } - public void sendActionBarMessage(String message, char colorChar) { + public void sendHeaderFooter(String header, String footer, char colorChar) { + sendHeaderFooter(Chat.fromLegacyText(header, colorChar), Chat.fromLegacyText(footer, colorChar)); + } + + private void sendTitle(Component title, TitlePacket.Action action) { TitlePacket titlePacket = new TitlePacket(); - titlePacket.action = TitlePacket.Action.SET_ACTION_BAR; - titlePacket.actionBarText = Chat.legacyText(message, colorChar).toJson().toString(); + titlePacket.action = action; + + switch (action) { + case SET_TITLE: + titlePacket.titleText = Chat.toJsonString(title); + break; + case SET_SUBTITLE: + titlePacket.subtitleText = Chat.toJsonString(title); + break; + case SET_ACTION_BAR: + titlePacket.actionBarText = Chat.toJsonString(title); + default: + throw new UnsupportedOperationException("Invalid TitlePacket.Action type!"); + } + playerConnection.sendPacket(titlePacket); } + public void sendTitleSubtitleMessage(Component title, Component subtitle) { + sendTitle(title, TitlePacket.Action.SET_TITLE); + sendTitle(subtitle, TitlePacket.Action.SET_SUBTITLE); + } + + public void sendTitleMessage(Component title) { + sendTitle(title, TitlePacket.Action.SET_TITLE); + } + + public void sendTitleMessage(String title, char colorChar) { + sendTitleMessage(Chat.fromLegacyText(title, colorChar)); + } + + public void sendTitleMessage(String title) { + sendTitleMessage(title, Chat.COLOR_CHAR); + } + + public void sendSubtitleMessage(Component subtitle) { + sendTitle(subtitle, TitlePacket.Action.SET_SUBTITLE); + } + + public void sendSubtitleMessage(String subtitle, char colorChar) { + sendSubtitleMessage(Chat.fromLegacyText(subtitle, colorChar)); + } + + public void sendSubtitleMessage(String subtitle) { + sendSubtitleMessage(subtitle, Chat.COLOR_CHAR); + } + + public void sendActionBarMessage(Component actionBar) { + sendTitle(actionBar, TitlePacket.Action.SET_ACTION_BAR); + } + + public void sendActionBarMessage(String message, char colorChar) { + sendActionBarMessage(Chat.fromLegacyText(message, colorChar)); + } + public void sendActionBarMessage(String message) { sendActionBarMessage(message, Chat.COLOR_CHAR); } @@ -703,13 +751,17 @@ public class Player extends LivingEntity { playerConnection.sendPacket(respawnPacket); } - public void kick(String message) { + public void kick(Component message) { DisconnectPacket disconnectPacket = new DisconnectPacket(); - disconnectPacket.message = message; + disconnectPacket.message = Chat.toJsonString(message); playerConnection.sendPacket(disconnectPacket); playerConnection.getChannel().close(); } + public void kick(String message) { + kick(Chat.fromLegacyText(message)); + } + public LevelType getLevelType() { return levelType; } diff --git a/src/main/java/net/minestom/server/entity/damage/DamageType.java b/src/main/java/net/minestom/server/entity/damage/DamageType.java index 548281534..c6c25a5c3 100644 --- a/src/main/java/net/minestom/server/entity/damage/DamageType.java +++ b/src/main/java/net/minestom/server/entity/damage/DamageType.java @@ -1,7 +1,8 @@ package net.minestom.server.entity.damage; -import club.thectm.minecraft.text.TextBuilder; -import club.thectm.minecraft.text.TextObject; +import net.kyori.text.Component; +import net.kyori.text.TextComponent; +import net.kyori.text.TranslatableComponent; import net.minestom.server.entity.Entity; import net.minestom.server.entity.LivingEntity; import net.minestom.server.entity.Player; @@ -34,15 +35,15 @@ public class DamageType { return new EntityProjectileDamage(shooter, projectile); } - public TextObject buildChatMessage(Player killed) { - return TextBuilder.ofTranslation("death."+identifier, TextBuilder.of(killed.getUsername()).build()).build(); + public Component buildChatMessage(Player killed) { + return TranslatableComponent.of("death."+identifier, TextComponent.of(killed.getUsername())); } public static DamageType fromPlayer(Player player) { return new EntityDamage(player); } - public TextObject buildDeathScreenMessage(Player killed) { + public Component buildDeathScreenMessage(Player killed) { return buildChatMessage(killed); } diff --git a/src/main/java/net/minestom/server/event/PlayerChatEvent.java b/src/main/java/net/minestom/server/event/PlayerChatEvent.java index 1a29e8dcb..7e35f800c 100644 --- a/src/main/java/net/minestom/server/event/PlayerChatEvent.java +++ b/src/main/java/net/minestom/server/event/PlayerChatEvent.java @@ -1,6 +1,6 @@ package net.minestom.server.event; -import club.thectm.minecraft.text.TextObject; +import net.kyori.text.TextComponent; import net.minestom.server.entity.Player; import java.util.ArrayList; @@ -12,7 +12,7 @@ public class PlayerChatEvent extends CancellableEvent { private Player sender; private Collection recipients; private String message; - private Function chatFormat; + private Function chatFormat; public PlayerChatEvent(Player sender, Collection recipients, String message) { this.sender = sender; @@ -20,7 +20,7 @@ public class PlayerChatEvent extends CancellableEvent { this.message = message; } - public void setChatFormat(Function chatFormat) { + public void setChatFormat(Function chatFormat) { this.chatFormat = chatFormat; } @@ -40,7 +40,7 @@ public class PlayerChatEvent extends CancellableEvent { this.message = message; } - public Function getChatFormatFunction() { + public Function getChatFormatFunction() { return chatFormat; } } diff --git a/src/main/java/net/minestom/server/listener/ChatMessageListener.java b/src/main/java/net/minestom/server/listener/ChatMessageListener.java index 4da67b846..05d8cea76 100644 --- a/src/main/java/net/minestom/server/listener/ChatMessageListener.java +++ b/src/main/java/net/minestom/server/listener/ChatMessageListener.java @@ -1,6 +1,10 @@ package net.minestom.server.listener; -import club.thectm.minecraft.text.*; +import net.kyori.text.TextComponent; +import net.kyori.text.event.ClickEvent; +import net.kyori.text.event.HoverEvent; +import net.kyori.text.format.TextColor; +import net.kyori.text.serializer.plain.PlainComponentSerializer; import net.minestom.server.MinecraftServer; import net.minestom.server.chat.Chat; import net.minestom.server.command.CommandManager; @@ -14,7 +18,7 @@ import java.util.function.Function; public class ChatMessageListener { public static void listener(ClientChatMessagePacket packet, Player player) { - String message = Chat.uncoloredLegacyText(packet.message); + String message = PlainComponentSerializer.INSTANCE.serialize(Chat.fromLegacyText(packet.message)); CommandManager commandManager = MinecraftServer.getCommandManager(); String cmdPrefix = commandManager.getCommandPrefix(); @@ -38,12 +42,11 @@ public class ChatMessageListener { playerChatEvent.setChatFormat((event) -> { String username = player.getUsername(); - TextObject usernameText = TextBuilder.of(String.format("<%s>", username)) - .color(ChatColor.WHITE) - .hoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, ChatColor.GRAY + "Its " + username)) - .clickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/msg " + username + " ")) - .append(" " + event.getMessage()) - .build(); + TextComponent usernameText = TextComponent.of(String.format("<%s>", username)) + .color(TextColor.WHITE) + .hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TextComponent.of("Its " + username).color(TextColor.GRAY))) + .clickEvent(ClickEvent.of(ClickEvent.Action.SUGGEST_COMMAND, "/msg " + username + " ")) + .append(TextComponent.of(" " + event.getMessage())); return usernameText; }); @@ -51,11 +54,11 @@ public class ChatMessageListener { // Call the event player.callCancellableEvent(PlayerChatEvent.class, playerChatEvent, () -> { - Function formatFunction = playerChatEvent.getChatFormatFunction(); + Function formatFunction = playerChatEvent.getChatFormatFunction(); if (formatFunction == null) throw new NullPointerException("PlayerChatEvent#chatFormat cannot be null!"); - TextObject textObject = formatFunction.apply(playerChatEvent); + TextComponent textObject = formatFunction.apply(playerChatEvent); for (Player recipient : playerChatEvent.getRecipients()) { recipient.sendMessage(textObject); diff --git a/src/main/java/net/minestom/server/listener/KeepAliveListener.java b/src/main/java/net/minestom/server/listener/KeepAliveListener.java index 2d8821470..3115f28a5 100644 --- a/src/main/java/net/minestom/server/listener/KeepAliveListener.java +++ b/src/main/java/net/minestom/server/listener/KeepAliveListener.java @@ -1,6 +1,6 @@ package net.minestom.server.listener; -import net.minestom.server.chat.ChatColor; +import net.kyori.text.format.TextColor; import net.minestom.server.entity.Player; import net.minestom.server.network.packet.client.play.ClientKeepAlivePacket; @@ -8,7 +8,7 @@ public class KeepAliveListener { public static void listener(ClientKeepAlivePacket packet, Player player) { if (packet.id != player.getLastKeepAlive()) { - player.kick(ChatColor.RED + "Bad Keep Alive packet"); + player.kick(TextColor.RED + "Bad Keep Alive packet"); return; } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/AdvancementsPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/AdvancementsPacket.java index 6081869f3..6ed240577 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/AdvancementsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/AdvancementsPacket.java @@ -92,8 +92,8 @@ public class AdvancementsPacket implements ServerPacket { public float y; private void write(PacketWriter writer) { - writer.writeSizedString(Chat.legacyTextString(title)); - writer.writeSizedString(Chat.legacyTextString(description)); + writer.writeSizedString(Chat.toJsonString(Chat.fromLegacyText(title))); + writer.writeSizedString(Chat.toJsonString(Chat.fromLegacyText(title))); writer.writeItemStack(icon); writer.writeVarInt(frameType.ordinal()); writer.writeInt(flags); diff --git a/src/main/java/net/minestom/server/network/packet/server/play/BossBarPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/BossBarPacket.java index 78cdb1840..b3be2424d 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/BossBarPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/BossBarPacket.java @@ -28,7 +28,7 @@ public class BossBarPacket implements ServerPacket { switch (action) { case ADD: - writer.writeSizedString(Chat.legacyTextString(title)); + writer.writeSizedString(Chat.toJsonString(Chat.fromLegacyText(title))); writer.writeFloat(health); writer.writeVarInt(color.ordinal()); writer.writeVarInt(division.ordinal()); @@ -41,7 +41,7 @@ public class BossBarPacket implements ServerPacket { writer.writeFloat(health); break; case UPDATE_TITLE: - writer.writeSizedString(Chat.legacyTextString(title)); + writer.writeSizedString(Chat.toJsonString(Chat.fromLegacyText(title))); break; case UPDATE_STYLE: writer.writeVarInt(color.ordinal()); diff --git a/src/main/java/net/minestom/server/network/packet/server/play/CombatEventPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/CombatEventPacket.java index 9deac0451..5cd6979ac 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/CombatEventPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/CombatEventPacket.java @@ -1,6 +1,7 @@ package net.minestom.server.network.packet.server.play; -import club.thectm.minecraft.text.TextObject; +import net.kyori.text.Component; +import net.kyori.text.TextComponent; import net.minestom.server.chat.Chat; import net.minestom.server.entity.Entity; import net.minestom.server.entity.Player; @@ -20,7 +21,7 @@ public class CombatEventPacket implements ServerPacket { private int duration; private int opponent; private int playerID; - private TextObject deathMessage; + private Component deathMessage; private CombatEventPacket() {} @@ -38,7 +39,7 @@ public class CombatEventPacket implements ServerPacket { return packet; } - public static CombatEventPacket death(Player player, Optional killer, TextObject message) { + public static CombatEventPacket death(Player player, Optional killer, Component message) { CombatEventPacket packet = new CombatEventPacket(); packet.type = EventType.DEATH; packet.playerID = player.getEntityId(); @@ -63,7 +64,7 @@ public class CombatEventPacket implements ServerPacket { case DEATH: writer.writeVarInt(playerID); writer.writeInt(opponent); - writer.writeSizedString(deathMessage.toJson().toString()); + writer.writeSizedString(Chat.toJsonString(deathMessage)); break; } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/DisconnectPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/DisconnectPacket.java index 1ab8f6866..4eaffd5f8 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/DisconnectPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/DisconnectPacket.java @@ -1,6 +1,5 @@ package net.minestom.server.network.packet.server.play; -import net.minestom.server.chat.Chat; import net.minestom.server.network.packet.PacketWriter; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; @@ -11,7 +10,7 @@ public class DisconnectPacket implements ServerPacket { @Override public void write(PacketWriter writer) { - writer.writeSizedString(Chat.legacyTextString(message)); + writer.writeSizedString(message); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ScoreboardObjectivePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ScoreboardObjectivePacket.java index 6ce6b0c56..ebf4e586a 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ScoreboardObjectivePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ScoreboardObjectivePacket.java @@ -18,7 +18,7 @@ public class ScoreboardObjectivePacket implements ServerPacket { writer.writeByte(mode); if (mode == 0 || mode == 2) { - writer.writeSizedString(Chat.legacyTextString(objectiveValue)); + writer.writeSizedString(Chat.toJsonString(Chat.fromLegacyText(objectiveValue))); writer.writeVarInt(type); } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/TeamsPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/TeamsPacket.java index a2eb77eaf..33584cc32 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/TeamsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/TeamsPacket.java @@ -27,13 +27,13 @@ public class TeamsPacket implements ServerPacket { switch (action) { case CREATE_TEAM: case UPDATE_TEAM_INFO: - writer.writeSizedString(Chat.legacyTextString(teamDisplayName)); + writer.writeSizedString(Chat.toJsonString(Chat.fromLegacyText(teamDisplayName))); writer.writeByte(friendlyFlags); writer.writeSizedString(nameTagVisibility.getIdentifier()); writer.writeSizedString(collisionRule.getIdentifier()); writer.writeVarInt(teamColor); - writer.writeSizedString(Chat.legacyTextString(teamPrefix)); - writer.writeSizedString(Chat.legacyTextString(teamSuffix)); + writer.writeSizedString(Chat.toJsonString(Chat.fromLegacyText(teamPrefix))); + writer.writeSizedString(Chat.toJsonString(Chat.fromLegacyText(teamSuffix))); break; case REMOVE_TEAM: diff --git a/src/main/java/net/minestom/server/network/packet/server/play/TitlePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/TitlePacket.java index 028e86a4e..41014cf9a 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/TitlePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/TitlePacket.java @@ -1,6 +1,5 @@ package net.minestom.server.network.packet.server.play; -import net.minestom.server.chat.Chat; import net.minestom.server.network.packet.PacketWriter; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; @@ -25,13 +24,13 @@ public class TitlePacket implements ServerPacket { switch (action) { case SET_TITLE: - writer.writeSizedString(Chat.legacyTextString(titleText)); + writer.writeSizedString(titleText); break; case SET_SUBTITLE: - writer.writeSizedString(Chat.legacyTextString(subtitleText)); + writer.writeSizedString(subtitleText); break; case SET_ACTION_BAR: - writer.writeSizedString(Chat.legacyTextString(actionBarText)); + writer.writeSizedString(actionBarText); break; case SET_TIMES_AND_DISPLAY: writer.writeInt(fadeIn); diff --git a/src/main/java/net/minestom/server/scoreboard/Team.java b/src/main/java/net/minestom/server/scoreboard/Team.java index 5ff0386db..45e36b1c8 100644 --- a/src/main/java/net/minestom/server/scoreboard/Team.java +++ b/src/main/java/net/minestom/server/scoreboard/Team.java @@ -1,7 +1,7 @@ package net.minestom.server.scoreboard; import io.netty.buffer.ByteBuf; -import net.minestom.server.chat.ChatColor; +import net.kyori.text.format.TextColor; import net.minestom.server.entity.Player; import net.minestom.server.network.packet.server.play.TeamsPacket; import net.minestom.server.utils.PacketUtils; @@ -17,7 +17,7 @@ public class Team { private byte friendlyFlags = 0x00; private TeamsPacket.NameTagVisibility nameTagVisibility = TeamsPacket.NameTagVisibility.ALWAYS; private TeamsPacket.CollisionRule collisionRule = TeamsPacket.CollisionRule.NEVER; - private ChatColor teamColor = ChatColor.WHITE; + private TextColor teamColor = TextColor.WHITE; private String prefix = "", suffix = ""; private String[] entities = new String[0]; private Set players = new CopyOnWriteArraySet<>(); @@ -36,7 +36,7 @@ public class Team { teamsCreationPacket.friendlyFlags = friendlyFlags; teamsCreationPacket.nameTagVisibility = nameTagVisibility; teamsCreationPacket.collisionRule = collisionRule; - teamsCreationPacket.teamColor = teamColor.getId(); + teamsCreationPacket.teamColor = teamColor.ordinal(); teamsCreationPacket.teamPrefix = prefix; teamsCreationPacket.teamSuffix = suffix; teamsCreationPacket.entities = entities; @@ -107,9 +107,9 @@ public class Team { sendUpdatePacket(); } - public void setTeamColor(ChatColor teamColor) { + public void setTeamColor(TextColor teamColor) { this.teamColor = teamColor; - this.teamsCreationPacket.teamColor = teamColor.getId(); + this.teamsCreationPacket.teamColor = teamColor.ordinal(); sendUpdatePacket(); } @@ -152,7 +152,7 @@ public class Team { updatePacket.friendlyFlags = friendlyFlags; updatePacket.nameTagVisibility = nameTagVisibility; updatePacket.collisionRule = collisionRule; - updatePacket.teamColor = teamColor.getId(); + updatePacket.teamColor = teamColor.ordinal(); updatePacket.teamPrefix = prefix; updatePacket.teamSuffix = suffix; ByteBuf buffer = PacketUtils.writePacket(updatePacket); diff --git a/src/main/java/net/minestom/server/utils/NbtReaderUtils.java b/src/main/java/net/minestom/server/utils/NbtReaderUtils.java index 4022a8104..ff8d59df6 100644 --- a/src/main/java/net/minestom/server/utils/NbtReaderUtils.java +++ b/src/main/java/net/minestom/server/utils/NbtReaderUtils.java @@ -1,8 +1,7 @@ package net.minestom.server.utils; -import club.thectm.minecraft.text.LegacyText; -import club.thectm.minecraft.text.TextObject; -import com.google.gson.JsonParser; +import net.kyori.text.Component; +import net.minestom.server.chat.Chat; import net.minestom.server.item.Enchantment; import net.minestom.server.item.ItemStack; import net.minestom.server.network.packet.PacketReader; @@ -122,8 +121,9 @@ public class NbtReaderUtils { if (stringName.equals("Name")) { String jsonDisplayName = reader.readShortSizedString(); - TextObject textObject = TextObject.fromJson(new JsonParser().parse(jsonDisplayName).getAsJsonObject()); - String displayName = LegacyText.toLegacy(textObject); + Component textObject = Chat.fromJsonString(jsonDisplayName); + String displayName = Chat.toLegacyText(textObject); + item.setDisplayName(displayName); readItemStackDisplayNBT(reader, item); } @@ -139,9 +139,9 @@ public class NbtReaderUtils { ArrayList lore = new ArrayList<>(size); for (int i = 0; i < size; i++) { String string = reader.readShortSizedString(); + Component textObject = Chat.fromJsonString(string); + String line = Chat.toLegacyText(textObject); - TextObject textObject = TextObject.fromJson(new JsonParser().parse(string).getAsJsonObject()); - String line = LegacyText.toLegacy(textObject); lore.add(line); if (lore.size() == size) { item.setLore(lore); diff --git a/src/main/java/net/minestom/server/utils/Utils.java b/src/main/java/net/minestom/server/utils/Utils.java index 7ab3faed5..f475af4ec 100644 --- a/src/main/java/net/minestom/server/utils/Utils.java +++ b/src/main/java/net/minestom/server/utils/Utils.java @@ -117,7 +117,7 @@ public class Utils { if (hasDisplayName) { packet.writeByte((byte) 0x08); packet.writeShortSizedString("Name"); - packet.writeShortSizedString(Chat.legacyTextString(itemStack.getDisplayName())); + packet.writeShortSizedString(Chat.toJsonString(Chat.fromLegacyText(itemStack.getDisplayName()))); } if (hasLore) { @@ -128,7 +128,7 @@ public class Utils { packet.writeByte((byte) 0x08); packet.writeInt(lore.size()); for (String line : lore) { - packet.writeShortSizedString(Chat.legacyTextString(line)); + packet.writeShortSizedString(Chat.toJsonString(Chat.fromLegacyText(line))); } }