/
BrewingRecipeUtil.java
74 lines (64 loc) · 2.52 KB
/
BrewingRecipeUtil.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
package mezz.jei.plugins.vanilla.brewing;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import mezz.jei.Internal;
import net.minecraft.init.Items;
import net.minecraft.init.PotionTypes;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionUtils;
public class BrewingRecipeUtil {
public static final ItemStack POTION = new ItemStack(Items.POTIONITEM);
public static final ItemStack WATER_BOTTLE = PotionUtils.addPotionToItemStack(POTION.copy(), PotionTypes.WATER);
private final Map<String, Integer> brewingStepCache = new HashMap<String, Integer>(); // output potion -> brewing steps
private final Multimap<String, String> potionMap = HashMultimap.create(); // output potion -> input potions
public BrewingRecipeUtil() {
clearCache();
}
public void addRecipe(ItemStack inputPotion, ItemStack outputPotion) {
String potionInputUid = Internal.getStackHelper().getUniqueIdentifierForStack(inputPotion);
String potionOutputUid = Internal.getStackHelper().getUniqueIdentifierForStack(outputPotion);
potionMap.put(potionOutputUid, potionInputUid);
clearCache();
}
public int getBrewingSteps(ItemStack outputPotion) {
String potionInputUid = Internal.getStackHelper().getUniqueIdentifierForStack(outputPotion);
return getBrewingSteps(potionInputUid, new HashSet<String>());
}
private void clearCache() {
if (brewingStepCache.size() != 1) {
brewingStepCache.clear();
String waterBottleUid = Internal.getStackHelper().getUniqueIdentifierForStack(WATER_BOTTLE);
brewingStepCache.put(waterBottleUid, 0);
}
}
private int getBrewingSteps(String potionOutputUid, Set<String> previousSteps) {
Integer brewingSteps = brewingStepCache.get(potionOutputUid);
if (brewingSteps == null) {
previousSteps.add(potionOutputUid);
Collection<String> prevPotions = potionMap.get(potionOutputUid);
if (!prevPotions.isEmpty()) {
int minPrevSteps = Integer.MAX_VALUE;
for (String prevPotion : prevPotions) {
if (!previousSteps.contains(prevPotion)) {
int prevSteps = getBrewingSteps(prevPotion, previousSteps);
minPrevSteps = Math.min(minPrevSteps, prevSteps);
}
}
if (minPrevSteps < Integer.MAX_VALUE) {
brewingSteps = minPrevSteps + 1;
brewingStepCache.put(potionOutputUid, brewingSteps);
}
}
}
if (brewingSteps == null) {
return Integer.MAX_VALUE;
} else {
return brewingSteps;
}
}
}