#Command repeating several times

1 messages · Page 1 of 1 (latest)

open kindle
#

I have a problem that when I click on an item it executes the command about 2, 3 or 4 times, but I only want it to execute it once.

How do I do it? Can someone help me?

crimson monolith
#

This is not a command question. The issue is click events happen as long as a button is pressed, so you just need to make sure it’s only the first button press if(eventData. isFirstEvent) that activates your code.

open kindle
#

How do I put this?

#

What do I have to put? For it to work

crimson monolith
#

It’s an if statement, put it around any code that you are running with brackets {} also you will need to change the name to use "data" instead of "eventData"

open kindle
#

Data.isFirtEvent Where is this command? Can you give me an example? I'm trying but I can't figure it out.

#

😦

#

Is it in player.runCommandAsync? Or in the execute command?

#

@crimson monolith

crimson monolith
#

It’s not a command it’s a property of some events. https://learn.microsoft.com/en-us/minecraft/creator/scriptapi/minecraft/server/itemuseonafterevent?view=minecraft-bedrock-stable
if(data.isFirstEvent) {world.sendMessage("player has clicked")}, it goes into your event code and you can add the test condition wherever you want. If you still can’t figure it out I can give you a concrete example if you provide your code.

Contents of the @minecraft/server.ItemUseOnAfterEvent class.

open kindle
#

import { world } from "@minecraft/server";
world.beforeEvents.itemUseOn.subscribe((data) => {
const item = data.itemStack;
const block = data.block;
const loc = block.location;
const player = data.source;

if (item.typeId == "forbidden:mundabitur_dust" && block.typeId == "minecraft:smithing_table") {
player.runCommandAsync(execute positioned ${loc.x} ${loc.y} ${loc.z} run function iniciar_forja);
}
else if (item.typeId == "forbidden:soul_extractor" && block.typeId == "minecraft:soul_sand")
if(data.isFirstEvent){
player.runCommandAsync(execute positioned ${loc.x} ${loc.y} ${loc.z} run summon pig ~ ~ ~);
player.startItemCooldown("soul_extractor", 20);
}
});

open kindle
#

The code is like this when I click on the item in the block it has to execute the command only once when I click

crimson monolith
#

it does; this bit of code only activates once. JS if(data.isFirstEvent){ player.runCommandAsync(`execute positioned ${loc.x} ${loc.y} ${loc.z} run summon pig ~ ~ ~`); player.startItemCooldown("soul_extractor", 20); }

if you want your other command to activate, once you just do the same thing

if(data.isFirstEvent){
player.runCommandAsync(`execute positioned ${loc.x} ${loc.y} ${loc.z} run function iniciar_forja`);
}
open kindle
#

@crimson monolith Thanks, but just one question, how do I make it detect the item? Because I want it to be a specific item in a specific block

crimson monolith
#

Not completely sure what you mean, please explain in more details. You want to check for a specific item in side of the block that you click? Also I thought you are using a smithing table and soul sand so those doesn’t have an inventory’s?

open kindle
#

Well, I'll explain, I want that when I click on the "forbidden:mundabitur" dust on a "smithing_table" The command is executed once the function "iniciar_forja"

#

Likewise when I click with the item "Forbidden:soul_extraxtor" on a soul sand it executes the setblock command

#

@crimson monolith

#

I am using Google translate sorry if it is written

crimson monolith
#

Google translate is fine, I just need several details to understand what you want to do.

Does your code currently work? And run the command?

If not you may need a custom component to do what you would like to do.

open kindle
#

Well I want that when I click with the item "mundabitur dust" on a "smithing table" ele execute a function iniciar forja

#

But it's not working

crimson monolith
#

Put into item_use.js

import { world } from "@minecraft/server";
world.beforeEvents.worldInitialize.subscribe((eventData) => {
    eventData.itemComponentRegistry.registerCustomComponent("forbidden:mundabitur_dust", {
        onUseOn() { }
    })
    eventData.itemComponentRegistry.registerCustomComponent("forbidden:soul_extractor", {
        onUseOn() { }
    })
})

world.afterEvents.itemUseOn.subscribe((data) => {
    const item = data.itemStack;
    const block = data.block;
    const loc = block.location;
    const player = data.source;

    if (item.typeId == "forbidden:mundabitur_dust" && block.typeId == "minecraft:smithing_table") {
        if (data.isFirstEvent) {
            player.runCommandAsync(`execute positioned ${loc.x} ${loc.y} ${loc.z} run function iniciar_forja`);
        }
    }
    else if (item.typeId == "forbidden:soul_extractor" && block.typeId == "minecraft:soul_sand")
        if (data.isFirstEvent) {
            player.runCommandAsync(`execute positioned ${loc.x} ${loc.y} ${loc.z} run summon pig ~ ~ ~`);
            player.startItemCooldown("soul_extractor", 20);
        }
});

Put into item.json files:

"minecraft:custom_components": [
                "forbidden:soul_extractor"
            ],```
```JSON
            "minecraft:custom_components": [
                "forbidden:mundabitur_dust"
            ],

For example:

        "components": {
            "minecraft:custom_components": [
                "forbidden:mundabitur_dust"
            ]
        }

