Java Code Examples for net.minecraft.world.World#spawnEntity()

The following examples show how to use net.minecraft.world.World#spawnEntity() . These examples are extracted from open source projects. 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
@Override
public void onBreak(World world, BlockPos blockPos, BlockState blockState, PlayerEntity playerEntity) {
    super.onBreak(world, blockPos, blockState, playerEntity);
    BlockEntity blockEntity = world.getBlockEntity(blockPos);

    if (blockEntity != null) {
        if (blockEntity instanceof OxygenCollectorBlockEntity) {
            OxygenCollectorBlockEntity be = (OxygenCollectorBlockEntity) blockEntity;

            for (int i = 0; i < be.getInventory().getSize(); i++) {
                ItemStack itemStack = be.getInventory().getStack(i);

                if (itemStack != null) {
                    world.spawnEntity(new ItemEntity(world, blockPos.getX(), blockPos.getY() + 1, blockPos.getZ(), itemStack));
                }
            }
        }
    }
}
 
Example 2
private void dropInventory(World world, BlockPos blockPos) {
    BlockEntity blockEntity = world.getBlockEntity(blockPos);

    if (blockEntity != null) {
        if (blockEntity instanceof BasicSolarPanelBlockEntity) {
            BasicSolarPanelBlockEntity basicSolarPanelBlockEntity = (BasicSolarPanelBlockEntity) blockEntity;

            for (int i = 0; i < basicSolarPanelBlockEntity.getInventory().getSize(); i++) {
                ItemStack itemStack = basicSolarPanelBlockEntity.getInventory().getStack(i);

                if (itemStack != null) {
                    world.spawnEntity(new ItemEntity(world, blockPos.getX(), blockPos.getY() + 1, blockPos.getZ(), itemStack));
                }
            }
        }
    }
}
 
Example 3
Source Project: AdvancedRocketry   File: BlockPress.java    License: MIT License 6 votes vote down vote up
private void checkForMove(World worldIn, BlockPos pos, IBlockState state)
{
	EnumFacing enumfacing = EnumFacing.DOWN;
	boolean flag = this.shouldBeExtended(worldIn, pos, enumfacing);

	ItemStack stack;
	if (flag && (stack = getRecipe(worldIn, pos, state)) != null && !((Boolean)state.getValue(EXTENDED)).booleanValue())
	{
		worldIn.setBlockToAir(pos.down());

		if(!worldIn.isRemote)
			worldIn.spawnEntity(new EntityItem(worldIn, pos.getX() + 0.5, pos.getY() - 0.5, pos.getZ() + 0.5, stack));
		if ((new BlockPistonStructureHelper(worldIn, pos, enumfacing, true)).canMove())
		{
			worldIn.addBlockEvent(pos, this, 0, enumfacing.getIndex());
		}
	}
	else if (!flag && ((Boolean)state.getValue(EXTENDED)).booleanValue())
	{
		worldIn.addBlockEvent(pos, this, 1, enumfacing.getIndex());
	}
}
 
Example 4
@Override
public void onBreak(World world, BlockPos blockPos, BlockState blockState, PlayerEntity playerEntity) {
    super.onBreak(world, blockPos, blockState, playerEntity);

    BlockEntity blockEntity = world.getBlockEntity(blockPos);

    if (blockEntity != null) {
        if (blockEntity instanceof ElectricCompressorBlockEntity) {
            ElectricCompressorBlockEntity be = (ElectricCompressorBlockEntity) blockEntity;

            for (int i = 0; i < be.getInventory().getSize(); i++) {
                ItemStack itemStack = be.getInventory().getStack(i);

                if (!itemStack.isEmpty()) {
                    world.spawnEntity(new ItemEntity(world, blockPos.getX(), blockPos.getY() + 1, blockPos.getZ(), itemStack.copy()));
                }
            }
        }
    }
}
 
Example 5
Source Project: Production-Line   File: ItemGravityRay.java    License: MIT License 6 votes vote down vote up
/**
 * called when the player releases the use item button. Args: itemstack, world, entityplayer, itemInUseCount
 */
