Search in sources :

Example 36 with EntityItem

use of net.minecraft.entity.item.EntityItem in project PneumaticCraft by MineMaarten.

the class TileEntityOmnidirectionalHopper method suckInItem.

protected boolean suckInItem(int maxItems) {
    TileEntity inputInv = IOHelper.getNeighbor(this, inputDir);
    boolean success = false;
    //Suck from input inventory.
    for (int i = 0; i < maxItems; i++) {
        if (hasEmptySlot()) {
            //simulate extraction from the neighbor.
            ItemStack extracted = IOHelper.extractOneItem(inputInv, inputDir.getOpposite(), true);
            if (extracted != null) {
                //if we can insert the item in this hopper.
                ItemStack inserted = IOHelper.insert(this, extracted, ForgeDirection.UNKNOWN, false);
                if (inserted == null) {
                    //actually retrieve it from the neighbor.
                    IOHelper.extractOneItem(inputInv, inputDir.getOpposite(), false);
                    success = true;
                } else {
                    break;
                }
            } else {
                break;
            }
        } else {
            for (int slot = 0; slot < 5; slot++) {
                ItemStack stack = inventory[slot];
                stack = stack.copy();
                stack.stackSize = 1;
                //simulate extraction from the neighbor.
                ItemStack extracted = IOHelper.extract(inputInv, inputDir.getOpposite(), stack, true, true);
                if (extracted != null) {
                    //if we can insert the item in this hopper.
                    ItemStack inserted = IOHelper.insert(this, extracted, ForgeDirection.UNKNOWN, false);
                    if (inserted == null) {
                        //actually retrieve it from the neighbor.
                        IOHelper.extract(inputInv, inputDir.getOpposite(), stack, true, false);
                        success = true;
                        break;
                    }
                }
            }
            if (!success)
                break;
        }
    }
    for (EntityItem entity : getNeighborItems(this, inputDir)) {
        if (!entity.isDead) {
            ItemStack remainder = IOHelper.insert(this, entity.getEntityItem(), ForgeDirection.UNKNOWN, false);
            if (remainder == null) {
                entity.setDead();
                //Don't set true when the stack could not be fully consumes, as that means next insertion there won't be any room.
                success = true;
            }
        }
    }
    return success;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem)

Example 37 with EntityItem

use of net.minecraft.entity.item.EntityItem in project PneumaticCraft by MineMaarten.

the class TileEntityPressureChamberInterface method outputInChamber.

private void outputInChamber() {
    TileEntityPressureChamberValve valve = getCore();
    if (valve != null) {
        for (int i = 0; i < 6; i++) {
            int x = xCoord + Facing.offsetsXForSide[i];
            int y = yCoord + Facing.offsetsYForSide[i];
            int z = zCoord + Facing.offsetsZForSide[i];
            if (valve.isCoordWithinChamber(worldObj, x, y, z)) {
                enoughAir = Math.abs(valve.currentAir) > inventory[0].stackSize * PneumaticValues.USAGE_CHAMBER_INTERFACE;
                if (enoughAir) {
                    valve.addAir((valve.currentAir > 0 ? -1 : 1) * inventory[0].stackSize * PneumaticValues.USAGE_CHAMBER_INTERFACE, ForgeDirection.UNKNOWN);
                    EntityItem item = new EntityItem(worldObj, x + 0.5D, y + 0.5D, z + 0.5D, inventory[0].copy());
                    worldObj.spawnEntityInWorld(item);
                    setInventorySlotContents(0, null);
                    break;
                }
            }
        }
    }
}
Also used : EntityItem(net.minecraft.entity.item.EntityItem)

Example 38 with EntityItem

use of net.minecraft.entity.item.EntityItem in project MinecraftForge by MinecraftForge.

the class ItemHandlerHelper method giveItemToPlayer.

/**
     * Inserts the given itemstack into the players inventory.
     * If the inventory can't hold it, the item will be dropped in the world at the players position.
     *
     * @param player The player to give the item to
     * @param stack  The itemstack to insert
     */
