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.
When a player is a passenger to an entity, that player is not saved with the entity in the region files (side-note that Player
is not a valid entity ID and is simply a feature of selector syntax).
The entity is instead saved with the player via the RootVehicle
compound in player files and is no longer saved within the chunk region files. Example structure within player.dat
:
{
Sleeping:0b,
Inventory:[],
RootVehicle:{
AttachLeast:1l,
AttachMost:1l,
Entity:{
id:"MinecartRideable"
}
}
}
To detect when a player is riding anything, you'll check the RootVehicle
tag:
/scoreboard players set @a ridingCart 1 {RootVehicle:{Entity:{id:"MinecartRideable"}}}
The entity, when ridden by a player, will not have the Passengers
tag containing the player and will not exist if only the player is riding it. If there are non-player entities riding the same vehicle as the player (such as a pig and player inside a boat), the Passengers
tag will exist on the boat within RootVehicle
but will only contain the pig.
If the vehicle has non-player passengers but no player passenger, Passengers
will exist like normal and the vehicle is stored within region files.
If the vehicle itself is riding another entity (such as a boat riding an armor stand), and the player rides the vehicle, the AttachLeast
and AttachMost
will be the UUID of the vehicle the player is riding (the boat) while Entity
starts with the bottom-most entity (the armor stand), complete with Passengers
containing the boat.
Note that thus far into 1.9 (15w47c), RootVehicle
is only available on players, meaning you cannot detect what a non-player entity is a passenger of.
Best Answer
@a
can detect dead players, so you use it instead of@e
if all you want to detect is players: