#cannot read property 'typeId' of undefined

1 messages · Page 1 of 1 (latest)

covert ginkgo
#

i need help

#
import { world } from "@minecraft/server";


world.afterEvents.playerBreakBlock.subscribe((ev) => {
    const player = ev.player;
    const block = ev.block;
    const item = player.getComponent("inventory").container.getItem(player.selectedSlot).typeId;
    const { x, y, z } = block.location;
#

player.getComponent("inventory").container.getItem(player.selectedSlot).typeId;

#

.typeId doesnt exist?

woeful thunder
woeful thunder
covert ginkgo
#

It worked in 1.10.0-beta

#

=== doesn't matter

#

alr

#

il try

covert ginkgo
#

doesnt work

#

@woeful thunder

spring creek
#

cannot read property 'typeId' of undefined
Is not saying typeId doesn't exist, it's saying the object before it doesn't exist.

woeful thunder
# covert ginkgo doesnt work
world.beforeEvents.playerBreakBlock.subscribe((ev) => {
    const player = ev.player;
    const block = ev.block;
    const item = player.getComponent("inventory").container.getItem(player.selectedSlot)
    const { x, y, z } = block.location;
    if(!item) return
    if (item.typeId === "minecraft:stone") {
        player.sendMessage(`${block?.typeId} In MainHand ${item?.typeId}`)
    }
})
carmine otter
#

wich version are you using

covert ginkgo
carmine otter
#

yes server version

gleaming silo
#

Just use optional chaining operator

covert ginkgo
#

1.11.0-beta

gleaming silo
#

item?.typeId

carmine otter
covert ginkgo
#

It worked on 1.10.0-beta

#

but that doesn't exist now

carmine otter
#

import { world } from "@minecraft/server";

const customItems = {
    "twm:drill": "drill",
    "twm:heavy_drill": "heavy_drill",
    "twm:smelting_pickaxe": "smelting",
    //    
    "twm:stone_hammer": "hammer/twm_break",
    "twm:iron_hammer": "hammer/twm_break",
    "twm:diamond_hammer": "hammer/twm_break",
    "twm:netherite_hammer": "hammer/twm_break",
    //
    "twm:advanced_iron_axe": "advanced_axe",
    "twm:advanced_diamond_axe": "advanced_axe",
    "twm:advanced_netherite_axe": "advanced_axe",
    //
    "twm:advanced_iron_shovel": "advanced_shovel",
    "twm:advanced_diamond_shovel": "advanced_shovel",
    "twm:advanced_netherite_shovel": "advanced_shovel",
    //
    "twm:advanced_iron_pickaxe": "advanced_pickaxe",
    "twm:advanced_diamond_pickaxe": "advanced_pickaxe",
    "twm:advanced_netherite_pickaxe": "advanced_pickaxe",
}

world.afterEvents.playerBreakBlock.subscribe((ev) => {
    const player = ev.player
    const item = player.getComponent("inventory").container.getItem(player.selectedSlot).typeId

    if (item) {
        const block = ev.block
        const { x, y, z } = block.location
        const functionName = customItems[item]
        
        if (functionName !== undefined) {
            player.runCommandAsync(`execute positioned ${x} ${y} ${z} run function ${functionName}`);
        }
    }
})
#

try this

#

better then if statements

woeful thunder
covert ginkgo
carmine otter
#

sure

woeful thunder
carmine otter
woeful thunder
carmine otter
carmine otter
covert ginkgo
woeful thunder
carmine otter
#

oh okay

#

mb i see

const item = new ItemStack('minecraft:apple', 1)

item.type.id // minecraft:apple
// or
item.typeId // minecraft:apple
#

is the same ( says microsoft )

woeful thunder
#

item.typeId // minecraft:apple

woeful thunder
carmine otter
#

you can create items

woeful thunder
carmine otter
#

it will be an object as first so yu need to insert this object into the players inventory wait:

#

like:

// instead of using runCommand you can also use: (idk if this even works):
import { Player, ItemStack } from "@minecraft/server"

/**
 * @param {Player} player 
 * @param {...*} datas 
 * @throws error if the Player, ItemStack or Enchantmentsa are invalid
 */
function addItems (player, ...datas) {
    try {
        if (!player instanceof Player) throw new Error("Provided \"player\" must be from the Players class")
            const playerInventory = player.getComponent('minecraft:inventory').container
            
            for (const index in datas) {
                const data = datas[index], item = data[1]

                if (!item instanceof ItemStack) { throw new Error(typeof item === 'object' ? JSON.stringify(item) : item + "§r, must be an insteance of ItemStack") }
                const enchantments = data[2]
        
                if (enchantments) {
                    const enchantable = item.getComponent("minecraft:enchantable")
                    if (!enchantable) { throw new Error(`Item in array [${index}] is not enchantable`) }
                    enchantable.addEnchantments([...enchantments]) // can throw errors
                }
        
                playerInventory.setItem(data[0], item)
            }
    } catch (error) {
        console.error("[addItems]" + error.message)
    }
}

// example usage
function whenTriggeredExample (player) {
    addItems(player, 
        [0, new ItemStack("minecraft:diamond_sword", 1), [ { id: 'minecraft:sharpness', level: 2 }, { id: 'minecraft:unbreaking', level: 1 } ]],
        [1, new ItemStack("minecraft:diamond_pickaxe", 1), [ { id: 'minecraft:unbreaking', level: 1 } ]],
        [8, new ItemStack("minecraft:breed", 16), []],
    )
}
#

idk if this is working

woeful thunder
carmine otter
#

yes

#

but i made a function that can add mutliple items with enchantments

#

when its even working

woeful thunder
#

okok

woeful thunder
carmine otter
#

I hope so

woeful thunder
carmine otter
#

wich code?

woeful thunder
carmine otter
covert ginkgo
#

i need it to work on realms btw

carmine otter
#

should wokr on realm ig

covert ginkgo
#

i play on xbox so it takes time to test

#

😭

carmine otter
#

ohh okay

covert ginkgo
#

doesnt work

#

@carmine otter

covert ginkgo
#

can anyone help? 😭

carmine otter
#
import { world } from "@minecraft/server"
// just an object
const customItemFunctionNames = {
    "twm:drill": "drill",
    "twm:heavy_drill": "heavy_drill",
    "twm:smelting_pickaxe": "smelting",
    //    
    "twm:stone_hammer": "hammer/twm_break",
    "twm:iron_hammer": "hammer/twm_break",
    "twm:diamond_hammer": "hammer/twm_break",
    "twm:netherite_hammer": "hammer/twm_break",
    //
    "twm:advanced_iron_axe": "advanced_axe",
    "twm:advanced_diamond_axe": "advanced_axe",
    "twm:advanced_netherite_axe": "advanced_axe",
    //
    "twm:advanced_iron_shovel": "advanced_shovel",
    "twm:advanced_diamond_shovel": "advanced_shovel",
    "twm:advanced_netherite_shovel": "advanced_shovel",
    //
    "twm:advanced_iron_pickaxe": "advanced_pickaxe",
    "twm:advanced_diamond_pickaxe": "advanced_pickaxe",
    "twm:advanced_netherite_pickaxe": "advanced_pickaxe"
}
//
world.afterEvents.playerBreakBlock.subscribe((ev) => {
    const player = ev.player
    const item = ev.itemStackBeforeBreak // if you want the orignal item
    const itemTypeId = item?.typeId // i use item.typeId twice so i added a const for this

    // item and itemTypeId will be undefined, if the holding Item was empty
    if (itemTypeId) {
        const block = ev.block
        const { x, y, z } = block.location
        
        if (itemTypeId in customItemFunctionNames) {
            player.runCommandAsync(`execute positioned ${x} ${y} ${z} run function ${customItemFunctionNames[itemTypeId]}`)
        }
    }
})
#

I improved the code a bit

covert ginkgo
carmine otter
#

Then i don't know why

#

Try sone Debugging with the console

covert ginkgo