4

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 :)

  • Does it only need to work with one player at a time? – FlyingPiMonster Apr 4 '15 at 13:33
2

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.
  • Hmmm... This method has a problem, as if the player has multiple iron ingots on them, the comparator output from the command block with the command /clear @p iron_ingot 0 1 in it is constantly on, as the block is repeatedly taking an iron ingot from the player, and so constantly outputting a signal. How could I fix this? – James Apr 5 '15 at 17:11
  • Oops, I forgot about that. Just use a slower clock. – FlyingPiMonster Apr 5 '15 at 17:19
1

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

0

In 1.9/1.10: Make a repeating command block, needs redstone, that does:

/clear @p minecraft:(currency)

Replace "(currency)" with your currency in your example iron ingots. So do:

/clear @p minecraft:iron_ingot 0 1

Replace the 1 with however many you need to buy the second item, so 10. Now, place a conditional chain command block facing away from the repeating. Put:

/give @p minecraft:(bought item)

Replace bought item with the bought item, so diamond. Now however many, so 1. Then put 0. And you're done!

  • This will not work as expected. The number of items in /clear is the maximum amount, not minimum. If the player had 1 iron ingot and you cleared a maximum of 10, it would still succeed. – Skylinerw Aug 16 '16 at 15:26

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.