package storagecraft.tile;

import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound;

public abstract class TileMachine extends TileBase implements INetworkTile, IRedstoneModeSetting
{
	protected boolean connected = false;
	protected boolean redstoneControlled = true;

	private RedstoneMode redstoneMode = RedstoneMode.IGNORE;

	private int xController;
	private int yController;
	private int zController;

	public void onConnected(TileController controller)
	{
		this.connected = true;

		this.xController = controller.xCoord;
		this.yController = controller.yCoord;
		this.zController = controller.zCoord;

		worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
	}

	public void onDisconnected()
	{
		this.connected = false;

		worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
	}

	@Override
	public void updateEntity()
	{
		super.updateEntity();

		if (!worldObj.isRemote && isConnected())
		{
			updateMachine();
		}
	}

	public boolean isConnected()
	{
		return connected;
	}

	@Override
	public RedstoneMode getRedstoneMode()
	{
		return redstoneMode;
	}

	@Override
	public void setRedstoneMode(RedstoneMode mode)
	{
		if (redstoneControlled)
		{
			this.redstoneMode = mode;
		}
	}

	@Override
	public int getX()
	{
		return xCoord;
	}

	@Override
	public int getY()
	{
		return yCoord;
	}

	@Override
	public int getZ()
	{
		return zCoord;
	}

	public TileController getController()
	{
		return (TileController) worldObj.getTileEntity(xController, yController, zController);
	}

	@Override
	public void fromBytes(ByteBuf buf)
	{
		connected = buf.readBoolean();

		if (connected)
		{
			xController = buf.readInt();
			yController = buf.readInt();
			zController = buf.readInt();
		}

		redstoneMode = RedstoneMode.getById(buf.readInt());
	}

	@Override
	public void toBytes(ByteBuf buf)
	{
		buf.writeBoolean(connected);

		if (connected)
		{
			buf.writeInt(xController);
			buf.writeInt(yController);
			buf.writeInt(zController);
		}

		buf.writeInt(redstoneMode.id);
	}

	@Override
	public void readFromNBT(NBTTagCompound nbt)
	{
		super.readFromNBT(nbt);

		if (nbt.hasKey(RedstoneMode.NBT))
		{
			redstoneMode = RedstoneMode.getById(nbt.getInteger(RedstoneMode.NBT));
		}
	}

	@Override
	public void writeToNBT(NBTTagCompound nbt)
	{
		super.writeToNBT(nbt);

		nbt.setInteger(RedstoneMode.NBT, redstoneMode.id);
	}

	public abstract int getEnergyUsage();

	public abstract void updateMachine();
}

@@ -484,37 +484,32 @@
 troller)%0D%0A%09%7B%0D%0A%09%09
-this.
 connected = true
@@ -515,21 +515,16 @@
 e;%0D%0A%0D%0A%09%09
-this.
 xControl
@@ -551,21 +551,16 @@
 ord;%0D%0A%09%09
-this.
 yControl
@@ -587,21 +587,16 @@
 ord;%0D%0A%09%09
-this.
 zControl
@@ -624,66 +624,8 @@
 rd;%0D
-%0A%0D%0A%09%09worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);%0D
 %0A%09%7D%0D
@@ -664,21 +664,16 @@
 %0D%0A%09%7B%0D%0A%09%09
-this.
 connecte
@@ -687,66 +687,8 @@
 se;%0D
-%0A%0D%0A%09%09worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);%0D
 %0A%09%7D%0D
@@ -1498,24 +1498,64 @@
 f buf)%0D%0A%09%7B%0D%0A
+%09%09boolean lastConnected = connected;%0D%0A%0D%0A
 %09%09connected 
@@ -1761,16 +1761,120 @@
 Int());%0D
+%0A%0D%0A%09%09if (lastConnected != connected)%0D%0A%09%09%7B%0D%0A%09%09%09worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);%0D%0A%09%09%7D%0D
 %0A%09%7D%0D%0A%0D%0A%09

fix textures not updating on connect / disconnect