4

I'm building a CTF map of sorts, using command blocks in vanilla 1.8. I've discovered two commands for detecting if an item is in a players inventory:

/clear @a[team=Red] banner 4 0

/testfor @a[team=Red] {Inventory:[{id:"minecraft:banner"}]}

Those work fine, unless you want to perform a command ON the player carrying that item. For example: I want to cause the player who picks up a banner of the opposite team, to automatically equip that banner in their head slot with a command similar to this:

/replaceitem entity @a[???????] slot.armor.head banner 1 4

Can this be done? If not, is there a way to mark a specific player carrying an item using scoreboards?

5 Answers 5

4

As of 1.13, doing this no longer needs a scoreboard and can be done in one command block:

/execute as @a[nbt={Inventory:[{id:"blue_banner"}]}] run ...
2

@ModDL had the right idea, although his commands error in game.

Here are the commands I ultimately used to mark players with the flag, and perform actions on them:

To set up the tracking, run these once at the start:

/scoreboard objectives add hasBlueFlag dummy
/scoreboard players set @a[team=Red] hasBlueFlag 0

To detect and mark who has the flag, run this on a clock:

/scoreboard players set @a[team=Red,score_hasBlueFlag=0] hasBlueFlag 1 {Inventory:[{id:"minecraft:banner",Damage:4s}]}

To reset who has the flag, run this command on a clock:

/testfor @a[score_hasBlueFlag=1] {Inventory:[{id:"minecraft:banner",Damage:4s}]}

and, invert the output into a command block with this:

/scoreboard players set @a[score_hasBlueFlag=1] hasBlueFlag 0

To perform an action on the flag carrier, you can just fire something like this off, whenever you'd like. The following commands replace the flag carriers helmet slot with the flag:

/clear @p[score_hasBlueFlag=1] banner 4 -1
/replaceitem entity @p[score_hasBlueFlag=1] slot.armor.head banner 1 4

Notes:

  • These commands are testing and running in 1.8.1
  • In the above commands, you'll notice Damage:4s, this is actually the base banner color (Blue). Read more about the Damage NBT property here.
  • More banner detecting options can be found here
2

First, you need to set up a dummy scoreboard objective:

scoreboard objectives add hasFlag dummy

Now, create a 20 Hz. clock, such as a fill clock or use minecraft 1.9's repeat and chain command blocks to run the following commands in order:

scoreboard players set @a hasFlag 0
scoreboard players set @a hasFlag 1 {Inventory:[{id:"minecraft:banner",Damage:4s}]}

This ensures that the flag-bearer (i.e. the player with a blue banner) has a hasFlag score of 1, and it's 0 for everyone else. Note that you can modify the data tag in the second command to your liking, using any of the NBT tags available for players.

Now, we can do whatever we want with that scoreboard, by using the score_hasFlag_min=1 target selector argument to target only flag-bearers, or score_hasFlag=0 to target everyone else. These commands have to be run on the same clock, after the above commands. For example:

replaceitem entity @a[score_hasFlag_min=1] slot.armor.head minecraft:banner 1 4 
replaceitem entity @a[score_hasFlag=0] slot.armor.head minecraft:air 1

Note: In 1.9, you can opt for using scoreboard tags instead of a dummy objective.

1
  • None of the existing answers are truly satisfying. ModDL's answer is blatantly wrong, and Albert's own answer is using testfor and a comparator, which is a recipe for disaster in the general case.
    – MrLemon
    Commented Nov 30, 2015 at 10:35
1

The command to put it on a players head is:
/replaceitem entity slot.armor.head minecraft:banner [amount] [datatag]
and that would place the banner in the helmet position, but the banner will be sticking out of the back of the head which doesn't look all that good but it still works and you can always try using wool if you prefer that. Also the datatag is that color of the flag whichever number in place of that will determine the color of the banner and you have to remember that you have to have an amount if you have a datatag.

Datatags:
Black = 0
Red = 1
Green = 2
Brown = 3
Blue = 4
Purple = 5
Cyan = 6
Light Grey = 7
Grey = 8
Pink = 9
Lime = 10
Yellow = 11
Light Blue = 12
Magenta = 13
Orange = 14
White = 15

3
  • I know how to give a player a banner of the right color, etc. What I don't know how to perform an action on ONLY the player that is holding the banner. (regardless of player's location) Commented Nov 13, 2014 at 23:08
  • Well what you would have to do is have a max of 8 people on each side then make every single player have there own then by themselves and put the in the big team aswell then you scan each player individually by there single teams Commented Nov 13, 2014 at 23:27
  • No you don't. scoreboard players takes datatag arguments. Example:scoreboard players set @a hasFlag 1 {Inventory:[{id:"minecraft:banner", Damage:4s}]} Commented Dec 22, 2015 at 5:26
1

Use a dummy scoreboard to keep track of what players are valid. For example the dummy scoreboard is isCarryingFlag

/scoreboard players isCarryingFlag 1 @a[team=Red] {Inventory:[{id:"minecraft:banner"}]}
/excecute @a[isCarryingFlag=1]
/scoreboard players isCarryingFlag 0 @a

Run these commands in order.

2
  • None of that works. :( Commented Nov 17, 2014 at 3:11
  • 1
    After more digging, the general concept of this answer is correct, but the commands are wrong. I'll accept this once the commands are corrected. Commented Nov 22, 2014 at 4:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.