Skip to content

Commit

Permalink
#3540: Add TextComponent#fromLegacy() as an array-free alternative to…
Browse files Browse the repository at this point in the history
… #fromLegacyText()
  • Loading branch information
2008Choco authored and md-5 committed Oct 28, 2023
1 parent 0f5f09b commit 0d45378
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 49 deletions.
6 changes: 3 additions & 3 deletions api/src/main/java/net/md_5/bungee/api/ServerPing.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ public static class ModItem
@Deprecated
public ServerPing(Protocol version, Players players, String description, String favicon)
{
this( version, players, new TextComponent( TextComponent.fromLegacyText( description ) ), favicon == null ? null : Favicon.create( favicon ) );
this( version, players, TextComponent.fromLegacy( description ), favicon == null ? null : Favicon.create( favicon ) );
}

@Deprecated
public ServerPing(Protocol version, Players players, String description, Favicon favicon)
{
this( version, players, new TextComponent( TextComponent.fromLegacyText( description ) ), favicon );
this( version, players, TextComponent.fromLegacy( description ), favicon );
}

@Deprecated
Expand Down Expand Up @@ -139,7 +139,7 @@ public void setFavicon(Favicon favicon)
@Deprecated
public void setDescription(String description)
{
this.description = new TextComponent( TextComponent.fromLegacyText( description ) );
this.description = TextComponent.fromLegacy( description );
}

@Deprecated
Expand Down
35 changes: 24 additions & 11 deletions api/src/main/java/net/md_5/bungee/api/event/LoginEvent.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package net.md_5.bungee.api.event;

import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Setter;
import lombok.ToString;
import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.chat.BaseComponent;
Expand All @@ -27,8 +25,7 @@ public class LoginEvent extends AsyncEvent<LoginEvent> implements Cancellable
/**
* Message to use when kicking if this event is canceled.
*/
@Setter(AccessLevel.NONE)
private BaseComponent[] cancelReasonComponents;
private BaseComponent reason;
/**
* Connection attempting to login.
*/
Expand All @@ -42,28 +39,44 @@ public LoginEvent(PendingConnection connection, Callback<LoginEvent> done)

/**
* @return reason to be displayed
* @deprecated Use component methods instead.
* @deprecated use component methods instead
*/
@Deprecated
public String getCancelReason()
{
return BaseComponent.toLegacyText( getCancelReasonComponents() );
return TextComponent.toLegacyText( getReason() );
}

/**
* @param cancelReason reason to be displayed
* @deprecated Use
* {@link #setCancelReason(net.md_5.bungee.api.chat.BaseComponent...)}
* instead.
* @deprecated use component methods instead
*/
@Deprecated
public void setCancelReason(String cancelReason)
{
setCancelReason( TextComponent.fromLegacyText( cancelReason ) );
setReason( TextComponent.fromLegacy( cancelReason ) );
}

/**
* @return reason to be displayed
* @deprecated use single component methods instead
*/
@Deprecated
public BaseComponent[] getCancelReasonComponents()
{
return new BaseComponent[]
{
getReason()
};
}

/**
* @param cancelReason reason to be displayed
* @deprecated use single component methods instead
*/
@Deprecated
public void setCancelReason(BaseComponent... cancelReason)
{
this.cancelReasonComponents = cancelReason;
setReason( TextComponent.fromArray( cancelReason ) );
}
}
35 changes: 24 additions & 11 deletions api/src/main/java/net/md_5/bungee/api/event/PreLoginEvent.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package net.md_5.bungee.api.event;

import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Setter;
import lombok.ToString;
import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.chat.BaseComponent;
Expand Down Expand Up @@ -32,8 +30,7 @@ public class PreLoginEvent extends AsyncEvent<PreLoginEvent> implements Cancella
/**
* Message to use when kicking if this event is canceled.
*/
@Setter(AccessLevel.NONE)
private BaseComponent[] cancelReasonComponents;
private BaseComponent reason;
/**
* Connection attempting to login.
*/
Expand All @@ -47,28 +44,44 @@ public PreLoginEvent(PendingConnection connection, Callback<PreLoginEvent> done)

/**
* @return reason to be displayed
* @deprecated Use component methods instead.
* @deprecated use component methods instead
*/
@Deprecated
public String getCancelReason()
{
return BaseComponent.toLegacyText( getCancelReasonComponents() );
return BaseComponent.toLegacyText( getReason() );
}

/**
* @param cancelReason reason to be displayed
* @deprecated Use
* {@link #setCancelReason(net.md_5.bungee.api.chat.BaseComponent...)}
* instead.
* @deprecated Use component methods instead
*/
@Deprecated
public void setCancelReason(String cancelReason)
{
setCancelReason( TextComponent.fromLegacyText( cancelReason ) );
setReason( TextComponent.fromLegacy( cancelReason ) );
}

/**
* @return reason to be displayed
* @deprecated use single component methods instead
*/
@Deprecated
public BaseComponent[] getCancelReasonComponents()
{
return new BaseComponent[]
{
getReason()
};
}

/**
* @param cancelReason reason to be displayed
* @deprecated use single component methods instead
*/
@Deprecated
public void setCancelReason(BaseComponent... cancelReason)
{
this.cancelReasonComponents = cancelReason;
setReason( TextComponent.fromArray( cancelReason ) );
}
}
45 changes: 41 additions & 4 deletions api/src/main/java/net/md_5/bungee/api/event/ServerKickEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ServerKickEvent extends Event implements Cancellable
/**
* Kick reason.
*/
private BaseComponent[] kickReasonComponent;
private BaseComponent reason;
/**
* Server to send player to if this event is cancelled.
*/
Expand Down Expand Up @@ -63,24 +63,61 @@ public ServerKickEvent(ProxiedPlayer player, BaseComponent[] kickReasonComponent
this( player, player.getServer().getInfo(), kickReasonComponent, cancelServer, state );
}

