Cannot read property "0" from undefined (Put a tooltip with an element with NBT data in an array)
MLDEG opened this issue · 1 comments
Minecraft Version
1.18.2
KubeJS Version
1802.5.5-build.569
Rhino Version
1802.2.1-build.255
Architectury Version
4.11.93
Forge/Fabric Version
Forge - 40.2.17
Describe your issue
Hi,
I tried to get NBT data from an item :
Item.of('immersiveengineering:coresample', '{mineralInfo:[{mineral:"immersiveengineering:mineral/lapis"}]}')
To put a tooltip specifically on this item with this:
tooltip.addAdvanced('immersiveengineering:coresample', (item, advanced, text) => { if (item.nbt?.mineralInfo[0].mineral.match("immersiveengineering:mineral/lapis")) { if (!tooltip.shift) { text.add(1, [Text.of('Hold ').gold(), Text.of('Shift ').yellow(), Text.of('to see more info.').gold()]) } else { text.add(1, Text.of('§5Ores').bold(true)) text.add(2, Text.of('§380% Lapis Lazuli')) text.add(3, Text.of('§315% Sulfur')) text.add(4, Text.of('§305% Fluorite')) text.add(5, Text.of('§5Spoils').bold(true)) text.add(6, Text.of('§3Cobblestone')) text.add(7, Text.of('§3Gravel')) } } })
It works when you launch the modpack, but as soon as you reload the textures it crashes.
Thanks in advance :
Crash report/logs
Something (probably your recipe viewer) is attempting to get a tooltip for that item from an ItemStack with no nbt data.
This causes item.nbt
to be undefined
, so the optional property mineralInfo
is also undefined. Then you try to get the 0
th element from that, however that fails because ?.mineralInfo
returned undefined
.
You can solve this by adding a check for if item.nbt?.mineralInfo != undefined
before trying to access anything else from that tag.