-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileAutoWorkbench.java
442 lines (371 loc) · 13.6 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/** Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team http://www.mod-buildcraft.com
* <p/>
* 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 java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.*;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.WorldServer;
import buildcraft.api.core.IInvSlot;
import buildcraft.api.power.IRedstoneEngine;
import buildcraft.api.power.IRedstoneEngineReceiver;
import buildcraft.api.tiles.IDebuggable;
import buildcraft.api.tiles.IHasWork;
import buildcraft.core.lib.RFBattery;
import buildcraft.core.lib.block.TileBuildCraft;
import buildcraft.core.lib.gui.ContainerDummy;
import buildcraft.core.lib.inventory.*;
import buildcraft.core.lib.utils.CraftingUtils;
import buildcraft.core.lib.utils.Utils;
import buildcraft.core.proxy.CoreProxy;
public class TileAutoWorkbench extends TileBuildCraft implements ISidedInventory, IHasWork, IRedstoneEngineReceiver, IDebuggable {
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[] bindingCounts = 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));
inputInv.addInvListener((slot, before, after) -> {
if (craftMatrix.isJammed) {
scheduledCacheRebuild = true;
}
});
}
@Override
public boolean hasWork() {
return hasWork;
}
@Override
public boolean canConnectRedstoneEngine(EnumFacing side) {
return true;
}
@Override
public boolean canConnectEnergy(EnumFacing side) {
TileEntity tile = worldObj.getTileEntity(pos.offset(side));
return tile instanceof IRedstoneEngine;
}
public class LocalInventoryCrafting extends InventoryCrafting {
public IRecipe currentRecipe;
public boolean useBindings, isJammed;
public LocalInventoryCrafting() {
super(new ContainerDummy(), 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;
}
} else {
return super.getStackInSlot(slot);
}
}
public ItemStack getRecipeOutput() {
currentRecipe = findRecipe(); // Fixes repair recipe handling (why is it not dynamic?)
if (currentRecipe == null) {
return null;
}
ItemStack result = currentRecipe.getCraftingResult(this);
if (result != null) {
result = result.copy();
}
return result;
}
private IRecipe findRecipe() {
for (IInvSlot slot : InventoryIterator.getIterable(this, EnumFacing.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();
if (currentRecipe == null) {
hasWork = false;
return;
}
ItemStack result = currentRecipe.getCraftingResult(craftMatrix);
hasWork = result != null;
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, pos.up());
}
@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 removeStackFromSlot(int slot) {
return inv.removeStackFromSlot(slot);
}
@Override
public String getInventoryName() {
return "";
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
return worldObj.getTileEntity(pos) == this && player.getDistanceSq(pos) <= 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();
}
@Override
public void writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
resultInv.writeToNBT(data);
InvUtils.writeInvToNBT(inputInv, "input", data);
InvUtils.writeInvToNBT(craftMatrix, "matrix", data);
}
@Override
public void update() {
super.update();
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++) {
bindingCounts[i] = 0;
}
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)) {
boolean found = false;
for (int j = 0; j < 9; j++) {
if (j == bindings[i]) {
continue;
}
ItemStack inputInvStack = inputInv.getStackInSlot(j);
if (StackHelper.isMatchingItem(inputInvStack, comparedStack, true, false) && inputInvStack.stackSize > bindingCounts[j]) {
found = true;
bindings[i] = j;
bindingCounts[j]++;
break;
}
}
if (!found) {
craftMatrix.isJammed = true;
progress = 0;
return;
}
} else {
bindingCounts[bindings[i]]++;
}
}
for (int i = 0; i < 9; i++) {
if (bindingCounts[i] > 0) {
ItemStack stack = inputInv.getStackInSlot(i);
if (stack != null && stack.stackSize < bindingCounts[i]) {
// Do not break progress yet, instead give it a chance to rebuild
// It will quit when trying to find a valid binding to "fit in"
for (int j = 0; j < 9; j++) {
if (bindings[j] == i) {
bindings[j] = -1;
}
}
return;
}
}
}
if (progress < CRAFT_TIME) {
return;
}
progress = 0;
craftMatrix.setUseBindings(true);
ItemStack result = craftMatrix.getRecipeOutput();
if (result != null && result.stackSize > 0) {
ItemStack resultInto = resultInv.getStackInSlot(0);
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(EntityPlayer player) {}
@Override
public void closeInventory(EntityPlayer player) {}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
if (slot == SLOT_RESULT) {
return false;
}
if (stack.getItem().hasContainerItem(stack)) {
return false;
}
return true;
}
@Override
public int[] getSlotsForFace(EnumFacing face) {
return SLOTS;
}
@Override
public boolean canInsertItem(int slot, ItemStack stack, EnumFacing side) {
if (slot >= 9) {
return false;
}
ItemStack slotStack = inv.getStackInSlot(slot);
if (StackHelper.canStacksMerge(stack, slotStack)) {
return true;
}
for (int i = 0; i < 9; i++) {
ItemStack inputStack = craftMatrix.getStackInSlot(i);
if (inputStack != null && StackHelper.isMatchingItem(inputStack, stack, true, false)) {
return true;
}
}
return false;
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side) {
return slot == SLOT_RESULT;
}
@Override
public boolean hasCustomName() {
return false;
}
@Override
public void getDebugInfo(List<String> left, List<String> right, EnumFacing side) {
TileAutoWorkbench server = CoreProxy.proxy.getServerTile(this);
left.add("");
left.add("isJammed = " + server.craftMatrix.isJammed);
left.add("isRemote = " + server.worldObj.isRemote);
}
}