Java Code Examples for net.minecraft.entity.item.EntityItem#setPickupDelay()

The following examples show how to use net.minecraft.entity.item.EntityItem#setPickupDelay() . 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
Source Project: WearableBackpacks   File: WorldUtils.java    License: MIT License 6 votes vote down vote up
/** Spawns an ItemStack as if it was dropped from an entity on death. */
public static EntityItem dropStackFromEntity(Entity entity, ItemStack stack, float speed) {
	EntityPlayer player = ((entity instanceof EntityPlayer) ? (EntityPlayer)entity : null);
	EntityItem item;
	if (player == null) {
		double y = entity.posY + entity.getEyeHeight() - 0.3;
		item = spawnItem(entity.world, entity.posX, y, entity.posZ, stack);
		if (item == null) return null;
		item.setPickupDelay(40);
		float f1 = RandomUtils.getFloat(0.5F);
		float f2 = RandomUtils.getFloat((float)Math.PI * 2.0F);
		item.motionX = -Math.sin(f2) * f1;
		item.motionY = 0.2;
		item.motionZ = Math.cos(f2) * f1;
		return item;
	} else item = player.dropItem(stack, true, false);
	if (item != null) {
		item.motionX *= speed / 4;
		item.motionZ *= speed / 4;
	}
	return item;
}
 
Example 2
Source Project: OpenModsLib   File: DropItemAction.java    License: MIT License 6 votes vote down vote up
@Override
public void usePlayer(OpenModsFakePlayer player) {
	player.setPositionAndRotation(x, y - player.getEyeHeight(), z, yaw, pitch);

	final EntityItem itemToDrop = new EntityItem(player.getEntityWorld(), x, y, z, stack.copy());
	itemToDrop.setPosition(itemToDrop.posX, itemToDrop.posY - itemToDrop.height, itemToDrop.posZ);
	itemToDrop.setPickupDelay(40);

	ItemTossEvent event = new ItemTossEvent(itemToDrop, player);
	if (MinecraftForge.EVENT_BUS.post(event)) {
		Log.info("Item %s drop from this %s aborted by event", stack, this);
	} else {
		final EntityItem droppedItem = event.getEntityItem();

		droppedItem.motionX = v.x;
		droppedItem.motionY = v.y;
		droppedItem.motionZ = v.z;

		player.getEntityWorld().spawnEntity(droppedItem);
	}
}
 
Example 3
protected void moveItemsInEffectRange() {
    List<EntityItem> itemsInRange = getWorld().getEntitiesWithinAABB(EntityItem.class, areaBoundingBox);
    for (EntityItem entityItem : itemsInRange) {
        double distanceX = (areaCenterPos.getX() + 0.5) - entityItem.posX;
        double distanceZ = (areaCenterPos.getZ() + 0.5) - entityItem.posZ;
        double distance = MathHelper.sqrt(distanceX * distanceX + distanceZ * distanceZ);
        if(!itemFilter.testItemStack(entityItem.getItem())) {
            continue;
        }
        if (distance >= 0.7) {
            if(!entityItem.cannotPickup()) {
                double directionX = distanceX / distance;
                double directionZ = distanceZ / distance;
                entityItem.motionX = directionX * MOTION_MULTIPLIER * getTier();
                entityItem.motionZ = directionZ * MOTION_MULTIPLIER * getTier();
                entityItem.velocityChanged = true;
                entityItem.setPickupDelay(1);
            }
        } else {
            ItemStack itemStack = entityItem.getItem();
            ItemStack remainder = ItemHandlerHelper.insertItemStacked(exportItems, itemStack, false);
            if (remainder.isEmpty()) {
                entityItem.setDead();
            } else if (itemStack.getCount() > remainder.getCount()) {
                entityItem.setItem(remainder);
            }
        }
    }
    if (getTimer() % 5 == 0) {
        pushItemsIntoNearbyHandlers(getFrontFacing());
    }
}
 
Example 4
@Override
public void onDeath(@Nonnull DamageSource cause) {
	if (getIsInvulnerable()) return;

	if (!world.isRemote) {
		FairyData dataFairy = getDataFairy();
		if (dataFairy == null) return;

		if (dataFairy.isDepressed) {

			ItemStack stack = new ItemStack(ModItems.FAIRY_ITEM);
			NBTHelper.setTag(stack, "fairy", dataFairy.serializeNBT());

			world.removeEntity(this);

			EntityItem entityItem = new EntityItem(world);
			entityItem.setPosition(posX, posY, posZ);
			entityItem.setItem(stack);
			entityItem.setPickupDelay(20);
			entityItem.setNoDespawn();

			world.spawnEntity(entityItem);
			return;
		}

		super.onDeath(cause);

		if (getHealth() <= 0)
			PacketHandler.NETWORK.sendToAllAround(new PacketExplode(getPositionVector().add(0, 0.25, 0), dataFairy.primaryColor, dataFairy.secondaryColor, 0.5, 0.5, RandUtil.nextInt(100, 200), 75, 25, true),
					new NetworkRegistry.TargetPoint(world.provider.getDimension(), posX, posY, posZ, 256));
	}
}
 
Example 5
private static void popItemFromHand(EntityFairy fairy) {
	ItemStack heldItem = fairy.getDataHeldItem();
	if (heldItem.isEmpty()) return;

	EntityItem entityItem = new EntityItem(fairy.world, fairy.posX, fairy.posY, fairy.posZ, heldItem.copy());
	entityItem.motionX = 0;
	entityItem.motionY = 0;
	entityItem.motionZ = 0;
	entityItem.setPickupDelay(50);
	if (fairy.world.spawnEntity(entityItem)) {
		fairy.setDataHeldItem(ItemStack.EMPTY);
	}
}
 
Example 6
/**
 * Makes it so when a player right clicks a rail block with a different rail item, it will be replaced, without having to remove and place a rail.
 * TODO: Replace signal types
 * @param e
 */
@SubscribeEvent
public void onBlockInteraction(RightClickBlock e){
    if(!e.getWorld().isRemote && e.getFace() == EnumFacing.UP) {
        ItemStack stack = e.getEntityPlayer().getHeldItemMainhand();
        BlockRailBase railBlock = getRailBlock(stack);
        if(railBlock != null) {
            IBlockState state = e.getWorld().getBlockState(e.getPos());
            IRail rail = RailManager.getInstance().getRail(e.getWorld(), e.getPos(), state);
            if(rail != null && state.getBlock() != railBlock) {
                EnumRailDirection dir = rail.getDirection(e.getWorld(), e.getPos(), state);
                e.getWorld().destroyBlock(e.getPos(), !e.getEntityPlayer().isCreative());
                List<EntityItem> drops = e.getWorld().getEntitiesWithinAABB(EntityItem.class, new AxisAlignedBB(e.getPos()));
                for(EntityItem drop : drops) {
                    drop.setPickupDelay(0);
                    drop.onCollideWithPlayer(e.getEntityPlayer());
                }

                e.getWorld().setBlockState(e.getPos(), railBlock.getDefaultState());
                if(!e.getEntityPlayer().isCreative()) stack.shrink(1);

                //Set the rail orientation equal to the old rail, if possible.
                if(railBlock.getShapeProperty().getAllowedValues().contains(dir)) {
                    IBlockState curState = e.getWorld().getBlockState(e.getPos());
                    e.getWorld().setBlockState(e.getPos(), curState.withProperty(railBlock.getShapeProperty(), dir));
                }
            }
        }
    }
}