-
Notifications
You must be signed in to change notification settings - Fork 497
/
TilePackager.java
266 lines (227 loc) · 6.63 KB
/
TilePackager.java
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package buildcraft.silicon;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.BuildCraftCore;
import buildcraft.BuildCraftSilicon;
import buildcraft.core.lib.block.TileBuildCraft;
import buildcraft.core.lib.inventory.InventoryConcatenator;
import buildcraft.core.lib.inventory.SimpleInventory;
import buildcraft.core.lib.inventory.StackHelper;
import buildcraft.core.lib.utils.NBTUtils;
import buildcraft.core.lib.utils.Utils;
public class TilePackager extends TileBuildCraft implements ISidedInventory {
private static final int[] SLOTS = Utils.createSlotArray(0, 12);
public SimpleInventory inventoryPublic = new SimpleInventory(12, "Packager", 64);
public SimpleInventory inventoryPattern = new SimpleInventory(9, "Packager", 64);
public IInventory visibleInventory = InventoryConcatenator.make().add(inventoryPublic).add(inventoryPattern);
private int patternsSet;
private int updateTime = BuildCraftCore.random.nextInt(5);
public boolean isPatternSlotSet(int p) {
return (patternsSet & (1 << p)) != 0;
}
public void setPatternSlot(int p, boolean v) {
if (v) {
patternsSet |= 1 << p;
} else {
patternsSet &= ~(1 << p);
}
}
@Override
public void updateEntity() {
if (worldObj.isRemote) {
return;
}
if ((updateTime++) % 5 == 0) {
attemptCrafting(inventoryPublic.getStackInSlot(9));
}
}
private boolean attemptCrafting(ItemStack input) {
// STEP 0: Make sure the conditions are correct.
if (inventoryPublic.getStackInSlot(11) != null) {
return false;
}
if (input == null || input.stackSize == 0 || !(input.getItem() == Items.paper || input.getItem() instanceof ItemPackage)) {
return false;
}
if (input.getItem() instanceof ItemPackage) {
NBTTagCompound inputTag = NBTUtils.getItemData(input);
for (int i = 0; i < 9; i++) {
if (isPatternSlotSet(i) && inputTag.hasKey("item" + i)) {
return false;
}
}
}
// STEP 1: Find how many "missing patterns" we have,
// and find the first item matching this.
// Also, match all the non-missing patterns.
int missingCount = 0;
int[] bindings = new int[9];
int[] usedItems = new int[9];
for (int i = 0; i < 9; i++) {
if (isPatternSlotSet(i)) {
ItemStack inputStack = inventoryPattern.getStackInSlot(i);
if (inputStack == null) {
missingCount++;
} else {
boolean found = false;
for (int j = 0; j < 9; j++) {
ItemStack comparedStack = inventoryPublic.getStackInSlot(j);
if (comparedStack != null && usedItems[j] < comparedStack.stackSize
&& StackHelper.isMatchingItem(inputStack, comparedStack, true, false)) {
usedItems[j]++;
bindings[i] = j;
found = true;
break;
}
}
if (!found) {
return false;
}
}
}
}
// STEP 2: If we have any missings, find the first stack
// which is NOT used elsewhere AND has enough items.
if (missingCount > 0) {
int missingPos = -1;
for (int i = 0; i < 9; i++) {
if (usedItems[i] == 0) {
ItemStack comparedStack = inventoryPublic.getStackInSlot(i);
if (comparedStack != null && comparedStack.stackSize >= missingCount) {
missingPos = i;
break;
}
}
}
if (missingPos < 0) {
return false;
} else {
for (int i = 0; i < 9; i++) {
if (isPatternSlotSet(i)) {
ItemStack inputStack = inventoryPattern.getStackInSlot(i);
if (inputStack == null) {
bindings[i] = missingPos;
}
}
}
}
}
// STEP 3: Craft and output.
ItemStack pkg;
if (input.getItem() instanceof ItemPackage) {
pkg = input.copy();
} else {
pkg = new ItemStack(BuildCraftSilicon.packageItem);
}
NBTTagCompound pkgTag = NBTUtils.getItemData(pkg);
for (int i = 0; i < 9; i++) {
if (isPatternSlotSet(i)) {
ItemStack usedStack = inventoryPublic.getStackInSlot(bindings[i]);
ItemStack output = usedStack.splitStack(1);
NBTTagCompound itemTag = new NBTTagCompound();
output.writeToNBT(itemTag);
pkgTag.setTag("item" + i, itemTag);
}
}
ItemPackage.update(pkg);
decrStackSize(9, 1);
setInventorySlotContents(11, pkg);
return true;
}
@Override
public void readData(ByteBuf stream) {
super.readData(stream);
patternsSet = stream.readUnsignedShort();
}
@Override
public void writeData(ByteBuf stream) {
super.writeData(stream);
stream.writeShort(patternsSet);
}
@Override
public void readFromNBT(NBTTagCompound cpd) {
super.readFromNBT(cpd);
inventoryPublic.readFromNBT(cpd, "items");
inventoryPattern.readFromNBT(cpd, "pattern");
patternsSet = cpd.getShort("patternSet");
}
@Override
public void writeToNBT(NBTTagCompound cpd) {
super.writeToNBT(cpd);
inventoryPublic.writeToNBT(cpd, "items");
inventoryPattern.writeToNBT(cpd, "pattern");
cpd.setShort("patternSet", (short) patternsSet);
}
@Override
public int getSizeInventory() {
return visibleInventory.getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int slot) {
return visibleInventory.getStackInSlot(slot);
}
@Override
public ItemStack decrStackSize(int slot, int amount) {
return visibleInventory.decrStackSize(slot, amount);
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
return visibleInventory.getStackInSlotOnClosing(slot);
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
visibleInventory.setInventorySlotContents(slot, stack);
}
@Override
public String getInventoryName() {
return "Packager";
}
@Override
public boolean hasCustomInventoryName() {
return false;
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
return visibleInventory.isUseableByPlayer(player);
}
@Override
public void openInventory() {
visibleInventory.openInventory();
}
@Override
public void closeInventory() {
visibleInventory.closeInventory();
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
if (slot == 9) {
return stack == null || stack.getItem() == Items.paper || stack.getItem() instanceof ItemPackage;
}
return visibleInventory.isItemValidForSlot(slot, stack);
}
@Override
public int[] getAccessibleSlotsFromSide(int side) {
return SLOTS;
}
@Override
public boolean canInsertItem(int slot, ItemStack stack, int side) {
if (side >= 2) {
return slot < 9;
} else {
return slot == 9;
}
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, int side) {
return slot == 11;
}
}