package protocolsupport.protocol.packet.middleimpl.readable.play.v_pe;

import java.text.MessageFormat;
import java.util.Collection;
import java.util.Collections;

import org.apache.commons.lang3.Validate;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.md_5.bungee.protocol.PacketWrapper;
import net.md_5.bungee.protocol.packet.Chat;
import protocolsupport.protocol.packet.id.PEPacketId;
import protocolsupport.protocol.packet.middleimpl.readable.PEDefinedReadableMiddlePacket;
import protocolsupport.protocol.serializer.StringSerializer;

public class FromClientChatPacket extends PEDefinedReadableMiddlePacket {

	public static final int CLIENT_CHAT_TYPE = 1;

	protected String message;

	public FromClientChatPacket() {
		super(PEPacketId.Dualbound.PLAY_CHAT);
	}

	@Override
	protected void read0(ByteBuf from) {
		int type = from.readUnsignedByte();
		Validate.isTrue(type == CLIENT_CHAT_TYPE, MessageFormat.format("Unexcepted serverbound chat type, expected {0}, but received {1}", CLIENT_CHAT_TYPE, type));
		from.readBoolean(); //needs translation
		StringSerializer.readVarIntUTF8String(from); //skip sender
		message = StringSerializer.readVarIntUTF8String(from);
		StringSerializer.readVarIntUTF8String(from); //Xbox user ID
	}

	@Override
	public Collection<PacketWrapper> toNative() {
		return Collections.singletonList(new PacketWrapper(new Chat(message), Unpooled.wrappedBuffer(readbytes)));
	}

}

@@ -561,16 +561,80 @@
 ializer;
+%0Aimport protocolsupport.protocol.serializer.VarNumberSerializer;
 %0A%0Apublic
@@ -1205,16 +1205,151 @@
  sender%0A
+%09%09StringSerializer.readVarIntUTF8String(from); //skip third party name%0A%09%09VarNumberSerializer.readSVarInt(from); //skip source platform%0A
 %09%09messag
@@ -1446,16 +1446,21 @@
 rom); //
+skip 
 Xbox use
@@ -1464,16 +1464,87 @@
 user ID%0A
+%09%09StringSerializer.readVarIntUTF8String(from); //skip platform chat ID%0A
 %09%7D%0A%0A%09@Ov

Fix chat