From db0f40aabed9a29e0055c3c9e6a6b1159b1444bf Mon Sep 17 00:00:00 2001 From: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Date: Sat, 1 Jul 2023 08:47:24 +1000 Subject: No more getItemStack() crashes hopefully (#276) Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> --- .../java/at/hannibal2/skyhanni/utils/NEUItems.kt | 36 +++++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt index 97f1b7ca3..848acd0ff 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/NEUItems.kt @@ -1,6 +1,9 @@ package at.hannibal2.skyhanni.utils +import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.config.ConfigManager +import at.hannibal2.skyhanni.data.ProfileStorageData +import at.hannibal2.skyhanni.test.command.CopyErrorCommand import at.hannibal2.skyhanni.utils.ItemBlink.checkBlinkItem import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimal @@ -16,9 +19,11 @@ import io.github.moulberry.notenoughupdates.overlays.BazaarSearchOverlay import io.github.moulberry.notenoughupdates.recipes.CraftingRecipe import io.github.moulberry.notenoughupdates.recipes.NeuRecipe import io.github.moulberry.notenoughupdates.util.ItemResolutionQuery +import io.github.moulberry.notenoughupdates.util.Utils import net.minecraft.client.Minecraft import net.minecraft.client.renderer.GlStateManager import net.minecraft.client.renderer.RenderHelper +import net.minecraft.init.Blocks import net.minecraft.init.Items import net.minecraft.item.ItemStack import net.minecraft.nbt.NBTTagCompound @@ -32,6 +37,15 @@ object NEUItems { private val enchantmentNamePattern = Pattern.compile("^(?(?:§.)+)(?[^§]+) (?[IVXL]+)$") var allItemsCache = mapOf() // item name -> internal name var allInternalNames = mutableListOf() + private var warnedAlready = false + + private val fallbackItem by lazy { + Utils.createItemStack( + ItemStack(Blocks.barrier).item, + "§cMissing Repo Item", + "§cYour NEU repo seems to be out of date" + ) + } fun getInternalName(itemName: String): String { return getInternalNameOrNull(itemName) ?: throw Error("getInternalName is null for '$itemName'") @@ -71,6 +85,11 @@ object NEUItems { return itemNameCache[lowercase]!! } + if (itemName == "§cmissing repo item") { + itemNameCache[lowercase] = "MISSING_ITEM" + return "MISSING_ITEM" + } + resolveEnchantmentByName(itemName)?.let { itemNameCache[itemName] = it return it @@ -137,11 +156,18 @@ object NEUItems { .withKnownInternalName(internalName) .resolveToItemStack()?.copy() - fun getItemStack(internalName: String): ItemStack = getItemStackOrNull(internalName) - ?: throw IllegalStateException( - "Could not find the Item '$internalName' in NEU Repo", - Error("ItemResolutionQuery returns null for internalName '$internalName'") - ) + fun getItemStack(internalName: String): ItemStack = getItemStackOrNull(internalName) ?: run { + if (ProfileStorageData.playerSpecific?.lastRepoIssueVersion != SkyHanniMod.version || !warnedAlready) { + Utils.showOutdatedRepoNotification() + CopyErrorCommand.logError( + IllegalStateException("Something went wrong!"), + "Encountered an error getting the item for §7$internalName§c. " + + "This is probably because your NEU repo is outdated" + ) + } + warnedAlready = true + fallbackItem + } fun isVanillaItem(item: ItemStack) = manager.auctionManager.isVanillaItem(item.getInternalName()) -- cgit