#Item Holding

1 messages · Page 1 of 1 (latest)

vale hearth
#
import { world, system } from "@minecraft/server";

world.afterEvents.itemStartUse.subscribe((event) => {
    const player = event.source;

    if (event.itemStack.typeId === "voon:fire_boost") {
        const callbackId = system.runInterval(() => {
                player.runCommandAsync("function fireboost");
            }, 0);
    }
});

world.afterEvents.itemReleaseUse.subscribe((event) => {

    if (event.itemStack.typeId === "voon:fire_boost") {
        system.run(() => {
                system.clearRun(callbackId);
            });
    }
});```

I am trying to make a script that runs a boosting system constantly when you hold (i meant holding the use button, charging, whatever) the item, and stops when you release the button.

I am not sure how this really works, could anyone help?
vale hearth
#

oh

raven eagle
#

Use the itemUse for hold

#

itemReleaseUse for like ender pearl and etc

vale hearth
#

oh i see

#

yeah i could use itemUse but then how do i make the holding release system

charred kettle
#

itemReleaseUse would be better for a bow or trident

charred kettle
#

make the eating time impossible to reach

vale hearth
#

is there a way i would be able to disable the eating animation

vale hearth
charred kettle
#

just use the shooter component

vale hearth
#

the what

charred kettle
#

...

vale hearth
#

let me see how i can set that

charred kettle
#

just use itemUse

vale hearth
#

alr

charred kettle
#
"minecraft:shooter": {
    "charge_on_draw": true
},
#

and

#
"minecraft:use_modifiers": {
    "use_duration": 0.05
}
vale hearth
#

ah i see

#

let me try this out

charred kettle
#

use complete use

vale hearth
charred kettle
#

im not sure about this

vale hearth
#

idk let me try it

#

cuz ive also tried a component named minecraft:throwable but it didnt make my item chargeable

#

thats why there could be a way with the shooter one

willow monolith
#

Are you trying to make something like a trident?

charred kettle
#

I swear ive made a full automatic gun before

vale hearth
#

either from releasing the button or complete use

charred kettle
#

I think I used the foot component actually, however I couldnt get to stop making the burp sound

willow monolith
#

Should the item be fully charged or does the event stop triggering after you stop using the item?

charred kettle
willow monolith
#

That is, while you are using an event occurs

vale hearth
#

as long as the charging system is no longer on progress (usually from complete charging or cancellation) it stops the function

charred kettle
#

well, you could give the player a tag when it clicks the item, then remove it when you release use.

#

then check if the player has the tag in systemn run interval

#

or you could use dynamic properties instead of tags

charred kettle
#

yeah I have no ideas

willow monolith
#
let itemChargeMap = {}

world.afterEvents.itemStartUse.subscribe(data => {
  let player = data.source
  let item = data.itemStack
  if (item.typeId != "my_item") return
  itemChargeMap[player.name] = false
  
  let interval = system.runInterval(() => {
    if (itemChargeMap[player.name]) {
      system.clearRun(interval)
      return
    }

    // Code
  }, 1)
})

world.afterEvents.itemReleaseUse.subscribe(data => {
  if (data.itemStack.typeId == "my_item") itemChargeMap[data.source.name] = true
})
#

something like this

vale hearth
#

hmm

vale hearth
willow monolith
#

you definitely need to use use_duration

vale hearth
vale hearth
#

nvm ill just set it no matter what

willow monolith
#

I think 1.20.50 and higher (if you are on a version lower than 1.21.2 then just write “minecraft:use_duration” and do not change the format version)

vale hearth
#

alr, cuz the format version of the item was 1.20.80

willow monolith
#

....

#

then I don't know || (actually I know but I can't explain it well) ||

vale hearth
#

wait

#
let interval = system.runInterval(() => {
    if (itemChargeMap[player.name]) {
      system.clearRun(interval)
      return
    }

    player.runCommandAsync("function fireboost");
  }, 1)
})```

was "player" the correct term to execute the command
vale hearth
warped lodge
#
import { world, system } from "@minecraft/server";

