-
Notifications
You must be signed in to change notification settings - Fork 497
/
AssemblyRecipeManager.java
155 lines (134 loc) · 4.78 KB
/
AssemblyRecipeManager.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
package buildcraft.core.recipes;
import buildcraft.api.recipes.IAssemblyRecipeManager;
import buildcraft.core.inventory.ITransactor;
import buildcraft.core.inventory.InventoryIterator;
import buildcraft.core.inventory.InventoryIterator.IInvSlot;
import buildcraft.core.inventory.Transactor;
import buildcraft.core.inventory.filters.ArrayStackFilter;
import java.util.LinkedList;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.oredict.OreDictionary;
public class AssemblyRecipeManager implements IAssemblyRecipeManager {
public static final AssemblyRecipeManager INSTANCE = new AssemblyRecipeManager();
private List<AssemblyRecipe> assemblyRecipes = new LinkedList<AssemblyRecipe>();
@Override
public void addRecipe(double energyCost, ItemStack output, Object... input) {
assemblyRecipes.add(new AssemblyRecipe(output, energyCost, input));
}
@Override
public List<AssemblyRecipe> getRecipes() {
return assemblyRecipes;
}
public static class AssemblyRecipe implements IAssemblyRecipe {
public final ItemStack output;
public final double energyCost;
private final Object[] originalInput;
private final Object[] processedInput;
public AssemblyRecipe(ItemStack output, double energyCost, Object... inputs) {
this.output = output.copy();
this.energyCost = energyCost;
this.originalInput = inputs;
processedInput = new Object[inputs.length];
for (int i = 0; i < inputs.length; i++) {
if (inputs[i] instanceof String)
processedInput[i] = OreDictionary.getOres((String) inputs[i]);
else if (inputs[i] instanceof ItemStack)
processedInput[i] = inputs[i];
else if (inputs[i] instanceof Item)
processedInput[i] = new ItemStack((Item) inputs[i]);
else if (inputs[i] instanceof Block)
processedInput[i] = new ItemStack((Block) inputs[i], 1, OreDictionary.WILDCARD_VALUE);
else if (inputs[i] instanceof Integer)
processedInput[i] = inputs[i];
else
throw new IllegalArgumentException("Unknown Object passed to recipe!");
}
}
@Override
public ItemStack getOutput() {
return output.copy();
}
@Override
public Object[] getInputs() {
return originalInput;
}
@Override
public double getEnergyCost() {
return energyCost;
}
public boolean canBeDone(IInventory inv) {
for (int i = 0; i < processedInput.length; i++) {
if (processedInput[i] == null)
continue;
if (processedInput[i] instanceof ItemStack) {
ItemStack requirement = (ItemStack) processedInput[i];
int found = 0; // Amount of ingredient found in inventory
int expected = requirement.stackSize;
for (IInvSlot slot : InventoryIterator.getIterable(inv, ForgeDirection.UNKNOWN)) {
ItemStack item = slot.getStackInSlot();
if (item == null)
continue;
if (item.isItemEqual(requirement))
found += item.stackSize; // Adds quantity of stack to amount found
if (found >= expected)
break;
}
// Return false if the amount of ingredient found
// is not enough
if (found < expected)
return false;
} else if (processedInput[i] instanceof List) {
List<ItemStack> oreList = (List<ItemStack>) processedInput[i];
int found = 0; // Amount of ingredient found in inventory
int expected = (Integer) processedInput[i++ + 1];
for (IInvSlot slot : InventoryIterator.getIterable(inv, ForgeDirection.UNKNOWN)) {
ItemStack item = slot.getStackInSlot();
if (item == null)
continue;
for (ItemStack oreItem : oreList) {
if (OreDictionary.itemMatches(oreItem, item, true)) {
found += item.stackSize;
break;
}
}
if (found >= expected)
break;
}
// Return false if the amount of ingredient found
// is not enough
if (found < expected)
return false;
}
}
return true;
}
public void useItems(IInventory inv) {
ITransactor tran = Transactor.getTransactorFor(inv);
Object[] input = processedInput;
for (int i = 0; i < input.length; i++) {
if (input[i] instanceof ItemStack) {
ItemStack requirement = (ItemStack) input[i];
for (int num = 0; num < requirement.stackSize; num++) {
tran.remove(new ArrayStackFilter(requirement), ForgeDirection.UNKNOWN, true);
}
} else if (input[i] instanceof List) {
List<ItemStack> oreList = (List<ItemStack>) input[i];
int required = (Integer) input[i + 1];
for (ItemStack ore : oreList) {
for (int num = 0; num < required; num++) {
if (tran.remove(new ArrayStackFilter(ore), ForgeDirection.UNKNOWN, true) != null)
required--;
}
if (required <= 0)
break;
}
}
}
}
}
}