@Override
public void onPlayerStoppedUsing(ItemStack itemStack, World world, EntityLivingBase player, int itemInUseCount) {
    if (ElectricItem.manager.getCharge(itemStack) >= 100) {
        int i = this.getMaxItemUseDuration(itemStack) - itemInUseCount;

        float damge = (float) i / 20.0F;
        damge = (damge * damge + damge * 2.0F) / 3.0F;
        if ((double)damge < 0.1D) {
            return;
        }
        if (damge > 1.0F) {
            damge = 1.0F;
        }

        world.playSound(null, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_ARROW_SHOOT,
                SoundCategory.PLAYERS, 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + damge * 0.5F);
        if (!(player instanceof EntityPlayer
                && ((EntityPlayer) player).capabilities.isCreativeMode)) {
            ElectricItem.manager.discharge(itemStack, 100, this.tier, false, true, false);
        }
        if (!world.isRemote) {
            world.spawnEntity(new EntityRay(world, player, damge * 2.0F));
        }
    }
}
 
Example 6
@Nonnull
@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, @Nonnull EnumHand hand) {
	ItemStack stack = player.getHeldItem(hand);

	if (!world.isRemote) {
		EntityBomb bomb = new EntityBomb(world, player);
		bomb.setBombItem(stack);
		bomb.setPosition(player.posX, player.posY + player.eyeHeight, player.posZ);
		bomb.shoot(player, player.rotationPitch, player.rotationYaw, 0.0f, 1.5f, 1.0f);
		stack.shrink(1);
		world.spawnEntity(bomb);
		bomb.velocityChanged = true;
	}

	player.getCooldownTracker().setCooldown(this, 20);

	return new ActionResult<>(EnumActionResult.PASS, stack);
}
 
Example 7
@Override
protected boolean specialBlockHandling(World world, String c, int x, int y, int z) {
	if (!c.equals("xx")) {
		return false;
	}
	
	setBlockState(world, Blocks.AIR.getDefaultState(), x, y, z, boundingBox);

	int j = this.getXWithOffset(x, z);
	int k = this.getYWithOffset(y);
	int l = this.getZWithOffset(x, z);

	/*
	 * if (!structurebb.isVecInside(new BlockPos(j, k, l))) { return; }
	 */

	EntityArmorStand stand = new EntityArmorStand(world);
	stand.setLocationAndAngles((double) j + 0.5D, (double) k, (double) l + 0.5D, 90F, 0.0F);
	world.spawnEntity(stand);

	List<String> entities = new ArrayList<String>();
	entities.add(ToroQuest.MODID + ":" + EntityGuard.NAME);
	specialHandlingForSpawner(world, "xx", c, x, y, z, entities);

	return true;

}
 
Example 8
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand hand) {
  ItemStack itemStackIn = playerIn.getHeldItem(hand);
  if (!playerIn.capabilities.isCreativeMode) {
    itemStackIn.shrink(1);
  }
  worldIn.playSound(playerIn, playerIn.getPosition(), SoundEvents.ENTITY_ARROW_SHOOT, SoundCategory.BLOCKS, 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
  if (!worldIn.isRemote) {    
    EntityOwlEgg entityEgg = new EntityOwlEgg(worldIn, playerIn);
    //without setHeading the egg just falls to the players feet
    entityEgg.setHeadingFromThrower(playerIn, playerIn.rotationPitch, playerIn.rotationYaw, PITCHOFFSET, VELOCITY_DEFAULT, INACCURACY_DEFAULT);
    worldIn.spawnEntity(entityEgg);
  }
  return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemStackIn);
}
 
