KubeJS

KubeJS

61M Downloads

Entity creation duplicates entities - 1.18.1

TheQKnight opened this issue ยท 2 comments

commented

I have a script set up as follows:

onEvent('block.right_click', (event) => {
  if (event.level.dimension !== 'minecraft:overworld') return
  var zombie = event.block.createEntity('minecraft:zombie')
  zombie.tags.add('special_zombie')
  zombie.spawn()
  var entities = event.server.getEntities(`@e[type=zombie,tag=special_zombie]`)
  console.log(entities.length)
})

So basically, this should run once when you click a block, spawning a zombie, then the console.log should print 1 since only one zombie spawned with the tag, but this does not happen. Instead 2 zombies spawn, then the console will log 3 and 6 consecutively.

commented

This is for 1.18, I have not checked on other versions.

I'm not sure about the 3 and 6 part, but the reason it spawns two might have to do with the fact that block.right_click triggers upon right clicking the block and then again when you release.

The method I use to get around (probably not the best) is to use the following:

onEvent('block.right_click', (event) => {
     if event.getFacing() == 'down'
	   return
    //code here
})

Which will remove the trigger on the release, but also remove the click of the underside of blocks.

commented

Specifically, the event is firing for both the main-hand and off-hand.

Adding a check for which hand (event.hand), and adding a check for a specific item (event.item) should prevent this from happening.

Questions, or confirmations of topics like this can be more-easily answered in the Discord: https://discord.gg/bPFfH6P