Hi, I'm making an escape room in Minecraft and I'm trying to check if the items on a specific block are diamonds, then get the count of them, add the count to the scoreboard and then delete the items. Getting the amount of diamonds in the player's inventory would also work.
I'm going to assume you understand how datapacks and functions work. If you don't, just let me know and I'd be happy to go more in depth (I just want to keep this as concise as possible).
All of these commands should be placed inside a datapack. The commands will be grouped by what function they are in.
NOTE: I made this for 1.18, so if you need it for a different version, let me know and I'll update it later (unless it's pre-1.13 cause the commands are much different).
Getting Setup
First we'll take care of adding scoreboards and setting constants.
The following commands should be placed inside a load function in the following order:
quest_name will be responsible for stopping the process of checking for diamonds on a specific block after the condition has been met.
diamond_count is where we will store (and constantly update) the amount of diamonds that are found on the specific block.
price will be a constant that denotes how many diamonds are needed to be present for the condition to be met (specifically the score of the player .price for the objective price).
Detecting The Diamonds
From now on, I'll explain line-by-line.
The following commands should be placed inside a tick function in the following order:
execute unless score quest_name quest_name matches 1 as @e[type=minecraft:item,nbt={Item:{id:"minecraft:diamond"}},x=-8,y=-59,z=16,dx=0,dy=0,dz=0] store result score @s diamond_count run data get entity @s Item.Count
This command will set the score of each diamond that is on the block located at -8, -60, 16 to the item count of that diamond on the scoreboard objective diamond_count until we set the score of the player quest_name for the objective quest_name to 1 as long as there is an item, specifically a diamond, on the block located at -8, -60, 16.
The following commands will work together to combine all the diamond scores to one player score called diamond_count on the scoreboard objective diamond_count.
execute unless score quest_name quest_name matches 1 run scoreboard players set diamond_count diamond_count 0
This command will constantly reset the score of the player diamond_count on the scoreboard objective diamond_count until we set the score of the player quest_name for the objective quest_name to 1.
This command will constantly add all the combined scores of the diamond counts to the player diamond_count on the scoreboard objective diamond_count and because the previous command is constantly resetting the scores of diamond_count diamond_count, the score of diamond_count diamond_count will be the total count of diamonds on the block located at -8, -60, 16.
Now we can check to see if the amount of diamonds on the block is greater than or equal to the "price" and if it is, we'll run a new function called take_items.
execute if score diamond_count diamond_count >= .price price run function ic:take_items
NOTE: take_items is just a normal function: not tick or load.
Deleting, Rewarding, and Refunding
The following commands should be placed inside a normal function (again that just means it's not a tick or load function) in the following order:
scoreboard players set quest_name quest_name 1
This command will stop the process of checking for diamonds.
execute store result storage minecraft:remainder remainder short 1 run scoreboard players operation diamond_count diamond_count -= .price price
This command will store the number of diamonds that are left after the price has been subtracted. This will be used to refund the player later if they dropped more than the required amount.
scoreboard players set diamond_count diamond_count 0
This command just resets the diamond count to 0 on the scoreboard and is not strictly necessary.
execute as @e[type=minecraft:item,nbt={Item:{id:"minecraft:diamond"}},x=-8,y=-59,z=16,dx=0,dy=0,dz=0] run kill @s
This command deletes all the diamonds that were thrown on the block located at -8, -60, 16.
This command will summon a diamond on the middle of the block located at -8, 60, 16. This diamond will be modified to provide the refund in the next command.
data modify entity @e[type=minecraft:item,nbt={Item:{id:"minecraft:diamond"}},x=-8,y=-59,z=16,dx=0,dy=0,dz=0,limit=1] Item.Count set from storage minecraft:remainder remainder
This command will change the count of the diamond summoned above to whatever we stored earlier as the remainder of the total diamond count minus the price. (If the remainder was 0, the diamond will disappear).
And that's it! Put whatever rewards or text you want after that last command!
For example:
tellraw @a {"text":"Congrats! You dropped the diamonds!","color":"green"}
give @a minecraft:emerald 64
Final Thoughts
I got kinda lazy and definitely over explained some things, but hopefully it was helpful nonetheless. I'll include a link to the datapack download so you can mess around with it if the explanation didn't make any sense. Oh, and change the coordinates and the player/objective "quest_name" to whatever you want. Again if you have any questions feel free to ask. Have a good day!
Hi, I'm making an escape room in Minecraft and I'm trying to check if the items on a specific block are diamonds, then get the count of them, add the count to the scoreboard and then delete the items. Getting the amount of diamonds in the player's inventory would also work.
Before We Begin
I'm going to assume you understand how datapacks and functions work. If you don't, just let me know and I'd be happy to go more in depth (I just want to keep this as concise as possible).
All of these commands should be placed inside a datapack. The commands will be grouped by what function they are in.
NOTE: I made this for 1.18, so if you need it for a different version, let me know and I'll update it later (unless it's pre-1.13 cause the commands are much different).
Getting Setup
First we'll take care of adding scoreboards and setting constants.
The following commands should be placed inside a load function in the following order:
quest_name will be responsible for stopping the process of checking for diamonds on a specific block after the condition has been met.
diamond_count is where we will store (and constantly update) the amount of diamonds that are found on the specific block.
price will be a constant that denotes how many diamonds are needed to be present for the condition to be met (specifically the score of the player .price for the objective price).
Detecting The Diamonds
From now on, I'll explain line-by-line.
The following commands should be placed inside a tick function in the following order:
This command will set the score of each diamond that is on the block located at -8, -60, 16 to the item count of that diamond on the scoreboard objective diamond_count until we set the score of the player quest_name for the objective quest_name to 1 as long as there is an item, specifically a diamond, on the block located at -8, -60, 16.
The following commands will work together to combine all the diamond scores to one player score called diamond_count on the scoreboard objective diamond_count.
This command will constantly reset the score of the player diamond_count on the scoreboard objective diamond_count until we set the score of the player quest_name for the objective quest_name to 1.
This command will constantly add all the combined scores of the diamond counts to the player diamond_count on the scoreboard objective diamond_count and because the previous command is constantly resetting the scores of diamond_count diamond_count, the score of diamond_count diamond_count will be the total count of diamonds on the block located at -8, -60, 16.
Now we can check to see if the amount of diamonds on the block is greater than or equal to the "price" and if it is, we'll run a new function called take_items.
NOTE: take_items is just a normal function: not tick or load.
Deleting, Rewarding, and Refunding
The following commands should be placed inside a normal function (again that just means it's not a tick or load function) in the following order:
This command will stop the process of checking for diamonds.
This command will store the number of diamonds that are left after the price has been subtracted. This will be used to refund the player later if they dropped more than the required amount.
This command just resets the diamond count to 0 on the scoreboard and is not strictly necessary.
This command deletes all the diamonds that were thrown on the block located at -8, -60, 16.
This command will summon a diamond on the middle of the block located at -8, 60, 16. This diamond will be modified to provide the refund in the next command.
This command will change the count of the diamond summoned above to whatever we stored earlier as the remainder of the total diamond count minus the price. (If the remainder was 0, the diamond will disappear).
And that's it! Put whatever rewards or text you want after that last command!
For example:
Final Thoughts
I got kinda lazy and definitely over explained some things, but hopefully it was helpful nonetheless. I'll include a link to the datapack download so you can mess around with it if the explanation didn't make any sense. Oh, and change the coordinates and the player/objective "quest_name" to whatever you want. Again if you have any questions feel free to ask. Have a good day!
https://www.mediafire.com/file/b6q2hz18res30p6/Detect+Diamonds.zip/file