#Right click event to spawn item at clicked location

76 messages · Page 1 of 1 (latest)

nimble belfry
#

Trying to make a script that spawns in flint shards when a player is holding a piece of flint and clicks a stone block, but I can't seem to get the shards to spawn at all, this is my code right now:

ItemEvents.firstRightClicked("minecraft:flint", e => {
    let {player, target, item} = e
    if(target.block.id != 'minecraft:stone') return
    item.shrink(1)
    target.spawnAtLocation('blonded:flint_shard')

    let {server, level} = player
    let bpos = player.blockPosition()
    level.players.forEach(lvlplayer => {
        if(Math.sqrt(player.distanceToSqr(lvlplayer)) <= 10)
            server.runCommandSilent(`playsound item.armor.equip_iron block ${lvlplayer.username} ${bpos.x} ${bpos.y} ${bpos.z} 1 1`)
    })
})```

Which gives this error:
`[10:03:20] [Server thread/ERROR] [KubeJS Server/]: blonded/flint_knapping.js#4: Error in 'ItemEvents.firstRightClicked': TypeError: Cannot find function spawnAtLocation in object dev.latvian.mods.kubejs.entity.RayTraceResultJS@21a93d8f.`
So I can't use target.spawnAtLocation which I found in an old post, but what can I use in place of that?
tall forumBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

nimble belfry
#

but its saying block is not defined now

#

i guess because this is an item event and not a block event?

#

where I'm at now:

ItemEvents.firstRightClicked("minecraft:flint", e => {
    let {player, target, item} = e
    if(target.block.id != 'minecraft:stone') return
    item.shrink(1)
    block.popItemFromFace("blonded:flint_shard", "up")

    let {server, level} = player
    let bpos = player.blockPosition()
    level.players.forEach(lvlplayer => {
        if(Math.sqrt(player.distanceToSqr(lvlplayer)) <= 10)
            server.runCommandSilent(`playsound item.armor.equip_iron block ${lvlplayer.username} ${bpos.x} ${bpos.y} ${bpos.z} 1 1`)
    })
})```
#

ok after finding this https://github.com/KubeJS-Mods/KubeJS/discussions/714 i can now get my flint shards to pop out of blocks, but i don't know how to incorperate it into my "if player is holding flint" and "if player is looking at stone"

BlockEvents.rightClicked('minecraft:stone', event => {
  // pop an item into the world
  event.block.popItemFromFace('blonded:flint_shard', event.facing)
  // or even have a percent chance to drop it/another one (this is 25%)
})```
GitHub

Like right click X block and get 4 things?

#

https://www.mcmod.cn/post/4580.html ok after finding this post, i'm like 90% there

  let block = event.block
  if (block.hasTag("forge:stones")) {
    let player = event.player
    if(player.getMainHandItem() == Item.of("minecraft:flint")) {
      let loot = [
        "blonded:flint_shard"
      ]
      let random = Math.floor(Math.random() * 2 * loot.length)
      let dropItem = Item.of(loot[random])
      block.popItemFromFace(dropItem,event.facing)
      player.swingArm(event.hand)
    }
  }
})```
Only problem with this is it gives an error saying it can't find function swingArm
#

so seems like maybe the player.swingArm is for 1.18 only

#

is there a replacement in 1.20.1

#

oh also dont know how to make the stack their holding shrink now with this

near birch
# nimble belfry oh also dont know how to make the stack their holding shrink now with this

try something like this, this I copied over from one of my rlcraft renewed pack im working on that replicates the old NoTreePunching knapping logic ```js
BlockEvents.rightClicked(event => {
let { player, block, server } = event
let item = player.mainHandItem
let air = player.mainHandItem.id == 'minecraft:air'
if (
(event.hand == 'OFF_HAND' ||
!block.hasTag('minecraft:base_stone_overworld') ||
air ||
item.count <= 0 ||
item.id != 'minecraft:flint')
) return
player.swing()
if (Math.random() >= 0.5) {
item.count--
block.popItemFromFace('2x kubejs:flint_shard', "up")
}
})

fleet plazaBOT
#

