6

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?

  • 1
    Can you invert the command? Something like testfor @p {SelectedItem:!{id:minecraft:stick}}? (Syntax might be off) – Ben Mar 4 '15 at 23:12
  • Of course I can...This is the biggest facepalm of my life! – Veritas025 Mar 5 '15 at 2:50
  • 1
    @Ben, I'd recommend you add that as an answer so OP can give you proper credit. – Gigazelle Mar 5 '15 at 23:50
6
+50

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]
| improve this answer | |
  • In my initial testing, I had found this to not be entirely stable, but I think there may have been another portion of my command setup that was failing, and not necessarily this one. More tests are required. – Unionhawk Aug 11 '15 at 1:33
  • 1
    @Unionhawk Should work every time so long as the /testfor is executed after the first two commands. – SirBenet Aug 11 '15 at 1:34
  • Also, if you're using 1.9 snapshots, you can use tags: /scoreboard players tag [player] add/remove [name], then test for a tag by: @e[tag=XXX], or multiple tags: @e[tag=XXX,tag=YYY] or not a tag: @e[tag=!XXX] – Ezekiel Elin Aug 14 '15 at 0:24
  • @EzekielElin Don't think you can test for multiple tags, whereas you can with scoreboard objectives. – SirBenet Aug 14 '15 at 0:43
  • 1
    @EzekielElin Pretty sure you can't; the tag furthest on just seems to overwrite the earlier ones. – SirBenet Aug 14 '15 at 0:50
0

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.

| improve this answer | |
0

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.

| improve this answer | |
0

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}}

enter image description here enter image description here

Then if the redstone signal is inverted, this will only trigger the mechanism when the player is not holding a stick.

| improve this answer | |
  • I've never heard of that so as @Ben said to invert the command I made an inverted signal which also worked. – Veritas025 Mar 6 '15 at 22:05
  • Are you sure this works? Because I just tested it in 1.8 and it doesn't work, specifically in the NBT tag portion of a command. – Unionhawk Aug 11 '15 at 1:03
  • @Unionhawk I did test it out a while ago (as you can see I provided this in March), but it would seem this is no longer the case. – Ben Aug 11 '15 at 1:05
  • Hmm. Well that's unfortunate. That would have been really handy :/ – Unionhawk Aug 11 '15 at 1:05
  • Can't remember inverting tags like that having ever worked, and I don't see why they'd remove it if it was once a feature. – SirBenet Aug 11 '15 at 1:30
0

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

| improve this answer | |
-1

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.

| improve this answer | |
  • This has never worked. "air" was never a valid inventory item, and doesn't represent an empty inventory slot. An empty slot simply does not exist, and SelectedItem will not exist at all if the player is not holding something. – Skylinerw Oct 7 '16 at 11:16
-2

Sure! Just use the command: /testfor @p {SelectedItem:{id:!stick}}, the exclamation point will make do so.

| improve this answer | |
  • Are you sure this works? Last I tested in 1.8, this didn't work. – Unionhawk Nov 28 '15 at 23:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.