Using /testfor in minecraft to check if a player has a certain item in their inventory

I am wondering how I can use the /testfor command in Minecraft 1.8+ to check whether a certain player (For example the nearest player to a command block) has a number of items.

For example, if a player pushes a button, this causes a command block to check if they have 10 iron ingots. If they do, the command block outputs a signal to another command block that takes 10 iron from them and gives them a diamond, making a simple shop.

IS this possible? If it is, could someone reply with the commands, as I am not very good with redstone. Thanks :)


Solution 1:

It's very difficult to see whether someone has an object anywhere in their inventory. That would take 36 command blocks, one for each slot! You can, however, take the object, then see if taking the object worked.

The Commands

  • Set up a fast clock (but not as fast as a fill clock)
  • One command block does /clear @p iron_ingot 0 1 (clears 1 iron ingot from the nearest player. You can choose which player, but it must be exactly one. Also, you must use the same selector throughout all these commands.)
  • Run a comparator from that command block to another command block running this command: /scoreboard players add @p ironIngots 1 (Increases the player's ironIngots score by 1. Make sure you create ironIngots first!)
  • Create another command block that does whatever you want. In the case of giving them a diamond, you would do this: /give @p[score_ironIngots_min=10] diamond. Note the target selector; it gives the player the diamond only if their ironIngots score is 10 or greater.
  • The last thing you want to do is take the ironIngots score away so they don't keep getting items. Just run /scoreboard players remove @p[score_ironIngots_min=10] ironIngots 10. This removes 10 iron ingots from the player if they have an ironIngots score of 10 or greater. Any player who matches this will have received a diamond in the previous step.

How to Set Them Up

The commands listed above must run in order. It is actually very simple to put them in order. Just open the debug menu and look at the stylish new crosshair. Align the command blocks along either the green or red line so that one of those lines points down the first to the last command block.

Notes

  • You don't have to press a button to use this, you just have to be the nearest player. You can change the command to the nearest player within 10 blocks, nearest player on a team, etc.
  • The commands will take a short amount of time to finish, but for 10 iron ingots, it will only take half a second. It's one tick (1/20 of a second) per item taken away.
  • It will take away iron ingots even if you don't have ten. However, you can submit one, then later submit five, then submit four more later, and it will still give you the diamond. You can also submit 14 at a time and the extra 4 will be saved for later.

Solution 2:

If you do these setup commands:

/scoreboard objectives add items dummy

/stats entity @a set AffectedItems @p items

/scoreboard players set @a items 0

Then run this command: (N.B. The execute is ESSENTIAL)

/execute @a ~ ~ ~ /clear @p minecraft:iron_ingot 0

This will then mean that each player has the number of iron ingots in their inventory as their items score. This will allow you to process the event you need