Skip to content

Translation notice

This page was translated with machine translation and may contain inaccuracies. If you can help improve it, please open an issue or submit a pull request.

Dynamic cooling based on vanilla cooling components

by CR_019
This article was also published in Redstone Relay and BiliBili column

This issue is short, so it’s a little trick.

use_cooldownIt is a component added in 1.21.2. It allows the item to enter a period of cooldown after being consumed by a consumable component, just like ender pearls. There can be obvious hints in visual effects. However, its behavior is rather old-fashioned and not particularly easy to use: This component can only specify a fixed cooling time and cannot dynamically change it, nor can it reduce the cooling time after triggering the cooling time. However, combined with macros, we can achieve dynamic cooling time to a certain extent. Let’s look at this example:

Question background

A certain item has a right-click skill (implemented using consumable components), which has two times of use and independent cooling. If we need to use the vanilla cooling component to achieve the cooling effect, we need to let the item enter cooling after the second use of the item. The cooling time is the remaining cooling time from the first use to now.

Timing calculation

We use two timer scoreboards to calculate the cooldown time between uses; Set up a third scoreboard to combine the final cooling time of the two timers, and then replace this value with the item's cooling component. According to the general idea, this comprehensive scoreboard should compare the values ​​​​of the two cooling scoreboards, and then take the smaller one. But note: the item will enter cooling immediately after the second use, so we need to start calculating the estimated cooling time before the second use.

Classification discussion: Both scoreboards are 0: the total scoreboard is 0, the cooling value of the item after use is 0, and it can be used again; One is 0: The item can still be used at this time, but before use, we need to simulate the situation after use, that is, the scoreboard set to 0 is refreshed for the cooling time, and the scoreboard that is not 0 at this time becomes a smaller one. Therefore, we need to take the one that is not 0 at this time. Neither is 0: the item cannot be used, and it does not matter which value is used.

Taken together, in order to avoid classification judgments, we can directly set the total scoreboard to the larger of the two independent scoreboards.

Generalizing to the case of three or more scoreboards, we should set the total scoreboard to the second smallest scoreboard value.

However, it needs to be noted thatuse_cooldownThe component does not support setting a 0 cooling value. At the same time, in order to prevent accidental triggering, we can set a basic cooling value, such as 10 ticks (0.5 seconds), and add this value to the final value of the total scoreboard.

item modification

Next, we need to copy the scoreboard value to the item's cooling component. The first thing that comes to mind is that you can use the item decorator to directly modify the item component; however, the item decorator does not support dynamically setting values; Then, you can use the inline item decorator mentioned in the first issue, inline the item decorator in the function, and then use macros to set dynamic parameters. Here are examples of related functions:

trident:spear/cooldown_set:

mcfunction
execute store result storage spear cooldown float 0.05 run scoreboard players get @s spear_cooldown
execute if predicate trident:spear/main run function trident:spear/modify with storage minecraft:spear

trident:spear/modify:

mcfunction
$item modify entity @s weapon.mainhand {\
    "function": "set_components",\
    "components": {\
        "use_cooldown": {\
            "seconds": $(cooldown),\
            "cooldown_group": "spear"\
        }\
    }\
}

Details

However, we cannot replace items frequently. Not to mention the loss of performance, it will also make the animation of the player holding the item strange. We can change the consumption time of the consumable component to 2 ticks, and when the use of the item is detected, the cooling time of the item is replaced. In the process of practice, it was found that if advancement detection is used and the replacement function is directly called in the advancement reward function, the item cannot be consumed. Therefore, in the advancement detection, a mark is given to the player instead, and the mark is detected in the tick function for replacement. It is normal.

Powered by VitePress and GitHub Pages