-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileAdvancedCraftingTable.java
485 lines (425 loc) · 14.6 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
package buildcraft.silicon;
import buildcraft.api.power.ILaserTarget;
import buildcraft.BuildCraftCore;
import buildcraft.BuildCraftSilicon;
import buildcraft.api.gates.IAction;
import buildcraft.api.gates.IActionReceptor;
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.SimpleInventory;
import buildcraft.core.inventory.StackHelper;
import buildcraft.core.inventory.Transactor;
import buildcraft.core.inventory.filters.CraftingFilter;
import buildcraft.core.inventory.filters.IStackFilter;
import buildcraft.core.network.PacketIds;
import buildcraft.core.network.PacketSlotChange;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.triggers.ActionMachineControl;
import buildcraft.core.utils.CraftingHelper;
import buildcraft.core.utils.StringUtils;
import buildcraft.core.utils.Utils;
import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
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.util.ChatMessageComponent;
import net.minecraft.util.ChunkCoordinates;
import net.minecraftforge.common.util.ForgeDirection;
import static net.minecraftforge.common.util.ForgeDirection.DOWN;
import static net.minecraftforge.common.util.ForgeDirection.EAST;
import static net.minecraftforge.common.util.ForgeDirection.NORTH;
import static net.minecraftforge.common.util.ForgeDirection.SOUTH;
import static net.minecraftforge.common.util.ForgeDirection.WEST;
import net.minecraftforge.oredict.OreDictionary;
import org.bouncycastle.util.Arrays;
public class TileAdvancedCraftingTable extends TileLaserTableBase implements IInventory, ILaserTarget, IMachine, IActionReceptor, ISidedInventory {
private final class InternalInventoryCraftingContainer extends Container {
@Override
public boolean canInteractWith(EntityPlayer var1) {
return false;
}
}
private final class CraftingGrid extends SimpleInventory {
public int[] oreIDs = new int[9];
public CraftingGrid() {
super(9, "CraftingSlots", 1);
Arrays.fill(oreIDs, -1);
}
@Override
public void setInventorySlotContents(int slotId, ItemStack itemstack) {
super.setInventorySlotContents(slotId, itemstack);
if (TileAdvancedCraftingTable.this.getWorldObj() == null || !TileAdvancedCraftingTable.this.getWorldObj().isRemote)
oreIDs[slotId] = itemstack == null ? -1 : OreDictionary.getOreID(itemstack);
}
}
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 && slot >= 0 && slot < 9 && bindings[slot] >= 0) {
tempStacks[bindings[slot]] = par2ItemStack;
}
}
@Override
public ItemStack decrStackSize(int slot, int amount) {
if (tempStacks != null && slot >= 0 && slot < 9 && bindings[slot] >= 0) {
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.getWorldObj(), "[BuildCraft]");
posX = TileAdvancedCraftingTable.this.xCoord;
posY = TileAdvancedCraftingTable.this.yCoord + 1;
posZ = TileAdvancedCraftingTable.this.zCoord;
}
@Override
public void sendChatToPlayer(ChatMessageComponent 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 CraftingGrid();
inv.addListener(this);
invInput = new InventoryMapper(inv, 0, 15);
invOutput = new InventoryMapper(inv, 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 CraftingGrid craftingSlots;
private final InventoryMapper invInput;
private final InventoryMapper invOutput;
private SlotCrafting craftSlot;
private boolean craftable;
private boolean justCrafted;
private InternalPlayer internalPlayer;
private IRecipe currentRecipe;
private ActionMachineControl.Mode lastMode = ActionMachineControl.Mode.Unknown;
private TileBuffer[] cache;
@Override
public void writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
craftingSlots.writeToNBT(data, "craftingSlots");
}
@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
if (data.hasKey("StorageSlots"))
inv.readFromNBT(data, "StorageSlots");
if (data.hasKey("items"))
craftingSlots.readFromNBT(data);
else
craftingSlots.readFromNBT(data, "craftingSlots");
}
@Override
public int getSizeInventory() {
return 24;
}
@Override
public String getInventoryName() {
return StringUtils.localize("tile.assemblyWorkbenchBlock");
}
@Override
public void onInventoryChanged() {
super.onInventoryChanged();
craftable = craftResult.getStackInSlot(0) != null;
}
@Override
public double getRequiredEnergy() {
return craftResult.getStackInSlot(0) != null ? REQUIRED_POWER : 0f;
}
public int getProgressScaled(int i) {
return (int) ((getEnergy() * 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);
updateRecipe();
}
if (!CoreProxy.proxy.isSimulating(worldObj))
return;
if (lastMode == ActionMachineControl.Mode.Off)
return;
updateRecipe();
searchNeighborsForIngredients();
locateAndBindIngredients();
updateRecipeOutputDisplay();
justCrafted = false;
if (canCraftAndOutput()) {
if (getEnergy() >= getRequiredEnergy()) {
craftItem();
justCrafted = true;
}
} else {
craftable = false;
internalInventoryCrafting.tempStacks = null;
internalInventoryCrafting.hitCount = null;
setEnergy(0);
}
}
private boolean canCraftAndOutput() {
if (!hasIngredients())
return false;
ItemStack output = getRecipeOutput();
if (output == null)
return false;
return InvUtils.isRoomForStack(output, ForgeDirection.UP, invOutput);
}
private void locateAndBindIngredients() {
internalInventoryCrafting.tempStacks = new InventoryCopy(inv).getItemStacks();
internalInventoryCrafting.hitCount = new int[internalInventoryCrafting.tempStacks.length];
ItemStack[] inputSlots = internalInventoryCrafting.tempStacks;
for (int gridSlot = 0; gridSlot < craftingSlots.getSizeInventory(); gridSlot++) {
internalInventoryCrafting.bindings[gridSlot] = -1;
if (craftingSlots.getStackInSlot(gridSlot) == null)
continue;
boolean foundMatch = false;
for (int inputSlot = 0; inputSlot < inputSlots.length; inputSlot++) {
if (!isMatchingIngredient(gridSlot, inputSlot))
continue;
if (internalInventoryCrafting.hitCount[inputSlot] < inputSlots[inputSlot].stackSize
&& internalInventoryCrafting.hitCount[inputSlot] < inputSlots[inputSlot].getMaxStackSize()) {
internalInventoryCrafting.bindings[gridSlot] = inputSlot;
internalInventoryCrafting.hitCount[inputSlot]++;
foundMatch = true;
break;
}
}
if (!foundMatch)
return;
}
}
private boolean isMatchingIngredient(int gridSlot, int inputSlot) {
ItemStack inputStack = internalInventoryCrafting.tempStacks[inputSlot];
if (inputStack == null)
return false;
if (StackHelper.instance().isMatchingItem(craftingSlots.getStackInSlot(gridSlot), inputStack, true, false))
return true;
if (StackHelper.instance().isCraftingEquivalent(craftingSlots.oreIDs[gridSlot], inputStack))
return true;
return false;
}
private boolean hasIngredients() {
return currentRecipe != null && currentRecipe.matches(internalInventoryCrafting, worldObj);
}
private void craftItem() {
ItemStack recipeOutput = getRecipeOutput();
craftSlot.onPickupFromSlot(internalPlayer, recipeOutput);
ItemStack[] tempStorage = internalInventoryCrafting.tempStacks;
for (int i = 0; i < tempStorage.length; i++) {
if (tempStorage[i] != null && tempStorage[i].stackSize <= 0) {
tempStorage[i] = null;
}
inv.getItemStacks()[i] = tempStorage[i];
}
subtractEnergy(getRequiredEnergy());
List<ItemStack> outputs = Lists.newArrayList(recipeOutput.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.addToRandomInventoryAround(worldObj, xCoord, yCoord, zCoord, output);
}
if (output.stackSize > 0) {
InvUtils.dropItems(worldObj, output, xCoord, yCoord + 1, zCoord);
}
}
}
private void searchNeighborsForIngredients() {
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 = InvUtils.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);
updateRecipe();
if (CoreProxy.proxy.isRenderWorld(worldObj)) {
PacketSlotChange packet = new PacketSlotChange(PacketIds.ADVANCED_WORKBENCH_SETSLOT, xCoord, yCoord, zCoord, slot, stack);
BuildCraftSilicon.instance.sendToServer(packet);
}
}
private void updateRecipe() {
if (internalInventoryCrafting == null) {
return;
}
internalInventoryCrafting.recipeUpdate(true);
if (this.currentRecipe == null || !this.currentRecipe.matches(internalInventoryCrafting, worldObj)) {
currentRecipe = CraftingHelper.findMatchingRecipe(internalInventoryCrafting, worldObj);
}
internalInventoryCrafting.recipeUpdate(false);
onInventoryChanged();
}
private void updateRecipeOutputDisplay() {
if (internalInventoryCrafting == null || currentRecipe == null) {
craftResult.setInventorySlotContents(0, null);
return;
}
ItemStack resultStack = getRecipeOutput();
if (resultStack == null) {
internalInventoryCrafting.recipeUpdate(true);
resultStack = getRecipeOutput();
internalInventoryCrafting.recipeUpdate(false);
}
craftResult.setInventorySlotContents(0, resultStack);
onInventoryChanged();
}
private ItemStack getRecipeOutput() {
if (internalInventoryCrafting == null || currentRecipe == null) {
return null;
}
return currentRecipe.getCraftingResult(internalInventoryCrafting);
}
public IInventory getCraftingSlots() {
return craftingSlots;
}
public IInventory getOutputSlot() {
return craftResult;
}
@Override
public boolean canCraft() {
return craftable && !justCrafted && lastMode != ActionMachineControl.Mode.Off;
}
@Override
public boolean isActive() {
return requiresLaserEnergy();
}
@Override
public boolean manageFluids() {
return false;
}
@Override
public boolean manageSolids() {
return false;
}
@Override
public boolean allowAction(IAction action) {
return action == BuildCraftCore.actionOn || action == BuildCraftCore.actionOff;
}
@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 isItemValidForSlot(slot, stack);
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, int side) {
return slot >= 15;
}
@Override
public boolean isItemValidForSlot(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;
}
}
@Override
public boolean hasCustomInventoryName() {
return false;
}
}