package com.slugterra.packets;

import com.slugterra.capabilities.ISlugInv;
import com.slugterra.capabilities.SlugInventoryProvider;

import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class SyncPlayerPropsPacket implements IMessage
{
	// Previously, we've been writing each field in our properties one at a time,
	// but that is really annoying, and we've already done it in the save and load
	// NBT methods anyway, so here's a slick way to efficiently send all of your
	// extended data, and no matter how much you add or remove, you'll never have
	// to change the packet / synchronization of your data.

	// this will store our ExtendedPlayer data, allowing us to easily read and write
	private NBTTagCompound data;
	private int slot;

	// The basic, no-argument constructor MUST be included to use the new automated handling
	public SyncPlayerPropsPacket() {}

	// We need to initialize our data, so provide a suitable constructor:
	public SyncPlayerPropsPacket(EntityPlayer player) {
		// create a new tag compound
		data = new NBTTagCompound();
		// and save our player's data into it
		ISlugInv props = player.getCapability(SlugInventoryProvider.INV_CAP, null);
		props.saveInventoryToNBT(data);
		slot = props.getSlot();
}

	@Override
	public void fromBytes(ByteBuf buf) {
		data = ByteBufUtils.readTag(buf);
		slot = buf.readInt();
	}

	@Override
	public void toBytes(ByteBuf buf) {
		buf.writeInt(slot);
		ByteBufUtils.writeTag(buf, data);
	}

	public static class Handler implements IMessageHandler<SyncPlayerPropsPacket, IMessage> {

		@Override
		public IMessage onMessage(SyncPlayerPropsPacket message, MessageContext ctx) {
			EntityPlayerMP player = ctx.getServerHandler().playerEntity;
			//Load the data back into the player capability
			ISlugInv props = player.getCapability(SlugInventoryProvider.INV_CAP, null);
			props.loadInventory(message.data);
			props.setSlot(message.slot);
			return null; // no response in this case
		}
	}
}
@@ -182,37 +182,77 @@
 t.minecraft.
-entity.player
+client.Minecraft;%0D%0Aimport net.minecraft.client.entity
 .EntityPlaye
@@ -252,16 +252,18 @@
 tyPlayer
+SP
 ;%0D%0Aimpor
@@ -304,18 +304,16 @@
 tyPlayer
-MP
 ;%0D%0Aimpor
@@ -1489,46 +1489,8 @@
 ound
-%0D%0A%09%09data = new NBTTagCompound();%0D%0A%09%09//
  and
@@ -1499,20 +1499,18 @@
 ave 
-our
+the
  player
-'s
  dat
@@ -1601,16 +1601,23 @@
 ll);%0D%0A%09%09
+data = 
 props.sa
@@ -1633,20 +1633,36 @@
 ryToNBT(
-data
+new NBTTagCompound()
 );%0D%0A%09%09sl
@@ -1740,16 +1740,41 @@
 buf) %7B%0D%0A
+%09%09slot = buf.readInt();%0D%0A
 %09%09data =
@@ -1806,33 +1806,8 @@
 );%0D%0A
-%09%09slot = buf.readInt();%0D%0A
 %09%7D%0D%0A
@@ -2112,16 +2112,107 @@
 ctx) %7B%0D%0A
+%09%09%09// Since this is a client side handler, we can access the singleton Minecraft instance%0D%0A
 %09%09%09Entit
@@ -2218,17 +2218,17 @@
 tyPlayer
-M
+S
 P player
@@ -2234,28 +2234,30 @@
 r = 
-ctx.getServerHandler
+Minecraft.getMinecraft
 ().p
@@ -2261,22 +2261,16 @@
 ).player
-Entity
 ;%0D%0A%09%09%09//

Fix IlegalCastException on SyncPlayerPropsPacket