and use ```JSON
"format_version": "1.21.50",


This will fix any issues that you are having.
open kindle
#

@crimson monolith THANKS

#

😁😁😁😁😁😁😁😁

#

🙏❤️Thank you very much, you helped me a lot 🥹

open kindle
#

Just one more question, if it doesn't work, no problem 😁

#

Can you set a time so that when I click on an item after about 10 seconds I can click again?

#

Coowdown

crimson monolith
#

Yes, player.startItemCooldown("soul_extractor", 20); already does that. Are you talking about a different type of cooldown? And for just 1 item or all of the same item?

open kindle
#

Just the item I set up

#

Ok thank you very much for the help 🙏

open kindle
#

@crimson monolith hey

#

Just one question and I'm trying to add more items with this code function but it doesn't work.

#

How do I get more items with this code?

#

And where do I put the cooldown and that's not it?

crimson monolith
#

Add the "custom_component" to your other items, you can just reuse the already made name forbidden:soul_extractor and then just use another else if statement.

And yes that is a cooldown, which activates for 20 ticks or 1 second every time that you use your item.
The reason that it might not be working is did you add a cooldown component into your items.json file?

open kindle
#

No

crimson monolith
open kindle
#

import { world } from "@minecraft/server";
world.beforeEvents.worldInitialize.subscribe((eventData) => {
eventData.itemComponentRegistry.registerCustomComponent("forbidden:mundabitur_dust", {
onUseOn() { }
})
eventData.itemComponentRegistry.registerCustomComponent("forbidden:edelwood_bottle", {
onUseOn() { }
})
eventData.itemComponentRegistry.registerCustomComponent("forbidden:soul_extractor", {
onUseOn() { }
})
})
world.afterEvents.itemUseOn.subscribe((data) => {
const item = data.itemStack;
const block = data.block;
const loc = block.location;
const player = data.source;

if (item.typeId == "forbidden:mundabitur_dust" && block.typeId == "minecraft:smithing_table") {
    if (data.isFirstEvent) {
        player.runCommandAsync(`execute positioned ${loc.x} ${loc.y} ${loc.z} run function iniciar_forja`);
    }
}

else if (item.typeId == "forbidden:soul_extractor"
&& block.typeId == "minecraft:soul_sand")
{
if (data.isFirstEvent) {
player.runCommandAsync(execute positioned ${loc.x} ${loc.y} ${loc.z} run function soul_block);
player.startItemCooldown("soul_extractor",20);
}
}
else if (item.typeId == "forbidden:edelwood_bottle"
&& block.typeId == "forbidden:edelwood_log_oil")
{
if (data.isFirstEvent) {
player.runCommandAsync(execute positioned ${loc.x} ${loc.y} ${loc.z} run function edelwood_log_block);
}
}
});

#

I'm trying but it's not just the "mundabitur dust" and the "Soul extractor" that I finance.

#

@crimson monolith

open kindle
#

It's not working with vanilla items, I don't know if I'm doing it right

crimson monolith
#

I mean if you have the code for your items and js file send the files and I can look at them and try to set it up for what you are wanting to do.

open kindle
#

Can I send you the script index code along with the code for all the items I want to work?

#

Because for me it's happening like this every time I click with mundabitur_dust and soul_extractor It works but every time I try to add a new item the code stops working.

#

I have already placed the custom components in the item but it doesn't work.

#

I want that when I click with the Edelwood_bottle in the Edelwood_log_oil block it executes the function Edelwood_block.mcfunct

#

Exactly like the other 2 items

crimson monolith
#

Learn javascript https://www.w3schools.com/js/ If you don't learn, you will never be able to make anything on your own.

You have to code each behavior in. That is what the script is doing; if you don't add new code, it won't do anything. I just used custom components as a way to get your deseried behavior.

main.js

import { world } from "@minecraft/server";
world.beforeEvents.worldInitialize.subscribe((eventData) => {
    eventData.itemComponentRegistry.registerCustomComponent("forbidden:force_itemuseon", {
        onUseOn() { }
    })
})
world.afterEvents.itemUseOn.subscribe((data) => {
    const item = data.itemStack;
    const block = data.block;
    const loc = block.location;
    const player = data.source;

    if (!data.isFirstEvent) return

    if (item.typeId == "forbidden:mundabitur_dust" && block.typeId == "minecraft:smithing_table") {
        player.runCommandAsync(`execute positioned ${loc.x} ${loc.y} ${loc.z} run function iniciar_forja`);
    }
    else if (item.typeId == "forbidden:edelwood_bottle" && block.typeId == "forbidden:edelwood_log_oil") {
        player.runCommandAsync(`execute positioned ${loc.x} ${loc.y} ${loc.z} run function edelwood_block`);
    }
    else if (item.typeId == "forbidden:soul_extractor" && block.typeId == "minecraft:soul_sand") {
        player.runCommandAsync(`execute positioned ${loc.x} ${loc.y} ${loc.z} run function soul_block`);
        player.startItemCooldown("soul_extractor", 100);
    }
});

And change every item's custom component to.

            "minecraft:custom_components": [
                "forbidden:force_itemuseon"
            ],
open kindle
#

@crimson monolith

#

Perfect function

#

Just one question, do I have to put a custom component in the Addon blocks?

#

Why does it only work with vanilla blocks?

#

That's all that's left to finish

#

And there are some Addon blocks that need this code but it doesn't work with custom blocks.

#

Do I need to put a custom component? In the block code?

open kindle
#

Sorry if I'm bothering you