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 custom item uses cooling

icuqALT10

icuqALT10

When I saw "Dynamic Cooling Based on Vanilla Cooling Components" written by CR_019, I remembered that the right-click detection I wrote a long time ago can also realize dynamic custom item cooling. It only uses 3 advancements and their corresponding reward functions and 1 empty recipe I will submit another article when I have nothing to do ()

Contains content

advancement:"using_item"、"consume_item"、"recipe_unlocked"

recipe: any empty recipe

Implementation method

General idea

vanillaconsumableComponents can customize the consumption time of items.use_cooldown组件You can set the cooling time after use

butuse_cooldownIn most cases, components are only triggered whenconsumablewill be triggered accordingly

So we can useconsumable来触发use_cooldown

There is no physical difference between 1t and 2t, so the consumption time can be set to 0.1s, and using_itemadvancement can be continuously triggered during the consumption process.

This way we can do:

In the 1tth time, using_itemadvancement is used to save the item information and modify its cooling

The corresponding effect after the right-click is used can be written in the function of the 2tth successfully consumed item (as of 1.21.6, when this function is triggered, data get can still be used to obtain the information of the consumed item)

When the 2t triggers the effect and gives the player an empty recipe, the third advancement can be triggered.

The third advancement can use the item information stored in the 1t time, and return the item to the player after the player updates in the 2t time (the recipe is updated after the player updates)

Specific implementation

Take the instructions I have written as an example

Advancement and function of Section 1t

json
{
    "criteria": {
        "test":{
            "trigger": "minecraft:using_item",
            "conditions": {
                "item": {
                    "predicates":{
                        "minecraft:custom_data":"{right_click_hand:\"main\"}"
                    }
                }
            }
        }
    },
    "rewards": {
        "function": "yyt:system/right_click/mainhand/consumable/main"
    }
}
mcfunction
advancement revoke @s only yyt:system/click_check/consumable/right_click_mainhand

#根据tag一次右键仅触发一次的动态更新cd
execute unless entity @s[tag=cooldown_set] run function yyt:system/right_click/mainhand/consumable/cd

#获取物品信息
data remove storage yyt:item modify
data modify storage yyt:item modify set from entity @s SelectedItem

#将物品信息保存到玩家专属storage中(具体自由发挥,不详细讲述)
function yyt:players/get/main
data modify storage yyt:player player.temp.item.modify set from storage yyt:item modify
function yyt:players/set/main

#revoke第三个进度,为下文做铺垫(?)
advancement revoke @s only yyt:system/click_check/consumable/mainhand_replace
mcfunction
#修改玩家手中物品的cd
item modify entity @s weapon.mainhand {function:"set_components",components:{"minecraft:use_cooldown":{seconds:0.5,"cooldown_group":"yyt:weapon/sword/1/1"}}}

#给予tag,让cd确保一次只修改一次
tag @s add cooldown_set

In this way, this advancement and its corresponding function are triggered at the 1tth time, the item information is successfully saved, and the cd of the item in hand is modified.

2t and second advancement

json
{
    "criteria": {
        "test":{
            "trigger": "minecraft:consume_item",
            "conditions": {
                "item": {
                    "predicates":{
                        "minecraft:custom_data":"{right_click_hand:\"main\"}"
                    }
                }
            }
        }
    },
    "rewards": {
        "function": "yyt:system/right_click/mainhand/main"
    }
}
mcfunction
advancement revoke @s only yyt:system/click_check/right_click_mainhand

#移除tag
tag @s remove cooldown_set

#执行对应函数
function yyt:system/right_click/function with entity @s SelectedItem.components.minecraft:custom_data

#重新给予配方	以触发第三个进度
recipe take @s yyt:click_check/mainhand
recipe give @s yyt:click_check/mainhand

The item consumption is triggered normally, the item's cooling group enters the cd, and a recipe is given.

If you want to modify the item, such as modifying the quantity or modifying nbt, you can do so by modifying the item information saved previously.

2t and 3rd advancement

json
{
    "criteria": {
      "requirement": {
        "trigger": "minecraft:recipe_unlocked",
        "conditions": {
          "recipe": "yyt:click_check/mainhand"
        }
        },
        "test":{
            "trigger": "minecraft:consume_item",
            "conditions": {
                "item": {
                    "predicates":{
                        "minecraft:custom_data":"{right_click_hand:\"main\"}"
                    }
                }
            }
        }
    },
    "rewards": {
        "function": "yyt:system/right_click/mainhand/consumable/replace_main"
    }
}
mcfunction
#读取物品信息
function yyt:players/get/main
data modify storage yyt:item modify set from storage yyt:player player.temp.item.modify

#写回原始cd 自由发挥    可以将原始cd保存在custom_data里在这里用宏修改上面的storage的内容	不过多写了
function 懒得写

#返还物品以及数量
item replace entity @s weapon.mainhand with stick
function yyt:system/item/components/mainhand with storage yyt:item modify
function yyt:system/item/count/mainhand

#清除物品信息并返还给玩家专属storage
data remove storage yyt:player player.temp.item.modify
function yyt:players/set/main

The most critical part is that the advancement obtained by the recipe will be triggered after the player is updated, so we can use this to seamlessly return the item to the player when the second player successfully consumes the item (it doesn’t matter if you don’t understand it, you just need to know to give the player the recipe in the second advancement and write its acquisition detection in the third advancement)

Epilogue

It’s actually not troublesome to do. It just took me a long time to think about it while I was writing it.

Hope this helps!

Powered by VitePress and GitHub Pages