Summon mob saved on item NBT

Started by Lewdoli on

Topic category: Help with modding (Java Edition)

Last seen on 11:55, 18. Jul 2024
Joined Aug 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Summon mob saved on item NBT

I have two procedures on my custom item: one that saves the mob NBT data when hit with the item and despawns the mob, and one that spawns the saved mob when right clicked on a block. Both procedures work fine until the spawning procedure gets to the last line of the custom code snippet that actually spawns the mob. No mob is spawned but other parts of the procedure work as intended.
 

Saving Procedure:

Custom code snippet:

itemstack.getOrCreateTag().put("entitySavedData", entity.saveWithoutId(new CompoundTag()));

Spawning Procedure:


Custom code snippet:

if (world instanceof ServerLevel _level) {
CompoundTag entityCompound = itemstack.getOrCreateTag().getCompound("entitySavedData");
Entity entityToSpawn = EntityType.loadEntityRecursive(entityCompound, _level, (e) -> {
           e.setPos(x, y + 1, z);
        e.setYRot(world.getRandom().nextFloat() * 360F);
           return e;
    });
_level.addFreshEntity(entityToSpawn);
}

Imgur link to all images: https://imgur.com/a/GYYTvtk