package mariculture.core.network;

import io.netty.buffer.ByteBuf;

import java.util.ArrayList;

import mariculture.api.util.CachedCoords;
import mariculture.fishery.tile.TileFeeder;
import mariculture.lib.helpers.ClientHelper;
import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;

public class PacketSyncFeeder implements IMessage, IMessageHandler<PacketSyncFeeder, IMessage> {
    public NBTTagCompound nbt;

    public PacketSyncFeeder() {}

    public PacketSyncFeeder(int x, int y, int z, ArrayList<CachedCoords> coord, Block block) {
        nbt = new NBTTagCompound();
        nbt.setInteger("x", x);
        nbt.setInteger("y", y);
        nbt.setInteger("z", z);
        NBTTagList list = new NBTTagList();
        for (CachedCoords c : coord) {
            NBTTagCompound tag = new NBTTagCompound();
            tag.setInteger("x", c.x);
            tag.setInteger("y", c.y);
            tag.setInteger("z", c.z);
            list.appendTag(tag);
        }

        nbt.setTag("Coordinates", list);
        nbt.setInteger("BlockID", Block.getIdFromBlock(block));
    }

    @Override
    public void toBytes(ByteBuf buffer) {
        try {
            new PacketBuffer(buffer).writeNBTTagCompoundToBuffer(nbt);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void fromBytes(ByteBuf buffer) {
        try {
            nbt = new PacketBuffer(buffer).readNBTTagCompoundFromBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public IMessage onMessage(PacketSyncFeeder message, MessageContext ctx) {
        World world = ClientHelper.getPlayer().worldObj;
        int x = message.nbt.getInteger("x");
        int y = message.nbt.getInteger("y");
        int z = message.nbt.getInteger("z");
        int id = message.nbt.getInteger("BlockID");
        ArrayList<CachedCoords> coords = new ArrayList();
        NBTTagList tagList = message.nbt.getTagList("Coordinates", 10);
        for (int i = 0; i < tagList.tagCount(); i++) {
            NBTTagCompound tag = tagList.getCompoundTagAt(i);
            coords.add(new CachedCoords(tag.getInteger("x"), tag.getInteger("y"), tag.getInteger("z")));
        }

        TileEntity tile = world.getTileEntity(x, y, z);
        if (tile instanceof TileFeeder) {
            ((TileFeeder) tile).coords = coords;
            ((TileFeeder) tile).tankBlock = id == 0? null: Block.getBlockById(id);
        }

        return null;
    }
}

@@ -1359,24 +1359,58 @@
 es%22, list);%0A
+%0A        if (block != null) %7B%0A    
         nbt.
@@ -1412,23 +1412,22 @@
  nbt.set
-Integer
+String
 (%22BlockI
@@ -1440,31 +1440,57 @@
 ock.
-getIdFromBlock(block));
+blockRegistry.getNameForObject(block));%0A        %7D
 %0A   
@@ -2241,17 +2241,131 @@
     
-int id = 
+Block block = null;%0A        if (message.nbt.hasKey(%22BlockID%22)) %7B%0A            block = (Block) Block.blockRegistry.getObject(
 mess
@@ -2375,23 +2375,22 @@
 .nbt.get
-Integer
+String
 (%22BlockI
@@ -2392,18 +2392,30 @@
 lockID%22)
+)
 ;%0A
+        %7D%0A%0A
         
@@ -2964,45 +2964,13 @@
 k = 
-id == 0? null: Block.getBlockById(id)
+block
 ;%0A  

Switch syncing fish feeders to use the block name. Hopefully fix broken fish feeders in smp?