[➤](#1364754026926968882 message)
the eat sound should be playable like this as well off leveljs level.playSound(null, entity.x, entity.y, entity.z, "minecraft:entity.generic.eat", "players", 0.5, 0.9)

nimble belfry
#

this is perfect thanks, where do i add the sound effect now?

#

oh

near birch
#

yeah, wherever you swing your hand basically

near birch
# fleet plaza

also if you copy this dont forget to define both entity and level correctly

nimble belfry
#

how would I define level? and do i have my let entity = player.blockPosition()?

nimble belfry
#

the (Math.random() >= 0.5) { is making it a 50/50 chance correct

near birch
# nimble belfry also this is giving me a syntax thing where "server" is never used should I just...

this is not a syntax error, that is vsc saying you dont use it anywhere and it will not error if you leave it though yes you can remove it. also what i meant by defining level and entity is in javascript you need to define variables for them to be used. js let entity = event.entity let level = event.level
ect.
as for the last question Math.random() will return a floating point number between 0 and 1, checking if it's greater than or equal to 0.5 would indeed give it a 50 percent chance of success or failure

nimble belfry
#

awesome tysm for the help

#

everything working now

#

final code:

BlockEvents.rightClicked(event => {
    let { player, block, server } = event
    let item = player.mainHandItem
    let air = player.mainHandItem.id == 'minecraft:air'
    if (
        (event.hand == 'OFF_HAND' ||
            !block.hasTag('minecraft:base_stone_overworld') ||
            air ||
            item.count <= 0 ||
            item.id != 'minecraft:flint')
    ) return
    player.swing()
    if (Math.random() >= 0.5) {
        item.count--
        block.popItemFromFace('2x blonded:flint_shard', "up")
    }


    let entity = event.entity
    let level = event.level
    level.playSound(null, entity.x, entity.y, entity.z, "minecraft:entity.generic.eat", "players", 0.5, 0.9)
})```
#

will make it not play an eating sound later lol

nimble belfry
#

@near birch am I able to define a sound event not listed in minecrafts sounds.json?

near birch
#

anything that is available to you via the /playsound command yes

#

as long as its registered as an actual sound event

nimble belfry
#
BlockEvents.rightClicked(event => {
    let { player, block, server } = event
    let item = player.mainHandItem
    let air = player.mainHandItem.id == 'minecraft:air'
    if (
        (event.hand == 'OFF_HAND' ||
            !block.hasTag('minecraft:base_stone_overworld') ||
            air ||
            item.count <= 0 ||
            item.id != 'minecraft:flint')
    ) return
    player.swing()
    if (Math.random() >= 0.5) {
        item.count--
        block.popItemFromFace('2x blonded:flint_shard', "up")
    }


    let entity = event.entity
    let level = event.level
    level.playSound(null, entity.x, entity.y, entity.z, "blonded:block.knapping", "players", 0.5, 0.9)
})```
#

its giving a crazy long error when i do this

#

i can /playsound blonded:knapping fine

fleet plazaBOT
#

Never say 'it crashed', 'it errored', or 'it didn't work' without providing the full scripts and log/crash report!

nimble belfry
fleet plazaBOT
#

Paste version of message.txt from @nimble belfry

nimble belfry
#

this is my sounds.json file:

  "knapping": {
    "category": "block",
    "subtitle": "blonded.subtitle.knapping",
    "sounds": [
      "blonded:knapping/1",
      "blonded:knapping/2",
      "blonded:knapping/3",
      "blonded:knapping/4"
    ]
  }
}```
near birch
#

it wouldnt be block.knapping

nimble belfry
#

🧐

near birch
#

try blonded:knapping

#

cause i believe it goes off this js "knapping": {

nimble belfry
#

hm same crazy long error

near birch
#

ok, whats your file path for this?

#

and do you have a startup registry?

nimble belfry
#

Blonded\minecraft\kubejs\assets\blonded\sounds\knapping

#

oh

near birch
#

you need a startup registry for it to be registered as a sound event

#
StartupEvents.registry('sound_event', event => {
event.create('minecraft:dragonfire_breath')
})```
nimble belfry
#

ohhhhh

#

thank you

near birch
#

wait sec

#

there

nimble belfry
#

for some reason just thought i would be able to load them in as resource packs

near birch
#

you can

#

it just wont be available to the java/server side

#

only to /playsound command

nimble belfry
#

ah i see

#

so it would be:

StartupEvents.registry('knapping', event => {
event.create('minecraft:knapping')
})```
near birch
#

no

nimble belfry
#

oh yeah

#
StartupEvents.registry('knapping', event => {
event.create('blonded:knapping')
})```
near birch
#

closer but still no

nimble belfry
#

or do i need the ID on the first row too

near birch
#

"knapping" is not a registry

nimble belfry
#

oh

#

BLOCK

near birch
#

nope

#

what did my original message have there

nimble belfry
#

oh lmfao

#

im overcomplicating

near birch
#

lmao all g

nimble belfry
#
StartupEvents.registry('sound_event', event => {
event.create('blonded:knapping')
})```
near birch
#

but yeah try that out and lmk

#

yup

nimble belfry