Merge pull request #100 from lucko/feature/player-spoof-chat

Implement Player#spoofChatInput method
This commit is contained in:
Andrew Steinborn
2018-09-20 16:54:12 -04:00
committed by GitHub
3 changed files with 23 additions and 4 deletions

View File

@@ -82,4 +82,11 @@ public interface Player extends CommandSource, InboundConnection, ChannelMessage
* @param reason component with the reason * @param reason component with the reason
*/ */
void disconnect(Component 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);
} }

View File

@@ -247,7 +247,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
connection.closeWith(Disconnect.create(friendlyReason)); connection.closeWith(Disconnect.create(friendlyReason));
} }
} else { } else {
connection.write(Chat.create(friendlyReason)); connection.write(Chat.createClientbound(friendlyReason));
} }
} }
@@ -350,6 +350,12 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
return true; 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 class ConnectionRequestBuilderImpl implements ConnectionRequestBuilder {
private final RegisteredServer server; private final RegisteredServer server;

View File

@@ -10,6 +10,8 @@ import net.kyori.text.serializer.ComponentSerializers;
public class Chat implements MinecraftPacket { public class Chat implements MinecraftPacket {
public static final byte CHAT = (byte) 0; public static final byte CHAT = (byte) 0;
public static final int MAX_SERVERBOUND_MESSAGE_LENGTH = 256;
private String message; private String message;
private byte type; private byte type;
@@ -61,12 +63,16 @@ public class Chat implements MinecraftPacket {
} }
} }
public static Chat create(Component component) { public static Chat createClientbound(Component component) {
return create(component, CHAT); return createClientbound(component, CHAT);
} }
public static Chat create(Component component, byte type) { public static Chat createClientbound(Component component, byte type) {
Preconditions.checkNotNull(component, "component"); Preconditions.checkNotNull(component, "component");
return new Chat(ComponentSerializers.JSON.serialize(component), type); return new Chat(ComponentSerializers.JSON.serialize(component), type);
} }
public static Chat createServerbound(String message) {
return new Chat(message, CHAT);
}
} }