-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileIntegrationTable.java
250 lines (203 loc) · 6.07 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
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
/**
* 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.item.ItemStack;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidStack;
import buildcraft.api.recipes.BuildcraftRecipeRegistry;
import buildcraft.api.recipes.CraftingResult;
import buildcraft.api.recipes.IFlexibleCrafter;
import buildcraft.api.recipes.IFlexibleRecipe;
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.utils.StringUtils;
public class TileIntegrationTable extends TileLaserTableBase implements IFlexibleCrafter {
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 int tick = 0;
private SimpleInventory invRecipeOutput = new SimpleInventory(1, "integrationOutput", 64);
private InventoryMapper invOutput = new InventoryMapper(inv, SLOT_OUTPUT, 1, false);
private IFlexibleRecipe<ItemStack> activeRecipe;
private CraftingResult<ItemStack> craftingPreview;
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 initialize() {
super.initialize();
updateRecipe();
}
@Override
public void updateEntity() {
super.updateEntity();
if (worldObj.isRemote) {
return;
}
if (activeRecipe == null || craftingPreview == null) {
setEnergy(0);
return;
}
tick++;
if (tick % CYCLE_LENGTH != 0) {
return;
}
if (!isRoomForOutput(craftingPreview.crafted)) {
setEnergy(0);
return;
}
if (getEnergy() >= craftingPreview.energyCost) {
setEnergy(0);
craftingPreview = null;
CraftingResult<ItemStack> craftResult = activeRecipe.craft(this, false);
if (craftResult != null) {
ItemStack result = craftResult.crafted.copy();
ITransactor trans = Transactor.getTransactorFor(invOutput);
trans.add(result, ForgeDirection.UP, true);
}
}
}
private void setNewActiveRecipe(ItemStack inputA, ItemStack inputB, ItemStack[] components) {
craftingPreview = null;
for (IIntegrationRecipe recipe : BuildcraftRecipeRegistry.integrationTable.getRecipes()) {
if (recipe.isValidInputA(inputA) && recipe.isValidInputB(inputB)) {
craftingPreview = recipe.craft(this, true);
if (craftingPreview != null) {
activeRecipe = recipe;
break;
}
}
}
}
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 int getRequiredEnergy() {
if (craftingPreview != null) {
return craftingPreview.energyCost;
} else {
return 0;
}
}
@Override
public boolean canCraft() {
return hasWork();
}
@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 : BuildcraftRecipeRegistry.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 : BuildcraftRecipeRegistry.integrationTable.getRecipes()) {
if (recipe.isValidInputB(stack) && (inputA == null || recipe.isValidInputA(inputA))) {
return true;
}
}
return false;
}
@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 hasWork() {
return craftingPreview != null;
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
super.setInventorySlotContents(slot, stack);
updateRecipe();
}
@Override
public ItemStack decrStackSize(int slot, int amount) {
ItemStack result = super.decrStackSize(slot, amount);
updateRecipe();
return result;
}
private void updateRecipe() {
ItemStack inputA = inv.getStackInSlot(SLOT_INPUT_A);
ItemStack inputB = inv.getStackInSlot(SLOT_INPUT_B);
ItemStack[] components = getComponents();
setNewActiveRecipe(inputA, inputB, components);
if (craftingPreview != null) {
invRecipeOutput.setInventorySlotContents(0, craftingPreview.crafted);
} else {
invRecipeOutput.setInventorySlotContents(0, null);
}
}
@Override
public int getCraftingItemStackSize() {
return getSizeInventory() - 3;
}
@Override
public ItemStack getCraftingItemStack(int slotid) {
return getStackInSlot(slotid + 3);
}
@Override
public ItemStack decrCraftingItemStack(int slotid, int val) {
return decrStackSize(slotid + 3, val);
}
@Override
public FluidStack getCraftingFluidStack(int tankid) {
return null;
}
@Override
public FluidStack decrCraftingFluidStack(int tankid, int val) {
return null;
}
@Override
public int getCraftingFluidStackSize() {
return 0;
}
}