Use kyori/text instead of minecraft-text

This commit is contained in:
Draycia 2020-05-03 04:59:53 -07:00
parent 7fd8362d6c
commit 83bb14d5a0
11 changed files with 79 additions and 100 deletions

View File

@ -1,6 +1,7 @@
plugins { plugins {
id 'java' id 'java'
id 'net.ltgt.apt' version '0.10' id 'net.ltgt.apt' version '0.10'
id 'com.github.johnrengelman.shadow' version '4.0.4'
} }
group 'net.minestom.server' group 'net.minestom.server'
@ -11,7 +12,6 @@ sourceCompatibility = 1.11
repositories { repositories {
mavenCentral() mavenCentral()
maven { url 'https://jitpack.io' } maven { url 'https://jitpack.io' }
} }
@ -34,7 +34,6 @@ dependencies {
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.5' implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
compile 'com.github.TheMode:CommandBuilder:f893cfbfe4' compile 'com.github.TheMode:CommandBuilder:f893cfbfe4'
compile 'com.github.Minestom:minecraft-text:69fd808e92'
// https://jitpack.io/#Articdive/Jnoise/1.0-SNAPSHOT // https://jitpack.io/#Articdive/Jnoise/1.0-SNAPSHOT
compile 'com.github.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 // https://mvnrepository.com/artifact/javax.vecmath/vecmath
compile group: 'javax.vecmath', name: 'vecmath', version: '1.5.2' // Used for Fastnoise 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'
} }

View File

@ -1,7 +1,10 @@
package net.minestom.server.chat; package net.minestom.server.chat;
import club.thectm.minecraft.text.LegacyText; import net.kyori.text.Component;
import club.thectm.minecraft.text.TextObject; 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: * 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 final char COLOR_CHAR = (char) 0xA7; // Represent the character '§'
public static TextObject legacyText(String text, char colorChar) { public static String toJsonString(Component component) {
return LegacyText.fromLegacy(text, colorChar); 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); 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) { 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) { 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));
} }
} }

View File

@ -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));
}
}

View File