public static void giveItemToPlayer(EntityPlayer player, @Nonnull ItemStack stack, int preferredSlot) {
    IItemHandler inventory = new PlayerMainInvWrapper(player.inventory);
    World world = player.world;
    // try adding it into the inventory
    ItemStack remainder = stack;
    // insert into preferred slot first
    if (preferredSlot >= 0) {
        remainder = inventory.insertItem(preferredSlot, stack, false);
    }
    // then into the inventory in general
    if (!remainder.isEmpty()) {
        remainder = insertItemStacked(inventory, remainder, false);
    }
    // play sound if something got picked up
    if (remainder.isEmpty() || remainder.getCount() != stack.getCount()) {
        world.playSound(player, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, 0.2F, ((world.rand.nextFloat() - world.rand.nextFloat()) * 0.7F + 1.0F) * 2.0F);
    }
    // drop remaining itemstack into the world
    if (!remainder.isEmpty() && !world.isRemote) {
        EntityItem entityitem = new EntityItem(world, player.posX, player.posY + 0.5, player.posZ, stack);
        entityitem.setPickupDelay(40);
        entityitem.motionX = 0;
        entityitem.motionZ = 0;
        world.spawnEntity(entityitem);
    }
}
Also used : PlayerMainInvWrapper(net.minecraftforge.items.wrapper.PlayerMainInvWrapper) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem)

Example 39 with EntityItem

use of net.minecraft.entity.item.EntityItem in project SimplyJetpacks by Tonius.

the class PlatingReturnHandler method onItemCrafted.

@SubscribeEvent
public void onItemCrafted(ItemCraftedEvent evt) {
    if (evt.player.worldObj.isRemote || !(evt.crafting.getItem() instanceof ItemPack)) {
        return;
    }
    PackBase outputPack = ((ItemPack) evt.crafting.getItem()).getPack(evt.crafting);
    if (outputPack.isArmored) {
        return;
    }
    for (int i = 0; i < evt.craftMatrix.getSizeInventory(); i++) {
        ItemStack input = evt.craftMatrix.getStackInSlot(i);
        if (input == null || !(input.getItem() instanceof ItemPack)) {
            continue;
        }
        PackBase inputPack = ((ItemPack) input.getItem()).getPack(input);
        if (inputPack != null && inputPack.isArmored && inputPack.platingMeta != null) {
            EntityItem item = evt.player.entityDropItem(new ItemStack(ModItems.armorPlatings, 1, inputPack.platingMeta), 0.0F);
            item.delayBeforeCanPickup = 0;
            break;
        }
    }
}
Also used : PackBase(tonius.simplyjetpacks.item.meta.PackBase) ItemPack(tonius.simplyjetpacks.item.ItemPack) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem) SubscribeEvent(cpw.mods.fml.common.eventhandler.SubscribeEvent)

Example 40 with EntityItem

use of net.minecraft.entity.item.EntityItem in project BluePower by Qmunity.

the class TileMachineBase method ejectItemInWorld.

public void ejectItemInWorld(ItemStack stack, ForgeDirection oppDirection) {
    float spawnX = xCoord + 0.5F + oppDirection.offsetX * 0.8F;
    float spawnY = yCoord + 0.5F + oppDirection.offsetY * 0.8F;
    float spawnZ = zCoord + 0.5F + oppDirection.offsetZ * 0.8F;
    EntityItem droppedItem = new EntityItem(worldObj, spawnX, spawnY, spawnZ, stack);
    droppedItem.motionX = oppDirection.offsetX * 0.20F;
    droppedItem.motionY = oppDirection.offsetY * 0.20F;
    droppedItem.motionZ = oppDirection.offsetZ * 0.20F;
    worldObj.spawnEntityInWorld(droppedItem);
}
Also used : EntityItem(net.minecraft.entity.item.EntityItem)

Aggregations

EntityItem (net.minecraft.entity.item.EntityItem)284 ItemStack (net.minecraft.item.ItemStack)178 TileEntity (net.minecraft.tileentity.TileEntity)45 EntityPlayer (net.minecraft.entity.player.EntityPlayer)36 Entity (net.minecraft.entity.Entity)26 ArrayList (java.util.ArrayList)19 BlockPos (net.minecraft.util.math.BlockPos)19 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)18 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)16 World (net.minecraft.world.World)16 Random (java.util.Random)15 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)14 Item (net.minecraft.item.Item)13 IInventory (net.minecraft.inventory.IInventory)12 AxisAlignedBB (net.minecraft.util.AxisAlignedBB)12 List (java.util.List)11 Block (net.minecraft.block.Block)11 EntityLivingBase (net.minecraft.entity.EntityLivingBase)11 IBlockState (net.minecraft.block.state.IBlockState)10 SubscribeEvent (cpw.mods.fml.common.eventhandler.SubscribeEvent)6