let callbackId
world.afterEvents.itemStartUse.subscribe((event) => {
    const player = event.source;

    if (event.itemStack.typeId === "voon:fire_boost") {
       callbackId = system.runInterval(() => {
                player.runCommandAsync("function fireboost");
            }, 0);
    }
});

world.afterEvents.itemReleaseUse.subscribe((event) => {

    if (event.itemStack.typeId === "voon:fire_boost") {
        system.run(() => {
                system.clearRun(callbackId);
            });
    }
});
#

try this

#

note, this won't work right if multiple players is using the item. you might need to change to a different alternative

#

@vale hearth

vale hearth
warped lodge
#

no

#

just test it first if it will solve your problem

#

then if it is, then ill make you one that works for every players

vale hearth
#

got it

vale hearth
#

Now the question is if its because of the script, or the item components

#

wiki says startUse and releaseUse are used for items that has a charging component

warped lodge
#

can you show me where do you actually using it for?

vale hearth
#

sure

#
{
  "format_version": "1.20.80",
  "minecraft:item": {
    "description": {
      "identifier": "voon:fire_boost",
      "category": "nature"
    },
    "components": {
      "minecraft:hand_equipped": false,
      "minecraft:max_stack_size": 1,
      "minecraft:foil": false,
      "minecraft:allow_off_hand": false,
      "minecraft:icon": {
        "texture": "voon:fire_boost"
      },
      "minecraft:display_name": {
        "value": "Fire Boost"
      },
      "minecraft:shooter": {
        "charge_on_draw": true
      },
      "minecraft:use_modifiers": {
        "use_duration": 1.0
      },
      "tag:minecraft:transform_materials": {}
    }
  }
}```
#

dont pay attention to the transform material thing

warped lodge
#

is it a throwabble item? or a bow-like weapon?

vale hearth
#

its just a chargeable item

#

for the purpose of the script (executing fireboost constantly while holding the use button to charge it, and stop when the charging process is over, either by cancelling or complete charge)

vale hearth
#

i might also add an itemCompleteUse event that kills you if you hold until the process is finished

#

could be fun

warped lodge
#

@vale hearth

#

i think i solved it

vale hearth
#

hi srry i was eating

vale hearth
warped lodge
#

just make your item on format 1.20.20 then add these components

  "minecraft:shooter": {
      "max_draw_duration": 1000,
      "charge_on_draw": true
  },
  "minecraft:use_duration": 1000,
import { world } from "@minecraft/server";

world.afterEvents.itemStartUse.subscribe((e)=>{
  e.source.sendMessage("start use")
})

world.afterEvents.itemStopUse.subscribe((e)=> {
  e.source.sendMessage("end use")
})
vale hearth
warped lodge
#

oh wait i think it will work with 1.20.80 format too, just change the use_duration component with this:

"minecraft:use_modifiers": {
        "use_duration": 1000
      },
vale hearth
#

Yoo it worked

warped lodge
vale hearth
#

@warped lodge if you have time could you please try making this work for every player

#

otherwise thanks for helping

warped lodge
#

try this @vale hearth

let callbackIds = {}
world.afterEvents.itemStartUse.subscribe((event) => {
    const player = event.source;
    if (event.itemStack.typeId === "voon:fire_boost") {
      callbackIds[player.name] = system.runInterval(() => {
        player.runCommandAsync("function fireboost");
    }, 0);
    }
});

world.afterEvents.itemReleaseUse.subscribe((event) => {
  const player = event.source
    if (event.itemStack.typeId === "voon:fire_boost") {
      if (!(Object.keys(callbackIds).includes(player.name))) return;
        system.clearRun(callbackIds[player.name]);
        callbackIds[player.name] = undefined
    }
});
#

this should work

vale hearth
#

why not runCommandAsync

warped lodge
#

thats much better

vale hearth
#

i see

warped lodge
vale hearth
#

same dw

vale hearth
vale hearth
#

with this you dont need to set a tag for every single scripts using the same format

warped lodge
# vale hearth with this you dont need to set a tag for every single scripts using the same for...

if you use tags, it will be like:

world.afterEvents.itemStartUse.subscribe((event) => {
    const player = event.source;
    if (event.itemStack.typeId === "voon:fire_boost") {
      player.addTag("onBoost")
    }
});

world.afterEvents.itemReleaseUse.subscribe((event) => {
  const player = event.source
    if (event.itemStack.typeId === "voon:fire_boost") {
      player.removeTag("onBoost")
    }
});

and a mcfunction with tick.json

execute as @a[tag=onBoost] at @s run function fireboost

but there would be some problems, like players might be left with the tag without removing it if the world shutdown unexpectedly or something

vale hearth
#

like this player iD stuff

warped lodge
vale hearth
#

@warped lodge

function getRandomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}```

