diff --git a/build.gradle b/build.gradle index b11240fed..2f09b976d 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,7 @@ plugins { id 'java' id 'net.ltgt.apt' version '0.10' + id 'com.github.johnrengelman.shadow' version '4.0.4' } group 'net.minestom.server' @@ -11,7 +12,6 @@ sourceCompatibility = 1.11 repositories { mavenCentral() maven { url 'https://jitpack.io' } - } @@ -34,7 +34,6 @@ dependencies { implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.5' compile 'com.github.TheMode:CommandBuilder:f893cfbfe4' - compile 'com.github.Minestom:minecraft-text:69fd808e92' // https://jitpack.io/#Articdive/Jnoise/1.0-SNAPSHOT compile 'com.github.Articdive:Jnoise:1.0-SNAPSHOT' @@ -42,4 +41,8 @@ dependencies { // https://mvnrepository.com/artifact/javax.vecmath/vecmath compile group: 'javax.vecmath', name: 'vecmath', version: '1.5.2' // Used for Fastnoise + compile 'net.kyori:text-api:3.0.3' + compile 'net.kyori:text-serializer-legacy:3.0.3' + compile 'net.kyori:text-serializer-gson:3.0.3' + compile '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..fc96687f0 100644 --- a/src/main/java/net/minestom/server/chat/Chat.java +++ b/src/main/java/net/minestom/server/chat/Chat.java @@ -1,7 +1,10 @@ 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; +import net.kyori.text.serializer.plain.PlainComponentSerializer; /** * Thank for the minecraft-text library made by rbrick: @@ -11,23 +14,35 @@ 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) { + public static Component fromJsonString(String json) { + return GsonComponentSerializer.INSTANCE.deserialize(json); + } + + public static String toLegacyText(Component component) { + return LegacyComponentSerializer.legacyLinking().serialize(component); + } + + public static TextComponent legacyText(String text, char colorChar) { + return LegacyComponentSerializer.legacyLinking().deserialize(text, colorChar); + } + + public static TextComponent legacyText(String text) { return legacyText(text, COLOR_CHAR); } - public static String legacyTextString(String text, char colorChar) { - return legacyText(text, colorChar).toJson().toString(); - } - public static String legacyTextString(String text) { - return legacyText(text).toJson().toString(); + // TODO: Find out where this is used and ensure this is correct + return GsonComponentSerializer.INSTANCE.serialize(legacyText(text, COLOR_CHAR)); } public static String uncoloredLegacyText(String text) { - return text.replace(String.valueOf(COLOR_CHAR), ""); + // TODO: Find out where this is used and ensure this is correct + // TODO: Improve this, I'm not sure the old method is correct + return PlainComponentSerializer.INSTANCE.serialize(legacyText(text)); } + } 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 5f442d8ba..28c02a95e 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.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; + TextComponent 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; + TextComponent 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); @@ -411,8 +410,8 @@ public class Player extends LivingEntity { playerConnection.sendPacket(chatMessagePacket); } - public void sendMessage(TextObject textObject) { - sendMessage(textObject.toJson()); + public void sendMessage(TextComponent textObject) { + sendMessage(Chat.toJsonString(textObject)); } public void playSound(Sound sound, SoundCategory soundCategory, int x, int y, int z, float volume, float pitch) { @@ -457,8 +456,8 @@ public class Player extends LivingEntity { 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(Chat.legacyText(header, colorChar)); + playerListHeaderAndFooterPacket.footer = Chat.toJsonString(Chat.legacyText(footer, colorChar)); playerConnection.sendPacket(playerListHeaderAndFooterPacket); } @@ -466,7 +465,7 @@ public class Player extends LivingEntity { public void sendActionBarMessage(String message, char colorChar) { TitlePacket titlePacket = new TitlePacket(); titlePacket.action = TitlePacket.Action.SET_ACTION_BAR; - titlePacket.actionBarText = Chat.legacyText(message, colorChar).toJson().toString(); + titlePacket.actionBarText = Chat.toJsonString(Chat.legacyText(message, colorChar)); playerConnection.sendPacket(titlePacket); } 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 e382e9008..16bbbaa67 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,7 @@ package net.minestom.server.entity.damage; -import club.thectm.minecraft.text.TextBuilder; -import club.thectm.minecraft.text.TextObject; +import net.kyori.text.TextComponent; +import net.kyori.text.TranslatableComponent; import net.minestom.server.entity.Entity; import net.minestom.server.entity.Player; @@ -26,15 +26,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 TextComponent buildChatMessage(Player killed) { + return TextComponent.builder().append(TranslatableComponent.of("death."+identifier)).append(killed.getUsername()).build(); } public static DamageType fromPlayer(Player player) { return new EntityDamage(player); } - public TextObject buildDeathScreenMessage(Player killed) { + public TextComponent 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..261f16ecf 100644 --- a/src/main/java/net/minestom/server/listener/ChatMessageListener.java +++ b/src/main/java/net/minestom/server/listener/ChatMessageListener.java @@ -1,6 +1,9 @@ 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.minestom.server.MinecraftServer; import net.minestom.server.chat.Chat; import net.minestom.server.command.CommandManager; @@ -38,12 +41,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 +53,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/CombatEventPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/CombatEventPacket.java index 9deac0451..d02d71456 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,6 @@ package net.minestom.server.network.packet.server.play; -import club.thectm.minecraft.text.TextObject; +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 +20,7 @@ public class CombatEventPacket implements ServerPacket { private int duration; private int opponent; private int playerID; - private TextObject deathMessage; + private TextComponent deathMessage; private CombatEventPacket() {} @@ -38,7 +38,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, TextComponent message) { CombatEventPacket packet = new CombatEventPacket(); packet.type = EventType.DEATH; packet.playerID = player.getEntityId(); @@ -63,7 +63,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/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..631825b96 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,8 @@ 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); } @@ -140,8 +139,8 @@ public class NbtReaderUtils { for (int i = 0; i < size; i++) { String string = reader.readShortSizedString(); - TextObject textObject = TextObject.fromJson(new JsonParser().parse(string).getAsJsonObject()); - String line = LegacyText.toLegacy(textObject); + Component textObject = Chat.fromJsonString(string); + String line = Chat.toLegacyText(textObject); lore.add(line); if (lore.size() == size) { item.setLore(lore);