Minecraft – How to get a random player’s head

minecraft-commandsminecraft-java-edition

I want to give myself a random (@r) player's head on my server but I don't know if this is possible.

I'm on Minecraft 1.14.4 and when I place minecraft:give @p player_head{SkullOwner:"@r"} into a command block, it simply gives me the player head of "@r"

(I also found that without putting the quotation marks around the @r in the command, the command wouldn't be valid)

give command in command block

player head of @r in hotbar

Best Answer

You need a loot table (archive) and the command /loot (archive) for this.

Firstly, the loot table that gives (or summons or inserts or whatever) the head of the player it is invoked on.
Minimal file structure of the datapack:

<datapack_name>
├pack.mcmeta
└data
 └head_loot
  └loot_tables
   └head.json

Here head_loot is an arbitrarily chosen namespace, which is reused in the command later. The same with head.json, only the file extension needs to be .json.

Contents of pack.mcmeta can vary depending on what you want, but this is a minimum example:

{"pack":{"pack_format":3,"description":""}}

Contents of head.json:

{
 "pools": [
  {
   "rolls": 1,
   "entries": [
    {
     "type":"minecraft:item",
     "name":"minecraft:player_head",
     "functions":
      [
       {
        "function":"fill_player_head",
        "entity":"this"
       }
      ]
    }
   ]
  }
 ]
}

Now you can create heads of arbitrary (currently online) players. There are multiple ways to do this, the one that is actually intended for exactly this is:

/execute as @r run loot spawn ~ ~ ~ loot head_loot:head

This runs "as" a random player, but not "at" their position and summons a player head item entity at the execution position (a command block or whatever).

Make sure that the datapack is enabled, this command tells you that:

/datapack list

If you just changed something in the files, use /reload.