Will i need to configure this for every player IDs (or name) or not
warped lodge
tight heron
#

I'm also trying to make my custom staff right clickable

vale hearth
tight heron
#

don't have any idea what events to use though since it's been 2days since I started

vale hearth
#

If so i have a template for you

tight heron
tight heron
vale hearth
#

Yeah sure

tight heron
vale hearth
#

@tight heron

import { world, system } from "@minecraft/server";

world.afterEvents.itemUse.subscribe((event) => {
    const player = event.source;

    if (event.itemStack.typeId === "identifier:your_item") {
            system.run(() => {
                player.runCommandAsync("function spell");
            });
    }
});```
warped lodge
vale hearth
tight heron
#

Alright

vale hearth
#

Wait

#

Klein dont copy it yet i made a mistake

#

There

tight heron
#

okay

warped lodge
#

Example:

let callbackIds = {}
world.afterEvents.itemStartUse.subscribe((event) => {
    const player = event.source;
    if (event.itemStack.typeId === "voon:fire_boost") {
      callbackIds[player.name] = system.runInterval(() => {
        player.runCommandAsync("function fireboost");
        let randomNumber = getRandomNumber(0,10)
        player.sendMessage(`Your number is ${randomNumber}`)
    }, 0);
    }
});

world.afterEvents.itemReleaseUse.subscribe((event) => {
  const player = event.source
    if (event.itemStack.typeId === "voon:fire_boost") {
      if (!(Object.keys(callbackIds).includes(player.name))) return;
        system.clearRun(callbackIds[player.name]);
        callbackIds[player.name] = undefined
    }
});
vale hearth
vale hearth
warped lodge
tight heron
#

wait is this a command thingy?

vale hearth
#

idk whats the difference between "local" and global (which im assuming is the server) in mc

warped lodge
#

if your functions are outside of any events or any brackets, then it can be called anywhere

tight heron
#

I haven't learn anything about commands T_T

#

it might not work for me

vale hearth
tight heron
#

I guess I should start learning the commands tonight

warped lodge
vale hearth
#

oh

warped lodge
#

i can't explain it in detail, but you can watch javascript tutorials

tight heron
#
    const player = event.source;
    const item = event.itemStack;

    if (item.typeId === "kayden:BloodStaff") {
        world.sendMessage("BloodStaff Holded");
            system.run(() => {
                player.runCommandAsync("function lifestealarea");
                player.runCommandAsync("say HELLO FUNCTION");
            });
    }
});```

```{
    "format_version": "1.20.30",
    "minecraft:item": {
        "description": {
            "identifier": "kayden:BloodStaff",
            "menu_category": {
                "category": "equipment",
                "group": "itemGroup.name.sword"
            }
        },
        "components": {
            "minecraft:food": {
                "can_always_eat": true,
                "nutrition": 0
            },
            "minecraft:use_modifiers": {
                "use_duration": 0.05
            },
            "minecraft:cooldown": {
                "category": "attack",
                "duration": 50
            },
            "minecraft:icon": "bloodstaff",
            "minecraft:rarity":"rare",
            "minecraft:glint": true,
            "minecraft:max_stack_size": 1,
            "minecraft:durability": {
                "max_durability": 800
            },
            "minecraft:damage": {
                "value": 10
            },
            "minecraft:display_name": {
                "value": "Blood Staff"
            }
        }
    }
}```

do you guys know what's wrong with this? I'm not getting any errors but the function is not getting executed
#

here's the file structure

midnight anchor
#

why y all like complex things

#

just run the commands on the script