Minecraft – How to create a copy of an entity using command blocks

minecraft-commandsminecraft-java-edition

More specifically, say there's a pile of items on the ground, for instance after someone dies, and I want to create an exact copy of each of those items (NBT tags and all), but in a different (specific) location. How would I go about doing that?

Taking the naive approach, the math gets pretty horrifying pretty quickly. For instance, adding a command block for every combination of every enchantment on every item that can hold those enchantments quickly becomes unmanageable. For instance, there are 6 enchantments that can be applied to every type of armour, there are 5 armour grades, 4 armor pieces at each grade, and at least 3 enchantment levels (with default enchanting) for these enchantments. That alone would take 720 command blocks (considering I need 2 command blocks to do the clone), to say nothing of the various combinations of enchantments that can be had within those 6, plus other enchantments available to individual armour pieces, and durability (oh god, the durability) of the items. Obviously, writing thousands (millions?) of command blocks with testfor @e[type=Item] {NBT:"some long and nested NBT tag} and summon Item x y z {NBT:"the same long and nested NBT tag} isn't feasible (and won't even produce the correct result). No, I need another way.

I've been through the commands page on the Minecraft wiki, and nothing jumped out at me. The closest command I could see is the clone command, but that only works on blocks and tile entities (i.e. chests, furnaces, and the like). Also, I know I can edit the NBT data directly, but the point is to be able to do this while the game is running.

Best Answer

My thought is to use a structure block, to clone the entity.

So this is what I'd do it:

  • First, spawn an armor stand (as a marker) in the location of the entity to be cloned.
  • Move the entity to a predefined location.
  • Clone it with the structure blocks.
  • Then move the entity back to the marker, and the new one where you want it.
  • Finally remove the marker.

I'll just show how to set up the structure blocks. I'll assume you know what your doing with the teleportation of the entities.

So the cloning device should look like this:

(The command block in the second picture removes the stone block when a red stone block is placed there.)

So the structure block on the left is the saving block, you place the entities to be cloned on top of it.

You must have "Include Entities" set to on, or it won't clone entities.

The block on the right loads the entities. (If you use red stone to save, it will only save to RAM, not your disk, which is good.)

(The structure name doesn't have to be "cloner", but it just has to be the same in both blocks.)

When you have the cloning device set up, you just have to teleport an entity on top of the structure block on the left, place a red stone block in place of the stone one, then one red stone tick later, you'll have two identical entities.

Necro edit: I though I'd clarify why there has to be a red stone tick between them, its because structure blocks are "slow" (compared to command blocks) and require time to save the structure data. A single red stone tick is simply easy to do, but it can be any amount of time, as long as it gives the structure blocks time to do their thing.