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.
You can use scoreboard in player selectors to find out which one they clicked. Usually this is done by either testfor
or execute
.
I recommend a 20 t/s clock to check these as soon as they click it. You don't have to use one, but it may help. great method to setup one.
In the case of testfor, it is actually not recommended to use it. You can't exactly distinguish which player clicked it, you just know that it was clicked. A better way is the execute method. Never-the-less, the command should look like this:
Testfor @a[score_test=1,score_test_min=1]
You will need a comparator to get if it succeeded to find it. You should also have a separate command block for each, then change the 1
to another number for each possible outcome.
In the case of execute: recommended method. You can use @p in the command
part to target that specific player. Command as follows:
Execute @a[score_test=1,score_test_min=1] ~ ~ ~ <command>
For this, you should set their score of that to 0 as soon as the command is run, otherwise that command may be run twice.
Best Answer
This can be done using "
AttributeModifiers
" that are changed dynamically using/execute store
. Previously you were able to abuse MC-123307 for this to do it directly in the player's hand, now you need to remove the item and summon a new one, which instantly gets picked up.Firstly, the commands, which need to be executed in the same tick (for example using a command block line or a function):
I think it's best to explain the third and fourth command first:
AttributeModifiers
, which in theory changes the player's maximum health, but in practice a value of 0 gets ignored here. This value will be changed dynamically in the next command:maxHealth
AttributeModifier, scaled by a factor of 2 (because health points are half a heart). So if your score was for example 63 (the maximum that works as expected), then it wil be changed toAmount:126
and you will have 126 health points=63 hearts when you pick up and hold the item.Now the commands are over for this tick, so the player instantly picks up the item (the item entity had no
PickupDelay
). When you trigger the commands the next time, the first two commands do the following:a:1
tag, this is just to prevent issues if there are multiple on the ground at once.a:1
, this is to remove the old item to make space for the new one and also to remove possible duplicates, in case there can be some.Then the third and fourth command are executed again, creating the new item.
Some problems with this system:
An alternative system that doesn't have these downsides: Write a single
/replaceitem
command for every possible amount of hearts. It only works with up to 63½ hearts anyway, so that's just a bit of effort.