@Deprecated
public ServerKickEvent(ProxiedPlayer player, ServerInfo kickedFrom, BaseComponent[] kickReasonComponent, ServerInfo cancelServer, State state)
{
this( player, kickedFrom, TextComponent.fromArray( kickReasonComponent ), cancelServer, state );
}

public ServerKickEvent(ProxiedPlayer player, ServerInfo kickedFrom, BaseComponent reason, ServerInfo cancelServer, State state)
{
this.player = player;
this.kickedFrom = kickedFrom;
this.kickReasonComponent = kickReasonComponent;
this.reason = reason;
this.cancelServer = cancelServer;
this.state = state;
}

/**
* @return the kick reason
* @deprecated use component methods instead
*/
@Deprecated
public String getKickReason()
{
return BaseComponent.toLegacyText( kickReasonComponent );
return BaseComponent.toLegacyText( getReason() );
}

/**
* @param reason the kick reason
* @deprecated use component methods instead
*/
@Deprecated
public void setKickReason(String reason)
{
kickReasonComponent = TextComponent.fromLegacyText( reason );
this.setReason( TextComponent.fromLegacy( reason ) );
}

/**
* @return the kick reason
* @deprecated use single component methods instead
*/
@Deprecated
public BaseComponent[] getKickReasonComponent()
{
return new BaseComponent[]
{
getReason()
};
}

/**
* @param kickReasonComponent the kick reason
* @deprecated use single component methods instead
*/
@Deprecated
public void setKickReasonComponent(BaseComponent[] kickReasonComponent)
{
this.setReason( TextComponent.fromArray( kickReasonComponent ) );
}
}
57 changes: 51 additions & 6 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 All @@ -27,6 +28,41 @@ public final class TextComponent extends BaseComponent
* @param message the text to convert
* @return the components needed to print the message to the client
*/
public static BaseComponent fromLegacy(String message)
{
return fromLegacy( message, ChatColor.WHITE );
}

/**
* Converts the old formatting system that used
* {@link net.md_5.bungee.api.ChatColor#COLOR_CHAR} into the new json based
* system.
*
* @param message the text to convert
* @param defaultColor color to use when no formatting is to be applied
* (i.e. after ChatColor.RESET).
* @return the components needed to print the message to the client
*/
public static BaseComponent fromLegacy(String message, ChatColor defaultColor)
{
ComponentBuilder componentBuilder = new ComponentBuilder();
populateComponentStructure( message, defaultColor, componentBuilder::append );
return componentBuilder.build();
}

/**
* Converts the old formatting system that used
* {@link net.md_5.bungee.api.ChatColor#COLOR_CHAR} into the new json based
* system.
*
* @param message the text to convert
* @return the components needed to print the message to the client
* @deprecated {@link #fromLegacy(String)} is preferred as it will
* consolidate all components into a single BaseComponent with extra
* contents as opposed to an array of components which is non-standard and
* may result in unexpected behavior.
*/
@Deprecated
public static BaseComponent[] fromLegacyText(String message)
{
return fromLegacyText( message, ChatColor.WHITE );
Expand All @@ -41,10 +77,21 @@ public static BaseComponent[] fromLegacyText(String message)
* @param defaultColor color to use when no formatting is to be applied
* (i.e. after ChatColor.RESET).
* @return the components needed to print the message to the client
* @deprecated {@link #fromLegacy(String, ChatColor)} is preferred as it
* will consolidate all components into a single BaseComponent with extra
* contents as opposed to an array of components which is non-standard and
* may result in unexpected behavior.
*/
@Deprecated
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 @@ -94,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 @@ -137,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 @@ -146,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 @@ -155,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
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void execute(CommandSender sender, String[] args)

String message = builder.substring( 0, builder.length() - 1 );

ProxyServer.getInstance().broadcast( TextComponent.fromLegacyText( message ) );
ProxyServer.getInstance().broadcast( TextComponent.fromLegacy( message ) );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ public void execute(CommandSender sender, String[] args)

if ( player == null )
{
sender.sendMessage( TextComponent.fromLegacyText( ProxyServer.getInstance().getTranslation( "user_not_online" ) ) );
sender.sendMessage( TextComponent.fromLegacy( ProxyServer.getInstance().getTranslation( "user_not_online" ) ) );
return;
}

if ( args.length == 1 )
{
player.disconnect( TextComponent.fromLegacyText( ProxyServer.getInstance().getTranslation( "kick_message" ) ) );
player.disconnect( TextComponent.fromLegacy( ProxyServer.getInstance().getTranslation( "kick_message" ) ) );
} else
{
String[] reason = new String[ args.length - 1 ];
System.arraycopy( args, 1, reason, 0, reason.length );
player.disconnect( TextComponent.fromLegacyText( ChatColor.translateAlternateColorCodes( '&', Joiner.on( ' ' ).join( reason ) ) ) );
player.disconnect( TextComponent.fromLegacy( ChatColor.translateAlternateColorCodes( '&', Joiner.on( ' ' ).join( reason ) ) ) );
}
}
}
Expand Down
Loading

0 comments on commit 0d45378

Please sign in to comment.