From 153f09ae7099a3f56f2db8b0fb98fa6150728999 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Mon, 30 Jul 2018 01:35:47 -0400 Subject: [PATCH] Add support for Plugin Message packet, full implementation later. --- .../proxy/protocol/StateRegistry.java | 6 +++++ .../netty/MinecraftVarintLengthEncoder.java | 1 + .../proxy/protocol/packets/PluginMessage.java | 24 +++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 src/main/java/com/velocitypowered/proxy/protocol/packets/PluginMessage.java diff --git a/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java b/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java index 10a76cab..31c3897e 100644 --- a/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java +++ b/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java @@ -35,6 +35,10 @@ public enum StateRegistry { map(0x02, MINECRAFT_1_11), map(0x03, MINECRAFT_1_12), map(0x02, MINECRAFT_1_12_2)); + SERVERBOUND.register(PluginMessage.class, PluginMessage::new, + map(0x0A, MINECRAFT_1_11), + map(0x0A, MINECRAFT_1_12), + map(0x09, MINECRAFT_1_12_1)); SERVERBOUND.register(KeepAlive.class, KeepAlive::new, map(0x0B, MINECRAFT_1_11), map(0x0C, MINECRAFT_1_12), @@ -48,6 +52,8 @@ public enum StateRegistry { map(0x0C, MINECRAFT_1_11)); CLIENTBOUND.register(Chat.class, Chat::new, map(0x0F, MINECRAFT_1_11)); + CLIENTBOUND.register(PluginMessage.class, PluginMessage::new, + map(0x18, MINECRAFT_1_11)); CLIENTBOUND.register(Disconnect.class, Disconnect::new, map(0x1A, MINECRAFT_1_11)); CLIENTBOUND.register(KeepAlive.class, KeepAlive::new, diff --git a/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintLengthEncoder.java b/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintLengthEncoder.java index 3c995233..5686b13f 100644 --- a/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintLengthEncoder.java +++ b/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftVarintLengthEncoder.java @@ -14,6 +14,7 @@ public class MinecraftVarintLengthEncoder extends MessageToByteEncoder @Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { + out.ensureWritable(msg.readableBytes() + 5); ProtocolUtils.writeVarInt(out, msg.readableBytes()); out.writeBytes(msg); } diff --git a/src/main/java/com/velocitypowered/proxy/protocol/packets/PluginMessage.java b/src/main/java/com/velocitypowered/proxy/protocol/packets/PluginMessage.java new file mode 100644 index 00000000..ed7ad1da --- /dev/null +++ b/src/main/java/com/velocitypowered/proxy/protocol/packets/PluginMessage.java @@ -0,0 +1,24 @@ +package com.velocitypowered.proxy.protocol.packets; + +import com.velocitypowered.proxy.protocol.MinecraftPacket; +import com.velocitypowered.proxy.protocol.ProtocolConstants; +import com.velocitypowered.proxy.protocol.ProtocolUtils; +import io.netty.buffer.ByteBuf; + +public class PluginMessage implements MinecraftPacket { + private String channel; + private byte[] data; + + @Override + public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + this.channel = ProtocolUtils.readString(buf, 20); + this.data = new byte[buf.readableBytes()]; + buf.readBytes(this.data); + } + + @Override + public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { + ProtocolUtils.writeString(buf, channel); + buf.writeBytes(data); + } +}