Java Code Examples for net.minecraft.entity.EntityList#createEntityFromNBT()

The following examples show how to use net.minecraft.entity.EntityList#createEntityFromNBT() . 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: EntityUtils.java    From litematica with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Nullable
private static Entity createEntityFromNBTSingle(NBTTagCompound nbt, World world)
{
    try
    {
        Entity entity = EntityList.createEntityFromNBT(nbt, world);

        if (entity != null)
        {
            entity.setUniqueId(UUID.randomUUID());
        }

        return entity;
    }
    catch (Exception e)
    {
        return null;
    }
}
 
Example 2
Source File: CraftHumanEntity.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
@Override
public org.bukkit.entity.Entity getShoulderEntityLeft() {
    if (!getHandle().getLeftShoulderEntity().hasNoTags()) {
        Entity shoulder = EntityList.createEntityFromNBT(getHandle().getLeftShoulderEntity(), getHandle().world);

        return (shoulder == null) ? null : shoulder.getBukkitEntity();
    }

    return null;
}
 
Example 3
Source File: CraftHumanEntity.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
@Override
public org.bukkit.entity.Entity getShoulderEntityRight() {
    if (!getHandle().getRightShoulderEntity().hasNoTags()) {
        Entity shoulder = EntityList.createEntityFromNBT(getHandle().getRightShoulderEntity(), getHandle().world);

        return (shoulder == null) ? null : shoulder.getBukkitEntity();
    }

    return null;
}
 
Example 4
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 5
Source File: BlockDrawingHelper.java    From malmo with MIT License 4 votes vote down vote up
/** Spawn a single entity at the specified position.
 * @param e the actual entity to be spawned.
 * @param w the world in which to spawn the entity.
 * @throws Exception
 */
private void DrawPrimitive( DrawEntity e, World w ) throws Exception
{
    String oldEntityName = e.getType().getValue();
    String id = null;
    for (EntityEntry ent : net.minecraftforge.fml.common.registry.ForgeRegistries.ENTITIES)
    {
       if (ent.getName().equals(oldEntityName))
       {
           id = ent.getRegistryName().toString();
           break;
       }
    }
    if (id == null)
        return;

    NBTTagCompound nbttagcompound = new NBTTagCompound();
    nbttagcompound.setString("id", id);
    nbttagcompound.setBoolean("PersistenceRequired", true); // Don't let this entity despawn
    Entity entity;
    try
    {
        entity = EntityList.createEntityFromNBT(nbttagcompound, w);
        if (entity != null)
        {
            positionEntity(entity, e.getX().doubleValue(), e.getY().doubleValue(), e.getZ().doubleValue(), e.getYaw().floatValue(), e.getPitch().floatValue());
            entity.setVelocity(e.getXVel().doubleValue(), e.getYVel().doubleValue(), e.getZVel().doubleValue());
            // Set all the yaw values imaginable:
            if (entity instanceof EntityLivingBase)
            {
                ((EntityLivingBase)entity).rotationYaw = e.getYaw().floatValue();
                ((EntityLivingBase)entity).prevRotationYaw = e.getYaw().floatValue();
                ((EntityLivingBase)entity).prevRotationYawHead = e.getYaw().floatValue();
                ((EntityLivingBase)entity).rotationYawHead = e.getYaw().floatValue();
                ((EntityLivingBase)entity).prevRenderYawOffset = e.getYaw().floatValue();
                ((EntityLivingBase)entity).renderYawOffset = e.getYaw().floatValue();
            }
            w.getBlockState(entity.getPosition());  // Force-load the chunk if necessary, to ensure spawnEntity will work.
            if (!w.spawnEntity(entity))
            {
                System.out.println("WARNING: Failed to spawn entity! Chunk not loaded?");
            }
        }
    }
    catch (RuntimeException runtimeexception)
    {
        // Cannot summon this entity.
        throw new Exception("Couldn't create entity type: " + e.getType().getValue());
    }
}
 
Example 6
Source File: PacketTransformationInfo.java    From Gadomancy with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void fromBytes(ByteBuf buf) {
    this.golem = (EntityGolemBase) EntityList.createEntityFromNBT(ByteBufUtils.readTag(buf), WORLD);
}
 
Example 7
Source File: SchematicEntity.java    From Framez with GNU General Public License v3.0 4 votes vote down vote up
public void writeToWorld(IBuilderContext context) {
	Entity e = EntityList.createEntityFromNBT(entityNBT, context.world());
	context.world().spawnEntityInWorld(e);
}
 
