#Run JS function on specific item use (right click)
1 messages · Page 1 of 1 (latest)
world.afterEvents.itemUse.subscribe((data) => {
if (data.itemStack.typeId == "minecraft:stick") {
data.source.sendMessage("Hello World!");
};
});
and for the command thing
- Command:
/scriptevent test:say hello
system.afterEvents.scriptEventReceive.subscribe((data) => {
if (data.id == "test:say") {
world.sendMessage(data.message);
};
});
Stunning, thanks!
One more thing, I found block placement with JS quite confusing in the docs. Do you have example for that? 🙏
//Gets the overworld dimension
let overworld = world.getDimension("minecraft:overworld");
//Gets the block at X: 1 / Y: 2 / Z: 3 from the overworld
let block = overworld.getBlock({ x: 1, y: 2, z: 3 });
//Sets the block to "minecraft:bedrock"
block.setType("minecraft:bedrock");
I would run a command from JS, but docs say that it's better to run native methods.
Perfect! Tyvm. 💙
Is it possible to use relative coordinates here? I need to place some blocks to the place, where player right clicks with custom item in hand.
If you’re trying to set a block where the pkayer is interacting then you can probably use this
world.afterEvents.playerInteractWithBlock.subscribe((data) => {
if (data.itemStack && data.itemStack.typeId == "minecraft:stick") {
data.block.setType("minecraft:bedrock");
};
});
Awesome! And there currently isn't native method for spawning in structures, right?
So, I have to call structure command instead.
Yeah, there isn’t one yet
It doesn't work as expected the data are apparently undefined.
[Scripting][error]-Plugin [Nogard BP - 1.0.0] - [main.js] ran with error: [TypeError: cannot read property 'subscribe' of undefined at <anonymous> (main.js:18)
]
Which version of the module are you using?
Stable.
{
"module_name": "@minecraft/server",
"version": "1.7.0"
}
Ah, that's why, the PlayerInteractWithBlock is only in beta currently but we might be able to use the ItemUseOn event
But will we be able to tell which block player is facing, when using the item?
I just realised that this might not be possible in stable sadly
the ItemUseOn event only gets triggered when the item gets used on a block
So with ItemUseOn we have to specify the block?
Anyway, hmmm, so, it will work in 1.8.0-beta, right?
Okay, on my way to try it.
Okay, that's cool, it replaces the actual block.
Would it be possible to get location of the block player clicked at?
It is possible
data.block.location
This returns vector?
it returns Vector3
player.runCommand(`/structure load nogard:start ${data.block.location.x} ${data.block.location.y} ${data.block.location.z} 0_degrees`);