-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileIntegrationTable.java
178 lines (150 loc) · 4.95 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
/*
* Copyright (c) SpaceToad, 2011-2012
* 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 buildcraft.api.recipes.BuildcraftRecipes;
import buildcraft.api.recipes.IIntegrationRecipeManager.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;
import buildcraft.core.utils.Utils;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.ForgeDirection;
/**
*
* @author CovertJaguar <http://www.railcraft.info/>
*/
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 int tick = 0;
private SimpleInventory invRecipeOutput = new SimpleInventory(1, "integrationOutput", 64);
private InventoryMapper invOutput = new InventoryMapper(inv, SLOT_OUTPUT, 1, false);
private IIntegrationRecipe currentRecipe;
public IInventory getRecipeOutput(){
return invRecipeOutput;
}
@Override
public void updateEntity() {
super.updateEntity();
if (worldObj.isRemote)
return;
tick++;
if (tick % CYCLE_LENGTH != 0)
return;
currentRecipe = findMatchingRecipe();
if (currentRecipe == null) {
setEnergy(0);
return;
}
ItemStack inputA = inv.getStackInSlot(SLOT_INPUT_A);
ItemStack inputB = inv.getStackInSlot(SLOT_INPUT_B);
invRecipeOutput.setInventorySlotContents(0, currentRecipe.getOutputForInputs(inputA, inputB));
if (getEnergy() >= currentRecipe.getEnergyCost())
tryCraftItem();
}
private IIntegrationRecipe findMatchingRecipe() {
ItemStack inputA = inv.getStackInSlot(SLOT_INPUT_A);
ItemStack inputB = inv.getStackInSlot(SLOT_INPUT_B);
for (IIntegrationRecipe recipe : BuildcraftRecipes.integrationTable.getRecipes()) {
if (recipe.isValidInputA(inputA) && recipe.isValidInputB(inputB))
return recipe;
}
return null;
}
private void tryCraftItem() {
ItemStack inputA = inv.getStackInSlot(SLOT_INPUT_A);
ItemStack inputB = inv.getStackInSlot(SLOT_INPUT_B);
ItemStack output = currentRecipe.getOutputForInputs(inputA, inputB);
if (isRoomForOutput(output)) {
setEnergy(0);
inv.decrStackSize(SLOT_INPUT_A, 1);
inv.decrStackSize(SLOT_INPUT_B, 1);
ITransactor trans = Transactor.getTransactorFor(invOutput);
trans.add(output, ForgeDirection.UP, true);
}
}
private boolean isRoomForOutput(ItemStack output) {
ItemStack existingOutput = inv.getStackInSlot(SLOT_OUTPUT);
if (existingOutput == null)
return true;
if (StackHelper.instance().canStacksMerge(output, existingOutput) && output.stackSize + existingOutput.stackSize <= output.getMaxStackSize())
return true;
return false;
}
@Override
public double getRequiredEnergy() {
if (currentRecipe != null) {
return currentRecipe.getEnergyCost();
}
return 0;
}
public int getProgressScaled(int i) {
if(currentRecipe == null)
return 0;
return (int) ((getEnergy() * i) / currentRecipe.getEnergyCost());
}
@Override
public boolean canCraft() {
return currentRecipe != null;
}
@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 3;
}
@Override
public String getInvName() {
return StringUtils.localize("tile.integrationTableBlock");
}
}