#CRASH GAME BUG

1 messages · Page 1 of 1 (latest)

dusty talon
#

im trying to make an world edit addon and trying to making a wand that when u click on ground it will get that block location and set it into second lores of the item and when u break the block it will get the location of that block and set it into first lores of the item like this
#1:§rpos1: x y z
#2:§rpos2: x y z

and i got this script:

import * as mc from "@minecraft/server"
mc.world.beforeEvents.itemUseOn.subscribe((data) => {
    mc.system.run(() => {
        const player = data.source
        if (data.itemStack?.typeId != "we:wand") return;
        let lores = data.itemStack.getLore() ?? ["§rpos1: - - -", ""]
        lores[1] = `§rpos2: ${data.block.location.x} ${data.block.location.y} ${data.block.location.z}`
        const item = new mc.ItemStack("we:wand", 1)
        item.setLore(lores)
        player.getComponent("minecraft:inventory").container.setItem(player.selectedSlot, item)
    })
})

the issue that when i click on the ground it crash my game PLS HELP :<

#

i didn't got any error log when it crash ._.

vernal sealBOT
#
Debug Result

There is an error in this [code](#1163724410579066971 message):

<repl>.js:10:9 - error TS2532: Object is possibly 'undefined'.

10         player.getComponent("minecraft:inventory").container.setItem(player.selectedSlot, item)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

dusty talon
#

hmm

#

i didn't understand tbh i think that the correct line ._.?

strong quiver
#

It's failing because of this line
let lores = data.itemStack.getLore() ?? ["§rpos1: - - -", ""]
getLore() always returns an array so it's returning [], not undefined

#

If it's crashing the game then that's a bug. It should throw an exception.

dusty sparrow