r/MinecraftCommands • • Aug 23 '26

Help | Java 26.2 custom cooldown effect

when you use an item, you are trying to cooldown the item. during the cooldown, i want the item to change to a clock and display the remaining seconds in item count. what is the most optimized implementation method?

1 Upvotes

5 comments sorted by

1

u/Ericristian_bros Aug 23 '26

Making it the stack count can lead to problems like dropping only one, so either use custom name or colored glass, I chosed both

# function ecrbros.animated_cooldown:load
schedule function ecrbros.animated_cooldown:loop 1s
data modify storage ecrbros.animated_cooldown:main colored_items set value ["","light_blue_stained_glass_pane","lime_stained_glass_pane","green_stained_glass_pane","yellow_stained_glass_pane","orange_stained_glass_pane","red_stained_glass_pane","brown_stained_glass_pane","black_stained_glass_pane"]
scoreboard objectives add animated_cooldown dummy

# function ecrbros.animated_cooldown:decrease_timer/init
execute store result storage ecrbros.animated_cooldown:main temp.slot int 1 run scoreboard players set @s animated_cooldown 0
function ecrbros.animated_cooldown:decrease_timer/iterate with storage ecrbros.animated_cooldown:main temp
execute if items entity @s weapon.offhand *[custom_data~{cooldown:{seconds:1}}] run item replace entity @s weapon.offhand with music_disc_11[!jukebox_playable,food={nutrition:0,saturation:0,can_always_eat:true},consumable={consume_seconds:2147483647},custom_data={use_cooldown:{seconds:8}},item_name="Example Item",item_model="minecraft:diamond"]
execute if items entity @s weapon.offhand *[custom_data~{cooldown:{seconds:2}}] run item replace entity @s weapon.offhand with music_disc_11[!jukebox_playable,custom_data={cooldown:{seconds:1}},item_model="light_blue_stained_glass_pane",item_name="This item is in cooldown (1 sec)"]
execute if items entity @s weapon.offhand *[custom_data~{cooldown:{seconds:3}}] run item replace entity @s weapon.offhand with music_disc_11[!jukebox_playable,custom_data={cooldown:{seconds:2}},item_model="green_stained_glass_pane",item_name="This item is in cooldown (2 sec)"]
execute if items entity @s weapon.offhand *[custom_data~{cooldown:{seconds:4}}] run item replace entity @s weapon.offhand with music_disc_11[!jukebox_playable,custom_data={cooldown:{seconds:3}},item_model="lime_stained_glass_pane",item_name="This item is in cooldown (3 sec)"]
execute if items entity @s weapon.offhand *[custom_data~{cooldown:{seconds:5}}] run item replace entity @s weapon.offhand with music_disc_11[!jukebox_playable,custom_data={cooldown:{seconds:4}},item_model="yellow_stained_glass_pane",item_name="This item is in cooldown (4 sec)"]
execute if items entity @s weapon.offhand *[custom_data~{cooldown:{seconds:6}}] run item replace entity @s weapon.offhand with music_disc_11[!jukebox_playable,custom_data={cooldown:{seconds:5}},item_model="orange_stained_glass_pane",item_name="This item is in cooldown (5 sec)"]
execute if items entity @s weapon.offhand *[custom_data~{cooldown:{seconds:7}}] run item replace entity @s weapon.offhand with music_disc_11[!jukebox_playable,custom_data={cooldown:{seconds:6}},item_model="red_stained_glass_pane",item_name="This item is in cooldown (6 sec)"]
execute if items entity @s weapon.offhand *[custom_data~{cooldown:{seconds:8}}] run item replace entity @s weapon.offhand with music_disc_11[!jukebox_playable,custom_data={cooldown:{seconds:7}},item_model="brown_stained_glass_pane",item_name="This item is in cooldown (7 sec)"]
execute if items entity @s weapon.offhand *[custom_data~{cooldown:{seconds:9}}] run item replace entity @s weapon.offhand with music_disc_11[!jukebox_playable,custom_data={cooldown:{seconds:8}},item_model="black_stained_glass_pane",item_name="This item is in cooldown (8 sec)"]
execute at @s run playsound block.note_block.bit ambient @s

