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?
#Item Holding
1 messages · Page 1 of 1 (latest)
wrong event
oh
itemUse would make more sense for ender pearl
itemReleaseUse would be better for a bow or trident
you will need to give your item the food component
make the eating time impossible to reach
is there a way i would be able to disable the eating animation
actually
so i can keep the itemStart and itemRelease right?
just use the shooter component
the what
...
let me see how i can set that
no
just use itemUse
alr
minecraft:shooter
"minecraft:shooter": {
"charge_on_draw": true
},
and
"minecraft:use_modifiers": {
"use_duration": 0.05
}
use complete use
thats when the item is done charging right?
im not sure about this
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
Are you trying to make something like a trident?
I swear ive made a full automatic gun before
No, im trying to make a chargeable item that constantly runs a command until it stops charging
either from releasing the button or complete use
I think I used the foot component actually, however I couldnt get to stop making the burp sound
Should the item be fully charged or does the event stop triggering after you stop using the item?
nvm, this isnt the same thing.
both
That is, while you are using an event occurs
as long as the charging system is no longer on progress (usually from complete charging or cancellation) it stops the function
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
could cause problems tho
yeah I have no ideas
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
hmm
Do i use the throwable or food component for the item?
you definitely need to use use_duration
oh
whats the required format version for this
nvm ill just set it no matter what
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)
alr, cuz the format version of the item was 1.20.80
both options didnt work
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
callbackId isn't a global
wdym
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
so i gotta make a copy of the item for it to work for another player?
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
got it
It doesnt seem to work
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
can you show me where do you actually using it for?
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
is it a throwabble item? or a bow-like weapon?
its not even a weapon
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)
oooohhh i see
i might also add an itemCompleteUse event that kills you if you hold until the process is finished
could be fun
hi srry i was eating
Yoo nice
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")
})
i had a guess it was a format version problem but didnt knew what
also remove the "minecraft:use_modifiers" you won't be needing it for this one
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
},
cool
@warped lodge if you have time could you please try making this work for every player
otherwise thanks for helping
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
that is so much to just make sure every player can use it without any mixing, thanks btw
why not runCommandAsync
or you could just add a tag to player tho, then run function fireboost if the player has that tag, which is runned by a mcfunction with tick.mcfunction
thats much better
i see
oh yeah right, mb, i always confused by those two
same dw
for now ill use how its indicated in the script
alr
with this you dont need to set a tag for every single scripts using the same format
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
One question, do i need to apply the same thing if it's a function
like this player iD stuff
i didn't understand
k ill just give u an example
@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
where are you using that for
hey bro can you send the full code for this?
I'm also trying to make my custom staff right clickable
i often use this in scriptevents
don't have any idea what events to use though since it's been 2days since I started
Right clickable once?
If so i have a template for you
Yess
REALLY?
Yeah sure
Thanks a lot
@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");
});
}
});```
you just need to put it one time
do not use identifier as it is, use the identifier of your item
Alright
okay
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
}
});
i dont think its needed to put the randomNumber function if (for example) the script runs a command that uses a scriptevent containing the function
Remember to import your scripts
im assuming you are talking about global functions
wait is this a command thingy?
i think
idk whats the difference between "local" and global (which im assuming is the server) in mc
if your functions are outside of any events or any brackets, then it can be called anywhere
dw i was just like you before
I guess I should start learning the commands tonight
i was talking about javascript
oh
i can't explain it in detail, but you can watch javascript tutorials
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
can i ask you WHY you re using scripts to run a function to run commands?
why y all like complex things
just run the commands on the script