#How to use conditional command blocks in a datapack
1 messages · Page 1 of 1 (latest)
<@&1201956957406109788>
💬 While you wait, take this time to provide more context and details.
🙇 If nobody has answered you by <t:1733331260:t>, feel free to use the Summon Helpers button to ping our helper team.
✅ Once your question has been resolved (or you no longer need it), please click Resolve Question or run /resolve
for conditional commands, you simply use execute if ... or execute unless ...
that's it
if you run a function (that is not the tick function in the best case) you can also use the return command to stop the execution of the function early and return a value.
i have my commands in the tick function so the locking and unlocking works
the problem is what every i try it gets spammed is there a way to play it only if the player interacts with it
i guess the locking and unlocking are triggered by the player interacting with the block/entity
That means you don't even have to use the tick function.
You can use advancements or enchantments instead.
They only trigger when the player does something
(advancements have to be revoked after trigger to make them work multiple times)
how does you current code look?
what triggers the lock/unlock?
Since i have no idea about these type of commands i used a YT video and got these command that work inside of the tick function
execute as @e[type=minecraft:interaction,tag=door] at @s on target store result score @e[type=minecraft:interaction,sort=nearest,limit=1] key run data get entity @s SelectedItem.components."minecraft:custom_data".key
execute as @e[type=minecraft:interaction,tag=door] if score @s key = @s lock run kill @s
execute as @e[type=minecraft:interaction,tag=door] run data remove entity @s interaction
let me guess CloudWolf?
yes
you can use an advancement like this to detect if you click the interaction entity with the correct item:
{
"criteria": {
"click": {
"trigger": "minecraft:player_interacted_with_entity",
"conditions": {
"item": {
"components": {
"minecraft:custom_data": "{key:1b}"
}
},
"entity": {
"type": "minecraft:interaction",
"nbt": "{Tags:[\"door\"]}"
}
}
}
},
"rewards": {
"function": "namespace:path/to/function"
}
}
as a reward you can make it run a function
this function is executed as and at the player
that means you can still target the interaction with execute on target ...
if you have more questions, I'll be back in 30 min. I have to install a new outlet quickly in our basement.
i have multiple doors with different keys the problem now is if i use key1:b on another door the sound is played as if it got unlocked
im planning on making a huge map with bunch of lockable doors and if i think correctly with this method i have to create a new advancement for each door right?