For my minigame, I want all the players to be able to build "towns" which all have a specific key assigned to them (each with a procedurally generated Lore that specifies its id [eg. the first city will have a key with the lore "1", second with the lore "2" etc.]) which allows the players to capture it. When one player uses a key on a town, I want all other instances of that key cleared from everyone's inventory so that they can't come in and steal it from under them. Is there a way for me to have a /clear that only erases a specific key assigned to the specific city? I don't want to have a bajillion /clear @a
commands for every possible id.
1 Answer
Well, it's not that hard. Here's what I understand; you want to clear a specific key from all the player's item slots other than the player that had used it.
Let us assume you have to consider the Nth town, I would recommend you to generate the key using an item generator (GamerGeeks - returns a /give command). I have generated an example:
/give @p stick{display:{Name:'[{"text":"key","italic":false}]',Lore:['[{"text":"town 1","italic":false}]']}} 1
Create the following scoreboard objective:
scoreboard objectives add TownN dummy
scoreboard players set @a TownN 0
Initially, each player has score 0 for the town, meaning it has not been claimed yet.
- Let us assume the player unlocks the town when they hold the key at a specific coordinate (assume 1383,63,-647. Once this player has used the key, you will have to set the score of that player to 1.
/execute at @p[x=1383,y=63,z=-647,distance=..1,nbt={SelectedItem:{
id:"minecraft:stick",tag:{display:{Name:'[{"text":"key","italic":false}]',Lore:['[{"text":"town 1","italic":false}]']}}}}] run scoreboard players set @p[x=1383,y=63,z=-647,distance=..1,nbt={SelectedItem:{id:"minecraft:stick",tag:{display:{Name:'[{"text":"key","italic":false}]',Lore:['[{"text":"town 1","italic":false}]']}}}}] TownN 1
- Now that you have incremented the score for the target player, you will test for all players having a score of 0 for that town, as follows; put this in a repeating command block:
execute at @p[scores={TownN=1}] run scoreboard players set @a[scores={TownN=0}] TownN 2
This step will be clearer in further points.
- Now, we have to continuously clear the key for TownN from any player with a score of 2 for that key. It's simple, put this in a repeating command block:
clear @a[scores={TownN=2}] minecraft:stick{display:{Name:'[{"text":"key","italic":false}]',Lore:['[{"text":"town 1","italic":false}]']}} 1
And that's pretty much it. You'll have to make this contraption for every town you've included.
TIP: To test for a player holding an item with a specific lore/display, use the tag:{} parameter as above and put the display:{} details into it.
clear @a
the key and thengive
the key to the anyone with the scoreboard objective set?