Skip to content

Commit

Permalink
Improve chat api toLegacy conversion and obj toString
Browse files Browse the repository at this point in the history
- Legacy conversion has been pimped to output less redundant legacy codes
- Fix bug introduced in 765858f of array toLegacy conversion with new methods keeping track of current string color
- Fix TranslatableComponent copy constructor not copying fallback
- Make TranslatableComponent format pattern static
- Change ComponentBuilder.build() to return not nested component if there is just one part
  • Loading branch information
Janmm14 committed Apr 14, 2024
1 parent 40577ed commit 0bbe3e6
Show file tree
Hide file tree
Showing 10 changed files with 448 additions and 81 deletions.
215 changes: 187 additions & 28 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 @@ -215,9 +213,12 @@ public BaseComponent duplicateWithoutFormatting()
public static String toLegacyText(BaseComponent... components)
{
StringBuilder builder = new StringBuilder();
ComponentStyle currentStyle = new ComponentStyle();
currentStyle.setColor( ChatColor.RESET );

for ( BaseComponent msg : components )
{
builder.append( msg.toLegacyText() );
currentStyle = msg.toLegacyText( builder, currentStyle );
}
return builder.toString();
}
Expand Down Expand Up @@ -273,15 +274,20 @@ public void setColor(ChatColor color)
*/
public ChatColor getColor()
{
if ( !style.hasColor() )
return getColor( ChatColor.WHITE );
}

ChatColor getColor(ChatColor def)
{
if ( style.hasColor() )
{
if ( parent == null )
{
return ChatColor.WHITE;
}
return parent.getColor();
return style.getColor();
}
return style.getColor();
if ( parent == null )
{
return def;
}
return parent.getColor( def );
}

/**
Expand Down Expand Up @@ -651,46 +657,199 @@ void toPlainText(StringBuilder builder)
public String toLegacyText()
{
StringBuilder builder = new StringBuilder();
toLegacyText( builder );
ComponentStyle currentLegacy = new ComponentStyle();
currentLegacy.setColor( ChatColor.RESET );
toLegacyText( builder, currentLegacy );
return builder.toString();
}

void toLegacyText(StringBuilder builder)
/**
* Converts the component to a string that uses the old formatting codes
* ({@link net.md_5.bungee.api.ChatColor#COLOR_CHAR}
*
* @param currentLegacy the style at the end of the string the result of this method will be appended to
* @return the string in the old format
*/
public String toLegacyText(ComponentStyle currentLegacy)
{
if ( extra != null )
StringBuilder builder = new StringBuilder();
toLegacyText( builder, currentLegacy );
return builder.toString();
}

/**
* Converts the component to a string that uses the old formatting codes
* ({@link net.md_5.bungee.api.ChatColor#COLOR_CHAR}
*
* @param builder the StringBuilder to append to
* @param currentLegacy the style at the end of {@code builder}
* @return the style at the end of the legacy string
*/
public ComponentStyle toLegacyText(StringBuilder builder, ComponentStyle currentLegacy)
{
currentLegacy = currentLegacy.clone();
if ( !currentLegacy.hasColor() )
{
for ( BaseComponent e : extra )
currentLegacy.setColor( ChatColor.RESET );
}
return toLegacyText( builder, currentLegacy.hasColor() ? currentLegacy.getColor() : ChatColor.RESET, currentLegacy );
}

/**
* Converts the component to a string that uses the old formatting codes
* ({@link net.md_5.bungee.api.ChatColor#COLOR_CHAR}
*
* @param builder the StringBuilder to append to
* @param baseColor the color to use if no color is set, but a format downgrade is needed
* @param currentLegacy the style at the end of {@code builder}
* @return the new current style at the end of the {@code builder}
*/
ComponentStyle toLegacyText(StringBuilder builder, ChatColor baseColor, ComponentStyle currentLegacy)
{
if ( extra == null )
{
return currentLegacy;
}
for ( BaseComponent e : extra )
{
currentLegacy = e.toLegacyText( builder, baseColor, currentLegacy );
}
return currentLegacy;
}

private static boolean colorEquals(ChatColor a, ChatColor b)
{
if ( a == b )
{
return true;
}
if ( a == null || b == null )
{
return false;
}
if ( ChatColor.RESET.equals( a ) )
{
return ChatColor.WHITE.equals( b ) || ChatColor.RESET.equals( b );
}
if ( ChatColor.RESET.equals( b ) )
{
return ChatColor.WHITE.equals( a ) || ChatColor.RESET.equals( a );
}
return a.equals( b );
}

