Fix multi-line JSON message parsing

This commit is contained in:
ME1312 2021-06-15 01:47:06 -04:00
parent 7d6396ec1c
commit 1fff679005
No known key found for this signature in database
GPG Key ID: FEFFE2F698E88FA8
7 changed files with 191 additions and 86 deletions

View File

@ -31,7 +31,7 @@ public abstract class RPSI {
* @param messages Messages to send
* @param response Success Status
*/
protected abstract void sendMessage(UUID[] players, BaseComponent[] messages, Callback<Integer> response);
protected abstract void sendMessage(UUID[] players, BaseComponent[][] messages, Callback<Integer> response);
/**
* Transfers this player to another server

View File

@ -75,7 +75,7 @@ public interface RemotePlayer {
}
/**
* Sends messages to all players
* Sends a message to all players
*
* @param message Message to send
* @param response Success Status
@ -104,7 +104,7 @@ public interface RemotePlayer {
}
/**
* Sends messages to this player
* Sends a message to this player
*
* @param message Message to send
* @param response Success Status
@ -124,8 +124,9 @@ public interface RemotePlayer {
}
/**
* Sends messages to this player
* Sends messages to these players
*
* @param players Players to select
* @param messages Messages to send
*/
static void sendMessage(UUID[] players, String... messages) {
@ -133,8 +134,9 @@ public interface RemotePlayer {
}
/**
* Sends messages to this player
* Sends a message to these players
*
* @param players Players to select
* @param message Message to send
* @param response Success Status
*/
@ -143,8 +145,9 @@ public interface RemotePlayer {
}
/**
* Sends messages to this player
* Sends messages to these players
*
* @param players Players to select
* @param messages Messages to send
* @param response Success Status
*/
@ -153,16 +156,16 @@ public interface RemotePlayer {
}
/**
* Sends messages to all players
* Sends a message to all players
*
* @param messages Messages to send
* @param message Message to send
*/
static void broadcastMessage(BaseComponent... messages) {
broadcastMessage(messages, i -> {});
static void broadcastMessage(BaseComponent... message) {
broadcastMessage(message, i -> {});
}
/**
* Sends messages to all players
* Sends a message to all players
*
* @param message Message to send
* @param response Success Status
@ -171,27 +174,46 @@ public interface RemotePlayer {
broadcastMessage(new BaseComponent[]{ message }, response);
}
/**
* Sends a messages to all players
*
* @param message Message to send
* @param response Success Status
*/
static void broadcastMessage(BaseComponent[] message, Callback<Integer> response) {
broadcastMessage(new BaseComponent[][]{ message }, response);
}
/**
* Sends messages to all players
*
* @param messages Messages to send
*/
static void broadcastMessage(BaseComponent[]... messages) {
broadcastMessage(messages, i -> {});
}
/**
* Sends messages to all players
*
* @param messages Messages to send
* @param response Success Status
*/
static void broadcastMessage(BaseComponent[] messages, Callback<Integer> response) {
static void broadcastMessage(BaseComponent[][] messages, Callback<Integer> response) {
sendMessage(null, messages, response);
}
/**
* Sends messages to this player
* Sends a message to this player
*
* @param messages Messages to send
* @param message Message to send
*/
default void sendMessage(BaseComponent... messages) {
sendMessage(messages, i -> {});
default void sendMessage(BaseComponent... message) {
sendMessage(message, i -> {});
}
/**
* Sends messages to this player
* Sends a message to this player
*
* @param message Message to send
* @param response Success Status
@ -200,28 +222,47 @@ public interface RemotePlayer {
sendMessage(new BaseComponent[]{ message }, response);
}
/**
* Sends a message to this player
*
* @param message Message to send
* @param response Success Status
*/
default void sendMessage(BaseComponent[] message, Callback<Integer> response) {
sendMessage(new BaseComponent[][]{ message }, response);
}
/**
* Sends messages to this player
*
* @param messages Messages to send
*/
default void sendMessage(BaseComponent[]... messages) {
sendMessage(messages, i -> {});
}
/**
* Sends messages to this player
*
* @param messages Messages to send
* @param response Success Status
*/
default void sendMessage(BaseComponent[] messages, Callback<Integer> response) {
default void sendMessage(BaseComponent[][] messages, Callback<Integer> response) {
sendMessage(new UUID[]{ getUniqueId() }, messages, response);
}
/**
* Sends messages to this player
* Sends a message to these players
*
* @param players Players to select
* @param messages Messages to send
* @param message Message to send
*/
static void sendMessage(UUID[] players, BaseComponent... messages) {
sendMessage(players, messages, i -> {});
static void sendMessage(UUID[] players, BaseComponent... message) {
sendMessage(players, message, i -> {});
}
/**
* Sends messages to this player
* Sends a message to these players
*
* @param players Players to select
* @param message Message to send
@ -232,14 +273,35 @@ public interface RemotePlayer {
}
/**
* Sends messages to this player
* Sends a message to these players
*
* @param players Players to select
* @param message Message to send
* @param response Success Status
*/
static void sendMessage(UUID[] players, BaseComponent[] message, Callback<Integer> response) {
instance.sendMessage(players, message, response);
sendMessage(players, new BaseComponent[][]{ message }, response);
}
/**
* Sends messages to these players
*
* @param players Players to select
* @param messages Messages to send
*/
static void sendMessage(UUID[] players, BaseComponent[]... messages) {
sendMessage(players, messages, i -> {});
}
/**
* Sends messages to these players
*
* @param players Players to select
* @param messages Message to send
* @param response Success Status
*/
static void sendMessage(UUID[] players, BaseComponent[][] messages, Callback<Integer> response) {
instance.sendMessage(players, messages, response);
}
/**
@ -262,7 +324,7 @@ public interface RemotePlayer {
}
/**
* Transfers this player to another server
* Transfers these players to another server
*
* @param players Players to select
* @param server Target server
@ -272,7 +334,7 @@ public interface RemotePlayer {
}
/**
* Transfers this player to another server
* Transfers these players to another server
*
* @param players Players to select
* @param server Target server
@ -302,7 +364,7 @@ public interface RemotePlayer {
}
/**
* Transfers this player to another server
* Transfers these players to another server
*
* @param players Players to select
* @param server Target server
@ -312,7 +374,7 @@ public interface RemotePlayer {
}
/**
* Transfers this player to another server
* Transfers these players to another server
*
* @param players Players to select
* @param server Target server
@ -358,7 +420,7 @@ public interface RemotePlayer {
}
/**
* Disconnects this player from the network
* Disconnects these players from the network
*
* @param players Players to select
*/
@ -367,7 +429,7 @@ public interface RemotePlayer {
}
/**
* Disconnects this player from the network
* Disconnects these players from the network
*
* @param players Players to select
* @param response Success status
@ -377,7 +439,7 @@ public interface RemotePlayer {
}
/**
* Disconnects this player from the network
* Disconnects these players from the network
*
* @param players Players to select
* @param reason Disconnect Reason
@ -387,7 +449,7 @@ public interface RemotePlayer {
}
/**
* Disconnects this player from the network
* Disconnects these players from the network
*
* @param players Players to select
* @param reason Disconnect Reason

View File

@ -14,13 +14,10 @@ import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.chat.ComponentSerializer;
import java.lang.reflect.InvocationTargetException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.UUID;
/**
@ -144,22 +141,58 @@ public class RemotePlayer implements net.ME1312.SubServers.Bungee.Library.Compat
new RPSI() {
@Override
protected void sendMessage(UUID[] players, String[] messages, Callback<Integer> response) {
PacketMessagePlayer.run(Arrays.asList(players), new ContainedPair<>(messages, null), null, response);
StackTraceElement[] origin = new Exception().getStackTrace();
PacketMessagePlayer.run(Arrays.asList(players), new ContainedPair<>(messages, null), null, i -> {
try {
response.run(i);
} catch (Throwable e) {
Throwable ew = new InvocationTargetException(e);
ew.setStackTrace(origin);
ew.printStackTrace();
}
});
}
@Override
protected void sendMessage(UUID[] players, BaseComponent[] messages, Callback<Integer> response) {
PacketMessagePlayer.run(Arrays.asList(players), new ContainedPair<>(null, messages), null, response);
protected void sendMessage(UUID[] players, BaseComponent[][] messages, Callback<Integer> response) {
StackTraceElement[] origin = new Exception().getStackTrace();
PacketMessagePlayer.run(Arrays.asList(players), new ContainedPair<>(null, messages), null, i -> {
try {
response.run(i);
} catch (Throwable e) {
Throwable ew = new InvocationTargetException(e);
ew.setStackTrace(origin);
ew.printStackTrace();
}
});
}
@Override
protected void transfer(UUID[] players, String server, Callback<Integer> response) {
PacketTransferPlayer.run(Arrays.asList(players), server, response);
StackTraceElement[] origin = new Exception().getStackTrace();
PacketTransferPlayer.run(Arrays.asList(players), server, i -> {
try {
response.run(i);
} catch (Throwable e) {
Throwable ew = new InvocationTargetException(e);
ew.setStackTrace(origin);
ew.printStackTrace();
}
});
}
@Override
protected void disconnect(UUID[] players, String reason, Callback<Integer> response) {
PacketDisconnectPlayer.run(Arrays.asList(players), reason, response);
StackTraceElement[] origin = new Exception().getStackTrace();
PacketDisconnectPlayer.run(Arrays.asList(players), reason, i -> {
try {
response.run(i);
} catch (Throwable e) {
Throwable ew = new InvocationTargetException(e);
ew.setStackTrace(origin);
ew.printStackTrace();
}
});
}
};
}

View File

@ -63,7 +63,7 @@ public class PacketMessagePlayer implements PacketObjectIn<Integer>, PacketObjec
}
@SuppressWarnings("deprecation")
public static void run(List<UUID> ids, ContainedPair<String[], BaseComponent[]> message, ObjectMap<Integer> data, Callback<Integer> callback) {
public static void run(List<UUID> ids, ContainedPair<String[], BaseComponent[][]> message, ObjectMap<Integer> data, Callback<Integer> callback) {
try {
Container<Integer> failures = new Container<>(0);
HashMap<Proxy, List<UUID>> requests = new HashMap<Proxy, List<UUID>>();
@ -73,8 +73,8 @@ public class PacketMessagePlayer implements PacketObjectIn<Integer>, PacketObjec
if (message.key != null) for (String s : message.key)
ProxyServer.getInstance().broadcast(s);
if (message.value != null)
ProxyServer.getInstance().broadcast(message.value);
if (message.value != null) for (BaseComponent[] c : message.value)
ProxyServer.getInstance().broadcast(c);
}
for (Proxy proxy : SubAPI.getInstance().getProxies().values()) {
if (proxy.getPlayers().size() > 0) requests.put(proxy, null);
@ -87,8 +87,8 @@ public class PacketMessagePlayer implements PacketObjectIn<Integer>, PacketObjec
if (message == null) message = parseMessage(data);
if (message.key != null)
local.sendMessages(message.key);
if (message.value != null)
local.sendMessage(message.value);
if (message.value != null) for (BaseComponent[] c : message.value)
local.sendMessage(c);
} else if ((remote = SubAPI.getInstance().getRemotePlayer(id)) != null && remote.getProxy().getSubData()[0] != null) {
Proxy proxy = remote.getProxy();
List<UUID> list = requests.getOrDefault(proxy, new ArrayList<>());
@ -109,7 +109,12 @@ public class PacketMessagePlayer implements PacketObjectIn<Integer>, PacketObjec
List<String> legacy, raw;
if (data == null) {
legacy = (message.key != null?Arrays.asList(message.key):null);
raw = (message.value != null?Collections.singletonList(ComponentSerializer.toString(message.value)):null);
if (message.value != null) {
raw = new LinkedList<String>();
for (BaseComponent[] c : message.value) raw.add(ComponentSerializer.toString(c));
} else {
raw = null;
}
} else {
legacy = (data.contains(0x0002)?data.getRawStringList(0x0002):null);
raw = (data.contains(0x0003)?data.getRawStringList(0x0003):null);
@ -128,15 +133,15 @@ public class PacketMessagePlayer implements PacketObjectIn<Integer>, PacketObjec
}
}
private static ContainedPair<String[], BaseComponent[]> parseMessage(ObjectMap<Integer> data) {
ContainedPair<String[], BaseComponent[]> value = new ContainedPair<>();
private static ContainedPair<String[], BaseComponent[][]> parseMessage(ObjectMap<Integer> data) {
ContainedPair<String[], BaseComponent[][]> value = new ContainedPair<>();
if (data.contains(0x0002))
value.key = data.getRawStringList(0x0002).toArray(new String[0]);
if (data.contains(0x0003)) {
List<String> messages = data.getRawStringList(0x0003);
LinkedList<BaseComponent> components = new LinkedList<BaseComponent>();
for (String message : messages) components.addAll(Arrays.asList(ComponentSerializer.parse(message)));
value.value = components.toArray(new BaseComponent[0]);
BaseComponent[][] components = new BaseComponent[messages.size()][];
for (int i = 0; i < components.length; ++i) components[i] = ComponentSerializer.parse(messages.get(i));
value.value = components;
}
return value;
}

View File

@ -178,7 +178,7 @@ public class RemotePlayer {
}
/**
* Sends messages to all players
* Sends a message to all players
*
* @param message Message to send
* @param response Success Status
@ -207,7 +207,7 @@ public class RemotePlayer {
}
/**
* Sends messages to this player
* Sends a message to this player
*
* @param message Message to send
* @param response Success Status
@ -227,8 +227,9 @@ public class RemotePlayer {
}
/**
* Sends messages to this player
* Sends messages to these players
*
* @param players Players to select
* @param messages Messages to send
*/
public static void sendMessage(UUID[] players, String... messages) {
@ -236,8 +237,9 @@ public class RemotePlayer {
}
/**
* Sends messages to this player
* Sends a message to these players
*
* @param players Players to select
* @param message Message to send
* @param response Success Status
*/
@ -246,8 +248,9 @@ public class RemotePlayer {
}
/**
* Sends messages to this player
* Sends messages to these players
*
* @param players Players to select
* @param messages Messages to send
* @param response Success Status
*/
@ -265,7 +268,7 @@ public class RemotePlayer {
}
/**
* Sends JSON format messages to all players
* Sends a JSON format message to all players
*
* @param message Message to send
* @param response Success Status
@ -294,7 +297,7 @@ public class RemotePlayer {
}
/**
* Sends JSON format messages to this player
* Sends a JSON format message to this player
*
* @param message Message to send
* @param response Success Status
@ -314,7 +317,7 @@ public class RemotePlayer {
}
/**
* Sends JSON format messages to this player
* Sends JSON format messages to these players
*
* @param players Players to select
* @param messages Messages to send
@ -324,7 +327,7 @@ public class RemotePlayer {
}
/**
* Sends JSON format messages to this player
* Sends a JSON format message to these players
*
* @param players Players to select
* @param message Message to send
@ -335,14 +338,14 @@ public class RemotePlayer {
}
/**
* Sends JSON format messages to this player
* Sends JSON format messages to these players
*
* @param players Players to select
* @param message Message to send
* @param messages Messages to send
* @param response Success Status
*/
public static void sendRawMessage(UUID[] players, String[] message, Callback<Integer> response) {
st4tic.sendRawMessage(SimplifiedData.client(ClientAPI.getInstance().getSubDataNetwork()[0]), players, message, response);
public static void sendRawMessage(UUID[] players, String[] messages, Callback<Integer> response) {
st4tic.sendRawMessage(SimplifiedData.client(ClientAPI.getInstance().getSubDataNetwork()[0]), players, messages, response);
}
/**
@ -365,7 +368,7 @@ public class RemotePlayer {
}
/**
* Transfers this player to another server
* Transfers these players to another server
*
* @param players Players to select
* @param server Target server
@ -375,7 +378,7 @@ public class RemotePlayer {
}
/**
* Transfers this player to another server
* Transfers these players to another server
*
* @param players Players to select
* @param server Target server
@ -421,7 +424,7 @@ public class RemotePlayer {
}
/**
* Disconnects this player from the network
* Disconnects these players from the network
*
* @param players Players to select
*/
@ -430,7 +433,7 @@ public class RemotePlayer {
}
/**
* Disconnects this player from the network
* Disconnects these players from the network
*
* @param players Players to select
* @param response Success status
@ -440,7 +443,7 @@ public class RemotePlayer {
}
/**
* Disconnects this player from the network
* Disconnects these players from the network
*
* @param players Players to select
* @param reason Disconnect Reason
@ -450,7 +453,7 @@ public class RemotePlayer {
}
/**
* Disconnects this player from the network
* Disconnects these players from the network
*
* @param players Players to select
* @param reason Disconnect Reason

View File

@ -57,31 +57,30 @@ public class PacketExMessagePlayer implements PacketObjectIn<Integer>, PacketObj
List<UUID> ids = (data.contains(0x0001)?data.getUUIDList(0x0001):null);
try {
String[] legacy = null;
BaseComponent[] components = null;
BaseComponent[][] components = null;
if (data.contains(0x0002))
legacy = data.getRawStringList(0x0002).toArray(new String[0]);
if (data.contains(0x0003)) {
List<String> messages = data.getRawStringList(0x0003);
LinkedList<BaseComponent> list = new LinkedList<BaseComponent>();
for (String message : messages) list.addAll(Arrays.asList(ComponentSerializer.parse(message)));
components = list.toArray(new BaseComponent[0]);
components = new BaseComponent[messages.size()][];
for (int i = 0; i < components.length; ++i) components[i] = ComponentSerializer.parse(messages.get(i));
}
int failures = 0;
if (ids == null || ids.size() == 0) {
if (legacy != null) for (String s : legacy)
ProxyServer.getInstance().broadcast(s);
if (components != null)
ProxyServer.getInstance().broadcast(components);
if (components != null) for (BaseComponent[] c : components)
ProxyServer.getInstance().broadcast(c);
} else {
for (UUID id : ids) {
ProxiedPlayer local;
if ((local = ProxyServer.getInstance().getPlayer(id)) != null) {
if (legacy != null)
local.sendMessages(legacy);
if (components != null)
local.sendMessage(components);
if (components != null) for (BaseComponent[] c : components)
local.sendMessage(c);
} else {
++failures;
}

View File

@ -90,8 +90,10 @@ public class CachedPlayer extends RemotePlayer implements net.ME1312.SubServers.
}
@Override
protected void sendMessage(UUID[] players, BaseComponent[] messages, Callback<Integer> response) {
RemotePlayer.sendRawMessage(players, ComponentSerializer.toString(messages), response);
protected void sendMessage(UUID[] players, BaseComponent[][] messages, Callback<Integer> response) {
String[] raw = new String[messages.length];
for (int i = 0; i < raw.length; ++i) raw[i] = ComponentSerializer.toString(messages[i]);
RemotePlayer.sendRawMessage(players, raw, response);
}
@Override
@ -138,16 +140,17 @@ public class CachedPlayer extends RemotePlayer implements net.ME1312.SubServers.
protected void sendRawMessage(SubDataClient client, UUID[] players, String[] messages, Callback<Integer> response) {
if (players != null && players.length > 0) {
ArrayList<UUID> ids = new ArrayList<UUID>();
BaseComponent[] components = null;
BaseComponent[][] components = null;
for (UUID id : players) {
ProxiedPlayer local = get(id);
if (local != null) {
if (components == null) {
LinkedList<BaseComponent> list = new LinkedList<BaseComponent>();
for (String message : messages) list.addAll(Arrays.asList(ComponentSerializer.parse(message)));
components = list.toArray(new BaseComponent[0]);
components = new BaseComponent[messages.length][];
for (int i = 0; i < components.length; ++i) components[i] = ComponentSerializer.parse(messages[i]);
}
for (BaseComponent[] c : components) {
local.sendMessage(c);
}
local.sendMessage(components);
} else {
ids.add(id);
}