#How to use conditional command blocks in a datapack

1 messages · Page 1 of 1 (latest)

stoic tinsel
#

Im currently trying to create a datapack that locks and unlocks doors with interactions. I have watched some videos and got it to work but now i want it to play a lock and unlock sound if certain conditions are met that are you played if you interact with the door. But i have no idea how to make it work any help is appreciated

mental stoneBOT
#

<@&1201956957406109788>

Someone will come and help soon!

💬 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

versed plume
#

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.

stoic tinsel
#

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

versed plume
#

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?

stoic tinsel
#

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

versed plume
#

let me guess CloudWolf?

stoic tinsel
#

yes

versed plume
#

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.

stoic tinsel
#

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?

versed plume
#

either that or you can add additional tags to identify the door and check if it's the correct door in the function.

#

using execute if items to check the key tag is much simpler for that.