Skip to content

Commit

Permalink
Create shorter toString of components
Browse files Browse the repository at this point in the history
- stop mentioning null values
- use shorter names
- seamless integration of style's toString

Also use just one string builder and keep track of whether a comma is needed before the next property.
  • Loading branch information
Janmm14 committed Apr 21, 2024
1 parent ee02d98 commit 0a97ab6
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 12 deletions.
53 changes: 51 additions & 2 deletions chat/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ComponentBuilder.FormatRetention;

@Setter
@ToString(exclude = "parent")
@EqualsAndHashCode(exclude = "parent")
public abstract class BaseComponent
{
Expand Down Expand Up @@ -690,4 +688,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;
}
}
55 changes: 55 additions & 0 deletions chat/src/main/java/net/md_5/bungee/api/chat/ComponentStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
18 changes: 16 additions & 2 deletions chat/src/main/java/net/md_5/bungee/api/chat/KeybindComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 );
}

}
22 changes: 20 additions & 2 deletions chat/src/main/java/net/md_5/bungee/api/chat/ScoreComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -25,7 +24,6 @@
*/
@Getter
@Setter
@ToString
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public final class ScoreComponent extends BaseComponent
Expand Down Expand Up @@ -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 );
}
}
21 changes: 19 additions & 2 deletions chat/src/main/java/net/md_5/bungee/api/chat/SelectorComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,7 +20,6 @@
*/
@Getter
@Setter
@ToString
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public final class SelectorComponent extends BaseComponent
Expand Down Expand Up @@ -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 );
}
}
17 changes: 15 additions & 2 deletions chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 );
}

}

0 comments on commit 0a97ab6

Please sign in to comment.