|
| 1 | +package li.cil.oc.api.prefab; |
| 2 | + |
| 3 | +import li.cil.oc.api.machine.Arguments; |
| 4 | +import li.cil.oc.api.machine.Callback; |
| 5 | +import li.cil.oc.api.machine.Context; |
| 6 | +import net.minecraft.item.ItemStack; |
| 7 | +import net.minecraft.nbt.NBTBase; |
| 8 | +import net.minecraft.nbt.NBTTagCompound; |
| 9 | +import net.minecraft.nbt.NBTTagList; |
| 10 | + |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.TreeMap; |
| 13 | + |
| 14 | +public class ItemStackArrayValue extends AbstractValue { |
| 15 | + |
| 16 | + private ItemStack[] array = null; |
| 17 | + private int iteratorIndex; |
| 18 | + |
| 19 | + private static final byte TAGLIST_ID = (new NBTTagList()).getId(); |
| 20 | + private static final byte COMPOUND_ID = (new NBTTagCompound()).getId(); |
| 21 | + private static final String ARRAY_KEY = "Array"; |
| 22 | + private static final String INDEX_KEY = "Index"; |
| 23 | + |
| 24 | + private static final HashMap<Object,Object> emptyMap = new HashMap<Object,Object>(); |
| 25 | + |
| 26 | + public ItemStackArrayValue(ItemStack[] arr){ |
| 27 | + if (arr != null){ |
| 28 | + this.array = new ItemStack[arr.length]; |
| 29 | + for (int i=0; i< arr.length; i++){ |
| 30 | + this.array[i] = arr[i] != null ? arr[i].copy() : null; |
| 31 | + } |
| 32 | + } |
| 33 | + this.iteratorIndex = 0; |
| 34 | + } |
| 35 | + |
| 36 | + public ItemStackArrayValue(){ |
| 37 | + this(null); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public Object[] call(Context context, Arguments arguments) { |
| 42 | + if (this.array == null) |
| 43 | + return null; |
| 44 | + if (this.iteratorIndex >= this.array.length) |
| 45 | + return null; |
| 46 | + int index = this.iteratorIndex++; |
| 47 | + if (this.array[index] == null)//TODO 1.11 change to ItemStack.EMPTY? |
| 48 | + return new Object[]{ emptyMap }; |
| 49 | + return new Object[]{ this.array[index] != null ? this.array[index] : emptyMap }; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Object apply(Context context, Arguments arguments) { |
| 54 | + if (arguments.count() == 0 || this.array == null) |
| 55 | + return null; |
| 56 | + if (arguments.isInteger(0)){//index access |
| 57 | + int luaIndex = arguments.checkInteger(0); |
| 58 | + if (luaIndex > this.array.length || luaIndex < 1){ |
| 59 | + return null; |
| 60 | + } |
| 61 | + return this.array[luaIndex-1]; |
| 62 | + } |
| 63 | + if (arguments.isString(0)){ |
| 64 | + String arg = arguments.checkString(0); |
| 65 | + if (arg.equals("n")){ |
| 66 | + return this.array.length; |
| 67 | + } |
| 68 | + } |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void load(NBTTagCompound nbt) { |
| 74 | + if (nbt.hasKey(ARRAY_KEY, TAGLIST_ID)){ |
| 75 | + NBTTagList tagList = nbt.getTagList(ARRAY_KEY,COMPOUND_ID); |
| 76 | + this.array = new ItemStack[tagList.tagCount()]; |
| 77 | + for (int i = 0; i < tagList.tagCount(); ++i){ |
| 78 | + NBTTagCompound el = tagList.getCompoundTagAt(i); |
| 79 | + if (el.hasNoTags()) |
| 80 | + this.array[i] = null;//TODO 1.11 change to ItemStack.EMPTY? |
| 81 | + else |
| 82 | + this.array[i] = ItemStack.loadItemStackFromNBT(el); |
| 83 | + } |
| 84 | + } else { |
| 85 | + this.array = null; |
| 86 | + } |
| 87 | + this.iteratorIndex = nbt.getInteger(INDEX_KEY); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void save(NBTTagCompound nbt) { |
| 92 | + |
| 93 | + NBTTagCompound nullnbt = new NBTTagCompound(); |
| 94 | + |
| 95 | + if (this.array != null) { |
| 96 | + NBTTagList nbttaglist = new NBTTagList(); |
| 97 | + for (ItemStack stack : this.array) { |
| 98 | + if (stack != null) { |
| 99 | + NBTTagCompound nbttagcompound = new NBTTagCompound(); |
| 100 | + stack.writeToNBT(nbttagcompound); |
| 101 | + nbttaglist.appendTag(nbttagcompound); |
| 102 | + } else { |
| 103 | + nbttaglist.appendTag(nullnbt); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + nbt.setTag(ARRAY_KEY, nbttaglist); |
| 108 | + } |
| 109 | + |
| 110 | + nbt.setInteger(INDEX_KEY, iteratorIndex); |
| 111 | + } |
| 112 | + |
| 113 | + @Callback(doc="function():nil -- Reset the iterator index so that the next call will return the first element.") |
| 114 | + public Object[] reset(Context context, Arguments arguments) throws Exception { |
| 115 | + this.iteratorIndex = 0; |
| 116 | + return null; |
| 117 | + } |
| 118 | + |
| 119 | + @Callback(doc="function():number -- Returns the number of elements in the this.array.") |
| 120 | + public Object[] count(Context context, Arguments arguments) throws Exception { |
| 121 | + return new Object[] { this.array != null ? this.array.length : 0 }; |
| 122 | + } |
| 123 | + |
| 124 | + @Callback(doc="function():table -- Returns ALL the stack in the this.array. Memory intensive.") |
| 125 | + public Object[] getAll(Context context, Arguments arguments) throws Exception { |
| 126 | + TreeMap<Integer,Object> map = new TreeMap<Integer,Object>(); |
| 127 | + for (int i=0; i<this.array.length; i++){ |
| 128 | + map.put(i, this.array[i] != null ? this.array[i] : emptyMap); |
| 129 | + } |
| 130 | + return new Object[] { map }; |
| 131 | + } |
| 132 | + |
| 133 | + public String toString(){ |
| 134 | + return "{ItemStack Array}"; |
| 135 | + } |
| 136 | +} |
| 137 | + |
0 commit comments