Test if player is holding something but the specified item [duplicate]

So I'm making a command block mechanism, but I need to test if the player is carrying something other than a specified item like testfor @p {SelectedItem:{id:minecraft:stick}}, whereas this instance it'd activate if the player is carrying anything but a stick. If that is impossible is there a way to test if the player is not carrying anything at all?


Solution 1:

Set up a dummy objective:

 /scoreboard objectives add NotHoldingItem dummy

On a clock (or just before you want to use the testfor), in this order, run the following commands:

 scoreboard players set @a NotHoldingItem 1
 scoreboard players set @a NotHoldingItem 0 {SelectedItem:{id:minecraft:stick}}

The commands above set everyone's NotHoldingItem score to 1, and then set it back to 0 for everyone holding the item, leaving only people not holding the item with a NotHoldingItem score of 1.

You can then test for people who have a NotHoldingItem score of 1:

/testfor @a[score_NotHoldingItem_min=1]