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.

  • One thing you might be able to do is set a scoreboard objective on the owner, clear @a the key and then give the key to the anyone with the scoreboard objective set? Mar 30 at 20:06
  • 1
    I really can't think of a way to do it... honestly, since you are using datapacks, just make a small script to build the commands, shoulnd't be so bad.
    – BunnyMerz
    Mar 30 at 23:44
  • this seems a bit backwards - why does everyone have all the keys? wouldn't it be a lot easier to activate the town in some other, more generic way, which THEN generates the one single key to that town (based on some global counter) and gives it to the activating player?
    – Plagiatus
    Apr 3 at 7:05
  • there are 2 reasons why I chose to do it that way: 1) so that the owner of the key doesn't have to carry it everywhere they go, and can just create one on the spot as they need it (for example, if they're forced to give it away) and 2) because I want a thing where if one player loses, all players including them will gain every key to every town that player has. This means that the losing player has a chance to secure a few town again but all the other players can snag some for themselves. Apr 3 at 12:09

1 Answer 1


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.

  1. 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
  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.

  1. 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .