How to /testfor a named item in a player's inventory?

I'm working on a PVP map that I have posted to pmc so random people can download it and play it with their friends. My problem is, each kit has a special buff (potion effect) but when they die it leaves them.

I need this to make kinda like a loop that when they have like some armor on when they die it will test for that armor and give more potion effects.


Solution 1:

Here is a solution that will give anyone wearing a leather chestplate named Hero's Tunic the tag Wearing. This tag can then be used to target that person for the effect. Commands are for 1.12.2.

Note: Using only these commands, if a player ever wears the chestplate, they will be tagged and the effect will be applied forever, even if no longer wearing the chestplate. (unless the tag is manually removed via a command)


Make command block that is RepeatUnconditionalAlways Active with command:

scoreboard players tag @a add Wearing {Inventory:[{Slot:102b,id:"minecraft:leather_chestplate",tag:{display:{Name:"Hero's Tunic"}}}]}

Here is an example command that gives them a level 2 slowness effect for 1 second. This will be a constant effect if the command is placed into a RepeatUnconditionalAlways Active command block:

effect @a[tag=Wearing] minecraft:slowness 1 2

To remove the tag from a player:

/scoreboard players tag <player_name> remove Wearing

Replace <player_name> with the name of the player that you want to remove the tag from.


To remove the tag from all players:

/scoreboard players tag @a remove Wearing

Edit: Response to comments.

To have more tags for more pieces of armor you would change the first command:

scoreboard players tag @a add <tagName> {Inventory:[{Slot:102b,id:"minecraft:leather_chestplate",tag:{display:{Name:"..."}}}]}

Replace <tagName> with whatever name you like. You could use numbers if wanted. (Wearing1, Wearing2, etc) You could also use other names that describe whatever you like.
Example: WearingHerosTunic You can use a different tag for each different chest plate.

Then use those tags in the other commands:

effect @a[tag=WearingHerosTunic] minecraft:slowness 1 2