How to check if item exists before giving the player a new one
Olivia Zamora
I have tried a few ways and tried many commands but I just don't seem to get it right.
What I'm trying to do :
I have a Minecraft realm with a town center where each player can press a button, which triggers a command block, giving them one pickaxe with a unique identifier (not sure what or how). Would be nice if the pickaxe could be named after the player who pressed the button like "Minecraftuser12312 Pickaxe" but not important,
If they press the button again I need to make sure they do not have the item and if the item is also not just thrown out to pick another one up from the command block.
Any help would be appreciated <3
Things to know :
I am the realm owner and command blocks are enabled in the world.
12 Answers
Your definition of the unique item is a little ambiguous. Do you mean that the item would have the name of the player who clicked the button?
Anyways
My solution is to basically remove all this special item and then give them a new one. If they don't have the item, good nothing happens. If they click it twice the first unique item gets removed before the second one comes into their hand.
Make it a datapack or put it into a chain command block sequence:
- When they press the button --
execute as @p run item remove pickaxe{nbt={idk},Count:1b} - Secondly ---
give @p whatever pickaxe{nbt={idk}}
Hopefully this helps
A simpler solution would be to prevent any player from ever having two "unique" pickaxes in their inventory.
Give the pickaxe
You can give it a unique identifier with a custom NBT tag:
give @p stone_pickaxe{unique:1b} 1Optionally, you can give it some lore so the player knows it is a unique pickaxe:
give @p stone_pickaxe{display:{Lore:['{"text":"One per person"}']},unique:1b} 1(It is likely possible to use some /item and sign parsing magic to put the player's name onto the item but that's not the main focus of this question.)
Setup
scoreboard objectives add unique_pickaxes dummyRepeating
Clear duplicates from their inventory
execute as @a store result score @s unique_pickaxes run clear @s stone_pickaxe{unique:1b} 0
execute as @a if score @s unique_pickaxes matches 2.. run clear @s stone_pickaxe{unique:1b} 1