Use components for ServerKickEvent (fixes #744) + minor refactoring
This commit is contained in:
@@ -19,6 +19,7 @@ public interface CommandSender
|
||||
*
|
||||
* @param message the message to send
|
||||
*/
|
||||
@Deprecated
|
||||
public void sendMessage(String message);
|
||||
|
||||
/**
|
||||
@@ -27,6 +28,7 @@ public interface CommandSender
|
||||
*
|
||||
* @param messages the messages to send
|
||||
*/
|
||||
@Deprecated
|
||||
public void sendMessages(String... messages);
|
||||
|
||||
/**
|
||||
|
@@ -232,6 +232,7 @@ public abstract class ProxyServer
|
||||
*
|
||||
* @param message the message to broadcast
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract void broadcast(String message);
|
||||
|
||||
/**
|
||||
|
@@ -49,6 +49,22 @@ public abstract class BaseComponent {
|
||||
setObfuscated(old.isObfuscatedRaw());
|
||||
}
|
||||
|
||||
public static String toLegacyText(BaseComponent[] components) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (BaseComponent msg : components) {
|
||||
builder.append(msg.toLegacyText());
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static String toPlainText(BaseComponent[] components) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (BaseComponent msg : components) {
|
||||
builder.append(msg.toPlainText());
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the color of this component. This uses the parent's color
|
||||
|
@@ -6,11 +6,104 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class TextComponent extends BaseComponent {
|
||||
|
||||
private static final Pattern url = Pattern.compile("^(?:(https?)://)?([-\\w_\\.]{2,}\\.[a-z]{2,4})(/\\S*)?$");
|
||||
|
||||
public static BaseComponent[] fromLegacyText(String message) {
|
||||
ArrayList<BaseComponent> components = new ArrayList<>();
|
||||
StringBuilder builder = new StringBuilder();
|
||||
TextComponent component = new TextComponent();
|
||||
Matcher matcher = url.matcher(message);
|
||||
|
||||
for ( int i = 0; i < message.length(); i++ ) {
|
||||
char c = message.charAt(i);
|
||||
if (c == ChatColor.COLOR_CHAR) {
|
||||
i++;
|
||||
c = message.charAt(i);
|
||||
if (c >= 'A' && c <= 'Z') {
|
||||
c += 32;
|
||||
}
|
||||
if (builder.length() > 0) {
|
||||
TextComponent old = component;
|
||||
component = new TextComponent(old);
|
||||
old.setText(builder.toString());
|
||||
builder = new StringBuilder();
|
||||
components.add(old);
|
||||
}
|
||||
ChatColor format = ChatColor.getByChar(c);
|
||||
switch (format) {
|
||||
case BOLD:
|
||||
component.setBold(true);
|
||||
break;
|
||||
case ITALIC:
|
||||
component.setItalic(true);
|
||||
break;
|
||||
case UNDERLINE:
|
||||
component.setUnderlined(true);
|
||||
break;
|
||||
case STRIKETHROUGH:
|
||||
component.setStrikethrough(true);
|
||||
break;
|
||||
case MAGIC:
|
||||
component.setObfuscated(true);
|
||||
break;
|
||||
case RESET:
|
||||
format = ChatColor.WHITE;
|
||||
default:
|
||||
component = new TextComponent();
|
||||
component.setColor(format);
|
||||
break;
|
||||
}
|
||||
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) {
|
||||
TextComponent old = component;
|
||||
component = new TextComponent(old);
|
||||
old.setText(builder.toString());
|
||||
builder = new StringBuilder();
|
||||
components.add(old);
|
||||
}
|
||||
|
||||
TextComponent old = component;
|
||||
component = new TextComponent(old);
|
||||
ClickEvent clickEvent = new ClickEvent();
|
||||
clickEvent.setAction(ClickEvent.Action.OPEN_URL);
|
||||
String urlString = message.substring(i, pos);
|
||||
if (urlString.startsWith("http")) {
|
||||
component.setText(urlString);
|
||||
clickEvent.setValue(urlString);
|
||||
} else {
|
||||
component.setText(urlString);
|
||||
clickEvent.setValue("http://" + urlString);
|
||||
}
|
||||
component.setClickEvent(clickEvent);
|
||||
components.add(component);
|
||||
i += pos - i - 1;
|
||||
component = old;
|
||||
continue;
|
||||
}
|
||||
builder.append(c);
|
||||
}
|
||||
if (builder.length() > 0) {
|
||||
component.setText(builder.toString());
|
||||
components.add(component);
|
||||
}
|
||||
return components.toArray(new BaseComponent[components.size()]);
|
||||
}
|
||||
|
||||
private String text;
|
||||
|
||||
public TextComponent(TextComponent old) {
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package net.md_5.bungee.api.connection;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.protocol.DefinedPacket;
|
||||
|
||||
/**
|
||||
@@ -26,8 +28,29 @@ public interface Connection
|
||||
* @param reason the reason shown to the player / sent to the server on
|
||||
* disconnect
|
||||
*/
|
||||
@Deprecated
|
||||
void disconnect(String reason);
|
||||
|
||||
/**
|
||||
* Disconnects this end of the connection for the specified reason. If this
|
||||
* is an {@link ProxiedPlayer} the respective server connection will be
|
||||
* closed too.
|
||||
*
|
||||
* @param reason the reason shown to the player / sent to the server on
|
||||
* disconnect
|
||||
*/
|
||||
void disconnect(BaseComponent[] reason);
|
||||
|
||||
/**
|
||||
* Disconnects this end of the connection for the specified reason. If this
|
||||
* is an {@link ProxiedPlayer} the respective server connection will be
|
||||
* closed too.
|
||||
*
|
||||
* @param reason the reason shown to the player / sent to the server on
|
||||
* disconnect
|
||||
*/
|
||||
void disconnect(BaseComponent reason);
|
||||
|
||||
/**
|
||||
* Get the unsafe methods of this class.
|
||||
*
|
||||
|
@@ -3,6 +3,8 @@ package net.md_5.bungee.api.event;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.api.config.ServerInfo;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
import net.md_5.bungee.api.plugin.Cancellable;
|
||||
@@ -28,7 +30,7 @@ public class ServerKickEvent extends Event implements Cancellable
|
||||
/**
|
||||
* Kick reason.
|
||||
*/
|
||||
private String kickReason;
|
||||
private BaseComponent[] kickReasonComponent;
|
||||
/**
|
||||
* Server to send player to if this event is cancelled.
|
||||
*/
|
||||
@@ -44,16 +46,26 @@ public class ServerKickEvent extends Event implements Cancellable
|
||||
CONNECTING, CONNECTED, UNKNOWN;
|
||||
}
|
||||
|
||||
public ServerKickEvent(ProxiedPlayer player, String kickReason, ServerInfo cancelServer)
|
||||
public ServerKickEvent(ProxiedPlayer player, BaseComponent[] kickReasonComponent, ServerInfo cancelServer)
|
||||
{
|
||||
this( player, kickReason, cancelServer, State.UNKNOWN );
|
||||
this( player, kickReasonComponent, cancelServer, State.UNKNOWN );
|
||||
}
|
||||
|
||||
public ServerKickEvent(ProxiedPlayer player, String kickReason, ServerInfo cancelServer, State state)
|
||||
public ServerKickEvent(ProxiedPlayer player, BaseComponent[] kickReasonComponent, ServerInfo cancelServer, State state)
|
||||
{
|
||||
this.player = player;
|
||||
this.kickReason = kickReason;
|
||||
this.kickReasonComponent = kickReasonComponent;
|
||||
this.cancelServer = cancelServer;
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public String getKickReason() {
|
||||
return BaseComponent.toLegacyText(kickReasonComponent);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void setKickReason(String reason) {
|
||||
kickReasonComponent = TextComponent.fromLegacyText(reason);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user