Use components for ServerKickEvent (fixes #744) + minor refactoring
This commit is contained in:
@@ -2,6 +2,7 @@ package net.md_5.bungee;
|
||||
|
||||
import com.google.common.io.ByteStreams;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.chat.ComponentSerializer;
|
||||
import net.md_5.bungee.log.BungeeLogger;
|
||||
import net.md_5.bungee.reconnect.YamlReconnectHandler;
|
||||
@@ -489,17 +490,13 @@ public class BungeeCord extends ProxyServer
|
||||
public void broadcast(String message)
|
||||
{
|
||||
getConsole().sendMessage( message );
|
||||
broadcast(ComponentSerializer.fromLegacyChat(message));
|
||||
broadcast(TextComponent.fromLegacyText(message));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void broadcast(BaseComponent[] message) {
|
||||
StringBuilder constr = new StringBuilder();
|
||||
for (BaseComponent msg : message) {
|
||||
constr.append( msg.toLegacyText() );
|
||||
}
|
||||
getConsole().sendMessage( constr.toString() );
|
||||
broadcast( new Chat(ComponentSerializer.toString(message)) );
|
||||
getConsole().sendMessage(BaseComponent.toLegacyText(message));
|
||||
broadcast(new Chat(ComponentSerializer.toString(message)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -5,7 +5,10 @@ import java.util.concurrent.TimeUnit;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.api.connection.Server;
|
||||
import net.md_5.bungee.chat.ComponentSerializer;
|
||||
import net.md_5.bungee.netty.ChannelWrapper;
|
||||
import net.md_5.bungee.protocol.DefinedPacket;
|
||||
import net.md_5.bungee.protocol.packet.PluginMessage;
|
||||
@@ -40,10 +43,15 @@ public class ServerConnection implements Server
|
||||
@Override
|
||||
public synchronized void disconnect(String reason)
|
||||
{
|
||||
disconnect( TextComponent.fromLegacyText(reason) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect(BaseComponent[] reason) {
|
||||
if ( !ch.isClosed() )
|
||||
{
|
||||
// TODO: Can we just use a future here?
|
||||
unsafe().sendPacket( new Kick( reason ) );
|
||||
unsafe().sendPacket( new Kick(ComponentSerializer.toString(reason) ) );
|
||||
ch.getHandle().eventLoop().schedule( new Runnable()
|
||||
{
|
||||
@Override
|
||||
@@ -53,6 +61,12 @@ public class ServerConnection implements Server
|
||||
}
|
||||
}, 100, TimeUnit.MILLISECONDS );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect(BaseComponent reason) {
|
||||
disconnect(new BaseComponent[]{reason});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -15,6 +15,7 @@ import net.md_5.bungee.api.event.ServerSwitchEvent;
|
||||
import net.md_5.bungee.api.score.Objective;
|
||||
import net.md_5.bungee.api.score.Scoreboard;
|
||||
import net.md_5.bungee.api.score.Team;
|
||||
import net.md_5.bungee.chat.ComponentSerializer;
|
||||
import net.md_5.bungee.connection.CancelSendSignal;
|
||||
import net.md_5.bungee.connection.DownstreamBridge;
|
||||
import net.md_5.bungee.netty.HandlerBoss;
|
||||
@@ -211,7 +212,7 @@ public class ServerConnector extends PacketHandler
|
||||
{
|
||||
def = null;
|
||||
}
|
||||
ServerKickEvent event = bungee.getPluginManager().callEvent( new ServerKickEvent( user, kick.getMessage(), def, ServerKickEvent.State.CONNECTING ) );
|
||||
ServerKickEvent event = bungee.getPluginManager().callEvent( new ServerKickEvent( user, ComponentSerializer.parse(kick.getMessage()), def, ServerKickEvent.State.CONNECTING ) );
|
||||
if ( event.isCancelled() && event.getCancelServer() != null )
|
||||
{
|
||||
user.connect( event.getCancelServer() );
|
||||
|
@@ -21,6 +21,7 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
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.event.PermissionCheckEvent;
|
||||
@@ -258,15 +259,25 @@ public final class UserConnection implements ProxiedPlayer
|
||||
@Override
|
||||
public synchronized void disconnect(String reason)
|
||||
{
|
||||
disconnect0( ComponentSerializer.toString( ComponentSerializer.fromLegacyChat( reason ) ) );
|
||||
disconnect0( TextComponent.fromLegacyText(reason) );
|
||||
}
|
||||
|
||||
public synchronized void disconnect0(String reason)
|
||||
@Override
|
||||
public void disconnect(BaseComponent[] reason) {
|
||||
disconnect0( reason );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect(BaseComponent reason) {
|
||||
disconnect0(new BaseComponent[]{reason});
|
||||
}
|
||||
|
||||
public synchronized void disconnect0(BaseComponent[] reason)
|
||||
{
|
||||
if ( ch.getHandle().isActive() )
|
||||
{
|
||||
bungee.getLogger().log( Level.INFO, "[" + getName() + "] disconnected with: " + reason );
|
||||
unsafe().sendPacket( new Kick( reason ) );
|
||||
bungee.getLogger().log( Level.INFO, "[" + getName() + "] disconnected with: " + BaseComponent.toLegacyText(reason) );
|
||||
unsafe().sendPacket( new Kick( ComponentSerializer.toString(reason) ) );
|
||||
ch.close();
|
||||
if ( server != null )
|
||||
{
|
||||
@@ -285,7 +296,7 @@ public final class UserConnection implements ProxiedPlayer
|
||||
@Override
|
||||
public void sendMessage(String message)
|
||||
{
|
||||
sendMessage(ComponentSerializer.fromLegacyChat(message));
|
||||
sendMessage(TextComponent.fromLegacyText(message));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -25,8 +25,6 @@ public class ComponentSerializer implements JsonSerializer<BaseComponent>, JsonD
|
||||
registerTypeAdapter(TranslatableComponent.class, new TranslatableComponentSerializer()).
|
||||
create();
|
||||
|
||||
private static final Pattern url = Pattern.compile("^(?:(https?)://)?([-\\w_\\.]{2,}\\.[a-z]{2,4})(/\\S*)?$");
|
||||
|
||||
public static BaseComponent[] parse(String json) {
|
||||
if (json.startsWith("[")) { //Array
|
||||
return gson.fromJson(json, BaseComponent[].class);
|
||||
@@ -42,92 +40,6 @@ public class ComponentSerializer implements JsonSerializer<BaseComponent>, JsonD
|
||||
return gson.toJson(components);
|
||||
}
|
||||
|
||||
public static BaseComponent[] fromLegacyChat(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()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseComponent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
if (json.isJsonPrimitive()) {
|
||||
|
@@ -37,11 +37,7 @@ public class ConsoleCommandSender implements CommandSender
|
||||
|
||||
@Override
|
||||
public void sendMessage(BaseComponent[] message) {
|
||||
StringBuilder constr = new StringBuilder();
|
||||
for (BaseComponent msg : message) {
|
||||
constr.append( msg.toLegacyText() );
|
||||
}
|
||||
sendMessage( constr.toString() );
|
||||
sendMessage(BaseComponent.toLegacyText(message));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -22,6 +22,7 @@ import net.md_5.bungee.api.score.Position;
|
||||
import net.md_5.bungee.api.score.Score;
|
||||
import net.md_5.bungee.api.score.Scoreboard;
|
||||
import net.md_5.bungee.api.score.Team;
|
||||
import net.md_5.bungee.chat.ComponentSerializer;
|
||||
import net.md_5.bungee.netty.ChannelWrapper;
|
||||
import net.md_5.bungee.netty.PacketHandler;
|
||||
import net.md_5.bungee.protocol.PacketWrapper;
|
||||
@@ -357,13 +358,13 @@ public class DownstreamBridge extends PacketHandler
|
||||
{
|
||||
def = null;
|
||||
}
|
||||
ServerKickEvent event = bungee.getPluginManager().callEvent( new ServerKickEvent( con, kick.getMessage(), def, ServerKickEvent.State.CONNECTED ) );
|
||||
ServerKickEvent event = bungee.getPluginManager().callEvent( new ServerKickEvent( con, ComponentSerializer.parse(kick.getMessage()), def, ServerKickEvent.State.CONNECTED ) );
|
||||
if ( event.isCancelled() && event.getCancelServer() != null )
|
||||
{
|
||||
con.connectNow( event.getCancelServer() );
|
||||
} else
|
||||
{
|
||||
con.disconnect0( event.getKickReason() ); // TODO: Json concat util method // TODO: Prefix our own stuff.
|
||||
con.disconnect0( event.getKickReasonComponent() ); // TODO: Prefix our own stuff.
|
||||
}
|
||||
server.setObsolete( true );
|
||||
throw new CancelSendSignal();
|
||||
|
@@ -16,6 +16,8 @@ import net.md_5.bungee.api.Callback;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import net.md_5.bungee.api.ServerPing;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.api.config.ListenerInfo;
|
||||
import net.md_5.bungee.api.config.ServerInfo;
|
||||
import net.md_5.bungee.api.connection.PendingConnection;
|
||||
@@ -398,11 +400,25 @@ public class InitialHandler extends PacketHandler implements PendingConnection
|
||||
{
|
||||
if ( !ch.isClosed() )
|
||||
{
|
||||
unsafe().sendPacket( new Kick( ComponentSerializer.toString(ComponentSerializer.fromLegacyChat(reason)) ) );
|
||||
unsafe().sendPacket( new Kick( ComponentSerializer.toString(TextComponent.fromLegacyText(reason)) ) );
|
||||
ch.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect(BaseComponent[] reason) {
|
||||
if ( !ch.isClosed() )
|
||||
{
|
||||
unsafe().sendPacket( new Kick( ComponentSerializer.toString(reason) ) );
|
||||
ch.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect(BaseComponent reason) {
|
||||
disconnect(new BaseComponent[]{reason});
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName()
|
||||
{
|
||||
|
Reference in New Issue
Block a user