Skip to content

Commit

Permalink
Re-use legacy parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
2008Choco committed Oct 26, 2023
1 parent 9df914c commit aaaae41
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 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 @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -44,15 +45,9 @@ public static BaseComponent fromLegacy(String message)
*/
public static BaseComponent fromLegacy(String message, ChatColor defaultColor)
{
TextComponent component = new TextComponent();

BaseComponent[] components = fromLegacyText( message, defaultColor );
if ( components.length > 0 )
{
component.setExtra( Arrays.asList( components ) );
}

return component;
ComponentBuilder componentBuilder = new ComponentBuilder();
populateComponentStructure( message, defaultColor, componentBuilder::append );
return componentBuilder.build();
}

/**
Expand Down Expand Up @@ -91,6 +86,12 @@ public static BaseComponent[] fromLegacyText(String message)
public static BaseComponent[] fromLegacyText(String message, ChatColor defaultColor)
{
ArrayList<BaseComponent> components = new ArrayList<>();
populateComponentStructure( message, defaultColor, components::add );
return components.toArray( new BaseComponent[ 0 ] );
}

private static void populateComponentStructure(String message, ChatColor defaultColor, Consumer<BaseComponent> appender)
{
StringBuilder builder = new StringBuilder();
TextComponent component = new TextComponent();
Matcher matcher = url.matcher( message );
Expand Down Expand Up @@ -140,7 +141,7 @@ public static BaseComponent[] fromLegacyText(String message, ChatColor defaultCo
component = new TextComponent( old );
old.setText( builder.toString() );
builder = new StringBuilder();
components.add( old );
appender.accept( old );
}
if ( format == ChatColor.BOLD )
{
Expand Down Expand Up @@ -183,7 +184,7 @@ public static BaseComponent[] fromLegacyText(String message, ChatColor defaultCo
component = new TextComponent( old );
old.setText( builder.toString() );
builder = new StringBuilder();
components.add( old );
appender.accept( old );
}

TextComponent old = component;
Expand All @@ -192,7 +193,7 @@ public static BaseComponent[] fromLegacyText(String message, ChatColor defaultCo
component.setText( urlString );
component.setClickEvent( new ClickEvent( ClickEvent.Action.OPEN_URL,
urlString.startsWith( "http" ) ? urlString : "http://" + urlString ) );
components.add( component );
appender.accept( component );
i += pos - i - 1;
component = old;
continue;
Expand All @@ -201,9 +202,7 @@ public static BaseComponent[] fromLegacyText(String message, ChatColor defaultCo
}

component.setText( builder.toString() );
components.add( component );

return components.toArray( new BaseComponent[ 0 ] );
appender.accept( component );
}

/**
Expand Down

0 comments on commit aaaae41

Please sign in to comment.