-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileAutoWorkbench.java
424 lines (354 loc) · 10.5 KB
/
TileAutoWorkbench.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.factory;
import java.lang.ref.WeakReference;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.inventory.InventoryCraftResult;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.SlotCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.core.IInvSlot;
import buildcraft.api.power.IRedstoneEngine;
import buildcraft.api.power.IRedstoneEngineReceiver;
import buildcraft.api.tiles.IHasWork;
import buildcraft.core.lib.RFBattery;
import buildcraft.core.lib.block.TileBuildCraft;
import buildcraft.core.lib.inventory.InvUtils;
import buildcraft.core.lib.inventory.InventoryConcatenator;
import buildcraft.core.lib.inventory.InventoryIterator;
import buildcraft.core.lib.inventory.SimpleInventory;
import buildcraft.core.lib.inventory.StackHelper;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.lib.utils.CraftingUtils;
import buildcraft.core.lib.utils.Utils;
public class TileAutoWorkbench extends TileBuildCraft implements ISidedInventory, IHasWork, IRedstoneEngineReceiver {
public static final int SLOT_RESULT = 9;
public static final int CRAFT_TIME = 256;
public static final int UPDATE_TIME = 16;
private static final int[] SLOTS = Utils.createSlotArray(0, 10);
public int progress = 0;
public LocalInventoryCrafting craftMatrix = new LocalInventoryCrafting();
private SimpleInventory resultInv = new SimpleInventory(1, "Auto Workbench", 64);
private SimpleInventory inputInv = new SimpleInventory(9, "Auto Workbench", 64);
private IInventory inv = InventoryConcatenator.make().add(inputInv).add(resultInv).add(craftMatrix);
private SlotCrafting craftSlot;
private InventoryCraftResult craftResult = new InventoryCraftResult();
private int[] bindings = new int[9];
private int update = Utils.RANDOM.nextInt();
private boolean hasWork = false;
private boolean scheduledCacheRebuild = false;
public TileAutoWorkbench() {
super();
this.setBattery(new RFBattery(16, 16, 0));
}
@Override
public boolean hasWork() {
return hasWork;
}
@Override
public boolean canConnectRedstoneEngine(ForgeDirection side) {
return true;
}
@Override
public boolean canConnectEnergy(ForgeDirection side) {
TileEntity tile = worldObj.getTileEntity(xCoord + side.offsetX, yCoord + side.offsetY, zCoord + side.offsetZ);
return (tile instanceof IRedstoneEngine);
}
public class LocalInventoryCrafting extends InventoryCrafting {
public IRecipe currentRecipe;
public boolean useBindings, isJammed;
public LocalInventoryCrafting() {super(new Container() {
@Override
public boolean canInteractWith(EntityPlayer entityplayer) {
return false;
}
}, 3, 3);
}
@Override
public ItemStack getStackInSlot(int slot) {
if (useBindings) {
if (slot >= 0 && slot < 9 && bindings[slot] >= 0) {
return inputInv.getStackInSlot(bindings[slot]);
} else {
return null;
}
}
return super.getStackInSlot(slot);
}
public ItemStack getRecipeOutput() {
if (currentRecipe == null) {
return null;
}
ItemStack result = currentRecipe.getCraftingResult(craftMatrix);
if (result != null) {
result = result.copy();
}
return result;
}
private IRecipe findRecipe() {
for (IInvSlot slot : InventoryIterator.getIterable(this, ForgeDirection.UP)) {
ItemStack stack = slot.getStackInSlot();
if (stack == null) {
continue;
}
if (stack.getItem().hasContainerItem(stack)) {
return null;
}
}
return CraftingUtils.findMatchingRecipe(craftMatrix, worldObj);
}
public void rebuildCache() {
currentRecipe = findRecipe();
hasWork = currentRecipe != null && currentRecipe.getRecipeOutput() != null;
ItemStack result = getRecipeOutput();
ItemStack resultInto = resultInv.getStackInSlot(0);
if (resultInto != null && (
!StackHelper.canStacksMerge(resultInto, result)
|| resultInto.stackSize + result.stackSize > resultInto.getMaxStackSize())
) {
isJammed = true;
} else {
isJammed = false;
}
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
if (useBindings) {
if (slot >= 0 && slot < 9 && bindings[slot] >= 0) {
inputInv.setInventorySlotContents(bindings[slot], stack);
}
return;
}
super.setInventorySlotContents(slot, stack);
scheduledCacheRebuild = true;
}
@Override
public void markDirty() {
super.markDirty();
scheduledCacheRebuild = true;
}
@Override
public ItemStack decrStackSize(int slot, int amount) {
if (useBindings) {
if (slot >= 0 && slot < 9 && bindings[slot] >= 0) {
return inputInv.decrStackSize(bindings[slot], amount);
} else {
return null;
}
}
scheduledCacheRebuild = true;
return decrStackSize(slot, amount);
}
public void setUseBindings(boolean use) {
useBindings = use;
}
}
public WeakReference<EntityPlayer> getInternalPlayer() {
return CoreProxy.proxy.getBuildCraftPlayer((WorldServer) worldObj, xCoord, yCoord + 1, zCoord);
}
@Override
public void markDirty() {
super.markDirty();
inv.markDirty();
}
@Override
public int getSizeInventory() {
return 10;
}
@Override
public ItemStack getStackInSlot(int slot) {
return inv.getStackInSlot(slot);
}
@Override
public ItemStack decrStackSize(int slot, int count) {
return inv.decrStackSize(slot, count);
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
inv.setInventorySlotContents(slot, stack);
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
return inv.getStackInSlotOnClosing(slot);
}
@Override
public String getInventoryName() {
return "";
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this && player.getDistanceSq(xCoord + 0.5D, yCoord + 0.5D, zCoord + 0.5D) <= 64.0D;
}
@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
resultInv.readFromNBT(data);
if (data.hasKey("input")) {
InvUtils.readInvFromNBT(inputInv, "input", data);
InvUtils.readInvFromNBT(craftMatrix, "matrix", data);
} else {
InvUtils.readInvFromNBT(inputInv, "matrix", data);
for (int i = 0; i < 9; i++) {
ItemStack inputStack = inputInv.getStackInSlot(i);
if (inputStack != null) {
ItemStack matrixStack = inputStack.copy();
matrixStack.stackSize = 1;
craftMatrix.setInventorySlotContents(i, matrixStack);
}
}
}
craftMatrix.rebuildCache();
// Legacy Code
if (data.hasKey("stackList")) {
ItemStack[] stacks = new ItemStack[9];
InvUtils.readStacksFromNBT(data, "stackList", stacks);
for (int i = 0; i < 9; i++) {
craftMatrix.setInventorySlotContents(i, stacks[i]);
}
}
}
@Override
public void writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
resultInv.writeToNBT(data);
InvUtils.writeInvToNBT(inputInv, "input", data);
InvUtils.writeInvToNBT(craftMatrix, "matrix", data);
}
@Override
public boolean canUpdate() {
return true;
}
@Override
public void updateEntity() {
super.updateEntity();
if (worldObj.isRemote) {
return;
}
if (scheduledCacheRebuild) {
craftMatrix.rebuildCache();
scheduledCacheRebuild = false;
}
if (craftMatrix.isJammed || craftMatrix.currentRecipe == null) {
progress = 0;
return;
}
if (craftSlot == null) {
craftSlot = new SlotCrafting(getInternalPlayer().get(), craftMatrix, craftResult, 0, 0, 0);
}
if (!hasWork) {
return;
}
int updateNext = update + getBattery().getEnergyStored() + 1;
int updateThreshold = (update & ~15) + 16;
update = Math.min(updateThreshold, updateNext);
if ((update % UPDATE_TIME) == 0) {
updateCrafting();
}
getBattery().setEnergy(0);
}
public int getProgressScaled(int i) {
return (progress * i) / CRAFT_TIME;
}
/**
* Increment craft job, find recipes, produce output
*/
private void updateCrafting() {
progress += UPDATE_TIME;
for (int i = 0; i < 9; i++) {
ItemStack comparedStack = craftMatrix.getStackInSlot(i);
if (comparedStack == null || comparedStack.getItem() == null) {
bindings[i] = -1;
continue;
}
if (bindings[i] == -1 || !StackHelper.isMatchingItem(inputInv.getStackInSlot(bindings[i]), comparedStack, true, true)) {
// Scan for a new binding
boolean found = false;
for (int j = 0; j < 9; j++) {
if (j == bindings[i]) {
continue;
}
if (StackHelper.isMatchingItem(inputInv.getStackInSlot(j), comparedStack, true, false)) {
found = true;
bindings[i] = j;
break;
}
}
if (!found) {
progress = 0;
return;
}
}
}
if (progress < CRAFT_TIME) {
return;
}
progress = 0;
ItemStack result = craftMatrix.getRecipeOutput();
ItemStack resultInto = resultInv.getStackInSlot(0);
craftMatrix.setUseBindings(true);
craftSlot.onPickupFromSlot(getInternalPlayer().get(), result);
if (resultInto == null) {
resultInv.setInventorySlotContents(0, result);
} else {
resultInto.stackSize += result.stackSize;
}
craftMatrix.setUseBindings(false);
craftMatrix.rebuildCache();
}
@Override
public void openInventory() {
}
@Override
public void closeInventory() {
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
if (slot == SLOT_RESULT) {
return false;
}
if (stack == null) {
return false;
}
if (stack.getItem().hasContainerItem(stack)) {
return false;
}
if (getStackInSlot(slot) == null) {
return false;
}
return true;
}
@Override
public int[] getAccessibleSlotsFromSide(int var1) {
return SLOTS;
}
@Override
public boolean canInsertItem(int slot, ItemStack stack, int side) {
return slot < 9;
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, int side) {
return slot == SLOT_RESULT;
}
@Override
public boolean hasCustomInventoryName() {
return false;
}
}