From 8bbbcdd355946b545bb4ed24bcde899f8866fea9 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sun, 14 Aug 2022 16:02:01 +0800 Subject: Bugfix: keep colors when converting messages. --- .../com/keuin/crosslink/messaging/action/FormatAction.java | 3 --- .../crosslink/messaging/message/ComponentBackedMessage.java | 6 +++++- .../com/keuin/crosslink/messaging/message/IMessage.java | 11 +++++++++-- .../java/com/keuin/crosslink/messaging/util/Messaging.java | 13 +++++++++++-- .../com/keuin/crosslink/plugin/bungee/BungeeAccessor.java | 7 ++----- 5 files changed, 27 insertions(+), 13 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/keuin/crosslink/messaging/action/FormatAction.java b/src/main/java/com/keuin/crosslink/messaging/action/FormatAction.java index cc8b554..420f2da 100644 --- a/src/main/java/com/keuin/crosslink/messaging/action/FormatAction.java +++ b/src/main/java/com/keuin/crosslink/messaging/action/FormatAction.java @@ -9,14 +9,12 @@ import net.kyori.adventure.text.format.TextDecoration; public class FormatAction extends BaseReplaceAction { public FormatAction(TextColor color) { super((message) -> { - // FIXME non-text-based message may lose information? var formatted = Messaging.duplicate(message.kyoriMessage()); return IMessage.create(message.source(), message.sender(), formatted.color(color)); }); } public FormatAction(TextDecoration decor) { super((message) -> { - // FIXME non-text-based message may lose information? // FIXME clear other decorations var formatted = Messaging.duplicate(message.kyoriMessage()); return IMessage.create(message.source(), message.sender(), formatted.decorate(decor)); @@ -24,7 +22,6 @@ public class FormatAction extends BaseReplaceAction { } public FormatAction(Style style) { super((message) -> { - // FIXME non-text-based message may lose information? // FIXME clear other styles var formatted = Messaging.duplicate(message.kyoriMessage()); return IMessage.create(message.source(), message.sender(), formatted.style(style)); diff --git a/src/main/java/com/keuin/crosslink/messaging/message/ComponentBackedMessage.java b/src/main/java/com/keuin/crosslink/messaging/message/ComponentBackedMessage.java index 4353c84..c3d45ad 100644 --- a/src/main/java/com/keuin/crosslink/messaging/message/ComponentBackedMessage.java +++ b/src/main/java/com/keuin/crosslink/messaging/message/ComponentBackedMessage.java @@ -6,6 +6,7 @@ import com.keuin.crosslink.messaging.util.Messaging; import com.keuin.crosslink.util.LazyEvaluated; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; +import net.md_5.bungee.api.chat.BaseComponent; import org.jetbrains.annotations.NotNull; import java.util.Objects; @@ -46,5 +47,8 @@ public class ComponentBackedMessage implements IMessage { return Messaging.duplicate(component); } - // FIXME implement bungeeMessage + @Override + public BaseComponent[] bungeeMessage() { + return Messaging.kyoriComponentToBungee(component); + } } diff --git a/src/main/java/com/keuin/crosslink/messaging/message/IMessage.java b/src/main/java/com/keuin/crosslink/messaging/message/IMessage.java index d2340b8..6d68336 100644 --- a/src/main/java/com/keuin/crosslink/messaging/message/IMessage.java +++ b/src/main/java/com/keuin/crosslink/messaging/message/IMessage.java @@ -3,6 +3,7 @@ package com.keuin.crosslink.messaging.message; import com.keuin.crosslink.messaging.endpoint.IEndpoint; import com.keuin.crosslink.messaging.sender.ISender; +import com.keuin.crosslink.messaging.util.Messaging; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.JoinConfiguration; import net.kyori.adventure.text.format.Style; @@ -23,8 +24,11 @@ public interface IMessage { @NotNull String pureString(); + /** + * This is a fallback (basic) implementation. + * Color information will be discarded after converting. + */ default Component kyoriMessage() { - // FIXME keep color data return Component.text().content(pureString()).build(); } @@ -49,8 +53,11 @@ public interface IMessage { return new ComponentBackedMessage(source, sender, component); } + /** + * This is a fallback (basic) implementation. + * Color information will be discarded after converting. + */ default BaseComponent[] bungeeMessage() { - // FIXME keep color data return new ComponentBuilder().append(pureString()).create(); } diff --git a/src/main/java/com/keuin/crosslink/messaging/util/Messaging.java b/src/main/java/com/keuin/crosslink/messaging/util/Messaging.java index 1029fe0..ae5f04a 100644 --- a/src/main/java/com/keuin/crosslink/messaging/util/Messaging.java +++ b/src/main/java/com/keuin/crosslink/messaging/util/Messaging.java @@ -1,6 +1,9 @@ package com.keuin.crosslink.messaging.util; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.TextComponent; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -9,9 +12,9 @@ import java.util.regex.Pattern; public class Messaging { public static @NotNull Component duplicate(@NotNull Component source) { - // FIXME non-text-based message may lose information? Objects.requireNonNull(source); - return Component.text().append(source).build(); + var ser = LegacyComponentSerializer.legacySection().serialize(source); + return LegacyComponentSerializer.legacySection().deserialize(ser); } public static @Nullable String[] splitIdSelector(@NotNull String pattern) { @@ -26,4 +29,10 @@ public class Messaging { } return parts; } + + public static @NotNull BaseComponent[] kyoriComponentToBungee(@NotNull Component comp) { + // convert between two incompatible Component objects using legacy string such as "&6Hello &b&lworld&c!" + return TextComponent.fromLegacyText( + LegacyComponentSerializer.legacySection().serialize(comp)); + } } diff --git a/src/main/java/com/keuin/crosslink/plugin/bungee/BungeeAccessor.java b/src/main/java/com/keuin/crosslink/plugin/bungee/BungeeAccessor.java index dd3fd4f..2e35426 100644 --- a/src/main/java/com/keuin/crosslink/plugin/bungee/BungeeAccessor.java +++ b/src/main/java/com/keuin/crosslink/plugin/bungee/BungeeAccessor.java @@ -5,11 +5,10 @@ import com.keuin.crosslink.data.PlayerInfo; import com.keuin.crosslink.data.ServerInfo; import com.keuin.crosslink.messaging.endpoint.IEndpoint; import com.keuin.crosslink.messaging.endpoint.local.BungeeServerChatEndpoint; +import com.keuin.crosslink.messaging.util.Messaging; import com.keuin.crosslink.plugin.bungee.checker.BungeeServerStatusChecker; import com.keuin.crosslink.plugin.common.ICoreAccessor; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import net.md_5.bungee.api.chat.TextComponent; import java.util.List; import java.util.Objects; @@ -63,9 +62,7 @@ public class BungeeAccessor implements ICoreAccessor { public void sendPlayerMessage(UUID playerUuid, Component message) { var player = plugin.getProxy().getPlayer(playerUuid); if (player == null) return; - // convert between two incompatible Component objects using legacy string such as "&6Hello &b&lworld&c!" - var msg = TextComponent - .fromLegacyText(LegacyComponentSerializer.legacySection().serialize(message)); + var msg = Messaging.kyoriComponentToBungee(message); player.sendMessage(msg); } } -- cgit v1.2.3