#3540: Add TextComponent#fromLegacy() as an array-free alternative to #fromLegacyText()

This commit is contained in:
Parker Hawke 2023-10-28 12:57:21 +11:00 committed by md_5
parent 0f5f09b6c5
commit 0d45378986
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11
10 changed files with 157 additions and 49 deletions

View File

@ -105,13 +105,13 @@ public class ServerPing
@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
@ -139,7 +139,7 @@ public class ServerPing
@Deprecated
public void setDescription(String description)
{
this.description = new TextComponent( TextComponent.fromLegacyText( description ) );
this.description = TextComponent.fromLegacy( description );
}
@Deprecated

View File

@ -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;
@ -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.
*/
@ -42,28 +39,44 @@ public class LoginEvent extends AsyncEvent<LoginEvent> implements Cancellable
/**
* @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 ) );
}
}

View File

@ -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;
@ -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.
*/
@ -47,28 +44,44 @@ public class PreLoginEvent extends AsyncEvent<PreLoginEvent> implements Cancella
/**
* @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 ) );
}
}

View File

@ -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.
*/
@ -63,24 +63,61 @@ public class ServerKickEvent extends Event implements Cancellable
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 ) );
}
}

View File

@ -2,6 +2,7 @@ package net.md_5.bungee.api.chat;
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;
@ -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 );
@ -41,10 +77,21 @@ public final class TextComponent extends BaseComponent
* @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 );
@ -94,7 +141,7 @@ public final class TextComponent extends BaseComponent
component = new TextComponent( old );
old.setText( builder.toString() );
builder = new StringBuilder();
components.add( old );
appender.accept( old );
}
if ( format == ChatColor.BOLD )
{
@ -137,7 +184,7 @@ public final class TextComponent extends BaseComponent
component = new TextComponent( old );
old.setText( builder.toString() );
builder = new StringBuilder();
components.add( old );
appender.accept( old );
}
TextComponent old = component;
@ -146,7 +193,7 @@ public final class TextComponent extends BaseComponent
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;
@ -155,9 +202,7 @@ public final class TextComponent extends BaseComponent
}
component.setText( builder.toString() );
components.add( component );
return components.toArray( new BaseComponent[ 0 ] );
appender.accept( component );
}
/**

View File

@ -41,7 +41,7 @@ public class CommandAlert extends Command
String message = builder.substring( 0, builder.length() - 1 );
ProxyServer.getInstance().broadcast( TextComponent.fromLegacyText( message ) );
ProxyServer.getInstance().broadcast( TextComponent.fromLegacy( message ) );
}
}
}

View File

@ -33,18 +33,18 @@ public class CommandKick extends Command implements TabExecutor
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 ) ) ) );
}
}
}

View File

@ -730,13 +730,13 @@ public class BungeeCord extends ProxyServer
@Override
public void broadcast(String message)
{
broadcast( TextComponent.fromLegacyText( message ) );
broadcast( TextComponent.fromLegacy( message ) );
}
@Override
public void broadcast(BaseComponent... message)
{
getConsole().sendMessage( BaseComponent.toLegacyText( message ) );
getConsole().sendMessage( message );
for ( ProxiedPlayer player : getPlayers() )
{
player.sendMessage( message );
@ -746,7 +746,7 @@ public class BungeeCord extends ProxyServer
@Override
public void broadcast(BaseComponent message)
{
getConsole().sendMessage( message.toLegacyText() );
getConsole().sendMessage( message );
for ( ProxiedPlayer player : getPlayers() )
{
player.sendMessage( message );

View File

@ -406,7 +406,7 @@ public final class UserConnection implements ProxiedPlayer
@Override
public void disconnect(String reason)
{
disconnect( TextComponent.fromLegacyText( reason ) );
disconnect( TextComponent.fromLegacy( reason ) );
}
@Override
@ -454,7 +454,7 @@ public final class UserConnection implements ProxiedPlayer
@Override
public void sendMessage(String message)
{
sendMessage( TextComponent.fromLegacyText( message ) );
sendMessage( TextComponent.fromLegacy( message ) );
}
@Override

View File

@ -433,8 +433,8 @@ public class InitialHandler extends PacketHandler implements PendingConnection
{
if ( result.isCancelled() )
{
BaseComponent[] reason = result.getCancelReasonComponents();
disconnect( ( reason != null ) ? reason : TextComponent.fromLegacyText( bungee.getTranslation( "kick_message" ) ) );
BaseComponent reason = result.getReason();
disconnect( ( reason != null ) ? reason : TextComponent.fromLegacy( bungee.getTranslation( "kick_message" ) ) );
return;
}
if ( ch.isClosed() )
@ -577,8 +577,8 @@ public class InitialHandler extends PacketHandler implements PendingConnection
{
if ( result.isCancelled() )
{
BaseComponent[] reason = result.getCancelReasonComponents();
disconnect( ( reason != null ) ? reason : TextComponent.fromLegacyText( bungee.getTranslation( "kick_message" ) ) );
BaseComponent reason = result.getReason();
disconnect( ( reason != null ) ? reason : TextComponent.fromLegacy( bungee.getTranslation( "kick_message" ) ) );
return;
}
if ( ch.isClosed() )
@ -645,7 +645,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
{
if ( canSendKickMessage() )
{
disconnect( TextComponent.fromLegacyText( reason ) );
disconnect( TextComponent.fromLegacy( reason ) );
} else
{
ch.close();