KubeJS

KubeJS

69M Downloads

Additional per item/block functions: block break and item right click

Raidobw2 opened this issue ยท 2 comments

commented

Describe the feature

If one wants to control what happens when a specific block is broken or when an item is used (right click), they can use global events. These events however get fired for anything block broken or item used.

The food item uses a eaten function which is bound to the use of the item, instead of being global.
In the following, once the player eats that specific food they get told they're awesome by some function:

event.create('kubejsadditions:bestfoodever', item => {
            item.food( (food) => {
                food.hunger(1);
                food.saturation(1);
                //Pass some information for further processing to some function
                food.eaten(event => global.tellAPlayerTheyreAwesome(event.player.getName().getString()));
            });
        })

For block break and item right click events, that is however not possible per block/item, so you would need to check what has been broken/used and have to return in some cases if the block/item is not the one you are looking for.

onEvent("block.break", event => {
  let brokenBlock = event.block
  if (brokenBlock.id == "minecraft:oak_planks") {
    //something
  }
})

onEvent("item.right_click", (event) => {
    if (event.item.hasTag("minecraft:boats")) {
        event.cancel()
    }
})

I'm not sure how much this would help with things (performance wise) or require a lot of code changes. I think that my suggestion would mostly help organizing code and making it on par with the behavior that foodBuilder has with that eaten function. Thanks for reading!

Additional info

I think this can be considered for code organization mainly. Possibly performance if block.break is called everytime a fake player such as the RFTools Builder breaks a block too. However, I'm not sure about that last statement, I don't know enough about how that works.

Alternatives are using the global events that currently function fine, using some if blocks or similar to handle logic.

commented

KubeJS would have to do the checking for different blocks and items anyway, there wouldn't be any noticable performance difference.

If you want to you can do a system like this with this code in KubeJS:

let rightClicks = {}
const registerRightClick = (item, handler) => {
  if (rightClicks[item] != null) {
    console.error(`Item ${item} already has a right click action registered! Overwriting`)
  }
  rightClicks[item] = handler
}
onEvent('item.right_click', event => {
  if (rightClicks[`${event.item.id}`] != null) {
    rightClicks[`${event.item.id}`](event)
  }
})

registerRightClick('minecraft:oak_boat', event => {
  event.server.tell(`Whatever floats your boat ${event.player}`)
}

You could also easily adapt this to tags, or both (detect if item starts with a #,if so register tag check, then check both for a tag and for an item id in the event)

commented

Sorry for the late reply, I only saw this now. That definitely sounds cool written like that. Thanks for explaining a clean solution with that code snippet!

Also, mini typo if you want :)
image