diff --git a/gradle.properties b/gradle.properties index 82a120a..74ed393 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,7 +7,7 @@ bta_version=7.1-pre1a loader_version=0.14.19-babric.3-bta # Mod -mod_version=3.1.1 +mod_version=3.1.2 mod_group=turniplabs mod_name=halplibe diff --git a/src/main/java/turniplabs/halplibe/HalpLibe.java b/src/main/java/turniplabs/halplibe/HalpLibe.java index 2b81d6e..04a4726 100644 --- a/src/main/java/turniplabs/halplibe/HalpLibe.java +++ b/src/main/java/turniplabs/halplibe/HalpLibe.java @@ -3,6 +3,8 @@ import net.fabricmc.api.ModInitializer; import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint; import net.minecraft.core.Global; +import net.minecraft.core.block.Block; +import net.minecraft.core.item.Item; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import turniplabs.halplibe.helper.AchievementHelper; @@ -15,6 +17,8 @@ import turniplabs.halplibe.util.toml.Toml; import turniplabs.halplibe.util.version.PacketModList; +import java.util.HashMap; + public class HalpLibe implements ModInitializer, PreLaunchEntrypoint{ public static final String MOD_ID = "halplibe"; public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); @@ -46,6 +50,30 @@ public class HalpLibe implements ModInitializer, PreLaunchEntrypoint{ public static String addModId(String modId, String name) { return modId + "." + name; } + public static HashMap itemKeyToIdMap = new HashMap<>(); + public static int getTrueItemOrBlockId(String key){ + // This all exists since the item key to id maps are somewhat unreliable due to blocks having their keys remapped after creation + if (itemKeyToIdMap.containsKey(key)) return itemKeyToIdMap.get(key); + if (key.startsWith("item")){ + for (Item item : Item.itemsList){ + if (item != null && item.getKey() != null && !item.getKey().isEmpty()){ + itemKeyToIdMap.put(item.getKey(), item.id); + if (item.getKey().matches(key)) return item.id; + } + } + throw new IllegalArgumentException("Could not find an item that corresponds to the key '" + key + "'"); + } + if (key.startsWith("tile")){ + for (Block item : Block.blocksList){ + if (item != null && item.getKey() != null && !item.getKey().isEmpty()){ + itemKeyToIdMap.put(item.getKey(), item.id); + if (item.getKey().matches(key)) return item.id; + } + } + throw new IllegalArgumentException("Could not find a block that corresponds to the key '" + key + "'"); + } + throw new IllegalArgumentException("Key '" + key + "' does not start with a valid predicate of 'item' or 'tile'"); + } @Override public void onInitialize() { AchievementHelper.addPage(VANILLA_ACHIEVEMENTS); diff --git a/src/main/java/turniplabs/halplibe/mixin/mixins/ItemStackJsonAdapterMixin.java b/src/main/java/turniplabs/halplibe/mixin/mixins/ItemStackJsonAdapterMixin.java index f5946aa..431b64c 100644 --- a/src/main/java/turniplabs/halplibe/mixin/mixins/ItemStackJsonAdapterMixin.java +++ b/src/main/java/turniplabs/halplibe/mixin/mixins/ItemStackJsonAdapterMixin.java @@ -11,6 +11,7 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import turniplabs.halplibe.HalpLibe; import java.lang.reflect.Type; @@ -22,21 +23,7 @@ protected void deserializeKey(JsonElement json, Type typeOfT, JsonDeserializatio JsonObject obj = json.getAsJsonObject(); if (obj.has("key")) { String key = obj.get("key").getAsString(); - Integer itemId; - if (key.startsWith("tile")){ - itemId = Block.keyToIdMap.get(key); - if (itemId == null) { - throw new IllegalArgumentException("Null return when trying to located block from key '" + obj.get("key") + "'"); - } - } else if (key.startsWith("item")) { - itemId = Item.nameToIdMap.get(key); - if (itemId == null) { - throw new IllegalArgumentException("Null return when trying to located item from key '" + obj.get("key") + "'"); - } - } else { - throw new IllegalArgumentException("Key '" + key + "' does not start with a valid predicate of 'item' or 'tile'"); - } - + int itemId = HalpLibe.getTrueItemOrBlockId(key); ItemStack stack = obj.has("amount") ? new ItemStack(itemId, obj.get("amount").getAsInt(), obj.get("meta").getAsInt()) : new ItemStack(itemId, 1, obj.get("meta").getAsInt()); cir.setReturnValue(stack); }