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?
Best Answer
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]
Pictures about "Test if player is holding something but the specified item [duplicate]"
![Test if player is holding something but the specified item [duplicate] - Man in Black Long Sleeve Shirt Holding Basketball Test if player is holding something but the specified item [duplicate] - Man in Black Long Sleeve Shirt Holding Basketball](/assets/images/test_if_player_is_holding_something_but_the_specified_item_duplicate_1.jpeg)
![Test if player is holding something but the specified item [duplicate] - Photo Of Woman Looking Through Camera Test if player is holding something but the specified item [duplicate] - Photo Of Woman Looking Through Camera](/assets/images/test_if_player_is_holding_something_but_the_specified_item_duplicate_2.jpeg)
![Test if player is holding something but the specified item [duplicate] - A Woman Doing an Experiment Test if player is holding something but the specified item [duplicate] - A Woman Doing an Experiment](/assets/images/test_if_player_is_holding_something_but_the_specified_item_duplicate_3.jpeg)
[1.17] How to Detect If a Player Is Holding a Named Item
More answers regarding test if player is holding something but the specified item [duplicate]
Answer 2
Here's one possible method:
Create one command block for each item slot. Then, create a command to check the item AND if the slot is active, then connect them all to a single input on one end and a single output on the other.
The first command block should have:
/testfor @p {SelectedItemSlot:0,Inventory:[{Slot:0b,id:"minecraft:stick"}]}
The second command block should have:
/testfor @p {SelectedItemSlot:1,Inventory:[{Slot:1b,id:"minecraft:stick"}]}
etc.
Notice that the two slot numbers have changed for each block. Continue changing the slot values for each block until you have 0-8 (requires 9 command blocks). Then, once all the command blocks are connected to the same output (you'll probably need a comparator and repeater for each), invert the output signal.
Note: You may need to adjust the output to be off by default with some additional redstone circuitry.
Answer 3
Just use a comparator to test if a player is holding a stick, and then invert it (via a redstone torch) and have the inverted signal leading to a command block. Now, whenever the player is holding a stick, the signal is inverted and the second command block deactivates.
Answer 4
You can simply add the syntax for "not" (!) to your command.
testfor @p {SelectedItem:!{id:minecraft:stick}}
The other option would be to just invert the output. So first, you would test that the player is holding a stick:
testfor @p {SelectedItem:{id:minecraft:stick}}
Then if the redstone signal is inverted, this will only trigger the mechanism when the player is not holding a stick.
Answer 5
Just use a T-Flip Flop....
(comparator running out of the command block with a repeater into a block which has a redstone torch on the other side which then powers a command block)
Then it should work
Answer 6
It's probably just easiest to use a comparator, and you can test to see whether or not they are holding the stick, and then just invert the output with a NOT gate. I did some additional research, and it turns out that in some versions of Minecraft, a player actually has the syntax of holding "minecraft:air". This means that to check that they don't have something in their hand, you actually have to check for air. This does not work in 1.9, as Mojang has removed "minecraft:air" from the database.
So it might be easiest to do this:
/testfor @p {SelectedItem:{id:minecraft:air}}
I feel like there should be a way in 1.9 to just test that there is nothing.
I'll look into this a bit more.
Answer 7
Sure! Just use the command: /testfor @p {SelectedItem:{id:!stick}}, the exclamation point will make do so.
Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.
Images: Centre for Ageing Better, Truman Rexti, Artem Podrez, Artem Podrez


