how do i set un interaction between blocks and items? since minecraft:on_use_on component is being depreciated in 1.20.40 i had to use scripts using block.afterEvents.itemUseOn.subscribe()
i managed to detect player using shovel on dirt, but here is the issue: if the item wasn't used, it won't trigger anything, like if i where to use an iron on a stone, nothing will happen because iron ingots don't interact with stone. what is the replacement then?
#On Use Item On Block
1 messages · Page 1 of 1 (latest)
playerInteractWithBlock
world.beforeEvents.playerInteractWithBlock.subscribe(data => {
const {block, player, itemStack} = data
//Other code ....
});
what's the difference between beforeEvents and afterEvents?
beforeEvents fire before the event occur, it fire before an actual action occur. In which in most cases you can able to cancel it. And it's the opposite on afterEvents, it fires after the action occur. Some example of beforeEvents are custom commands, preventing the player to place a block.
it returns an error cannot read property subscribe of undefined at...
@zenith field
cus its in 1.7.0-beta
I'm in 1.5
Why don't you update?
what can i do?
"WorldAfterEvents": [
"buttonPush",
"leverAction",
"playerPlaceBlock",
"playerBreakBlock",
"entityLoad",
"entitySpawn",
"projectileHitEntity",
"projectileHitBlock",
"entityHitEntity",
"entityHitBlock",
"entityHurt",
"entityHealthChanged",
"entityDie",
"entityRemove",
"itemStartUseOn",
"itemUseOn",
"itemUse",
"itemStopUseOn",
"itemStartUse",
"itemCompleteUse",
"itemReleaseUse",
"itemStopUse",
"playerJoin",
"playerSpawn",
"playerLeave",
"playerDimensionChange",
"pressurePlatePush",
"pressurePlatePop",
"targetBlockHit",
"tripWireTrip"
],
"WorldBeforeEvents": [
"itemUse",
"itemUseOn",
"playerBreakBlock",
"entityRemove"
],
my pack needs to be compatible with realms
Show us your code
Ohh, is 1.6.0 isn't compatible?
import { world, system } from "@minecraft/server";
function first_player() {
const players = world.getAllPlayers();
return players[0];
}
function first_player_dimension() {
return first_player().dimension;
}
function first_player_location() {
return first_player().location;
}
function mainTick() {
world.beforeEvents.playerInteractWithBlock.subscribe(event => {
if ((event.itemStack.typeId === 'minecraft:diamond_shovel') && (event.block.typeId === 'minecraft:dirt')) {
event.source.runCommand('setblock ~~5~ emerald_block');
}
})
system.run(mainTick);
}
system.run(mainTick);```
bruh
will beforeEvents work in 1.5.0 anyway?
so only those 4 classes can be used with world. beforeEvents?
yes
1.7.0-beta
and that won't be compatible with realms for a while, right?
that's for chargeables
try it
but i will try
it still doesn't work with items that doesn't have an interaction with blocks
block.typeId === "minecraft:grass_path"
world.afterEvents.itemStartUseOn.subscribe(event => {
if ((event.itemStack.typeId === 'minecraft:emerald')) {
event.source.runCommand('setblock ~~5~ emerald_block');
}
})```
it's just a place holder, i don't want to use a shovel in my script
then startuseon wont work with that item
i know
its itemUseOn that'll trigger
even that won't trigger with an emerald, it will work with a fishing rod or food items
never mind it doesn't
whats the goal tho
ok, i want to right click a stone block with a raw iron for example, to consume 1 raw iron from the player and conver the stone into iron ore
for now i am looking for what works and what doesn't so i don't waste time with the wrong class
try this one```js
import { BlockPermutation, world } from "@minecraft/server";
const itemToBlockType = {
"minecraft:emerald": "emerald_ore",
"minecraft:diamond": "diamond_ore",
"minecraft:iron_ingot": "iron_ore",
"minecraft:gold_ingot": "gold_ore"
}
world.afterEvents.itemUseOn.subscribe(data => {
const blockType = itemToBlockType[data.itemStack.typeId]
if (!blockType || !data.block.permutation.matches("stone")) return
block.setPermutation(BlockPermutation.resolve(blockType))
})```
ok
where is the game loop?
do you need one?
data.block.setPermutation
still doesn't
is there a method for doing that?
yes
which class?
entityHitBlock
what should i change?
how can i access the damagingEntity held item?
i can't find the class that returns what item is in the player hand
world.afterEvents.entityHitBlock.subscribe(({ damagingEntity, hitBlock }) => {
const equippable = damagingEntity.getComponent("equippable")
const itemStack = equippable.getEquipment("Mainhand")
const blockType = itemToBlockType[itemStack?.typeId]
if (!blockType || !hitBlock.permutation.matches("stone")) return
if (itemStack.amount > 1) {
itemStack.amount--
equippable.setEquipment("Mainhand", itemStack)
} else equippable.setEquipment("Mainhand")
hitBlock.setPermutation(BlockPermutation.resolve(blockType))
})
it doesnt...
the only change i did is to remove the mc. because i had the BlockPermutation imported directly
oh
i can delete the itemStack.amount-- part and do it with commands instead
ah wait, i can't get the item name in the command can i?
ye, its ur choice
itemStack.typeId
its better to use the itemStack than cmds, cus its the exact copy of the item
oh yeah, i can put it in the string
even without any method to consuming an item the entire stack is still consumed? how?
?
what does this line do?
equippable.setEquipment("Mainhand", itemStack)
replaceItem kinda
world.afterEvents.entityHitBlock.subscribe(({ damagingEntity, hitBlock }) => {
const equippable = damagingEntity.getComponent("equippable")
const itemStack = equippable.getEquipment("Mainhand")
if (!itemStack?.hasTag("trim_materials") || !hitBlock.permutation.matches("stone")) return
try {
hitBlock.setPermutation(BlockPermutation.resolve(itemStack.typeId.split(/:|_/)[1] + "_ore"))
} catch { return }
if (itemStack.amount > 1) {
itemStack.amount--
equippable.setEquipment("Mainhand", itemStack)
} else equippable.setEquipment("Mainhand")
})
```here's all in one
hmm, except the netherite_ingot
and amethyst
thank you very much for helping me today, i will try to make the most use of all the information that you have provided me with, and hopefully i will get it to work.
btw, how do i get a potion type of my itemStack? or any nbt data regarding the item whatsoever?
me a month and a half later: 🥲