package storagecraft.tile;

import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.BlockPos;
import storagecraft.block.BlockMachine;
import storagecraft.tile.settings.IRedstoneModeSetting;
import storagecraft.tile.settings.RedstoneMode;

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

	private RedstoneMode redstoneMode = RedstoneMode.IGNORE;

	private BlockPos controllerPos;

	public void onConnected(TileController controller)
	{
		markDirty();

		connected = true;

		controllerPos = controller.getPos();

		worldObj.setBlockState(pos, worldObj.getBlockState(pos).withProperty(BlockMachine.CONNECTED, true));
	}

	public void onDisconnected()
	{
		markDirty();

		connected = false;

		if (!worldObj.isAirBlock(pos))
		{
			worldObj.setBlockState(pos, worldObj.getBlockState(pos).withProperty(BlockMachine.CONNECTED, false));
		}
	}

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

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

	public boolean isConnected()
	{
		return connected;
	}

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

	@Override
	public void setRedstoneMode(RedstoneMode mode)
	{
		if (redstoneControlled)
		{
			markDirty();

			this.redstoneMode = mode;
		}
	}

	@Override
	public BlockPos getMachinePos()
	{
		return pos;
	}

	@Override
	public BlockPos getTilePos()
	{
		return pos;
	}

	public TileController getController()
	{
		return (TileController) worldObj.getTileEntity(controllerPos);
	}

	@Override
	public void fromBytes(ByteBuf buf)
	{
		boolean lastConnected = connected;

		connected = buf.readBoolean();

		if (connected)
		{
			controllerPos = new BlockPos(buf.readInt(), buf.readInt(), buf.readInt());
		}

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

		if (lastConnected != connected)
		{
			worldObj.markBlockForUpdate(pos);
		}
	}

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

		if (connected)
		{
			buf.writeInt(controllerPos.getX());
			buf.writeInt(controllerPos.getY());
			buf.writeInt(controllerPos.getZ());
		}

		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();
}

@@ -53,16 +53,50 @@
 yteBuf;%0A
+import net.minecraft.block.Block;%0A
 import n
@@ -583,16 +583,52 @@
 ollerPos
+ition;%0A%0A%09private Block originalBlock
 ;%0A%0A%09publ
@@ -673,24 +673,92 @@
 troller)%0A%09%7B%0A
+%09%09if (worldObj.getBlockState(pos).getBlock() == originalBlock)%0A%09%09%7B%0A%09
 %09%09markDirty(
@@ -753,32 +753,33 @@
 %09%09markDirty();%0A%0A
+%09
 %09%09connected = tr
@@ -788,47 +788,8 @@
 ;%0A%0A%09
-%09controllerPos = controller.getPos();%0A%0A
 %09%09wo
@@ -891,45 +891,163 @@
 ));%0A
-%09%7D%0A%0A%09public void onDisconnected()%0A%09%7B%0A
+%0A%09%09%09controllerPosition = controller.getPos();%0A%09%09%7D%0A%09%7D%0A%0A%09public void onDisconnected()%0A%09%7B%0A%09%09if (worldObj.getBlockState(pos).getBlock() == originalBlock)%0A%09%09%7B%0A%09
 %09%09ma
@@ -1058,16 +1058,17 @@
 rty();%0A%0A
+%09
 %09%09connec
@@ -1085,45 +1085,8 @@
 e;%0A%0A
-%09%09if (!worldObj.isAirBlock(pos))%0A%09%09%7B%0A
 %09%09%09w
@@ -1226,24 +1226,110 @@
 update()%0A%09%7B%0A
+%09%09if (ticks == 0)%0A%09%09%7B%0A%09%09%09originalBlock = worldObj.getBlockState(pos).getBlock();%0A%09%09%7D%0A%0A
 %09%09super.upda
@@ -1924,16 +1924,21 @@
 ollerPos
+ition
 );%0A%09%7D%0A%0A%09
@@ -2095,16 +2095,21 @@
 ollerPos
+ition
  = new B
@@ -2431,24 +2431,29 @@
 ontrollerPos
+ition
 .getX());%0A%09%09
@@ -2475,24 +2475,29 @@
 ontrollerPos
+ition
 .getY());%0A%09%09
@@ -2523,16 +2523,21 @@
 ollerPos
+ition
 .getZ())

fix crash on explosion in machines