summaryrefslogtreecommitdiff
path: root/ihl/processing/invslots/IHLInvSlotOutput.java
blob: d683221c0ba365a12f37d61ff160338bb542dcd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package ihl.processing.invslots;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;

import ic2.core.block.TileEntityInventory;
import ic2.core.block.invslot.InvSlotOutput;
import ihl.recipes.RecipeOutputItemStack;
import ihl.utils.IHLUtils;

public class IHLInvSlotOutput extends InvSlotOutput {

	private final Map<Long, Float> substanceAmount = new HashMap<Long, Float>();

	public IHLInvSlotOutput(TileEntityInventory base1, String name1, int oldStartIndex1, int count) {
		super(base1, name1, oldStartIndex1, count);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public boolean canAdd(List itemOutputs) {
		if (itemOutputs == null || itemOutputs.isEmpty()) {
			return true;
		}
		Iterator ioi = itemOutputs.iterator();
		if (this.size() >= itemOutputs.size()) {
			Object rois;
			if (ioi.hasNext()) {
				rois = ioi.next();
			} else {
				return true;
			}
			for (int i = 0; i < this.size(); i++) {
				if (this.get(i) == null || (this.objectMatchesSlot(rois, i)
						&& this.get(i).stackSize + this.getAmoutOfObject(rois) < this.getStackSizeLimit()
						&& this.get(i).stackSize + this.getAmoutOfObject(rois) <= this.get(i).getMaxStackSize())) {
					if (ioi.hasNext()) {
						rois = ioi.next();
					} else {
						return true;
					}
				} else {
					if (i == this.size() - 1) {
						return false;
					}
				}
			}

		}
		return false;
	}

	private float getAmoutOfObject(Object obj) {
		if (obj instanceof ItemStack) {
			return ((ItemStack) obj).stackSize;
		} else if (obj instanceof RecipeOutputItemStack) {
			return ((RecipeOutputItemStack) obj).quantity;
		}
		return Short.MAX_VALUE;
	}

	public boolean objectMatchesSlot(Object obj, int slot) {
		if (this.get(slot) == null) {
			return true;
		} else {
			if (obj instanceof ItemStack) {
				return IHLUtils.isItemStacksIsEqual(this.get(slot), (ItemStack) obj, true);
			} else if (obj instanceof RecipeOutputItemStack) {
				return ((RecipeOutputItemStack) obj).matches(this.get(slot));
			}
		}
		return false;
	}

	public void add(RecipeOutputItemStack rois) {
		for (int i = 0; i < this.size(); i++) {
			if (this.get(i) == null || (this.objectMatchesSlot(rois, i)
					&& this.get(i).stackSize + this.getAmoutOfObject(rois) < this.getStackSizeLimit())) {
				this.add(i, rois);
				break;
			}
		}
	}

	private void add(int i, RecipeOutputItemStack rois) {
		long key = (Item.getIdFromItem(rois.itemStack.getItem()) << 32) + rois.itemStack.getItemDamage();
		float amount = 0f;
		if (this.substanceAmount.containsKey(key)) {
			amount = this.substanceAmount.get(key);
		}
		amount += rois.quantity;
		while (amount >= 1) {
			amount--;
			this.add(rois.itemStack.copy());
		}
		this.substanceAmount.put(key, amount);
	}

	@Override
	@SuppressWarnings("rawtypes")
	public int add(List itemOutputs) {
		if (itemOutputs == null || itemOutputs.isEmpty()) {
			return 0;
		}
		Iterator ioi = itemOutputs.iterator();
		if (this.size() >= itemOutputs.size() && ioi.hasNext()) {
			Object rois = ioi.next();
			for (int i = 0; i < this.size(); i++) {
				if (this.get(i) == null || (this.objectMatchesSlot(rois, i)
						&& this.get(i).stackSize + this.getAmoutOfObject(rois) < this.getStackSizeLimit())) {
					if (rois instanceof ItemStack) {
						this.add(((ItemStack) rois).copy());
					} else if (rois instanceof RecipeOutputItemStack) {
						this.add(i, (RecipeOutputItemStack) rois);
					}
					if (ioi.hasNext()) {
						rois = ioi.next();
					} else {
						return itemOutputs.size();
					}
				} else {
					if (i == this.size() - 1) {
						return 0;
					}
				}
			}

		}
		return 0;
	}

	@Override
	public void readFromNbt(NBTTagCompound nbtTagCompound) {
		super.readFromNbt(nbtTagCompound);
		NBTTagList amountTagList = nbtTagCompound.getTagList("substanceAmountMap", 10);
		for (int i = 0; i < amountTagList.tagCount(); i++) {
			if (amountTagList.getCompoundTagAt(i).hasKey("substanceKey")) {
				long substanceKey = amountTagList.getCompoundTagAt(i).getLong("substanceKey");
				float substanceAmount = amountTagList.getCompoundTagAt(i).getFloat("substanceAmount");
				this.substanceAmount.put(substanceKey, substanceAmount);
			}
		}
	}

	@Override
	public void writeToNbt(NBTTagCompound nbtTagCompound) {
		super.writeToNbt(nbtTagCompound);
		NBTTagList sAmountsList = new NBTTagList();
		Iterator<Entry<Long, Float>> entrySetIterator = this.substanceAmount.entrySet().iterator();
		while (entrySetIterator.hasNext()) {
			Entry<Long, Float> entry = entrySetIterator.next();
			NBTTagCompound tag = new NBTTagCompound();
			tag.setLong("substanceKey", entry.getKey());
			tag.setFloat("substanceAmount", entry.getValue());
			sAmountsList.appendTag(tag);
		}
		nbtTagCompound.setTag("substanceAmountMap", sAmountsList);
	}
}