Example 9
private static void dropListOfItems(World world, BlockPos pos, List<ItemStack> drops) {
    Random rand = new Random();
    for (ItemStack stack : drops) {
        float f = 0.7F;
        double offX = (rand.nextFloat() * f) + (1.0F - f) * 0.5D;
        double offY = (rand.nextFloat() * f) + (1.0F - f) * 0.5D;
        double offZ = (rand.nextFloat() * f) + (1.0F - f) * 0.5D;
        EntityItem entityItem = new EntityItem(world, pos.getX() + offX, pos.getY() + offY, pos.getZ() + offZ, stack);
        entityItem.setDefaultPickupDelay();
        world.spawnEntity(entityItem);
    }
}
 
Example 10
Source Project: AdvancedRocketry   File: BlockSeat.java    License: MIT License 5 votes vote down vote up
@Override
public boolean onBlockActivated(World world, BlockPos pos,
		IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY,
		float hitZ) {
	
	if(!world.isRemote) {
		List<Entity> list = world.getEntitiesWithinAABBExcludingEntity(player, new AxisAlignedBB(pos, pos.add(1,1,1)));

		//Try to mount player to dummy entity in the block
		for(Entity e : list) {
			if(e instanceof EntityDummy) {
				if(!e.getPassengers().isEmpty()) {
					return true;
				}
				else {
					//Ensure that the entity is in the correct position
					e.setPosition(pos.getX() + 0.5f, pos.getY() + 0.2f, pos.getZ() + 0.5f);
					player.startRiding(e);
					return true;
				}
			}
		}
		EntityDummy entity = new EntityDummy(world, pos.getX() + 0.5f, pos.getY() + 0.2f, pos.getZ() + 0.5f);
		world.spawnEntity(entity);
		player.startRiding(entity);
	}

	return true;
}
 
Example 11
protected void spawnVillagers(World worldIn, StructureBoundingBox structurebb, int x, int y, int z, int count,int profession) {
    if (this.villagersSpawned < count) {
        for (int i = this.villagersSpawned; i < count; ++i) {
            int j = this.getXWithOffset(x + i, z);
            int k = this.getYWithOffset(y);
            int l = this.getZWithOffset(x + i, z);

            if (!structurebb.isVecInside(new BlockPos(j, k, l))) {
                break;
            }

            ++this.villagersSpawned;

            if (this.field_189929_i) {
                EntityZombieVillager entityzombie = new EntityZombieVillager(worldIn);
                entityzombie.setLocationAndAngles((double) j + 0.5D, (double) k, (double) l + 0.5D, 0.0F, 0.0F);
                entityzombie.onInitialSpawn(worldIn.getDifficultyForLocation(new BlockPos(entityzombie)), (IEntityLivingData) null);
                entityzombie.enablePersistence();
                worldIn.spawnEntity(entityzombie);
            } else {
                EntityTofunian entityvillager = new EntityTofunian(worldIn);
                entityvillager.setLocationAndAngles((double) j + 0.5D, (double) k, (double) l + 0.5D, 0.0F, 0.0F);
                entityvillager.onInitialSpawn(worldIn.getDifficultyForLocation(new BlockPos(entityvillager)), (IEntityLivingData) null);
                entityvillager.setTofuProfession(profession);
                worldIn.spawnEntity(entityvillager);
            }
        }
    }
}
 
Example 12
Source Project: TFC2   File: Core.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void dropItem(World world, double posX, double posY, double posZ, ItemStack is)
{
	EntityItem ei = new EntityItem(world, posX, posY, posZ, is);
	ei.motionX = -0.07+world.rand.nextFloat() * 0.14;
	ei.motionY = 0.15;
	ei.motionZ = -0.07+world.rand.nextFloat() * 0.14;
	world.spawnEntity(ei);
}
 
