Can you /testfor a specific inventory slot?

I'm working on a project and I need help with a /testfor command: is there any way of testing an inventory slot (e.g. slot 20) in the inventory? And can you do this with scoreboard?
Let me clarify that I don't want to /testfor the item I'm holding in any of the hotbars.


Solution 1:

This is done by using the dataTag portion of the testfor command. The tag information to be tested can be found in the wiki on player data. In this case you are searching in Inventory for Slot information.

Here is the command:

/testfor @a {Inventory:[{Slot:20b}]}

This simply tests for the existence of Slot 20 which would mean there is any item stored in that slot.

This command tests slot 20 for stone:

/testfor @a {Inventory:[{Slot:20b,id:"minecraft:stone"}]}

On the same wiki page for player data is an image which contains all the slot numbers.

Here is a similar image I had created for 1.12.2: Slot numbers


Update after question edit. This can be done with scoreboard objectives and scoreboard tags. This will use the same tag information as used in the testfor commands.

To set an objective score to 1:

scoreboard players set @a <objective> 1 {Inventory:[{Slot:20b}]}

To add a tag use the command:

scoreboard players tag @a add <tagName> {Inventory:[{Slot:20b}]}

Generally, you want these commands in a chain command block after a repeat command block which sets the score to 0 or removes the tag. This way if the player then removes the item from the slot, they will no longer be targeted.