@ -1,8 +1,7 @@
package net.minestom.server.entity; package net.minestom.server.entity;
import club.thectm.minecraft.text.TextBuilder;
import club.thectm.minecraft.text.TextObject;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import net.kyori.text.TextComponent;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.bossbar.BossBar; import net.minestom.server.bossbar.BossBar;
import net.minestom.server.chat.Chat; import net.minestom.server.chat.Chat;
@ -249,21 +248,21 @@ public class Player extends LivingEntity {
public void kill() { public void kill() {
if (!isDead()) { if (!isDead()) {
// send death message to player // send death message to player
TextObject deathMessage; TextComponent deathMessage;
if (lastDamageSource != null) { if (lastDamageSource != null) {
deathMessage = lastDamageSource.buildDeathScreenMessage(this); deathMessage = lastDamageSource.buildDeathScreenMessage(this);
} else { // may happen if killed by the server without applying damage } 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); CombatEventPacket deathPacket = CombatEventPacket.death(this, Optional.empty(), deathMessage);
playerConnection.sendPacket(deathPacket); playerConnection.sendPacket(deathPacket);
// send death message to chat // send death message to chat
TextObject chatMessage; TextComponent chatMessage;
if (lastDamageSource != null) { if (lastDamageSource != null) {
chatMessage = lastDamageSource.buildChatMessage(this); chatMessage = lastDamageSource.buildChatMessage(this);
} else { // may happen if killed by the server without applying damage } 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 -> { MinecraftServer.getConnectionManager().getOnlinePlayers().forEach(player -> {
player.sendMessage(chatMessage); player.sendMessage(chatMessage);
@ -411,8 +410,8 @@ public class Player extends LivingEntity {
playerConnection.sendPacket(chatMessagePacket); playerConnection.sendPacket(chatMessagePacket);
} }
public void sendMessage(TextObject textObject) { public void sendMessage(TextComponent textObject) {
sendMessage(textObject.toJson()); sendMessage(Chat.toJsonString(textObject));
} }
public void playSound(Sound sound, SoundCategory soundCategory, int x, int y, int z, float volume, float pitch) { 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.emptyHeader = header == null;
playerListHeaderAndFooterPacket.emptyFooter = footer == null; playerListHeaderAndFooterPacket.emptyFooter = footer == null;
playerListHeaderAndFooterPacket.header = Chat.legacyText(header, colorChar).toJson().toString(); playerListHeaderAndFooterPacket.header = Chat.toJsonString(Chat.legacyText(header, colorChar));
playerListHeaderAndFooterPacket.footer = Chat.legacyText(footer, colorChar).toJson().toString(); playerListHeaderAndFooterPacket.footer = Chat.toJsonString(Chat.legacyText(footer, colorChar));
playerConnection.sendPacket(playerListHeaderAndFooterPacket); playerConnection.sendPacket(playerListHeaderAndFooterPacket);
} }
@ -466,7 +465,7 @@ public class Player extends LivingEntity {
public void sendActionBarMessage(String message, char colorChar) { public void sendActionBarMessage(String message, char colorChar) {
TitlePacket titlePacket = new TitlePacket(); TitlePacket titlePacket = new TitlePacket();
titlePacket.action = TitlePacket.Action.SET_ACTION_BAR; 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); playerConnection.sendPacket(titlePacket);
} }

View File

@ -1,7 +1,7 @@
package net.minestom.server.entity.damage; package net.minestom.server.entity.damage;
import club.thectm.minecraft.text.TextBuilder; import net.kyori.text.TextComponent;
import club.thectm.minecraft.text.TextObject; import net.kyori.text.TranslatableComponent;
import net.minestom.server.entity.Entity; import net.minestom.server.entity.Entity;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
@ -26,15 +26,15 @@ public class DamageType {
return new EntityProjectileDamage(shooter, projectile); return new EntityProjectileDamage(shooter, projectile);
} }
public TextObject buildChatMessage(Player killed) { public TextComponent buildChatMessage(Player killed) {
return TextBuilder.ofTranslation("death."+identifier, TextBuilder.of(killed.getUsername()).build()).build(); return TextComponent.builder().append(TranslatableComponent.of("death."+identifier)).append(killed.getUsername()).build();
} }
public static DamageType fromPlayer(Player player) { public static DamageType fromPlayer(Player player) {
return new EntityDamage(player); return new EntityDamage(player);
} }
public TextObject buildDeathScreenMessage(Player killed) { public TextComponent buildDeathScreenMessage(Player killed) {
return buildChatMessage(killed); return buildChatMessage(killed);
} }
} }

View File

@ -1,6 +1,6 @@
package net.minestom.server.event; package net.minestom.server.event;
import club.thectm.minecraft.text.TextObject; import net.kyori.text.TextComponent;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import java.util.ArrayList; import java.util.ArrayList;
@ -12,7 +12,7 @@ public class PlayerChatEvent extends CancellableEvent {
private Player sender; private Player sender;
private Collection<Player> recipients; private Collection<Player> recipients;
private String message; private String message;
private Function<PlayerChatEvent, TextObject> chatFormat; private Function<PlayerChatEvent, TextComponent> chatFormat;
public PlayerChatEvent(Player sender, Collection<Player> recipients, String message) { public PlayerChatEvent(Player sender, Collection<Player> recipients, String message) {
this.sender = sender; this.sender = sender;
@ -20,7 +20,7 @@ public class PlayerChatEvent extends CancellableEvent {
this.message = message; this.message = message;
} }
public void setChatFormat(Function<PlayerChatEvent, TextObject> chatFormat) { public void setChatFormat(Function<PlayerChatEvent, TextComponent> chatFormat) {
this.chatFormat = chatFormat; this.chatFormat = chatFormat;
} }
@ -40,7 +40,7 @@ public class PlayerChatEvent extends CancellableEvent {
this.message = message; this.message = message;
} }
public Function<PlayerChatEvent, TextObject> getChatFormatFunction() { public Function<PlayerChatEvent, TextComponent> getChatFormatFunction() {
return chatFormat; return chatFormat;
} }
} }

View File

@ -1,6 +1,9 @@
package net.minestom.server.listener; 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.MinecraftServer;
import net.minestom.server.chat.Chat; import net.minestom.server.chat.Chat;
import net.minestom.server.command.CommandManager; import net.minestom.server.command.CommandManager;
@ -38,12 +41,11 @@ public class ChatMessageListener {
playerChatEvent.setChatFormat((event) -> { playerChatEvent.setChatFormat((event) -> {
String username = player.getUsername(); String username = player.getUsername();
TextObject usernameText = TextBuilder.of(String.format("<%s>", username)) TextComponent usernameText = TextComponent.of(String.format("<%s>", username))
.color(ChatColor.WHITE) .color(TextColor.WHITE)
.hoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, ChatColor.GRAY + "Its " + username)) .hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TextComponent.of("Its " + username).color(TextColor.GRAY)))
.clickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/msg " + username + " ")) .clickEvent(ClickEvent.of(ClickEvent.Action.SUGGEST_COMMAND, "/msg " + username + " "))
.append(" " + event.getMessage()) .append(TextComponent.of(" " + event.getMessage()));
.build();
return usernameText; return usernameText;
}); });
@ -51,11 +53,11 @@ public class ChatMessageListener {
// Call the event // Call the event
player.callCancellableEvent(PlayerChatEvent.class, playerChatEvent, () -> { player.callCancellableEvent(PlayerChatEvent.class, playerChatEvent, () -> {
Function<PlayerChatEvent, TextObject> formatFunction = playerChatEvent.getChatFormatFunction(); Function<PlayerChatEvent, TextComponent> formatFunction = playerChatEvent.getChatFormatFunction();
if (formatFunction == null) if (formatFunction == null)
throw new NullPointerException("PlayerChatEvent#chatFormat cannot be null!"); throw new NullPointerException("PlayerChatEvent#chatFormat cannot be null!");
TextObject textObject = formatFunction.apply(playerChatEvent); TextComponent textObject = formatFunction.apply(playerChatEvent);
for (Player recipient : playerChatEvent.getRecipients()) { for (Player recipient : playerChatEvent.getRecipients()) {
recipient.sendMessage(textObject); recipient.sendMessage(textObject);

View File

@ -1,6 +1,6 @@
package net.minestom.server.listener; 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.entity.Player;
import net.minestom.server.network.packet.client.play.ClientKeepAlivePacket; import net.minestom.server.network.packet.client.play.ClientKeepAlivePacket;
@ -8,7 +8,7 @@ public class KeepAliveListener {
public static void listener(ClientKeepAlivePacket packet, Player player) { public static void listener(ClientKeepAlivePacket packet, Player player) {
if (packet.id != player.getLastKeepAlive()) { if (packet.id != player.getLastKeepAlive()) {
player.kick(ChatColor.RED + "Bad Keep Alive packet"); player.kick(TextColor.RED + "Bad Keep Alive packet");
return; return;
} }

View File

@ -1,6 +1,6 @@
package net.minestom.server.network.packet.server.play; 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.chat.Chat;
import net.minestom.server.entity.Entity; import net.minestom.server.entity.Entity;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
@ -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 TextObject deathMessage; private TextComponent deathMessage;
private CombatEventPacket() {} private CombatEventPacket() {}
@ -38,7 +38,7 @@ public class CombatEventPacket implements ServerPacket {
return packet; return packet;
} }
public static CombatEventPacket death(Player player, Optional<Entity> killer, TextObject message) { public static CombatEventPacket death(Player player, Optional<Entity> killer, TextComponent 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();
@ -63,7 +63,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.toJson().toString()); writer.writeSizedString(Chat.toJsonString(deathMessage));
break; break;
} }
} }

View File

@ -1,7 +1,7 @@
package net.minestom.server.scoreboard; package net.minestom.server.scoreboard;
import io.netty.buffer.ByteBuf; 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.entity.Player;
import net.minestom.server.network.packet.server.play.TeamsPacket; import net.minestom.server.network.packet.server.play.TeamsPacket;
import net.minestom.server.utils.PacketUtils; import net.minestom.server.utils.PacketUtils;
@ -17,7 +17,7 @@ public class Team {
private byte friendlyFlags = 0x00; private byte friendlyFlags = 0x00;
private TeamsPacket.NameTagVisibility nameTagVisibility = TeamsPacket.NameTagVisibility.ALWAYS; private TeamsPacket.NameTagVisibility nameTagVisibility = TeamsPacket.NameTagVisibility.ALWAYS;
private TeamsPacket.CollisionRule collisionRule = TeamsPacket.CollisionRule.NEVER; private TeamsPacket.CollisionRule collisionRule = TeamsPacket.CollisionRule.NEVER;
private ChatColor teamColor = ChatColor.WHITE; private TextColor teamColor = TextColor.WHITE;
private String prefix = "", suffix = ""; private String prefix = "", suffix = "";
private String[] entities = new String[0]; private String[] entities = new String[0];
private Set<Player> players = new CopyOnWriteArraySet<>(); private Set<Player> players = new CopyOnWriteArraySet<>();
@ -36,7 +36,7 @@ public class Team {
teamsCreationPacket.friendlyFlags = friendlyFlags; teamsCreationPacket.friendlyFlags = friendlyFlags;
teamsCreationPacket.nameTagVisibility = nameTagVisibility; teamsCreationPacket.nameTagVisibility = nameTagVisibility;
teamsCreationPacket.collisionRule = collisionRule; teamsCreationPacket.collisionRule = collisionRule;
teamsCreationPacket.teamColor = teamColor.getId(); teamsCreationPacket.teamColor = teamColor.ordinal();
teamsCreationPacket.teamPrefix = prefix; teamsCreationPacket.teamPrefix = prefix;
teamsCreationPacket.teamSuffix = suffix; teamsCreationPacket.teamSuffix = suffix;
teamsCreationPacket.entities = entities; teamsCreationPacket.entities = entities;
@ -107,9 +107,9 @@ public class Team {
sendUpdatePacket(); sendUpdatePacket();
} }
public void setTeamColor(ChatColor teamColor) { public void setTeamColor(TextColor teamColor) {
this.teamColor = teamColor; this.teamColor = teamColor;
this.teamsCreationPacket.teamColor = teamColor.getId(); this.teamsCreationPacket.teamColor = teamColor.ordinal();
sendUpdatePacket(); sendUpdatePacket();
} }
@ -152,7 +152,7 @@ public class Team {
updatePacket.friendlyFlags = friendlyFlags; updatePacket.friendlyFlags = friendlyFlags;
updatePacket.nameTagVisibility = nameTagVisibility; updatePacket.nameTagVisibility = nameTagVisibility;
updatePacket.collisionRule = collisionRule; updatePacket.collisionRule = collisionRule;
updatePacket.teamColor = teamColor.getId(); updatePacket.teamColor = teamColor.ordinal();
updatePacket.teamPrefix = prefix; updatePacket.teamPrefix = prefix;
updatePacket.teamSuffix = suffix; updatePacket.teamSuffix = suffix;
ByteBuf buffer = PacketUtils.writePacket(updatePacket); ByteBuf buffer = PacketUtils.writePacket(updatePacket);

View File

@ -1,8 +1,7 @@
package net.minestom.server.utils; package net.minestom.server.utils;
import club.thectm.minecraft.text.LegacyText; import net.kyori.text.Component;
import club.thectm.minecraft.text.TextObject; import net.minestom.server.chat.Chat;
import com.google.gson.JsonParser;
import net.minestom.server.item.Enchantment; import net.minestom.server.item.Enchantment;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.PacketReader; import net.minestom.server.network.packet.PacketReader;
@ -122,8 +121,8 @@ public class NbtReaderUtils {
if (stringName.equals("Name")) { if (stringName.equals("Name")) {
String jsonDisplayName = reader.readShortSizedString(); String jsonDisplayName = reader.readShortSizedString();
TextObject textObject = TextObject.fromJson(new JsonParser().parse(jsonDisplayName).getAsJsonObject()); Component textObject = Chat.fromJsonString(jsonDisplayName);
String displayName = LegacyText.toLegacy(textObject); String displayName = Chat.toLegacyText(textObject);
item.setDisplayName(displayName); item.setDisplayName(displayName);
readItemStackDisplayNBT(reader, item); readItemStackDisplayNBT(reader, item);
} }
@ -140,8 +139,8 @@ public class NbtReaderUtils {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
String string = reader.readShortSizedString(); String string = reader.readShortSizedString();
TextObject textObject = TextObject.fromJson(new JsonParser().parse(string).getAsJsonObject()); Component textObject = Chat.fromJsonString(string);
String line = LegacyText.toLegacy(textObject); String line = Chat.toLegacyText(textObject);
lore.add(line); lore.add(line);
if (lore.size() == size) { if (lore.size() == size) {
item.setLore(lore); item.setLore(lore);