2

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

  • 1
    usually, you can use the JSON text components to specify a selector like this: "{\"selector\":\"@p\"}". This concept can be used on signs, the /tellraw command and for a few more things but not for something like this. Is your server a Bukkit/Spigot server by any chance? because if so, you can use a plugin for that. – MegaCrafter10 Nov 30 at 22:17
  • It's possible to do with loot tables, but a bit complicated. Good question! I'll figure it out later today and then answer if nobody does until then. – Fabian Röling Dec 1 at 0:37
1

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.

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.