33import com .google .common .collect .Iterables ;
44import it .unimi .dsi .fastutil .Hash ;
55import it .unimi .dsi .fastutil .objects .Object2ObjectOpenCustomHashMap ;
6+ import java .util .ArrayList ;
67import java .util .Collection ;
78import java .util .Collections ;
8- import java .util .HashSet ;
9+ import java .util .List ;
910import java .util .Map ;
10- import java .util .Set ;
1111import mekanism .api .recipes .MekanismRecipe ;
1212import mekanism .api .recipes .ingredients .InputIngredient ;
1313
@@ -22,7 +22,7 @@ public abstract class ComponentSensitiveInputCache<KEY, INPUT, INGREDIENT extend
2222 * Map of NBT based keys representing inputs to a set of the recipes that contain said input. This allows for quick contains checking by checking if a key exists, as
2323 * well as quicker recipe lookup.
2424 */
25- private final Map <INPUT , Set <RECIPE >> componentInputCache ;
25+ private final Map <INPUT , List <RECIPE >> componentInputCache ;
2626
2727 protected ComponentSensitiveInputCache (Hash .Strategy <? super INPUT > componentHashStrategy ) {
2828 this .componentInputCache = new Object2ObjectOpenCustomHashMap <>(componentHashStrategy );
@@ -51,7 +51,7 @@ public boolean contains(INPUT input) {
5151 */
5252 @ Override
5353 public Iterable <RECIPE > getRecipes (INPUT input ) {
54- Set <RECIPE > nbtRecipes = componentInputCache .getOrDefault (input , Collections .emptySet ());
54+ List <RECIPE > nbtRecipes = componentInputCache .getOrDefault (input , Collections .emptyList ());
5555 if (nbtRecipes .isEmpty ()) {
5656 return super .getRecipes (input );
5757 }
@@ -69,6 +69,17 @@ public Iterable<RECIPE> getRecipes(INPUT input) {
6969 * @param recipe Recipe to add.
7070 */
7171 protected void addNbtInputCache (INPUT input , RECIPE recipe ) {
72- componentInputCache .computeIfAbsent (input , i -> new HashSet <>()).add (recipe );
72+ if (!componentInputCache .containsKey (input )) {
73+ componentInputCache .put (input , Collections .singletonList (recipe ));
74+ } else {
75+ List <RECIPE > existing = componentInputCache .get (input );
76+ if (existing .size () == 1 ) {
77+ List <RECIPE > newList = new ArrayList <>(existing );
78+ newList .add (recipe );
79+ componentInputCache .put (input , newList );
80+ } else {
81+ existing .add (recipe );
82+ }
83+ }
7384 }
7485}
0 commit comments