#add cooldown to bottle item

61 messages · Page 1 of 1 (latest)

grave trellis
#

I have the following code, but the problem is it can be spammed infinitely, motivating the use of autoclickers. How would I add a cooldown to the bottle item?

ItemEvents.entityInteracted("minecraft:glass_bottle", event=>{
    if (event.target.type == "minecraft:interaction") {
        event.server.runCommandSilent(`execute in ${event.entity.level.dimension} positioned ${event.entity.x} ${event.entity.y} ${event.entity.z} run playsound minecraft:item.bottle.fill_dragonbreath master @a ~ ~ ~ 1 1`)
        event.item.count--
        event.player.give('minecraft:dragon_breath')
    }
})```
$aceplanteissue
karmic scrollBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

obtuse kelpBOT
#

You can write your code in a codeblock by typing it between the codeblock delimiters:
Note that these are backticks, not apostrophes

```js :arrow_left:

ServerEvents.recipes(event => {
event.smelting('minecraft:glass', '#forge:sand').xp(.1)
})

``` :arrow_left:

This example will look like this:

ServerEvents.recipes(event => {
  event.smelting('minecraft:glass', '#forge:sand').xp(.1)
})
carmine ore
#

you add item cooldowns with player.cooldowns.addCooldown() method taking in the item or item id and the cooldown length in ticks. I also took the liberty of organizing your code a bit more, destructuring variables at the top and I also converted your command to directly use minecraft's playSound method off of level. This will skip unnecessary parsing that comes from running minecraft commands which in turn will improve the performance.

ItemEvents.entityInteracted("minecraft:glass_bottle", event => {
    let { level, target, player, item, server, hand } = event
    if (target.type == "minecraft:interaction") {
        level.playSound(null, target.x, target.y, target.z, "minecraft:item.bottle.fill_dragonbreath", "master", 1, 1)
        player.cooldowns.addCooldown(item, 300)
        item.count--
        player.give('minecraft:dragon_breath')
    }
})```
@grave trellis
grave trellis
#

also i just realized im on 1.20.1, so perhaps thats the issue

carmine ore
#

yes, this is the 1.21 support channel though it should still work. do you have it in server scripts?

#

also i think you might need to elaborate on your exact end goal here, are you trying to stop people from filling up empty bottles from dragons breath by adding a cooldown or are you trying to prevent people from spamming dragon breath bottles from your own custom implementation you have from something else

grave trellis
#

Currently there is no cooldown so you can collect dragons breath infinitely fast, allowing autoclickers to be abused. This is a custom entity that gives dragons breath so it does not drain the way normal dragons breath does. So having it summon actual dragons breath or implementing a bottle cooldown so autoclickers do not get an unfair advantage would both work

carmine ore
#

i would honestly just do it the same way normal dragons breath works, after 6-7 fills it dissappears and is removed as an entity

grave trellis
#

sure, that would be fine. limited by uses rather than time? though i would also want it to fade if uncollected for enough time

carmine ore
#

ok, wheres your implementation of this interaction entity

#

like how are you putting this thing into the world

grave trellis
#

its an eidilon summoning ritual, which works fine

carmine ore
#

is there an entity tag that differentiates this entity from the rest?

grave trellis
#

and it does fade after a few seconds as desired, but becuase its a custom entity rather than actual dragons breath it doesnt decrease per use

#

no? its all interaction entities

carmine ore
#

ok so really we just need to worry about removing it after enough uses

grave trellis
carmine ore
#

but again you already have that handled so no need

#

so what i would do is use persistentData inside your right clicked event and increment it each time and then at a certain point you do js target.remove("discarded")

obtuse kelpBOT
#

What is persistent data and how to use it?
Persistent data is an object which exists on players, levels and servers!
It's super useful and easy to use!
Here's a simple example:

event.player.persistentData.myCoolNumber = 15
console.log(event.player.persistentData.myCoolNumber + 6) // 21
carmine ore
#

you increment this data with persistentData.Mytag++

grave trellis
#

and manually kill it when used enough times?

carmine ore
#

yup

grave trellis
#

ItemEvents.entityInteracted("minecraft:glass_bottle", event => {
let { level, target, player, item, server, hand } = event
if (target.type == "minecraft:interaction") {
level.playSound(null, target.x, target.y, target.z, "minecraft:item.bottle.fill_dragonbreath", "master", 1, 1)
player.cooldowns.addCooldown(item, 300)
item.count--
event.player.persistentData.BottlesCollected + 1
player.give('minecraft:dragon_breath')
if (target.persistentData.BottlesCollected = 8)
{
target.remove()
}
}
})

#

something like that?

#

and this hopefully works across all players, and the Bottlescollected tag is stored in the entity, not the players?

#

if so no cooldown is needed

obtuse kelpBOT
#

You can write your code in a codeblock by typing it between the codeblock delimiters:
Note that these are backticks, not apostrophes

```js :arrow_left:

ServerEvents.recipes(event => {
event.smelting('minecraft:glass', '#forge:sand').xp(.1)
})

``` :arrow_left:

This example will look like this:

ServerEvents.recipes(event => {
  event.smelting('minecraft:glass', '#forge:sand').xp(.1)
})
grave trellis
#
ItemEvents.entityInteracted("minecraft:glass_bottle", event => {
    let { level, target, player, item, server, hand } = event
    if (target.type == "minecraft:interaction") {
        level.playSound(null, target.x, target.y, target.z, "minecraft:item.bottle.fill_dragonbreath", "master", 1, 1)
        item.count--
        event.player.persistentData.BottlesCollected++
        player.give('minecraft:dragon_breath')
      if (target.persistentData.BottlesCollected = 8)
      {
        target.remove("discarded")
      }
    }
})


#

better?

carmine ore
#

youre very close

grave trellis
#

thats not what i meant

#

is all of what was provided before needed? i guess i should remove the addcooldown part

carmine ore
#

i know

#

and im fixing it for you so give me a second

grave trellis
#

much appreciated

carmine ore
grave trellis
#

ah right

carmine ore
grave trellis
#

i wasnt sure if the discarded part was necessary

#

guess it wouldnt be there if it wasnt

carmine ore
#

lol yup

#

lastly youll need to initiate the data beforehand to be able to increment it because without doing so it will be undefined and you cannot increment undefined. heres the revised code ```js
ItemEvents.entityInteracted("minecraft:glass_bottle", event => {
let { level, target, player, item, server, hand } = event
if (target.type == "minecraft:interaction") {
level.playSound(null, target.x, target.y, target.z, "minecraft:item.bottle.fill_dragonbreath", "master", 1, 1)

    item.count--
    player.give('minecraft:dragon_breath')
    if (target.persistentData.BottlesCollected == undefined) target.persistentData.BottlesCollected = 0
    target.persistentData.BottlesCollected++        
    if (target.persistentData.BottlesCollected >= 8) {
        target.remove("discarded")
    }
}

})```
if you have an issue with it still then please send your server logs

#

ah wait

#

for some reason your codes pdata was adding the data to the player which you should be adding to the target instead

#

i just updated it in the code i sent

grave trellis
#

so im currently in game, did reload, and it didnt kill the entity

#

ill try again

#

is it persistent across the server?

#

or just that entity

carmine ore
#

yup

grave trellis
#

what if there was multiple of this entity

carmine ore
#

it would store differently in each entity

grave trellis
#

okay fantastic

#

🎉

#

you are an amazing person

#

thank you random internet stranger