Solved Save entity to file (with complete entity data)

Discussion in 'Spigot Plugin Development' started by TaskID, Nov 29, 2020.

  1. Hello, what would be the best way to save animals to a file to spawn them again later?
    I want to make something like a Mob Transporter, where I can pick up mobs and spawn them somewhere else later. What would be the best way to do so without hardcoding the entity types? I also want that the Custom Name, Color (Sheep), and other stats (Villager, what he trades etc.) gets saved.. how would I do that?

    Solved:
    Getting the entity data:
    Code (Java):
    net.minecraft.server.v1_16_R3.Entity nmsEntity = ((CraftEntity) /* Pass the org.bukkit.entity */).getHandle();
    NBTTagCompound nbtTagCompound = new NBTTagCompound();
    nmsEntity.save(nbtTagCompound);
    String result = nbtTagCompound.toString();

    Applying the entity data:
    Code (Java):
    Location location = /* The location where the entity should be spawned */;
    Entity entity = location.getWorld().spawnEntity(location, /* Pass the entity type here */);
    net.minecraft.server.v1_16_R3.Entity nmsEntity = ((CraftEntity) entity).getHandle();
    try {
        nmsEntity.load(MojangsonParser.parse(/* Pass the data string here */);
    } catch (CommandSyntaxException ex) {
        ex.printStackTrace();
    }
    entity.teleport(location);
    (if someone knows a better way to spawn the entity, so applying the data before the spawning the entity, please let me know)
     
    #1 TaskID, Nov 29, 2020
    Last edited: Nov 29, 2020
  2. Maybe would be more easier save a mob in a file but (Custom Name, Items in hand, armor, and so) and then spawn it in location, when you want, cuz mobs don't have Name or UUID like players