Example usage for net.minecraftforge.items IItemHandler getSlots

List of usage examples for net.minecraftforge.items IItemHandler getSlots

Introduction

In this page you can find the example usage for net.minecraftforge.items IItemHandler getSlots.

Prototype

int getSlots();

Source Link

Document

Returns the number of slots available

Usage

From source file:blusunrize.immersiveengineering.common.blocks.BlockIETileProvider.java

@Override
public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, IBlockState state,
        int fortune)
//   public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune)
{
    TileEntity tile = world.getTileEntity(pos);
    DimensionBlockPos dpos = new DimensionBlockPos(pos,
            world instanceof World ? ((World) world).provider.getDimension() : 0);
    if (tile == null && tempTile.containsKey(dpos))
        tile = tempTile.get(dpos);//from   w w w  . j a va  2s  . co  m
    if (tile != null && (!(tile instanceof ITileDrop) || !((ITileDrop) tile).preventInventoryDrop())) {
        if (tile instanceof IIEInventory && ((IIEInventory) tile).getDroppedItems() != null) {
            for (ItemStack s : ((IIEInventory) tile).getDroppedItems())
                if (!s.isEmpty())
                    drops.add(s);
        } else if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) {
            IItemHandler h = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
            if (h instanceof IEInventoryHandler)
                for (int i = 0; i < h.getSlots(); i++)
                    if (!h.getStackInSlot(i).isEmpty()) {
                        drops.add(h.getStackInSlot(i));
                        ((IEInventoryHandler) h).setStackInSlot(i, ItemStack.EMPTY);
                    }
        }
    }
    if (tile instanceof ITileDrop) {
        NonNullList<ItemStack> s = ((ITileDrop) tile).getTileDrops(harvesters.get(), state);
        drops.addAll(s);
    } else
        super.getDrops(drops, world, pos, state, fortune);

    tempTile.remove(dpos);
}

From source file:blusunrize.immersiveengineering.common.blocks.metal.conveyors.ConveyorExtract.java

@Override
public void onUpdate(TileEntity tile, EnumFacing facing) {
    if (!tile.getWorld().isRemote) {
        if (this.transferCooldown > 0) {
            this.transferCooldown--;
        }//from  w w w.ja  v  a  2  s .  c om
        if (!isPowered(tile) && this.transferCooldown <= 0) {
            World world = tile.getWorld();
            BlockPos neighbour = tile.getPos().offset(this.extractDirection);
            if (!world.isAirBlock(neighbour)) {
                TileEntity neighbourTile = world.getTileEntity(neighbour);
                if (neighbourTile != null && neighbourTile.hasCapability(
                        CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, this.extractDirection.getOpposite())) {
                    IItemHandler itemHandler = neighbourTile.getCapability(
                            CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, this.extractDirection.getOpposite());
                    for (int i = 0; i < itemHandler.getSlots(); i++) {
                        ItemStack extractItem = itemHandler.extractItem(i, 1, true);
                        if (!extractItem.isEmpty()) {
                            extractItem = itemHandler.extractItem(i, 1, false);
                            EntityItem entity = new EntityItem(world, tile.getPos().getX() + .5,
                                    tile.getPos().getY() + .1875, tile.getPos().getZ() + .5, extractItem);
                            entity.motionX = 0;
                            entity.motionY = 0;
                            entity.motionZ = 0;
                            world.spawnEntity(entity);
                            this.onItemDeployed(tile, entity, facing);
                            this.transferCooldown = this.transferTickrate;
                            return;
                        }
                    }
                }
            }
        }
    }
}

From source file:blusunrize.immersiveengineering.common.blocks.metal.TileEntityToolbox.java

@Override
public void readOnPlacement(EntityLivingBase placer, ItemStack stack) {
    if (stack.getItem() instanceof ItemInternalStorage) {
        IItemHandler inv = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
        if (inv != null) {
            inventory = NonNullList.withSize(inv.getSlots(), ItemStack.EMPTY);
            for (int i = 0; i < inv.getSlots(); i++)
                inventory.set(i, inv.getStackInSlot(i));
        }/* w  w w . j  av a2 s.  c o m*/

        if (stack.hasDisplayName())
            this.name = stack.getDisplayName();
        enchantments = stack.getEnchantmentTagList();
    }
}

From source file:blusunrize.immersiveengineering.common.blocks.wooden.TileEntitySorter.java

public ItemStack pullItem(EnumFacing outputSide, int amount, boolean simulate) {
    if (!world.isRemote && canRoute()) {
        boolean first = startRouting();
        for (EnumFacing side : EnumFacing.values())
            if (side != outputSide) {
                TileEntity neighbourTile = world.getTileEntity(getPos().offset(side));
                if (neighbourTile != null && neighbourTile
                        .hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite())) {
                    Predicate<ItemStack> concatFilter = null;
                    IItemHandler itemHandler = neighbourTile
                            .getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite());
                    for (int i = 0; i < itemHandler.getSlots(); i++) {
                        ItemStack extractItem = itemHandler.extractItem(i, amount, true);
                        if (!extractItem.isEmpty()) {
                            if (concatFilter == null)//Init the filter here, to save on resources
                                concatFilter = this.concatFilters(outputSide, side);
                            if (concatFilter.test(extractItem)) {
                                if (first)
                                    routed = null;
                                if (!simulate)
                                    itemHandler.extractItem(i, amount, false);
                                return extractItem;
                            }/* ww w. j  ava 2s.com*/
                        }
                    }
                }
            }
        if (first)
            routed = null;
    }
    return ItemStack.EMPTY;
}

