From b805891d1fa7005bd522d80a941e863fa723e719 Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 20 Sep 2018 19:42:27 +0100 Subject: [PATCH] Implement Player#spoofChatInput method --- .../java/com/velocitypowered/api/proxy/Player.java | 7 +++++++ .../proxy/connection/client/ConnectedPlayer.java | 8 +++++++- .../velocitypowered/proxy/protocol/packet/Chat.java | 12 +++++++++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/proxy/Player.java b/api/src/main/java/com/velocitypowered/api/proxy/Player.java index 0bdc9b607..6d1fa94df 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/Player.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/Player.java @@ -82,4 +82,11 @@ public interface Player extends CommandSource, InboundConnection, ChannelMessage * @param reason component with the reason */ void disconnect(Component reason); + + /** + * Sends chat input onto the players current server as if they typed it + * into the client chat box. + * @param input the chat input to send + */ + void spoofChatInput(String input); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index 8917fec7e..77f8722a8 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -247,7 +247,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { connection.closeWith(Disconnect.create(friendlyReason)); } } else { - connection.write(Chat.create(friendlyReason)); + connection.write(Chat.createClientbound(friendlyReason)); } } @@ -350,6 +350,12 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { return true; } + @Override + public void spoofChatInput(String input) { + Preconditions.checkArgument(input.length() <= Chat.MAX_SERVERBOUND_MESSAGE_LENGTH, "input cannot be greater than " + Chat.MAX_SERVERBOUND_MESSAGE_LENGTH + " characters in length"); + connectedServer.getMinecraftConnection().write(Chat.createServerbound(input)); + } + private class ConnectionRequestBuilderImpl implements ConnectionRequestBuilder { private final RegisteredServer server; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Chat.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Chat.java index f289b7c54..749854506 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Chat.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Chat.java @@ -10,6 +10,8 @@ import net.kyori.text.serializer.ComponentSerializers; public class Chat implements MinecraftPacket { public static final byte CHAT = (byte) 0; + public static final int MAX_SERVERBOUND_MESSAGE_LENGTH = 256; + private String message; private byte type; @@ -61,12 +63,16 @@ public class Chat implements MinecraftPacket { } } - public static Chat create(Component component) { - return create(component, CHAT); + public static Chat createClientbound(Component component) { + return createClientbound(component, CHAT); } - public static Chat create(Component component, byte type) { + public static Chat createClientbound(Component component, byte type) { Preconditions.checkNotNull(component, "component"); return new Chat(ComponentSerializers.JSON.serialize(component), type); } + + public static Chat createServerbound(String message) { + return new Chat(message, CHAT); + } }