From 7496b0a2c8b7be45c5414b87abde4a1e9b995948 Mon Sep 17 00:00:00 2001 From: kamcio96 Date: Sat, 19 Jan 2019 19:48:29 +0100 Subject: [PATCH] #2584: Add ComponentBuilder#appendLegacy --- .../net/md_5/bungee/api/chat/ComponentBuilder.java | 13 +++++++++++++ .../net/md_5/bungee/api/chat/ComponentsTest.java | 12 ++++++++++++ 2 files changed, 25 insertions(+) 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 dfdc7bf0..6e8306ff 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 @@ -146,6 +146,19 @@ public final class ComponentBuilder return append( text, FormatRetention.ALL ); } + /** + * Parse text to BaseComponent[] with colors and format, appends the text to + * the builder and makes it the current target for formatting. The component + * will have all the formatting from previous part. + * + * @param text the text to append + * @return this ComponentBuilder for chaining + */ + public ComponentBuilder appendLegacy(String text) + { + return append( TextComponent.fromLegacyText( text ) ); + } + /** * Appends the text to the builder and makes it the current target for * formatting. You can specify the amount of formatting retained from 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 27b5bbea..bb38f1b1 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 @@ -92,6 +92,18 @@ public class ComponentsTest Assert.assertEquals( ChatColor.YELLOW + "Hello " + ChatColor.GREEN + "world!", BaseComponent.toLegacyText( components ) ); } + @Test + public void testBuilderAppendLegacy() + { + ComponentBuilder builder = new ComponentBuilder( "Hello " ).color( ChatColor.YELLOW ); + builder.appendLegacy( "§aworld!" ); + + BaseComponent[] components = builder.create(); + + Assert.assertEquals( "Hello world!", BaseComponent.toPlainText( components ) ); + Assert.assertEquals( ChatColor.YELLOW + "Hello " + ChatColor.GREEN + "world!", BaseComponent.toLegacyText( components ) ); + } + @Test public void testBasicComponent() {