From source file:blusunrize.immersiveengineering.common.items.ItemInternalStorage.java

public void setContainedItems(ItemStack stack, NonNullList<ItemStack> inventory) {
    IItemHandler handler = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    if (handler instanceof IItemHandlerModifiable) {
        if (inventory.size() != handler.getSlots())
            throw new IllegalArgumentException("Parameter inventory has " + inventory.size()
                    + " slots, capability inventory has " + handler.getSlots());
        for (int i = 0; i < handler.getSlots(); i++)
            ((IItemHandlerModifiable) handler).setStackInSlot(i, inventory.get(i));
    } else/*from   w  ww.  ja  va2  s  .  co m*/
        IELogger.warn("No valid inventory handler found for " + stack);
}

From source file:blusunrize.immersiveengineering.common.items.ItemInternalStorage.java

public NonNullList<ItemStack> getContainedItems(ItemStack stack) {
    IItemHandler handler = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    if (handler instanceof IEItemStackHandler)
        return ((IEItemStackHandler) handler).getContainedItems();
    else if (handler != null) {
        IELogger.warn(//from  w ww  .java 2 s.  co  m
                "Inefficiently getting contained items. Why does " + stack + " have a non-IE IItemHandler?");
        NonNullList<ItemStack> inv = NonNullList.withSize(handler.getSlots(), ItemStack.EMPTY);
        for (int i = 0; i < handler.getSlots(); i++)
            inv.set(i, handler.getStackInSlot(i));
        return inv;
    } else
        IELogger.info("No valid inventory handler found for " + stack);
    return NonNullList.create();
}

From source file:blusunrize.immersiveengineering.common.items.ItemRevolver.java

public boolean isEmpty(ItemStack stack, boolean allowCasing) {
    IItemHandler inv = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    if (inv != null)
        for (int i = 0; i < inv.getSlots(); i++) {
            ItemStack b = inv.getStackInSlot(i);
            if (!b.isEmpty() && b.getItem() instanceof ItemBullet
                    && (allowCasing || ItemNBTHelper.hasKey(b, "bullet")))
                return false;
        }/*  ww  w .  java 2 s .c  om*/
    return true;
}

From source file:blusunrize.immersiveengineering.common.items.ItemSpeedloader.java

public boolean isEmpty(ItemStack stack) {
    IItemHandler inv = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    if (inv != null)
        for (int i = 0; i < inv.getSlots(); i++) {
            ItemStack b = inv.getStackInSlot(i);
            if (!b.isEmpty() && b.getItem() instanceof ItemBullet && ItemNBTHelper.hasKey(b, "bullet"))
                return false;
        }//from w w  w. j  av  a2s. c  o m
    return true;
}

From source file:blusunrize.immersiveengineering.common.items.ItemUpgradeableTool.java

@Override
public void recalculateUpgrades(ItemStack stack) {
    if (FMLCommonHandler.instance().getEffectiveSide().isClient())
        return;// ww w .j av  a2  s.c o m
    clearUpgrades(stack);
    IItemHandler inv = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    NBTTagCompound upgradeTag = getUpgradeBase(stack).copy();
    if (inv != null) {
        for (int i = 0; i < inv.getSlots(); i++) {
            ItemStack u = inv.getStackInSlot(i);
            if (!u.isEmpty() && u.getItem() instanceof IUpgrade) {
                IUpgrade upg = (IUpgrade) u.getItem();
                if (upg.getUpgradeTypes(u).contains(upgradeType) && upg.canApplyUpgrades(stack, u))
                    upg.applyUpgrades(stack, u, upgradeTag);
            }
        }
        ItemNBTHelper.setTagCompound(stack, "upgrades", upgradeTag);
        finishUpgradeRecalculation(stack);
    }
}

From source file:com.buuz135.industrial.item.addon.movility.ItemStackTransferAddon.java

License:Open Source License

@Override
public boolean actionTransfer(World world, BlockPos pos, EnumFacing facing, int transferAmount) {
    TileEntity tileEntityOrigin = world.getTileEntity(pos);
    TileEntity tileEntityDestination = world.getTileEntity(pos.offset(facing));
    if (tileEntityOrigin != null && tileEntityDestination != null
            && tileEntityOrigin.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, facing)
            && tileEntityDestination.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY,
                    facing.getOpposite())) {
        IItemHandler itemHandlerOrigin = getCapabilityOriginByMode(tileEntityOrigin, tileEntityDestination,
                facing);//from   www.  j av a 2s.c o m
        IItemHandler itemHandlerDestination = getCapabilityDestinationByMode(tileEntityOrigin,
                tileEntityDestination, facing);
        for (int i = 0; i < itemHandlerOrigin.getSlots(); ++i) {
            ItemStack input = itemHandlerOrigin.extractItem(i, transferAmount, true);
            if (!input.isEmpty()) {
                ItemStack result = ItemHandlerHelper.insertItem(itemHandlerDestination, input, true);
                int amountInserted = input.getCount() - result.getCount();
                if (amountInserted > 0) {
                    result = ItemHandlerHelper.insertItem(itemHandlerDestination,
                            itemHandlerOrigin.extractItem(i, amountInserted, false), false);
                    if (!result.isEmpty())
                        ItemHandlerHelper.insertItem(itemHandlerOrigin, result, false);
                    return true;
                }
            }
        }
    }
    return false;
}