11package net .modificationstation .stationapi .api .recipe ;
22
3- import it .unimi .dsi .fastutil .objects .Reference2IntMap ;
4- import it .unimi .dsi .fastutil .objects .Reference2IntMaps ;
5- import it .unimi .dsi .fastutil .objects .Reference2IntOpenHashMap ;
3+ import it .unimi .dsi .fastutil .ints .*;
4+ import it .unimi .dsi .fastutil .objects .*;
65import net .minecraft .item .Item ;
76import net .minecraft .item .ItemStack ;
87import net .modificationstation .stationapi .api .registry .ItemRegistry ;
98import net .modificationstation .stationapi .api .registry .RegistryEntry ;
109import net .modificationstation .stationapi .api .tag .TagKey ;
1110import net .modificationstation .stationapi .api .util .API ;
1211
12+ import java .util .Map ;
13+ import java .util .OptionalInt ;
14+
1315public class FuelRegistry {
1416
1517 private static final Reference2IntMap <TagKey <Item >> TAG_FUEL_TIME = new Reference2IntOpenHashMap <>();
16- private static final Reference2IntMap <Item > ITEM_FUEL_TIME = new Reference2IntOpenHashMap <>();
18+ /**
19+ * Item > Metadata, which returns the smelt time.
20+ */
21+ private static final Reference2ObjectMap <Item , Int2IntMap > ITEM_FUEL_TIME = new Reference2ObjectOpenHashMap <>();
1722
1823 private static final Reference2IntMap <ItemStack > FUELS = new Reference2IntOpenHashMap <>();
1924 private static final Reference2IntMap <ItemStack > FUELS_VIEW = Reference2IntMaps .unmodifiable (FUELS );
2025 private static boolean viewInvalidated ;
2126
2227 @ API
2328 public static int getFuelTime (ItemStack stack ) {
24- return stack == null ? 0 : stack .getRegistryEntry ().streamTags ().mapToInt (TAG_FUEL_TIME ::getInt ).filter (value -> value > 0 ).findFirst ().orElseGet (() -> ITEM_FUEL_TIME .getInt (stack .getItem ()));
29+ if (stack == null )
30+ return 0 ;
31+
32+ // First see if a tag matches, and if so, use that
33+ OptionalInt foundKey = stack .getRegistryEntry ().streamTags ().mapToInt (TAG_FUEL_TIME ::getInt ).filter (value -> value > 0 ).findFirst ();
34+ if (foundKey .isPresent ())
35+ return foundKey .getAsInt ();
36+
37+ // Then check if the item has a specific entry
38+ int fuelTime = ITEM_FUEL_TIME .get (stack .getItem ()).getOrDefault (stack .getDamage (), 0 );
39+ if (fuelTime != 0 )
40+ return fuelTime ;
41+
42+ // Finally check if there's a wildcard
43+ return ITEM_FUEL_TIME .get (stack .getItem ()).getOrDefault (-1 , 0 );
2544 }
2645
2746 @ API
@@ -32,16 +51,25 @@ public static void addFuelTag(TagKey<Item> tag, int fuelTime) {
3251
3352 @ API
3453 public static void addFuelItem (Item item , int fuelTime ) {
54+ addFuelItem (item , -1 , fuelTime );
55+ }
56+
57+ @ API
58+ public static void addFuelItem (Item item , int metadata , int fuelTime ) {
3559 viewInvalidated = true ;
36- ITEM_FUEL_TIME .put (item , fuelTime );
60+ ITEM_FUEL_TIME .computeIfAbsent (item , ( o ) -> new Int2IntOpenHashMap ()). put ( metadata , fuelTime );
3761 }
3862
3963 @ API
4064 public static Reference2IntMap <ItemStack > getFuelsView () {
4165 if (viewInvalidated ) {
4266 FUELS .clear ();
43- for (Reference2IntMap .Entry <TagKey <Item >> entry : TAG_FUEL_TIME .reference2IntEntrySet ()) for (RegistryEntry <Item > item : ItemRegistry .INSTANCE .getOrCreateEntryList (entry .getKey ())) FUELS .put (new ItemStack (item .value ()), entry .getIntValue ());
44- for (Reference2IntMap .Entry <Item > entry : ITEM_FUEL_TIME .reference2IntEntrySet ()) FUELS .put (new ItemStack (entry .getKey ()), entry .getIntValue ());
67+ for (Reference2IntMap .Entry <TagKey <Item >> entry : TAG_FUEL_TIME .reference2IntEntrySet ())
68+ for (RegistryEntry <Item > item : ItemRegistry .INSTANCE .getOrCreateEntryList (entry .getKey ()))
69+ FUELS .put (new ItemStack (item .value ()), entry .getIntValue ());
70+ for (Map .Entry <Item , Int2IntMap > item2Meta2FuelValues : ITEM_FUEL_TIME .reference2ObjectEntrySet ())
71+ for (Int2IntMap .Entry meta2FuelValues : item2Meta2FuelValues .getValue ().int2IntEntrySet ())
72+ FUELS .put (new ItemStack (item2Meta2FuelValues .getKey (), 1 , meta2FuelValues .getIntKey ()), meta2FuelValues .getIntValue ());
4573 viewInvalidated = false ;
4674 }
4775 return FUELS_VIEW ;
0 commit comments