Skip to content

Commit c61a3b6

Browse files
authored
UDOC-0 - Attempts to add metadata to fuel registry… (#98)
1 parent c788750 commit c61a3b6

3 files changed

Lines changed: 39 additions & 9 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ fabric.loom.multiProjectOptimisation=true
3434

3535
# Test properties
3636
gcapi_version = 1.3.1
37-
hmi_version = 5.1.1
37+
hmi_version = 5.2.1
3838
modmenu_version = v1.8.5-beta.3
3939
mojangfix_version = 0.5.2

src/test/java/net/modificationstation/sltest/recipe/RecipeListener.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import net.modificationstation.stationapi.api.event.recipe.RecipeRegisterEvent;
1010
import net.modificationstation.stationapi.api.item.StationItemNbt;
1111
import net.modificationstation.stationapi.api.recipe.CraftingRegistry;
12+
import net.modificationstation.stationapi.api.recipe.FuelRegistry;
1213
import net.modificationstation.stationapi.api.recipe.SmeltingRegistry;
1314

1415
import static net.modificationstation.sltest.SLTest.NAMESPACE;
@@ -41,6 +42,7 @@ public void registerRecipes(RecipeRegisterEvent event) {
4142
ItemStack itemInstance = new ItemStack(ItemListener.testNBTItem);
4243
StationItemNbt.class.cast(itemInstance).getStationNbt().putInt(of(NAMESPACE, "rand_num").toString(), 10);
4344
SmeltingRegistry.addSmeltingRecipe(ItemListener.testItem.id, itemInstance);
45+
FuelRegistry.addFuelItem(Item.ITEMS[Block.LEAVES.id], 1, 500);
4446
}
4547
}
4648
}

station-recipes-v0/src/main/java/net/modificationstation/stationapi/api/recipe/FuelRegistry.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,46 @@
11
package 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.*;
65
import net.minecraft.item.Item;
76
import net.minecraft.item.ItemStack;
87
import net.modificationstation.stationapi.api.registry.ItemRegistry;
98
import net.modificationstation.stationapi.api.registry.RegistryEntry;
109
import net.modificationstation.stationapi.api.tag.TagKey;
1110
import net.modificationstation.stationapi.api.util.API;
1211

12+
import java.util.Map;
13+
import java.util.OptionalInt;
14+
1315
public 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

Comments
 (0)