From 2a716bbc7f91887e31bbc37fb608581673893605 Mon Sep 17 00:00:00 2001 From: Janmm14 Date: Thu, 30 Jun 2022 13:22:16 +0200 Subject: [PATCH] #3344: Fix legacy chat conversion losing format reset information. In legacy chat format, colors and reset do not retain any formatting. In order to prevent this behaviour from creating unnecessary long json containing many redundant `formatting: false`, the original `fromLegacyText(...)` idea was to just override the color to white and handle the format reset just internally. However eventual previous format rejection (aka reset) information was lost when appending multiple legacy format strings to a `ComponentBuilder`. With this change we save the "reset wish" in the `BaseComponent` and update `ComponentBuilder`'s append function to not copy over formatting if the component has the reset flag set. --- .../md_5/bungee/api/chat/BaseComponent.java | 6 ++++ .../bungee/api/chat/ComponentBuilder.java | 2 +- .../md_5/bungee/api/chat/TextComponent.java | 12 +++---- .../md_5/bungee/api/chat/ComponentsTest.java | 32 ++++++++++++++++++- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java index 60213def..ea183cdb 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java @@ -78,6 +78,12 @@ public abstract class BaseComponent @Getter private HoverEvent hoverEvent; + /** + * Whether this component rejects previous formatting + */ + @Getter + private transient boolean reset; + /** * Default constructor. * diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/ComponentBuilder.java b/chat/src/main/java/net/md_5/bungee/api/chat/ComponentBuilder.java index a7e50537..bf990f1b 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/ComponentBuilder.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/ComponentBuilder.java @@ -161,7 +161,7 @@ public final class ComponentBuilder previous = dummy; dummy = null; } - if ( previous != null ) + if ( previous != null && !component.isReset() ) { component.copyFormatting( previous, retention, false ); } diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java index 8601793a..4371374a 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java @@ -44,7 +44,7 @@ public final class TextComponent extends BaseComponent */ public static BaseComponent[] fromLegacyText(String message, ChatColor defaultColor) { - ArrayList components = new ArrayList(); + ArrayList components = new ArrayList<>(); StringBuilder builder = new StringBuilder(); TextComponent component = new TextComponent(); Matcher matcher = url.matcher( message ); @@ -111,15 +111,15 @@ public final class TextComponent extends BaseComponent } else if ( format == ChatColor.MAGIC ) { component.setObfuscated( true ); - } else if ( format == ChatColor.RESET ) - { - format = defaultColor; - component = new TextComponent(); - component.setColor( format ); } else { + if ( format == ChatColor.RESET ) + { + format = defaultColor; + } component = new TextComponent(); component.setColor( format ); + component.setReset( true ); } continue; } diff --git a/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java b/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java index 20c62f1d..9884f29b 100644 --- a/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java +++ b/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java @@ -572,7 +572,37 @@ public class ComponentsTest Assert.assertArrayEquals( hexColored, reColored ); } - private String fromAndToLegacyText(String legacyText) + /** + * In legacy chat, colors and reset both reset all formatting. + * Make sure it works in combination with ComponentBuilder. + */ + @Test + public void testLegacyResetInBuilder() + { + ComponentBuilder builder = new ComponentBuilder(); + BaseComponent[] a = TextComponent.fromLegacyText( "§4§n44444§rdd§6§l6666" ); + + String expected = "{\"extra\":[{\"underlined\":true,\"color\":\"dark_red\",\"text\":\"44444\"},{\"color\":" + + "\"white\",\"text\":\"dd\"},{\"bold\":true,\"color\":\"gold\",\"text\":\"6666\"}],\"text\":\"\"}"; + Assert.assertEquals( expected, ComponentSerializer.toString( a ) ); + + builder.append( a ); + + String test1 = ComponentSerializer.toString( builder.create() ); + Assert.assertEquals( expected, test1 ); + + BaseComponent[] b = TextComponent.fromLegacyText( "§rrrrr" ); + builder.append( b ); + + String test2 = ComponentSerializer.toString( builder.create() ); + Assert.assertEquals( + "{\"extra\":[{\"underlined\":true,\"color\":\"dark_red\",\"text\":\"44444\"}," + + "{\"color\":\"white\",\"text\":\"dd\"},{\"bold\":true,\"color\":\"gold\",\"text\":\"6666\"}," + + "{\"color\":\"white\",\"text\":\"rrrr\"}],\"text\":\"\"}", + test2 ); + } + + private static String fromAndToLegacyText(String legacyText) { return BaseComponent.toLegacyText( TextComponent.fromLegacyText( legacyText ) ); }