-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathRecyclingHandler.java
More file actions
187 lines (162 loc) · 8.13 KB
/
Copy pathRecyclingHandler.java
File metadata and controls
187 lines (162 loc) · 8.13 KB
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
package gregtech.api.recipes;
import gregtech.api.GTValues;
import gregtech.api.GregTechAPI;
import gregtech.api.items.metaitem.MetaItem;
import gregtech.api.items.toolitem.ToolHelper;
import gregtech.api.recipes.ingredients.GTRecipeInput;
import gregtech.api.unification.FluidUnifier;
import gregtech.api.unification.OreDictUnifier;
import gregtech.api.unification.material.MarkerMaterial;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.properties.PropertyKey;
import gregtech.api.unification.ore.OrePrefix;
import gregtech.api.unification.stack.MaterialStack;
import gregtech.api.unification.stack.RecyclingData;
import gregtech.api.unification.stack.UnificationEntry;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import it.unimi.dsi.fastutil.chars.Char2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2LongMap;
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class RecyclingHandler {
public static @Nullable RecyclingData getRecyclingIngredients(int outputCount, @NotNull Object... recipe) {
Char2IntOpenHashMap inputCountMap = new Char2IntOpenHashMap();
Object2LongMap<Material> materialStacksExploded = new Object2LongOpenHashMap<>();
int itr = 0;
while (recipe[itr] instanceof String s) {
for (char c : s.toCharArray()) {
if (ToolHelper.getToolFromSymbol(c) != null) continue; // skip tools
int count = inputCountMap.getOrDefault(c, 0);
inputCountMap.put(c, count + 1);
}
itr++;
}
char lastChar = ' ';
for (int i = itr; i < recipe.length; i++) {
Object ingredient = recipe[i];
// Track the current working ingredient symbol
if (ingredient instanceof Character) {
lastChar = (char) ingredient;
continue;
}
// Should never happen if recipe is formatted correctly
// In the case that it isn't, this error should be handled
// by an earlier method call parsing the recipe.
if (lastChar == ' ') return null;
ItemStack stack;
if (ingredient instanceof MetaItem.MetaValueItem) {
stack = ((MetaItem<?>.MetaValueItem) ingredient).getStackForm();
} else if (ingredient instanceof UnificationEntry) {
stack = OreDictUnifier.get((UnificationEntry) ingredient);
} else if (ingredient instanceof ItemStack) {
stack = (ItemStack) ingredient;
} else if (ingredient instanceof Item) {
stack = new ItemStack((Item) ingredient, 1);
} else if (ingredient instanceof Block) {
stack = new ItemStack((Block) ingredient, 1);
} else if (ingredient instanceof String) {
stack = OreDictUnifier.get((String) ingredient);
} else continue; // throw out bad entries
addItemStackToMaterialStacks(stack, materialStacksExploded, inputCountMap.get(lastChar));
}
return new RecyclingData(materialStacksExploded.entrySet().stream()
.map(e -> new MaterialStack(e.getKey(), e.getValue() / outputCount))
.sorted(Comparator.comparingLong(m -> -m.amount))
.collect(Collectors.toList()));
}
public static @Nullable RecyclingData getRecyclingIngredients(int outputCount,
@Nullable List<GTRecipeInput> itemInputs,
@Nullable List<GTRecipeInput> fluidInputs) {
Object2LongMap<Material> materialStacksExploded = new Object2LongOpenHashMap<>();
if (itemInputs != null) {
populateExplodedMaterialStacks(materialStacksExploded, itemInputs);
}
if (fluidInputs != null) {
populateExplodedMaterialStacks(materialStacksExploded, fluidInputs);
}
if (materialStacksExploded.isEmpty()) {
return null;
}
var materialStacks = materialStacksExploded.entrySet().stream()
.map(e -> new MaterialStack(e.getKey(), e.getValue() / outputCount))
.sorted(Comparator.comparingLong(m -> -m.amount))
.collect(Collectors.toList());
return new RecyclingData(materialStacks);
}
private static void populateExplodedMaterialStacks(@NotNull Object2LongMap<Material> materialStacksExploded,
@NotNull List<GTRecipeInput> inputs) {
for (GTRecipeInput input : inputs) {
if (input == null || input.isNonConsumable()) {
continue;
}
ItemStack[] inputStacks = input.getInputStacks();
if (inputStacks == null) {
FluidStack fluidStack = input.getInputFluidStack();
if (fluidStack != null && fluidStack.amount > 0) {
addFluidStackToMaterialStacks(fluidStack, materialStacksExploded);
}
} else if (inputStacks.length != 0) {
ItemStack inputStack = inputStacks[0];
addItemStackToMaterialStacks(inputStack, materialStacksExploded, inputStack.getCount());
}
}
}
private static void addItemStackToMaterialStacks(@NotNull ItemStack itemStack,
@NotNull Object2LongMap<Material> materialStacksExploded,
int inputCount) {
// First try to get Recycling Data
RecyclingData data = GregTechAPI.RECYCLING_MANAGER.getRecyclingData(itemStack);
if (data != null) {
for (MaterialStack ms : data.getMaterials()) {
if (!(ms.material instanceof MarkerMaterial)) {
addMaterialStack(materialStacksExploded, inputCount, ms);
}
}
return;
}
// Then try to get a single Material (UnificationEntry needs this, for example)
MaterialStack materialStack = OreDictUnifier.getMaterial(itemStack);
if (materialStack != null && !(materialStack.material instanceof MarkerMaterial)) {
addMaterialStack(materialStacksExploded, inputCount, materialStack);
}
// Gather any secondary materials if this item has an OrePrefix
OrePrefix prefix = OreDictUnifier.getPrefix(itemStack);
if (prefix != null && !prefix.secondaryMaterials.isEmpty()) {
for (MaterialStack ms : prefix.secondaryMaterials) {
addMaterialStack(materialStacksExploded, inputCount, ms);
}
}
}
private static void addFluidStackToMaterialStacks(@NotNull FluidStack fluidStack,
@NotNull Object2LongMap<Material> materialStacksExploded) {
Material material = FluidUnifier.getMaterialFromFluid(fluidStack.getFluid());
if (material == null) {
return;
}
// must be a solid with a fluid
if (!material.hasProperty(PropertyKey.DUST)) {
return;
}
long amount = GTValues.M * fluidStack.amount / GTValues.L;
addMaterialStack(materialStacksExploded, 1, new MaterialStack(material, amount));
}
/**
* Adds a MaterialStack to a map of {@code <Material, Quantity>}
*
* @param materialStacksExploded the map to add to
* @param inputCount the number of items in the stack
* @param ms the stack to add
*/
private static void addMaterialStack(@NotNull Object2LongMap<Material> materialStacksExploded,
int inputCount, @NotNull MaterialStack ms) {
long amount = materialStacksExploded.getOrDefault(ms.material, 0L);
materialStacksExploded.put(ms.material, (ms.amount * inputCount) + amount);
}
}