-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileIntegrationTable.java
216 lines (182 loc) · 5.97 KB
/
TileIntegrationTable.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
/**
* Copyright (c) 2011-2014, 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.silicon;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.recipes.BuildcraftRecipes;
import buildcraft.api.recipes.IIntegrationRecipe;
import buildcraft.core.inventory.ITransactor;
import buildcraft.core.inventory.InventoryMapper;
import buildcraft.core.inventory.SimpleInventory;
import buildcraft.core.inventory.StackHelper;
import buildcraft.core.inventory.Transactor;
import buildcraft.core.triggers.ActionMachineControl;
import buildcraft.core.utils.StringUtils;
import buildcraft.core.utils.Utils;
public class TileIntegrationTable extends TileLaserTableBase implements ISidedInventory {
public static final int SLOT_INPUT_A = 0;
public static final int SLOT_INPUT_B = 1;
public static final int SLOT_OUTPUT = 2;
private static final int CYCLE_LENGTH = 32;
private static final int[] SLOTS = Utils.createSlotArray(0, 3);
private static final int[] SLOT_COMPONENTS = Utils.createSlotArray(3, 9);
private int tick = 0;
private SimpleInventory invRecipeOutput = new SimpleInventory(1, "integrationOutput", 64);
private InventoryMapper invOutput = new InventoryMapper(inv, SLOT_OUTPUT, 1, false);
private IIntegrationRecipe.IntegrationResult integrationResult;
private boolean canCraft = false;
public IInventory getRecipeOutput() {
return invRecipeOutput;
}
private ItemStack[] getComponents() {
ItemStack[] components = new ItemStack[9];
for (int i = SLOT_OUTPUT + 1; i < 12; i++) {
components[i - SLOT_OUTPUT - 1] = inv.getStackInSlot(i);
}
return components;
}
@Override
public void updateEntity() {
super.updateEntity();
if (worldObj.isRemote) {
return;
}
tick++;
if (tick % CYCLE_LENGTH != 0) {
return;
}
canCraft = false;
ItemStack inputA = inv.getStackInSlot(SLOT_INPUT_A);
ItemStack inputB = inv.getStackInSlot(SLOT_INPUT_B);
ItemStack[] components = getComponents();
integrationResult = integrate(inputA, inputB, components);
if (integrationResult == null || integrationResult.output == null) {
setEnergy(0);
return;
}
invRecipeOutput.setInventorySlotContents(0, integrationResult.output);
if (!isRoomForOutput(integrationResult.output)) {
setEnergy(0);
return;
}
canCraft = true;
if (getEnergy() >= integrationResult.energyCost && lastMode != ActionMachineControl.Mode.Off) {
setEnergy(0);
inv.decrStackSize(SLOT_INPUT_A, 1);
inv.decrStackSize(SLOT_INPUT_B, 1);
// For each required component, loop through the component inventory
for (ItemStack stack : integrationResult.usedComponents) {
int decreased = 0;
for (int i = SLOT_OUTPUT + 1; i < 12; i++) {
ItemStack stack1 = inv.getStackInSlot(i);
if (stack1 != null) {
if (StackHelper.isMatchingItem(stack, stack1, true, false)) {
decreased += stack1.stackSize;
inv.decrStackSize(i, stack.stackSize - decreased);
}
}
if (decreased >= stack.stackSize) {
break;
}
}
}
ITransactor trans = Transactor.getTransactorFor(invOutput);
trans.add(integrationResult.output, ForgeDirection.UP, true);
}
}
private IIntegrationRecipe.IntegrationResult integrate(ItemStack inputA, ItemStack inputB, ItemStack[] components) {
IIntegrationRecipe.IntegrationResult result;
for (IIntegrationRecipe recipe : BuildcraftRecipes.integrationTable.getRecipes()) {
if (recipe.isValidInputA(inputA) && recipe.isValidInputB(inputB) &&
(result = recipe.integrate(inputA, inputB, components)) != null) {
return result;
}
}
return null;
}
private boolean isRoomForOutput(ItemStack output) {
ItemStack existingOutput = inv.getStackInSlot(SLOT_OUTPUT);
if (existingOutput == null) {
return true;
}
if (StackHelper.canStacksMerge(output, existingOutput) && output.stackSize + existingOutput.stackSize <= output.getMaxStackSize()) {
return true;
}
return false;
}
@Override
public double getRequiredEnergy() {
if (integrationResult != null) {
return integrationResult.energyCost;
}
return 0.0;
}
@Override
public boolean canCraft() {
return canCraft && isActive();
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
switch (slot) {
case SLOT_INPUT_A:
return isValidInputA(stack);
case SLOT_INPUT_B:
return isValidInputB(stack);
}
return false;
}
private boolean isValidInputA(ItemStack stack) {
ItemStack inputB = inv.getStackInSlot(SLOT_INPUT_B);
for (IIntegrationRecipe recipe : BuildcraftRecipes.integrationTable.getRecipes()) {
if (recipe.isValidInputA(stack) && (inputB == null || recipe.isValidInputB(inputB))) {
return true;
}
}
return false;
}
private boolean isValidInputB(ItemStack stack) {
ItemStack inputA = inv.getStackInSlot(SLOT_INPUT_A);
for (IIntegrationRecipe recipe : BuildcraftRecipes.integrationTable.getRecipes()) {
if (recipe.isValidInputB(stack) && (inputA == null || recipe.isValidInputA(inputA))) {
return true;
}
}
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 == SLOT_OUTPUT;
}
@Override
public int getSizeInventory() {
return 12;
}
@Override
public String getInventoryName() {
return StringUtils.localize("tile.integrationTableBlock.name");
}
@Override
public boolean hasCustomInventoryName() {
return false;
}
@Override
public boolean isActive() {
return integrationResult != null && super.isActive();
}
}