Example 8
Source File: ItemLivingManipulator.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
private EnumActionResult releaseEntity(ItemStack containerStack, EntityPlayer player, World world,
        BlockPos pos, double x, double y, double z, EnumFacing side)
{
    ItemStack moduleStack = this.getSelectedModuleStack(containerStack, ModuleType.TYPE_MEMORY_CARD_MISC);

    if (moduleStack.isEmpty())
    {
        return EnumActionResult.PASS;
    }

    NBTTagList tagList = NBTUtils.getTagList(moduleStack, WRAPPER_TAG_NAME, "Entities", Constants.NBT.TAG_COMPOUND, false);

    if (tagList == null || tagList.tagCount() == 0)
    {
        return EnumActionResult.PASS;
    }

    int current = NBTUtils.getByte(moduleStack, WRAPPER_TAG_NAME, "Current");
    int numEntities = tagList.tagCount();

    if (current >= numEntities)
    {
        current = (numEntities > 0) ? numEntities - 1 : 0;
    }

    NBTTagCompound tag = tagList.getCompoundTagAt(current);

    if (tag != null)
    {
        boolean isShulker = false;

        if (tag.getString("id").equals("minecraft:shulker") || tag.getString("id").equals("Shulker"))
        {
            // Special case to update the Shulker's attached position and position
            if (tag.hasKey("APX", Constants.NBT.TAG_INT))
            {
                int xi = pos.getX() + side.getXOffset();
                int yi = pos.getY() + side.getYOffset();
                int zi = pos.getZ() + side.getZOffset();

                tag.setTag("Pos", NBTUtils.writeDoubles(new double[] {xi + 0.5d, yi, zi + 0.5d}));
                tag.setInteger("APX", xi);
                tag.setInteger("APY", yi);
                tag.setInteger("APZ", zi);
                tag.setByte("AttachFace", (byte)side.getIndex());
                isShulker = true;
            }
        }

        Entity entity = EntityList.createEntityFromNBT(tag, world);

        if (entity == null)
        {
            return EnumActionResult.FAIL;
        }

        if (isShulker == false)
        {
            if (tag.hasKey("playerYaw", Constants.NBT.TAG_FLOAT))
            {
                float yawDiff = player.rotationYaw - tag.getFloat("playerYaw");
                entity.rotationYaw = (entity.rotationYaw + yawDiff) % 360;
            }

            PositionHelper posHelper = new PositionHelper(x, y, z);
            posHelper.adjustPositionToTouchFace(entity, side);
            entity.setLocationAndAngles(posHelper.posX, posHelper.posY, posHelper.posZ, entity.rotationYaw, entity.rotationPitch);
        }

        entity.motionY = 0.0;
        entity.fallDistance = 0.0f;
        entity.onGround = true;

        if (entity instanceof EntityLiving && this.getInstalledModuleCount(containerStack, ModuleType.TYPE_MOBPERSISTENCE) > 0)
        {
            EntityUtils.applyMobPersistence((EntityLiving)entity);
        }

        world.spawnEntity(entity);
        tagList.removeTag(current);
    }

    numEntities = tagList.tagCount();

    if (current >= numEntities)
    {
        current = (numEntities > 0) ? numEntities - 1 : 0;
    }

    NBTUtils.setByte(moduleStack, WRAPPER_TAG_NAME, "Current", (byte)current);
    this.setSelectedModuleStack(containerStack, ModuleType.TYPE_MEMORY_CARD_MISC, moduleStack);

    return EnumActionResult.SUCCESS;
}
 
Example 9
Source File: TemplateEnderUtilities.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void addEntitiesToWorld(World world, BlockPos posStart)
{
    if (this.placement.getIgnoreEntities())
    {
        return;
    }

    Mirror mirror = this.placement.getMirror();
    Rotation rotation = this.placement.getRotation();

    BlockPos posEnd = posStart.add(PositionUtils.getRelativeEndPositionFromAreaSize(this.size));
    BlockPos pos1 = PositionUtils.getMinCorner(posStart, posEnd);
    BlockPos pos2 = PositionUtils.getMaxCorner(posStart, posEnd).add(1, 1, 1);
    List<Entity> existingEntities = world.getEntitiesWithinAABBExcludingEntity(null, new AxisAlignedBB(pos1, pos2));

    for (TemplateEnderUtilities.TemplateEntityInfo entityInfo : this.entities)
    {
        BlockPos pos = transformedBlockPos(this.placement, entityInfo.blockPos).add(posStart);

        NBTTagCompound nbt = entityInfo.entityData;
        UUID uuidOriginal = nbt.getUniqueId("UUID");
        Vec3d vec3d = PositionUtils.transformedVec3d(entityInfo.pos, mirror, rotation);
        Vec3d vec3d1 = vec3d.add((double)posStart.getX(), (double)posStart.getY(), (double)posStart.getZ());
        NBTTagList tagList = new NBTTagList();
        tagList.appendTag(new NBTTagDouble(vec3d1.x));
        tagList.appendTag(new NBTTagDouble(vec3d1.y));
        tagList.appendTag(new NBTTagDouble(vec3d1.z));
        nbt.setTag("Pos", tagList);
        nbt.setUniqueId("UUID", UUID.randomUUID());
        Entity entity;

        try
        {
            entity = EntityList.createEntityFromNBT(nbt, world);
        }
        catch (Exception e)
        {
            entity = null;
        }

        if (entity != null)
        {
            if (entity instanceof EntityPainting)
            {
                entity.getMirroredYaw(mirror);
                entity.getRotatedYaw(rotation);
                entity.setPosition(pos.getX(), pos.getY(), pos.getZ());
                entity.setLocationAndAngles(vec3d1.x, vec3d1.y, vec3d1.z, entity.rotationYaw, entity.rotationPitch);
            }
            else
            {
                float f = entity.getMirroredYaw(mirror);
                f = f + (entity.rotationYaw - entity.getRotatedYaw(rotation));
                entity.setLocationAndAngles(vec3d1.x, vec3d1.y, vec3d1.z, f, entity.rotationPitch);
                // FIXME should call updateFacingWithBoundingBox(EnumFacing) for EntityHanging, otherwise a world reload is needed
                // for ItemFrames for example to visually update to the correct facing
            }

            // Use the original UUID if possible. If there is an entity with the same UUID within the pasted area,
            // then the old one will be killed. Otherwise if there is no entity currently in the world with
            // the same UUID, then the original UUID will be used.
            Entity existing = EntityUtils.findEntityByUUID(existingEntities, uuidOriginal);
            if (existing != null)
            {
                world.removeEntityDangerously(existing);
                entity.setUniqueId(uuidOriginal);
            }
            else if (world instanceof WorldServer && ((WorldServer) world).getEntityFromUuid(uuidOriginal) == null)
            {
                entity.setUniqueId(uuidOriginal);
            }

            world.spawnEntity(entity);
        }
    }
}