package zmaster587.advancedRocketry.tile;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;

import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagCompound;
import zmaster587.advancedRocketry.tile.multiblock.TilePlaceholder;
import zmaster587.libVulpes.block.BlockMeta;

public class TileSchematic extends TilePlaceholder {

	private final int ttl = 6000;
	private int timeAlive = 0;
	List<BlockMeta> possibleBlocks;

	public TileSchematic() {
		possibleBlocks = new ArrayList<BlockMeta>();
	}
	
	@Override
	public boolean canUpdate() {
		return true;
	}

	public void setReplacedBlock(List<BlockMeta> block) {
		possibleBlocks = block;
	}
	
	@Override
	public void setReplacedBlock(Block block) {
		super.setReplacedBlock(block);
		possibleBlocks.clear();
	}
	
	@Override
	public void setReplacedBlockMeta(byte meta) {
		super.setReplacedBlockMeta(meta);
		possibleBlocks.clear();
	}

	@Override
	public Block getReplacedBlock() {
		if(possibleBlocks.isEmpty())
			return super.getReplacedBlock();
		else
			return possibleBlocks.get((timeAlive/20) % possibleBlocks.size()).getBlock();
	}
	
	@Override
	public byte getReplacedBlockMeta() {
		if(possibleBlocks.isEmpty())
			return super.getReplacedBlockMeta();
		else
			return possibleBlocks.get((timeAlive/20) % possibleBlocks.size()).getMeta();
	}

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

		if(!worldObj.isRemote) {
			if(timeAlive == ttl) {
				worldObj.setBlockToAir(xCoord, yCoord, zCoord);
			}
		}
		timeAlive++;
	}

	@Override
	public void writeToNBT(NBTTagCompound nbt) {
		super.writeToNBT(nbt);
		nbt.setInteger("timeAlive", timeAlive);
		
		List<Integer> blockIds = new ArrayList<Integer>();
		List<Integer> blockMetas = new ArrayList<Integer>();
		for(int i = 0;  i < possibleBlocks.size();i++) {
			blockIds.add(Block.getIdFromBlock(possibleBlocks.get(i).getBlock()));
			blockMetas.add((int)possibleBlocks.get(i).getMeta());
		}
		
		if(!blockIds.isEmpty()) {
			Integer[] bufferSpace1 = new Integer[blockIds.size()];
			Integer[] bufferSpace2 = new Integer[blockIds.size()];
			nbt.setIntArray("blockIds", ArrayUtils.toPrimitive(blockIds.toArray(bufferSpace1)));
			nbt.setIntArray("blockMetas", ArrayUtils.toPrimitive(blockMetas.toArray(bufferSpace2)));
		}
	}

	@Override
	public void readFromNBT(NBTTagCompound nbt) {
		super.readFromNBT(nbt);
		timeAlive = nbt.getInteger("timeAlive");
		
		if(nbt.hasKey("blockIds")) {
			int[] block = nbt.getIntArray("blockIds");
			int[] metas = nbt.getIntArray("blockMetas");
			possibleBlocks.clear();
			
			for(int i = 0; i < block.length; i++) {
				possibleBlocks.add(new BlockMeta(Block.getBlockById(block[i]), metas[i]));
			}
		}
	}
}

@@ -167,16 +167,50 @@
 .Block;%0A
+import net.minecraft.init.Blocks;%0A
 import n
@@ -242,16 +242,16 @@
 mpound;%0A
-
 import z
@@ -575,26 +575,25 @@
 Meta%3E();%0A%09%7D%0A
-%09
 %0A
+
 %09@Override%0A%09
@@ -720,26 +720,25 @@
 = block;%0A%09%7D%0A
-%09
 %0A
+
 %09@Override%0A%09
@@ -839,26 +839,25 @@
 clear();%0A%09%7D%0A
-%09
 %0A
+
 %09@Override%0A%09
@@ -1079,32 +1079,34 @@
 dBlock();%0A%09%09else
+ %7B
 %0A%09%09%09return possi
@@ -1175,13 +1175,16 @@
 ();%0A
+%09%09%7D%0A
 %09%7D%0A
-%09
 %0A
+
 %09@Ov
@@ -1713,19 +1713,17 @@
 Alive);%0A
-%09%09
 %0A
+
 %09%09List%3CI
@@ -2007,19 +2007,17 @@
 ));%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09if(!bl
@@ -2467,19 +2467,17 @@
 live%22);%0A
-%09%09
 %0A
+
 %09%09if(nbt
@@ -2624,12 +2624,9 @@
 ();%0A
-%09%09%09
 %0A
+
 %09%09%09f
@@ -2660,24 +2660,76 @@
 gth; i++) %7B%0A
+%09%09%09%09if(Block.getBlockById(block%5Bi%5D) != Blocks.air)%0A%09
 %09%09%09%09possible

Fix NPEs with the schematics that can be caused by removal of a mod (like IC2)