#On Use Item On Block

1 messages · Page 1 of 1 (latest)

night yarrow
#

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?

twin flint
#

playerInteractWithBlock

junior folio
night yarrow
zenith field
night yarrow
#

it returns an error cannot read property subscribe of undefined at...

#

@zenith field

empty cairn
#

cus its in 1.7.0-beta

night yarrow
#

I'm in 1.5

zenith field
#

Why don't you update?

night yarrow
#

what can i do?

empty cairn
#
      "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"
      ],
night yarrow
zenith field
#

Show us your code

zenith field
night yarrow
#
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);```
empty cairn
#

bruh

night yarrow
#

will beforeEvents work in 1.5.0 anyway?

night yarrow
empty cairn
#

yes

night yarrow
#

itemUseOn won't help

#

do i need 1.6.0 for playerInteractWithBlock?

empty cairn
#

1.7.0-beta

night yarrow
empty cairn
#

mhm

#

try the itemStartUseOn

#
world.afterEvents.itemStartUseOn
night yarrow
#

that's for chargeables

empty cairn
#

try it

night yarrow
#

but i will try

#

it still doesn't work with items that doesn't have an interaction with blocks

empty cairn
#

block.typeId === "minecraft:grass_path"

night yarrow
#
world.afterEvents.itemStartUseOn.subscribe(event => {
      if ((event.itemStack.typeId === 'minecraft:emerald')) {
        event.source.runCommand('setblock ~~5~ emerald_block');
      }
   })```
empty cairn
#

why emerald item

#

is it to bury the block?

night yarrow
#

it's just a place holder, i don't want to use a shovel in my script

empty cairn
#

then startuseon wont work with that item

night yarrow
#

i know

empty cairn
#

its itemUseOn that'll trigger

night yarrow
#

even that won't trigger with an emerald, it will work with a fishing rod or food items

#

never mind it doesn't

empty cairn
#

whats the goal tho

night yarrow
#

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

empty cairn
#

stone => ore

#

ok

night yarrow
#

for now i am looking for what works and what doesn't so i don't waste time with the wrong class

empty cairn
# night yarrow for now i am looking for what works and what doesn't so i don't waste time with ...

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))
})```

night yarrow
#

ok

night yarrow
#

do you need one?

empty cairn
#

no

#

its an event

#

so it triggers when itemUseOn happens

night yarrow
#

nothing is happening

empty cairn
#

data.block.setPermutation

night yarrow
#

still doesn't

empty cairn
#

hmmm, its only trigerring itemUse

#

oof

#

oh

#

try touching the top

night yarrow
#

nope

#

itemUseOn won't work unless the item is used

empty cairn
#

what do u think if u use hitblock

#

left click

night yarrow
#

is there a method for doing that?

empty cairn
#

yes

night yarrow
#

which class?

empty cairn
#

entityHitBlock

night yarrow
#

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

empty cairn
#
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))
})
night yarrow
#

It WORKS

#

it consumes the entire stack though

empty cairn
night yarrow
#

the only change i did is to remove the mc. because i had the BlockPermutation imported directly

empty cairn
#

oh

night yarrow
#

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?

empty cairn
#

ye, its ur choice

#

itemStack.typeId

#

its better to use the itemStack than cmds, cus its the exact copy of the item

night yarrow
#

oh yeah, i can put it in the string

#

even without any method to consuming an item the entire stack is still consumed? how?

empty cairn
#

?

night yarrow
#

what does this line do?
equippable.setEquipment("Mainhand", itemStack)

empty cairn
#

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

night yarrow
#

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.

night yarrow
#

btw, how do i get a potion type of my itemStack? or any nbt data regarding the item whatsoever?

night yarrow
#

me a month and a half later: 🥲