#cannot read property 'typeId' of undefined
1 messages · Page 1 of 1 (latest)
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?
if(item.typeId === "minecraft:stone") {
}
world.beforeEvents.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;
that doesnt work
ðŸ˜
cannot read property 'typeId' of undefined
Is not saying typeId doesn't exist, it's saying the object before it doesn't exist.
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}`)
}
})
wich version are you using
the server version or mc version
yes server version
Just use optional chaining operator
1.11.0-beta
item?.typeId
okay ill send you a better code
alr
It worked on 1.10.0-beta
but that doesn't exist now
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
1.11.0-beta
yea
il try that in a second
sure
yes
??
I thought you were talking to me sorry
its okay
when typeId isnt working use id
alr
id is stable
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 )
item.typeId // minecraft:apple
what do you want to do with the itemStack??
you can create items
or I it is but you want to add a direct item in the inventory to make it spawn in a location??,
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
const inventory = player.getComponent("inventory")?.container
inventory.addItem(new ItemStack("minecraft:diamond_sword", 1))
yes
but i made a function that can add mutliple items with enchantments
when its even working
okok
its work ?
I hope so
for me it works so it would be strange if it didn't work
wich code?
him
^
|
|
he is testing this code ig
i need it to work on realms btw
should wokr on realm ig
il test now
i play on xbox so it takes time to test
ðŸ˜
ohh okay
can anyone help? ðŸ˜
any errors?
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
No errors
Doesn't work
what console