/**
* @param builder the StringBuilder to append to
* @param baseColor the color to use if no color is set, but a format downgrade is needed
* @param currentLegacy the style at the end of {@code builder}
* @return the new current style at the end of {@code builder}
*/
ComponentStyle addFormat(StringBuilder builder, ChatColor baseColor, ComponentStyle currentLegacy)
{
// Check if we can skip adding color code
if ( colorEquals( getColor(), currentLegacy.getColor() ) && currentLegacy.isLegacyFormattingUpgrade( style ) )
{
if ( isBold() && !currentLegacy.isBold() )
{
builder.append( ChatColor.BOLD );
}
if ( isItalic() && !currentLegacy.isItalic() )
{
builder.append( ChatColor.ITALIC );
}
if ( isUnderlined() && !currentLegacy.isUnderlined() )
{
builder.append( ChatColor.UNDERLINE );
}
if ( isStrikethrough() && !currentLegacy.isStrikethrough() )
{
e.toLegacyText( builder );
builder.append( ChatColor.STRIKETHROUGH );
}
if ( isObfuscated() && !currentLegacy.isObfuscated() )
{
builder.append( ChatColor.MAGIC );
}
} else
{
builder.append( getColor( baseColor ) == null ? baseColor : getColor( baseColor ) );
if ( isBold() )
{
builder.append( ChatColor.BOLD );
}
if ( isItalic() )
{
builder.append( ChatColor.ITALIC );
}
if ( isUnderlined() )
{
builder.append( ChatColor.UNDERLINE );
}
if ( isStrikethrough() )
{
builder.append( ChatColor.STRIKETHROUGH );
}
if ( isObfuscated() )
{
builder.append( ChatColor.MAGIC );
}
}
currentLegacy = style;
if ( currentLegacy.getColor() == null )
{
currentLegacy = style.clone();
currentLegacy.setColor( getColor( baseColor ) == null ? baseColor : getColor( baseColor ) );
}
return currentLegacy;
}

void addFormat(StringBuilder builder)
@Override
public String toString()
{
if ( style.hasColor() || parent != null )
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 )
{
builder.append( getColor() );
comma = style.toString( builder, comma );
}
if ( isBold() )
if ( insertion != null )
{
builder.append( ChatColor.BOLD );
if ( comma ) builder.append( ", " );
comma = true;
builder.append( "insertion=" ).append( insertion );
}
if ( isItalic() )
if ( clickEvent != null )
{
builder.append( ChatColor.ITALIC );
if ( comma ) builder.append( ", " );
comma = true;
builder.append( "clickEvent=" ).append( clickEvent );
}
if ( isUnderlined() )
if ( hoverEvent != null )
{
builder.append( ChatColor.UNDERLINE );
if ( comma ) builder.append( ", " );
comma = true;
builder.append( "hoverEvent=" ).append( hoverEvent );
}
if ( isStrikethrough() )
if ( reset )
{
builder.append( ChatColor.STRIKETHROUGH );
if ( comma ) builder.append( ", " );
comma = true;
builder.append( "reset" );
}
if ( isObfuscated() )
if ( extra != null )
{
builder.append( ChatColor.MAGIC );
if ( comma ) builder.append( ", " );
builder.append( "extra=" ).append( extra );
}
return comma;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,10 @@ public ComponentBuilder retain(FormatRetention retention)
*/
public BaseComponent build()
{
if ( parts.size() == 1 )
{
return parts.get( 0 ).duplicate();
}
TextComponent base = new TextComponent();
if ( !parts.isEmpty() )
{
Expand Down
64 changes: 64 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,70 @@ 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;
}

boolean isLegacyFormattingUpgrade(ComponentStyle newStyle)
{
return ( newStyle.isBold() || !isBold() )
&& ( newStyle.isItalic() || !isItalic() )
&& ( newStyle.isUnderlined() || !isUnderlined() )
&& ( newStyle.isStrikethrough() || !isStrikethrough() )
&& ( newStyle.isObfuscated() || !isObfuscated() );
}

@Override
public ComponentStyle clone()
{
Expand Down
24 changes: 19 additions & 5 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,10 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import net.md_5.bungee.api.ChatColor;

@Getter
@Setter
@ToString
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public final class KeybindComponent extends BaseComponent
Expand Down Expand Up @@ -57,10 +56,25 @@ protected void toPlainText(StringBuilder builder)
}

@Override
protected void toLegacyText(StringBuilder builder)
ComponentStyle toLegacyText(StringBuilder builder, ChatColor baseColor, ComponentStyle currentLegacy)
{
addFormat( builder );
currentLegacy = addFormat( builder, baseColor, currentLegacy );
builder.append( getKeybind() );
super.toLegacyText( builder );
return super.toLegacyText( builder, baseColor, currentLegacy );
}

@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 );
}
}
Loading

0 comments on commit 0bbe3e6

Please sign in to comment.