Example 13
@Redirect(method = "dropStack(Lnet/minecraft/item/ItemStack;F)Lnet/minecraft/entity/ItemEntity;",
		at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;spawnEntity(Lnet/minecraft/entity/Entity;)Z"))
private boolean hookDropStackForCapture(World world, Entity entity) {
	ItemEntity itemEntity = (ItemEntity) entity;

	if (captureDrops() != null) {
		captureDrops().add(itemEntity);
		return true;
	} else {
		return world.spawnEntity(itemEntity);
	}
}
 
Example 14
@NotNull
@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
	ItemStack stack = player.getHeldItem(hand);
	if (stack.isEmpty()) return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);
	if (worldIn.isRemote) return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);

	FairyData object = FairyData.deserialize(NBTHelper.getCompound(stack, "fairy"));
	if (object == null) return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);

	TileEntity tileEntity = worldIn.getTileEntity(pos);
	if (tileEntity instanceof TileJar) {
		TileJar jar = (TileJar) tileEntity;

		if (jar.fairy != null) return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);

		jar.fairy = object;
		jar.markDirty();
		worldIn.checkLight(pos);

	} else {

		BlockPos offsetpos = pos.offset(facing);
		EntityFairy entity = new EntityFairy(worldIn, object);
		entity.setPosition(offsetpos.getX() + 0.5, offsetpos.getY() + 0.5, offsetpos.getZ() + 0.5);
		entity.originPos = offsetpos;

		worldIn.spawnEntity(entity);
	}

	stack.shrink(1);
	worldIn.playSound(pos.getX(), pos.getY(), pos.getZ(), ModSounds.FAIRY, SoundCategory.BLOCKS, 1, 1, false);

	return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);
}
 
Example 15
@Redirect(method = "dropItem(Lnet/minecraft/item/ItemStack;ZZ)Lnet/minecraft/entity/ItemEntity;",
		at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;spawnEntity(Lnet/minecraft/entity/Entity;)Z"))
private boolean hookDropItemForCapture(World world, Entity entity) {
	ItemEntity itemEntity = (ItemEntity) entity;
	IForgeEntity forgeEntity = (IForgeEntity) this;

	if (forgeEntity.captureDrops() != null) {
		forgeEntity.captureDrops().add(itemEntity);
		return true;
	} else {
		return world.spawnEntity(itemEntity);
	}
}
 
Example 16
Source Project: Valkyrien-Skies   File: ValkyrienUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static void fixEntityToShip(Entity toFix, Vector posInLocal,
    PhysicsObject mountingShip) {
    World world = mountingShip.world();
    EntityMountable entityMountable = new EntityMountable(world, posInLocal.toVec3d(),
        CoordinateSpaceType.SUBSPACE_COORDINATES, mountingShip.getReferenceBlockPos());
    world.spawnEntity(entityMountable);
    toFix.startRiding(entityMountable);
}
 
Example 17
Source Project: TFC2   File: BlockThatch.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void createFallingEntity(World world, BlockPos pos, IBlockState state)
{
	world.setBlockToAir(pos);
	EntityItem ei = new EntityItem(world, pos.getX()+0.5, pos.getY(), pos.getZ()+0.5, new ItemStack(TFCItems.Straw, 1+world.rand.nextInt(3)));
	world.spawnEntity(ei);
}
 
Example 18
protected void onIgnitedByNeighbour(World world, int x, int y, int z, EntityLivingBase placedBy) {
  EntityPrimedCharge entity = new EntityPrimedCharge(this, world, x + 0.5F, y + 0.5F, z + 0.5F, placedBy);
  entity.setFuse(world.rand.nextInt(entity.getFuse() / 4) + entity.getFuse() / 8);
  world.spawnEntity(entity);
}
 
Example 19
Source Project: the-hallow   File: CraftingFluidBlock.java    License: MIT License 4 votes vote down vote up
private void spawnCraftingResult(World world, BlockPos pos, ItemStack result) {
	ItemEntity itemEntity = new ItemEntity(world, pos.getX() + .5, pos.getY() + 1, pos.getZ() + .5, result);
	world.spawnEntity(itemEntity);
	// todo: add particles and/or an animation when dropping the recipe result
}
 
Example 20
private void spawnMage(World world, BlockPos pos) {
	EntityMage e = new EntityMage(world);
	e.setPosition(pos.getX() + 3, pos.getY() + (floors * floorHieght) + 1, pos.getZ() + 3);
	world.spawnEntity(e);
}