From 49e837f5f149c145d03fac6648702b652bcbb538 Mon Sep 17 00:00:00 2001 From: Jedediah Smith Date: Sun, 6 Mar 2016 03:08:58 -0500 Subject: [PATCH] Don't autolink the MOTD --- .../java/net/md_5/bungee/api/ServerPing.java | 2 +- .../md_5/bungee/api/chat/TextComponent.java | 58 +++++++++++-------- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/api/src/main/java/net/md_5/bungee/api/ServerPing.java b/api/src/main/java/net/md_5/bungee/api/ServerPing.java index 4eb04cd13f..78c40c136b 100644 --- a/api/src/main/java/net/md_5/bungee/api/ServerPing.java +++ b/api/src/main/java/net/md_5/bungee/api/ServerPing.java @@ -137,7 +137,7 @@ public void setFavicon(Favicon favicon) @Deprecated public void setDescription(String description) { - this.description = new TextComponent( TextComponent.fromLegacyText( description ) ); + this.description = new TextComponent( TextComponent.fromLegacyText( description, false ) ); } @Deprecated diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java index 2609b00ecf..b96bb2eb96 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java @@ -22,15 +22,23 @@ public class TextComponent extends BaseComponent private static final Pattern url = Pattern.compile( "^(?:(https?)://)?([-\\w_\\.]{2,}\\.[a-z]{2,4})(/\\S*)?$" ); + /** + * Calls {@link #fromLegacyText(String, boolean)} with autolink true + */ + public static BaseComponent[] fromLegacyText(String message) { + return fromLegacyText(message, true); + } + /** * 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 autolink detect links and make them clickable * @return the components needed to print the message to the client */ - public static BaseComponent[] fromLegacyText(String message) + public static BaseComponent[] fromLegacyText(String message, boolean autolink) { ArrayList components = new ArrayList(); StringBuilder builder = new StringBuilder(); @@ -87,34 +95,38 @@ public static BaseComponent[] fromLegacyText(String message) } continue; } - int pos = message.indexOf( ' ', i ); - if ( pos == -1 ) - { - pos = message.length(); - } - if ( matcher.region( i, pos ).find() ) - { //Web link handling - if ( builder.length() > 0 ) + if(autolink) { + int pos = message.indexOf( ' ', i ); + if ( pos == -1 ) { + pos = message.length(); + } + if ( matcher.region( i, pos ).find() ) + { //Web link handling + + if ( builder.length() > 0 ) + { + TextComponent old = component; + component = new TextComponent( old ); + old.setText( builder.toString() ); + builder = new StringBuilder(); + components.add( old ); + } + TextComponent old = component; component = new TextComponent( old ); - old.setText( builder.toString() ); - builder = new StringBuilder(); - components.add( old ); + String urlString = message.substring( i, pos ); + component.setText( urlString ); + component.setClickEvent( new ClickEvent( ClickEvent.Action.OPEN_URL, + urlString.startsWith( "http" ) ? urlString : "http://" + urlString ) ); + components.add( component ); + i += pos - i - 1; + component = old; + continue; } - - TextComponent old = component; - component = new TextComponent( old ); - String urlString = message.substring( i, pos ); - component.setText( urlString ); - component.setClickEvent( new ClickEvent( ClickEvent.Action.OPEN_URL, - urlString.startsWith( "http" ) ? urlString : "http://" + urlString ) ); - components.add( component ); - i += pos - i - 1; - component = old; - continue; } + builder.append( c ); } if ( builder.length() > 0 )