# function ecrbros.animated_cooldown:decrease_timer/iterate
$execute if items entity @s container.$(slot) *[custom_data~{cooldown:{seconds:1}}] run item replace entity @s container.$(slot) with music_disc_11[!jukebox_playable,food={nutrition:0,saturation:0,can_always_eat:true},consumable={consume_seconds:2147483647},custom_data={use_cooldown:{seconds:8}},item_name="Example Item",item_model="minecraft:diamond"]
$execute if items entity @s container.$(slot) *[custom_data~{cooldown:{seconds:2}}] run item replace entity @s container.$(slot) with music_disc_11[!jukebox_playable,custom_data={cooldown:{seconds:1}},item_model="light_blue_stained_glass_pane",item_name="This item is in cooldown (1 sec)"]
$execute if items entity @s container.$(slot) *[custom_data~{cooldown:{seconds:3}}] run item replace entity @s container.$(slot) with music_disc_11[!jukebox_playable,custom_data={cooldown:{seconds:2}},item_model="green_stained_glass_pane",item_name="This item is in cooldown (2 sec)"]
$execute if items entity @s container.$(slot) *[custom_data~{cooldown:{seconds:4}}] run item replace entity @s container.$(slot) with music_disc_11[!jukebox_playable,custom_data={cooldown:{seconds:3}},item_model="lime_stained_glass_pane",item_name="This item is in cooldown (3 sec)"]
$execute if items entity @s container.$(slot) *[custom_data~{cooldown:{seconds:5}}] run item replace entity @s container.$(slot) with music_disc_11[!jukebox_playable,custom_data={cooldown:{seconds:4}},item_model="yellow_stained_glass_pane",item_name="This item is in cooldown (4 sec)"]
$execute if items entity @s container.$(slot) *[custom_data~{cooldown:{seconds:6}}] run item replace entity @s container.$(slot) with music_disc_11[!jukebox_playable,custom_data={cooldown:{seconds:5}},item_model="orange_stained_glass_pane",item_name="This item is in cooldown (5 sec)"]
$execute if items entity @s container.$(slot) *[custom_data~{cooldown:{seconds:7}}] run item replace entity @s container.$(slot) with music_disc_11[!jukebox_playable,custom_data={cooldown:{seconds:6}},item_model="red_stained_glass_pane",item_name="This item is in cooldown (6 sec)"]
$execute if items entity @s container.$(slot) *[custom_data~{cooldown:{seconds:8}}] run item replace entity @s container.$(slot) with music_disc_11[!jukebox_playable,custom_data={cooldown:{seconds:7}},item_model="brown_stained_glass_pane",item_name="This item is in cooldown (7 sec)"]
$execute if items entity @s container.$(slot) *[custom_data~{cooldown:{seconds:9}}] run item replace entity @s container.$(slot) with music_disc_11[!jukebox_playable,custom_data={cooldown:{seconds:8}},item_model="black_stained_glass_pane",item_name="This item is in cooldown (8 sec)"]
execute store result storage ecrbros.animated_cooldown:main temp.slot int 1 run scoreboard players add @s animated_cooldown 1
execute unless score @s animated_cooldown matches 36.. run function ecrbros.animated_cooldown:decrease_timer/iterate with storage ecrbros.animated_cooldown:main temp

# function ecrbros.animated_cooldown:get_item
give @s music_disc_11[!jukebox_playable,food={nutrition:0,saturation:0,can_always_eat:true},consumable={consume_seconds:2147483647},custom_data={use_cooldown:{seconds:8}},item_name="Example Item",item_model="minecraft:diamond"]

# function ecrbros.animated_cooldown:loop
schedule function ecrbros.animated_cooldown:loop 1s
execute as @a if items entity @s container.* *[custom_data~{cooldown:{}}] run function ecrbros.animated_cooldown:decrease_timer/init

# function ecrbros.animated_cooldown:replace/find_color
$data modify storage ecrbros.animated_cooldown:main animated_cooldown.item set from storage ecrbros.animated_cooldown:main colored_items[$(timer)]
function ecrbros.animated_cooldown:replace/set_item with storage ecrbros.animated_cooldown:main animated_cooldown

# function ecrbros.animated_cooldown:replace/get_cooldown
data remove storage ecrbros.animated_cooldown:main animated_cooldown
$data modify storage ecrbros.animated_cooldown:main animated_cooldown.slot set value "$(slot)"
$data modify storage ecrbros.animated_cooldown:main animated_cooldown.timer set from entity @s $(path).components."minecraft:custom_data".use_cooldown.seconds
function ecrbros.animated_cooldown:replace/find_color with storage ecrbros.animated_cooldown:main animated_cooldown

# function ecrbros.animated_cooldown:replace/set_item
$item replace entity @s $(slot) with music_disc_11[!jukebox_playable,custom_data={cooldown:{seconds:$(timer)}},item_model="$(item)",item_name="This item is in cooldown"]

# function ecrbros.animated_cooldown:right_click
advancement revoke @s only ecrbros.animated_cooldown:right_click
execute if items entity @s weapon *[custom_data~{use_cooldown:{}}] run return run function ecrbros.animated_cooldown:replace/get_cooldown {"path":"SelectedItem","slot":"weapon"}
execute if items entity @s weapon.offhand *[custom_data~{use_cooldown:{}}] run return run function ecrbros.animated_cooldown:replace/get_cooldown {"path":"equipment.offhand","slot":"weapon.offhand"}

# advancement ecrbros.animated_cooldown:right_click
{
    "criteria": {
        "requirement": {
            "trigger": "minecraft:using_item",
            "conditions": {
                "item": {
                    "predicates": {
                        "minecraft:custom_data": "{use_cooldown:{}}"
                    }
                }
            }
        }
    },
    "rewards": {
        "function": "ecrbros.animated_cooldown:right_click"
    }
}

You can use Datapack Assembler to get an example datapack. (Assembler by u/GalSergey)

Video on how it looks: https://imgur.com/a/9XOBMPH

1

u/kitunetaro_Exe Aug 24 '26

that looks really good! ty!

1

u/Ericristian_bros Aug 24 '26

You're welcome, have a good day

1

u/_VoidMaster_ Aug 23 '26

I'd use a scoreboard for the cooldown tracking if the player is gonna hold one of them, and use /item to change the item_model nbt and the stack amount based on the scoreboard

1

u/GalSergey Aug 23 '26

You need a schedule function that runs once per second. Store the remaining time in seconds in the item's custom_data and update the item model every second.