package net.minecraft.src.weasel;

import java.util.ArrayList;

import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.NBTTagList;
import net.minecraft.src.PC_INBT;

public class InstructionList implements PC_INBT {

	private ArrayList<IInstruction> list = new ArrayList<IInstruction>();
	private WeaselVM vm;

	public InstructionList(WeaselVM vm) {
		this.vm = vm;
	}

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound tag) {

		NBTTagList tags = new NBTTagList();

		int index = 0;
		int size = list.size();
		for(index = 0; index < size; index++){
			NBTTagCompound tag1 = list.get(index).writeToNBT(new NBTTagCompound());
			tag1.setInteger("Index", index);
			tags.appendTag(tag1);
		}
		tag.setTag("Stack", tags);
		tag.setInteger("Size", size);

		return tag;

	}

	@Override
	public PC_INBT readFromNBT(NBTTagCompound tag) {

		int size = tag.getInteger("Size");
		
		ArrayList<WeaselObject> list = new ArrayList<WeaselObject>(size);

		NBTTagList tags = tag.getTagList("Stack");

		for (int i = 0; i < tags.tagCount(); i++) {
			NBTTagCompound tag1 = (NBTTagCompound) tags.tagAt(i);
			list.set(tag1.getInteger("sIndex"), WeaselObject.loadObjectFromNBT(tag1));
		}

		for (WeaselObject obj : list) {
			stack.push(obj);
		}

		return this;

	}

}
