-1

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.

  • 2
    Have you made any attempt to solve this yourself? Arqade works better when askers show effort to solve their own problems; we see that you have a problem you've worked on, and answerers respond to that. You also get a more specific answer that's tailored exactly to the part you're stuck, and Arqade gets a very specific question. Everybody wins! – Frank Jan 27 '18 at 23:41
  • You can't test slots. The slots are always there. What would you expect as the result? – Fabian Röling Jan 28 '18 at 8:38
2

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.

0

I'm still very new to command blocks so this may or may not be helpful or complete, but I wanted to share what I found on this topic while researching a similar issue of my own. @IronAnvil's answers to the question were helpful but in my case, I also needed to execute some commands after testing a specific inventory slot so I'm using this to test for what is in an inventory slot and then run a command:

execute as @a[nbt={Inventory:[{Slot: 9b,id:"minecraft:paper",Count:1b,tag:{display: {Name: '{"text":"Ticket to ride"}'}}}]}] run say Lets go!

This command runs for all players (@a) that contain the specified NBT, which in this case includes a specific player inventory slot and item with a custom display name. The way I came up with that NBT stuff was to put the custom paper in the upper left player inventory (which is what I wanted to test for) and ran the following command from chat:

/data get entity @p

Then I looked through all the NBT that Minecraft stores about my player and found the section on inventory and how it represented the custom named paper entity when stored in slot 9 of a player's inventory. I just copied that NBT into the selector for @a[nbt=...] and it works! Also, since I execute "as" @a, the run command runs as the matching entity so in this case it will say [playername] says Lets Go! whenever a player puts a paper named "Ticket to ride" in their inventory slot 9 (upper left).

Also, in case you don't know how I got the paper to display "Ticket to ride" you can either use an anvil to give it a name, or use the command:

/give @p minecraft:paper{display:{Name:'{"text":"Ticket to ride"}'}}

If you ever need to get the NBT data on an item in a specific inventory slot, you can use this command:

/data get entity @p Inventory[{Slot:9b}]

Or to see the data on the item currently selected on your hotbar:

/data get entity @s SelectedItem

Hope that helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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