How to replace a SPECIFIC item in a players hotbar with another specific item
I want to make it so that i have a command block that tests for if a player has a glass bottle selected in slot 1 of their hotbar, if this is true, that specific player and only that player is added to a team. Any player that is in that team gets a temporary invisibilty potion effect for 30 seconds and then it replaces the glass bottle in the persons hotbar with a water bottle and removes them from the team. The reason i want to do this is i want to make a temporary, rechargeable power up that a player can activate so that they use an item, get an ability, after a certain amount of time lose the ability and get the original item back after the "power" has recharged.
The problem i have is... I can test if a player has a specific item in a specific slot sure, and then have a comparator come out of the command block that gets an output if the testfor command becomes true for any player. But then i dont know how to only target that specific player with the glass bottle to be put into a team since the only targeting commands are @a, @p, or @r. These targeting commands dont work since i dont want all players to be added to the team and get the potion effects and i dont want the player closest to the command block get added to the team and get the potion effects as the player that activated the testfor command block may be further away from the command block than another person. And ofcourse, a random player wouldnt work either. So is there a command where i can target all players that have a glass bottle in slot 1 of their hotbar and add them to a team rather than seeing if a player has a glass bottle in slot 1 of their hotbar and adding all people to a team.
Sorry for the long explanation, im not very good at explaining things, if anyone can help, that would be greatly appreciated.
You'll need to "translate" the glass bottle data tag into something that works with target selectors (@a and so on). This can be achieved most easily using dummy scoreboard values or, in 1.9, scoreboard tags.
In 1.8, create an objective using
scoreboard objectives add bottleInOne dummy
Create a fill clock and run the following two commands
scoreboard players set @a[score_bottleInOne_min=1] bottleInOne 0
scoreboard players set @a[score_bottleInOne=0] bottleInOne 1 {SelectedItemSlot:0,SelectedItem:{id:minecraft:glass_bottle}}
If the team part is necessary, you can now add @a[score_bottleInOne_min=1]
to the team, and remove @a[score_bottleInOne=0]
from it, using the same fill clock. You could also use these target selectors directly for the /effect
and /replaceitem
commands, if the team part was just your idea of selecting that player for the effect.
In 1.9, you can omit the scoreboard objective in favor of tags. Using a Repeat/Chain command block chain, run
scoreboard players tag @a[tag=bottleInOne] remove bottleInOne
scoreboard players tag @a[tag=!bottleInOne] add bottleInOne {SelectedItemSlot:0,SelectedItem:{id:minecraft:glass_bottle}}
In the following commands, you can then use @a[tag=bottleInOne]
or @a[tag=!bottleInOne]
to target players with and without the bottle selected in slot 0, respectively.