Minecraft – how to get and store player name

minecraft-commandsminecraft-java-edition

I have a set of command blocks that do the following:

  1. Test that all players have a skull in their inventory
  2. if not, give all players a skull
  3. kill all players

basicly, if someone drops the skull they are given another and then killed. (a way of getting back to their spawn point).

obviously, in multilayer this causes some problems.

  1. if one player does not have the skull, the test runs positive even if others do have it.
  2. all players are then given another skull, leaving those who did not drop it with more than one.
  3. all players are then killed.

is there a way where i can store the player's name who dropped the skull, then only take the following actions on them?

basicly, i want to be able to dynamicly replace @a with a player name.

also, is there a way that i can detect if a player has right clicked the item in their hot bar rather than if they have dropped it?

Best Answer

Context:

In the Minecraft 14w08a, a snapshot between 1.8 and 1.7, you can now test for an item in a player's inventory by clearing 0 items of that type, as in the snapshot, it added the ability to specify the number of items to remove.

/clear [Player] [Item] [Data] [Count] [NBT Tags]

Except, it means that we have to identify the person and there is no way that you can capture the person in this. So, we need to give the player an identifier.


How do we do it?

enter image description here
This is a module. Repeat this contraption as many times as the number of players you want to track. So, 50 players means making this 50 times, but substituting the playerID for a different number, preferably plus one to the previous one.

Step 1: Create an identifier.
Using command blocks, you'll have to assign each player you want to test a value in a dummy score. Let's make a score called playerID that is a dummy value.

/scoreboard objectives add playerID dummy playerID

So, whenever you want to test for a player (if it's a PvP/command-block related map, all players have to be assigned a number when the connect/join the game; not spectating).

Step 1b.

Create a gate. All players have to go onto it when the game starts. When the first player steps on it, they are assigned a scoreboard value in the objective "playerID". This is what we'll be using to identify each player.

/scoreboard players set @p playerID 1

Now, the next player should either have to go onto another pressure plate, or you can make that same pressure plate give a different number (more complicated, not going to say how).
Based on your comments, make a second gate for the second player and so on.

This gate should have /scoreboard players set @p playerID 2 and so on.

Step 2: Now, we'll have to a /testfor for each player.

Place a fast clock (at about 10Hz; turns on 10 times a second). You may go faster - But it will not work with repeaters/comparitors etc. That pulse goes into a command block with this command:

/clear @p[score_playerID=1,score_playerID_min=1] minecraft:Skull 0 0

This will clear the player with the playerID score of 1 zero Skulls.
If the player has a Skull in their inventory, it'll clear none of it and output 'true'.
If the player doesn't have a Skull in their inventory... (Although it's trying to clear no skulls, it checks if there are skulls. /clear commands output false if the item is not found, reguardless of how many needs to be removed.)

So, place a comparitor facing out of that /clear [blah blah blah] command block outputting to another command block. Now, let's put the "Oh, this guy fits this citeria."

/scoreboard objectives create hasSkull dummy hasSkull
/scoreboard players set @p[score_playerID=1,score_playerID_min=1] hasSkull 1

If you want the score to automatically revert to 0 when the player loses the skull, just make a flip-flop gate (one that outputs to the left if the input is on, and to the right if the input is off etc.), with when the input is false, it outputs a signal to a command block that does:

/scoreboard players set @p[score_playerID=1,score_playerID_min=1] hasSkull 0

Step 3: The annoying bit.
Now, repeat as many modules as you need for players. This time, change the score you're testing for, for example: /clear @p[score_playerID=2,score_playerID_min=2] minecraft:Skull 0 0 instead of /clear @p[score_playerID=1,score_playerID_min=1] minecraft:Skull 0 0 etcra.

Step 4: Doing something with them.
Now that you have an objective that gives a score of '1' to anyone with a skull in their inventory... Or '0' to those without one, you can just make a command block which does things to the players with the score of 1.

Create a command block that tests for this, on a 20Hz clock (refreshes 20 times a second, the fastest possible clock)... Or slower if you wish.

/[something] @p[score_hasSkull=1,score_hasSkull_min=1] [more parameters]

For example: /kill @a[score_hasSkull=1,score_hasSkull_min=1] (Kills everyone who has a skull)