-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileAdvancedCraftingTable.java
520 lines (456 loc) · 15 KB
/
TileAdvancedCraftingTable.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package buildcraft.silicon;
import buildcraft.BuildCraftCore;
import buildcraft.api.gates.IAction;
import buildcraft.api.gates.IActionReceptor;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.IInventory;
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.util.ChunkCoordinates;
import buildcraft.core.IMachine;
import buildcraft.core.TileBuffer;
import buildcraft.core.inventory.InvUtils;
import buildcraft.core.inventory.InventoryCopy;
import buildcraft.core.inventory.InventoryIterator;
import buildcraft.core.inventory.InventoryIterator.IInvSlot;
import buildcraft.core.inventory.InventoryMapper;
import buildcraft.core.inventory.Transactor;
import buildcraft.core.network.PacketIds;
import buildcraft.core.network.PacketSlotChange;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.CraftingHelper;
import buildcraft.core.inventory.SimpleInventory;
import buildcraft.core.inventory.StackHelper;
import buildcraft.core.inventory.filters.CraftingFilter;
import buildcraft.core.inventory.filters.IStackFilter;
import buildcraft.core.triggers.ActionMachineControl;
import buildcraft.core.utils.Utils;
import com.google.common.collect.Lists;
import java.util.EnumSet;
import net.minecraft.inventory.ISidedInventory;
import net.minecraftforge.common.ForgeDirection;
import static net.minecraftforge.common.ForgeDirection.*;
public class TileAdvancedCraftingTable extends TileEntity implements IInventory, ILaserTarget, IMachine, IActionReceptor, ISidedInventory {
private final class InternalInventoryCraftingContainer extends Container {
@Override
public boolean canInteractWith(EntityPlayer var1) {
return false;
}
}
private final class InternalInventoryCrafting extends InventoryCrafting {
int[] bindings = new int[9];
ItemStack[] tempStacks;
public int[] hitCount;
private boolean useRecipeStack;
private InternalInventoryCrafting() {
super(new InternalInventoryCraftingContainer(), 3, 3);
}
@Override
public ItemStack getStackInSlot(int slot) {
if (slot >= 0 && slot < 9) {
if (useRecipeStack || tempStacks == null) {
return craftingSlots.getStackInSlot(slot);
} else {
if (bindings[slot] >= 0) {
return tempStacks[bindings[slot]];
}
}
}
// vanilla returns null for out of bound stacks in InventoryCrafting as well
return null;
}
@Override
public void setInventorySlotContents(int slot, ItemStack par2ItemStack) {
if (tempStacks != null) {
tempStacks[bindings[slot]] = par2ItemStack;
}
}
@Override
public ItemStack decrStackSize(int slot, int amount) {
if (tempStacks != null) {
if (tempStacks[bindings[slot]].stackSize <= amount) {
ItemStack result = tempStacks[bindings[slot]];
tempStacks[bindings[slot]] = null;
return result;
} else {
ItemStack result = tempStacks[bindings[slot]].splitStack(amount);
if (tempStacks[bindings[slot]].stackSize <= 0) {
tempStacks[bindings[slot]] = null;
}
return result;
}
} else {
return null;
}
}
public void recipeUpdate(boolean flag) {
useRecipeStack = flag;
}
}
private final class InternalPlayer extends EntityPlayer {
public InternalPlayer() {
super(TileAdvancedCraftingTable.this.worldObj);
posX = TileAdvancedCraftingTable.this.xCoord;
posY = TileAdvancedCraftingTable.this.yCoord + 1;
posZ = TileAdvancedCraftingTable.this.zCoord;
username = "[Buildcraft]";
}
@Override
public void sendChatToPlayer(String var1) {
}
@Override
public boolean canCommandSenderUseCommand(int var1, String var2) {
return false;
}
@Override
public ChunkCoordinates getPlayerCoordinates() {
return null;
}
}
public InventoryCraftResult craftResult;
private InternalInventoryCrafting internalInventoryCrafting;
public TileAdvancedCraftingTable() {
craftingSlots = new SimpleInventory(9, "CraftingSlots", 1);
storageSlots = new SimpleInventory(24, "StorageSlots", 64);
storageSlots.addListener(this);
invInput = new InventoryMapper(storageSlots, 0, 15);
invOutput = new InventoryMapper(storageSlots, 15, 9);
craftResult = new InventoryCraftResult();
}
private static final int[] SLOTS = Utils.createSlotArray(0, 24);
private static final EnumSet<ForgeDirection> SEARCH_SIDES = EnumSet.of(DOWN, NORTH, SOUTH, EAST, WEST);
private static final float REQUIRED_POWER = 500F;
private final SimpleInventory craftingSlots;
private final SimpleInventory storageSlots;
private final InventoryMapper invInput;
private final InventoryMapper invOutput;
private SlotCrafting craftSlot;
private float storedEnergy;
private float[] recentEnergy = new float[20];
private boolean craftable;
private boolean justCrafted;
private int tick;
private int recentEnergyAverage;
private InternalPlayer internalPlayer;
private IRecipe currentRecipe;
private ActionMachineControl.Mode lastMode = ActionMachineControl.Mode.Unknown;
private TileBuffer[] cache;
@Override
public int getSizeInventory() {
return storageSlots.getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int slot) {
return storageSlots.getStackInSlot(slot);
}
@Override
public ItemStack decrStackSize(int slot, int amount) {
return storageSlots.decrStackSize(slot, amount);
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
return storageSlots.getStackInSlotOnClosing(slot);
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
storageSlots.setInventorySlotContents(slot, stack);
}
@Override
public void writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
storageSlots.writeToNBT(data, "StorageSlots");
craftingSlots.writeToNBT(data);
data.setFloat("StoredEnergy", storedEnergy);
}
@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
storageSlots.readFromNBT(data, "StorageSlots");
craftingSlots.readFromNBT(data);
storedEnergy = data.getFloat("StoredEnergy");
}
@Override
public String getInvName() {
return "AdvancedWorkbench";
}
@Override
public void onInventoryChanged() {
super.onInventoryChanged();
craftable = craftResult.getStackInSlot(0) != null;
}
@Override
public int getInventoryStackLimit() {
return storageSlots.getInventoryStackLimit();
}
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this;
}
@Override
public void openChest() {
}
@Override
public void closeChest() {
}
public int getRecentEnergyAverage() {
return recentEnergyAverage;
}
public float getStoredEnergy() {
return storedEnergy;
}
public float getRequiredEnergy() {
return craftResult.getStackInSlot(0) != null ? REQUIRED_POWER : 0f;
}
public int getProgressScaled(int i) {
return (int) ((storedEnergy * i) / REQUIRED_POWER);
}
@Override
public void invalidate() {
super.invalidate();
cache = null;
}
@Override
public void updateEntity() {
if (internalPlayer == null) {
internalInventoryCrafting = new InternalInventoryCrafting();
internalPlayer = new InternalPlayer();
craftSlot = new SlotCrafting(internalPlayer, internalInventoryCrafting, craftResult, 0, 0, 0);
updateCraftingResults();
}
if (!CoreProxy.proxy.isSimulating(worldObj)) {
return;
}
if (lastMode == ActionMachineControl.Mode.Off) {
return;
}
updateCraftingResults();
findIngredients();
justCrafted = false;
tick++;
tick = tick % recentEnergy.length;
recentEnergy[tick] = 0.0f;
if (craftResult.getStackInSlot(0) != null) {
internalInventoryCrafting.tempStacks = new InventoryCopy(storageSlots).getItemStacks();
internalInventoryCrafting.hitCount = new int[internalInventoryCrafting.tempStacks.length];
if (hasIngredients() && InvUtils.isRoomForStack(craftResult.getStackInSlot(0), ForgeDirection.UP, invOutput)) {
if (storedEnergy >= getRequiredEnergy()) {
craftItem();
justCrafted = true;
}
} else {
craftable = false;
internalInventoryCrafting.tempStacks = null;
internalInventoryCrafting.hitCount = null;
storedEnergy = 0;
}
}
}
private boolean hasIngredients() {
ItemStack[] tempStorage = internalInventoryCrafting.tempStacks;
for (int j = 0; j < craftingSlots.getSizeInventory(); j++) {
if (craftingSlots.getStackInSlot(j) == null) {
internalInventoryCrafting.bindings[j] = -1;
continue;
}
boolean matchedStorage = false;
for (int i = 0; i < tempStorage.length; i++) {
if (tempStorage[i] != null && StackHelper.instance().isCraftingEquivalent(craftingSlots.getStackInSlot(j), tempStorage[i], true)
&& internalInventoryCrafting.hitCount[i] < tempStorage[i].stackSize
&& internalInventoryCrafting.hitCount[i] < tempStorage[i].getMaxStackSize()) {
internalInventoryCrafting.bindings[j] = i;
internalInventoryCrafting.hitCount[i]++;
matchedStorage = true;
break;
}
}
if (!matchedStorage) {
return false;
}
}
return true;
}
private void craftItem() {
craftSlot.onPickupFromSlot(internalPlayer, craftResult.getStackInSlot(0));
ItemStack[] tempStorage = internalInventoryCrafting.tempStacks;
for (int i = 0; i < tempStorage.length; i++) {
if (tempStorage[i] != null && tempStorage[i].stackSize <= 0) {
tempStorage[i] = null;
}
storageSlots.getItemStacks()[i] = tempStorage[i];
}
storedEnergy -= getRequiredEnergy();
List<ItemStack> outputs = Lists.newArrayList(craftResult.getStackInSlot(0).copy());
for (int i = 0; i < internalPlayer.inventory.mainInventory.length; i++) {
if (internalPlayer.inventory.mainInventory[i] != null) {
outputs.add(internalPlayer.inventory.mainInventory[i]);
internalPlayer.inventory.mainInventory[i] = null;
}
}
for (ItemStack output : outputs) {
output.stackSize -= Transactor.getTransactorFor(invOutput).add(output, ForgeDirection.UP, true).stackSize;
if (output.stackSize > 0) {
output.stackSize -= Utils.addToRandomInventory(output, worldObj, xCoord, yCoord, zCoord).stackSize;
}
if (output.stackSize > 0) {
Utils.dropItems(worldObj, output, xCoord, yCoord + 1, zCoord);
}
}
}
private void findIngredients() {
if (cache == null) {
cache = TileBuffer.makeBuffer(worldObj, xCoord, yCoord, zCoord, false);
}
for (IInvSlot slot : InventoryIterator.getIterable(craftingSlots, ForgeDirection.UP)) {
ItemStack ingred = slot.getStackInSlot();
if (ingred == null)
continue;
IStackFilter filter = new CraftingFilter(ingred);
if (InvUtils.countItems(invInput, ForgeDirection.UP, filter) < InvUtils.countItems(craftingSlots, ForgeDirection.UP, filter)) {
for (ForgeDirection side : SEARCH_SIDES) {
TileEntity tile = cache[side.ordinal()].getTile();
if (tile instanceof IInventory) {
IInventory inv = Utils.getInventory(((IInventory) tile));
ItemStack result = InvUtils.moveOneItem(inv, side.getOpposite(), invInput, side, filter);
if (result != null) {
return;
}
}
}
}
}
}
public void updateCraftingMatrix(int slot, ItemStack stack) {
craftingSlots.setInventorySlotContents(slot, stack);
updateCraftingResults();
if (CoreProxy.proxy.isRenderWorld(worldObj)) {
PacketSlotChange packet = new PacketSlotChange(PacketIds.ADVANCED_WORKBENCH_SETSLOT, xCoord, yCoord, zCoord, slot, stack);
CoreProxy.proxy.sendToServer(packet.getPacket());
}
}
private void updateCraftingResults() {
if (internalInventoryCrafting == null) {
return;
}
internalInventoryCrafting.recipeUpdate(true);
if (this.currentRecipe == null || !this.currentRecipe.matches(internalInventoryCrafting, worldObj)) {
currentRecipe = CraftingHelper.findMatchingRecipe(internalInventoryCrafting, worldObj);
}
ItemStack resultStack = null;
if (currentRecipe != null) {
resultStack = currentRecipe.getCraftingResult(internalInventoryCrafting);
}
craftResult.setInventorySlotContents(0, resultStack);
internalInventoryCrafting.recipeUpdate(false);
onInventoryChanged();
}
public IInventory getCraftingSlots() {
return craftingSlots;
}
public IInventory getOutputSlot() {
return craftResult;
}
@Override
public boolean hasCurrentWork() {
return craftable && !justCrafted && lastMode != ActionMachineControl.Mode.Off;
}
@Override
public void receiveLaserEnergy(float energy) {
storedEnergy += energy;
recentEnergy[tick] += energy;
}
@Override
public int getXCoord() {
return xCoord;
}
@Override
public int getYCoord() {
return yCoord;
}
@Override
public int getZCoord() {
return zCoord;
}
@Override
public boolean isActive() {
return hasCurrentWork();
}
@Override
public boolean manageLiquids() {
return false;
}
@Override
public boolean manageSolids() {
return false;
}
@Override
public boolean allowAction(IAction action) {
return action == BuildCraftCore.actionOn || action == BuildCraftCore.actionOff;
}
public void getGUINetworkData(int id, int data) {
int currentStored = (int) (storedEnergy * 100.0);
switch (id) {
case 1:
currentStored = (currentStored & 0xFFFF0000) | (data & 0xFFFF);
storedEnergy = (currentStored / 100.0f);
break;
case 3:
currentStored = (currentStored & 0xFFFF) | ((data & 0xFFFF) << 16);
storedEnergy = (currentStored / 100.0f);
break;
case 4:
recentEnergyAverage = recentEnergyAverage & 0xFFFF0000 | (data & 0xFFFF);
break;
case 5:
recentEnergyAverage = (recentEnergyAverage & 0xFFFF) | ((data & 0xFFFF) << 16);
break;
}
}
public void sendGUINetworkData(Container container, ICrafting iCrafting) {
int currentStored = (int) (storedEnergy * 100.0);
int lRecentEnergy = 0;
for (int i = 0; i < recentEnergy.length; i++) {
lRecentEnergy += (int) (recentEnergy[i] * 100.0 / (recentEnergy.length - 1));
}
iCrafting.sendProgressBarUpdate(container, 1, currentStored & 0xFFFF);
iCrafting.sendProgressBarUpdate(container, 3, (currentStored >>> 16) & 0xFFFF);
iCrafting.sendProgressBarUpdate(container, 4, lRecentEnergy & 0xFFFF);
iCrafting.sendProgressBarUpdate(container, 5, (lRecentEnergy >>> 16) & 0xFFFF);
}
@Override
public boolean isInvNameLocalized() {
// TODO Auto-generated method stub
return false;
}
@Override
public int[] getAccessibleSlotsFromSide(int side) {
return SLOTS;
}
@Override
public boolean canInsertItem(int slot, ItemStack stack, int side) {
return isStackValidForSlot(slot, stack);
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, int side) {
return slot >= 15;
}
@Override
public boolean isStackValidForSlot(int slot, ItemStack stack) {
return slot < 15;
}
@Override
public void actionActivated(IAction action) {
if (action == BuildCraftCore.actionOn) {
lastMode = ActionMachineControl.Mode.On;
} else if (action == BuildCraftCore.actionOff) {
lastMode = ActionMachineControl.Mode.Off;
}
}
}