From e58bb50a6d7d155c44737bc8861963445b5bdc5a Mon Sep 17 00:00:00 2001 From: Janmm14 Date: Sun, 21 Apr 2024 12:29:29 +0200 Subject: [PATCH] Create shorter toString of components - Stop mentioning null values - Use shorter names - seemless integration of style's toString Also use just one string builder and keep track of whether a comma is needed before the next property. --- .../md_5/bungee/api/chat/BaseComponent.java | 52 +++++++++++++++++- .../md_5/bungee/api/chat/ComponentStyle.java | 55 +++++++++++++++++++ .../bungee/api/chat/KeybindComponent.java | 18 +++++- .../md_5/bungee/api/chat/ScoreComponent.java | 22 +++++++- .../bungee/api/chat/SelectorComponent.java | 21 ++++++- .../md_5/bungee/api/chat/TextComponent.java | 17 +++++- .../api/chat/TranslatableComponent.java | 26 ++++++++- 7 files changed, 200 insertions(+), 11 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 558343c3380..7d6ff556518 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 @@ -11,7 +11,6 @@ import net.md_5.bungee.api.chat.ComponentBuilder.FormatRetention; @Setter -@ToString(exclude = "parent") @EqualsAndHashCode(exclude = "parent") public abstract class BaseComponent { @@ -690,4 +689,55 @@ void addFormat(StringBuilder builder) builder.append( ChatColor.MAGIC ); } } + + @Override + public String toString() + { + StringBuilder builder = new StringBuilder( toStringName() ).append( "{" ); + toString( builder, false ); + return builder.append( "}" ).toString(); + } + + String toStringName() + { + return "BaseComponent"; + } + + boolean toString(StringBuilder builder, boolean comma) + { + if ( style != null ) + { + comma = style.toString( builder, comma ); + } + if ( insertion != null ) + { + if ( comma ) builder.append( ", " ); + comma = true; + builder.append( "insertion=" ).append( insertion ); + } + if ( clickEvent != null ) + { + if ( comma ) builder.append( ", " ); + comma = true; + builder.append( "clickEvent=" ).append( clickEvent ); + } + if ( hoverEvent != null ) + { + if ( comma ) builder.append( ", " ); + comma = true; + builder.append( "hoverEvent=" ).append( hoverEvent ); + } + if ( reset ) + { + if ( comma ) builder.append( ", " ); + comma = true; + builder.append( "reset" ); + } + if ( extra != null ) + { + if ( comma ) builder.append( ", " ); + builder.append( "extra=" ).append( extra ); + } + return comma; + } } diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/ComponentStyle.java b/chat/src/main/java/net/md_5/bungee/api/chat/ComponentStyle.java index 15f44ea730c..34a61542e28 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/ComponentStyle.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/ComponentStyle.java @@ -200,6 +200,61 @@ public boolean isEmpty() && strikethrough == null && obfuscated == null; } + @Override + public String toString() + { + StringBuilder builder = new StringBuilder( "ComponentStyle{" ); + toString( builder, false ); + return builder.append( "}" ).toString(); + } + + boolean toString(StringBuilder builder, boolean comma) + { + if ( color != null ) + { + if ( comma ) builder.append( ", " ); + comma = true; + builder.append( "color=" ).append( color ); + } + if ( font != null ) + { + if ( comma ) builder.append( ", " ); + comma = true; + builder.append( "font=" ).append( font ).append( ", " ); + } + if ( bold != null ) + { + if ( comma ) builder.append( ", " ); + comma = true; + builder.append( "bold=" ).append( bold ); + } + if ( italic != null ) + { + if ( comma ) builder.append( ", " ); + comma = true; + builder.append( "italic=" ).append( italic ); + } + if ( underlined != null ) + { + if ( comma ) builder.append( ", " ); + comma = true; + builder.append( "underlined=" ).append( underlined ); + } + if ( strikethrough != null ) + { + if ( comma ) builder.append( ", " ); + comma = true; + builder.append( "strikethrough=" ).append( strikethrough ); + } + if ( obfuscated != null ) + { + if ( comma ) builder.append( ", " ); + comma = true; + builder.append( "obfuscated=" ).append( obfuscated ); + } + return comma; + } + @Override public ComponentStyle clone() { diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/KeybindComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/KeybindComponent.java index 4cefe259bd7..9e16236e7f6 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/KeybindComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/KeybindComponent.java @@ -4,11 +4,9 @@ import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; -import lombok.ToString; @Getter @Setter -@ToString @NoArgsConstructor @EqualsAndHashCode(callSuper = true) public final class KeybindComponent extends BaseComponent @@ -63,4 +61,20 @@ protected void toLegacyText(StringBuilder builder) builder.append( getKeybind() ); super.toLegacyText( builder ); } + + @Override + String toStringName() + { + return "Keybind"; + } + + @Override + boolean toString(StringBuilder builder, boolean comma) + { + if ( comma ) builder.append( ", " ); + comma = true; + builder.append( "keybind=" ).append( keybind ); + return super.toString( builder, comma ); + } + } diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/ScoreComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/ScoreComponent.java index ebf7bb31421..58da77f0149 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/ScoreComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/ScoreComponent.java @@ -4,7 +4,6 @@ import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; -import lombok.ToString; /** * This component displays the score based on a player score on the scoreboard. @@ -25,7 +24,6 @@ */ @Getter @Setter -@ToString @AllArgsConstructor @EqualsAndHashCode(callSuper = true) public final class ScoreComponent extends BaseComponent @@ -98,4 +96,24 @@ protected void toLegacyText(StringBuilder builder) builder.append( this.value ); super.toLegacyText( builder ); } + + @Override + String toStringName() + { + return "Score"; + } + + @Override + boolean toString(StringBuilder builder, boolean comma) + { + if ( comma ) builder.append( ", " ); + comma = true; + builder.append( "name=" ).append( name ).append( ", " ) + .append( "objective=" ).append( objective ); + if ( value != null ) + { + builder.append( ", " ).append( "value=" ).append( value ); + } + return super.toString( builder, comma ); + } } diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/SelectorComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/SelectorComponent.java index ff1e260bcb6..a2cdf1e17da 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/SelectorComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/SelectorComponent.java @@ -4,7 +4,6 @@ import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; -import lombok.ToString; /** * This component processes a target selector into a pre-formatted set of @@ -21,7 +20,6 @@ */ @Getter @Setter -@ToString @AllArgsConstructor @EqualsAndHashCode(callSuper = true) public final class SelectorComponent extends BaseComponent @@ -82,4 +80,23 @@ protected void toLegacyText(StringBuilder builder) builder.append( this.selector ); super.toLegacyText( builder ); } + + @Override + String toStringName() + { + return "Selector"; + } + + @Override + boolean toString(StringBuilder builder, boolean comma) + { + if ( comma ) builder.append( ", " ); + comma = true; + builder.append( "selector=" ).append( selector ); + if ( separator != null ) + { + builder.append( ", " ).append( "separator=" ).append( separator ); + } + return super.toString( builder, comma ); + } } 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 0971a384c70..a8fce27a95f 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 @@ -295,8 +295,21 @@ protected void toLegacyText(StringBuilder builder) } @Override - public String toString() + String toStringName() { - return "TextComponent{text=" + text + ", " + super.toString() + '}'; + return "Text"; } + + @Override + boolean toString(StringBuilder builder, boolean comma) + { + if ( comma ) + { + builder.append( ", " ); + } + comma = true; + builder.append( "t=" ).append( text ); + return super.toString( builder, comma ); + } + } diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java index 13c095646ac..5d0e4b4c254 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java @@ -8,12 +8,10 @@ import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; -import lombok.ToString; import net.md_5.bungee.chat.TranslationRegistry; @Getter @Setter -@ToString @NoArgsConstructor @EqualsAndHashCode(callSuper = true) public final class TranslatableComponent extends BaseComponent @@ -227,4 +225,28 @@ private void convert(StringBuilder builder, boolean applyFormat) builder.append( trans.substring( position, trans.length() ) ); } } + + @Override + String toStringName() + { + return "Translatable"; + } + + @Override + boolean toString(StringBuilder builder, boolean comma) + { + if ( comma ) builder.append( ", " ); + comma = true; + builder.append( "translate=" ).append( translate ); + if ( with != null ) + { + builder.append( ", with=" ).append( with ); + } + if ( fallback != null ) + { + builder.append( ", fallback=" ).append( fallback ); + } + return super.toString( builder, comma ); + } + }