Java Code Examples for net.minecraft.entity.Entity#writeToNBTOptional()

The following examples show how to use net.minecraft.entity.Entity#writeToNBTOptional() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchematicCreationUtils.java    From litematica with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void takeEntitiesFromWorld(LitematicaSchematic schematic, World world, List<SelectionBox> boxes, BlockPos origin)
{
    for (SelectionBox box : boxes)
    {
        String regionName = box.getName();
        ISchematicRegion region = schematic.getSchematicRegion(regionName);
        List<EntityInfo> schematicEntityList = region != null ? region.getEntityList() : null;

        if (schematicEntityList == null)
        {
            InfoUtils.showGuiOrInGameMessage(MessageType.ERROR, "litematica.message.error.schematic_save.missing_entity_list", box.getName());
            continue;
        }

        AxisAlignedBB bb = PositionUtils.createEnclosingAABB(box.getPos1(), box.getPos2());
        BlockPos regionPosAbs = box.getPos1();
        List<EntityInfo> list = new ArrayList<>();
        List<Entity> entities = world.getEntitiesInAABBexcluding(null, bb, null);

        for (Entity entity : entities)
        {
            NBTTagCompound tag = new NBTTagCompound();

            if (entity.writeToNBTOptional(tag))
            {
                Vec3d posVec = new Vec3d(entity.posX - regionPosAbs.getX(), entity.posY - regionPosAbs.getY(), entity.posZ - regionPosAbs.getZ());
                NBTUtils.writeVec3dToListTag(posVec, tag);
                list.add(new EntityInfo(posVec, tag));
            }
        }

        schematicEntityList.addAll(list);
    }
}
 
Example 2
Source File: EntityUtils.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new, identical instance of the given entity, by writing it to NBT
 * and then constructing a new entity based on that NBT data.
 * @param entityOld the original entity
 * @return the new entity, or null if the operation failed
 */
@Nullable
public static Entity recreateEntityViaNBT(Entity entityOld)
{
    NBTTagCompound tag = new NBTTagCompound();
    Entity entityNew = null;

    if (entityOld.writeToNBTOptional(tag))
    {
        entityNew = EntityList.createEntityFromNBT(tag, entityOld.getEntityWorld());
    }

    return entityNew;
}
 
Example 3
Source File: SchematicCreationUtils.java    From litematica with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void takeEntitiesFromWorldWithinChunk(LitematicaSchematic schematic, World world, int chunkX, int chunkZ,
        ImmutableMap<String, IntBoundingBox> volumes, ImmutableMap<String, SelectionBox> boxes, Set<UUID> existingEntities, BlockPos origin)
{
    for (Map.Entry<String, IntBoundingBox> entry : volumes.entrySet())
    {
        String regionName = entry.getKey();
        Box box = boxes.get(regionName);
        ISchematicRegion region = schematic.getSchematicRegion(regionName);
        List<EntityInfo> schematicEntityList = region != null ? region.getEntityList() : null;

        if (box == null || schematicEntityList == null)
        {
            InfoUtils.showGuiOrInGameMessage(MessageType.ERROR, "litematica.message.error.schematic_save.missing_entity_list", regionName);
            continue;
        }

        AxisAlignedBB bb = PositionUtils.createAABBFrom(entry.getValue());
        List<Entity> entities = world.getEntitiesInAABBexcluding(null, bb, null);
        BlockPos regionPosAbs = box.getPos1();

        for (Entity entity : entities)
        {
            UUID uuid = entity.getUniqueID();
            /*
            if (entity.posX >= bb.minX && entity.posX < bb.maxX &&
                entity.posY >= bb.minY && entity.posY < bb.maxY &&
                entity.posZ >= bb.minZ && entity.posZ < bb.maxZ)
            */
            if (existingEntities.contains(uuid) == false)
            {
                NBTTagCompound tag = new NBTTagCompound();

                if (entity.writeToNBTOptional(tag))
                {
                    Vec3d posVec = new Vec3d(entity.posX - regionPosAbs.getX(), entity.posY - regionPosAbs.getY(), entity.posZ - regionPosAbs.getZ());
                    NBTUtils.writeVec3dToListTag(posVec, tag);
                    schematicEntityList.add(new EntityInfo(posVec, tag));
                    existingEntities.add(uuid);
                }
            }
        }
    }
}
 
Example 4
Source File: SchematicEntity.java    From Framez with GNU General Public License v3.0 4 votes vote down vote up
public void readFromWorld(IBuilderContext context, Entity entity) {
	entity.writeToNBTOptional(entityNBT);
}