Example usage for net.minecraftforge.items IItemHandlerModifiable getStackInSlot

List of usage examples for net.minecraftforge.items IItemHandlerModifiable getStackInSlot

Introduction

In this page you can find the example usage for net.minecraftforge.items IItemHandlerModifiable getStackInSlot.

Prototype

@Nonnull
ItemStack getStackInSlot(int slot);

Source Link

Document

Returns the ItemStack in a given slot.

Usage

From source file:com.github.pelepmc.unlittorch.TorchItem.java

License:Open Source License

@Override
public void onUpdate(ItemStack stack, World world, Entity entity, int slot, boolean held) {
    if (world.isRemote || world.getTotalWorldTime() % UPDATE_INTERVAL != 0)
        return;/*w  w  w. j a v a  2 s .  c  om*/

    IItemHandler handler = entity.getCapability(ITEM_HANDLER_CAPABILITY, null);
    if (!(handler instanceof IItemHandlerModifiable))
        return;

    if (entity instanceof EntityPlayer && ((EntityPlayer) entity).capabilities.isCreativeMode)
        return;

    IItemHandlerModifiable inventory = (IItemHandlerModifiable) handler;

    if (stack.getMetadata() == LIT_METADATA && entity.isWet()) {
        for (int i = 0; i < inventory.getSlots(); i++) {
            stack = inventory.getStackInSlot(i);
            if (stack != null && stack.getItem() == this && stack.stackSize != 0) {
                stack = stack.copy();
                stack.setItemDamage(UNLIT_METADATA);
                inventory.setStackInSlot(i, stack);
            }
        }
        playDousingSound(entity.worldObj, entity.posX, entity.posY + entity.getEyeHeight(), entity.posZ);
    } else if (stack.getMetadata() == UNLIT_METADATA && entity.isBurning()) {
        for (int i = 0; i < inventory.getSlots(); i++) {
            stack = inventory.getStackInSlot(i);
            if (stack != null && stack.getItem() == this && stack.stackSize != 0) {
                stack = stack.copy();
                stack.setItemDamage(LIT_METADATA);
                inventory.setStackInSlot(i, stack);
            }
        }
        playIgnitingSound(entity.worldObj, entity.posX, entity.posY + entity.getEyeHeight(), entity.posZ);
    }
}

From source file:de.ellpeck.actuallyadditions.mod.items.ItemChestToCrateUpgrade.java

@Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand,
        EnumFacing facing, float par8, float par9, float par10) {
    ItemStack heldStack = player.getHeldItem(hand);
    if (player.isSneaking()) {
        TileEntity tileHit = world.getTileEntity(pos);
        if (tileHit.getClass() == this.start) {
            if (!world.isRemote) {

                //Copy Slots
                IItemHandlerModifiable chest = null;
                if (tileHit instanceof IInventory) {
                    chest = new InvWrapper((IInventory) tileHit);
                } else if (tileHit instanceof TileEntityInventoryBase) {
                    chest = ((TileEntityInventoryBase) tileHit).slots;
                }/*from   w w  w  .  j av  a 2s.com*/

                if (chest != null) {
                    ItemStack[] stacks = new ItemStack[chest.getSlots()];
                    for (int i = 0; i < stacks.length; i++) {
                        ItemStack aStack = chest.getStackInSlot(i);
                        if (StackUtil.isValid(aStack)) {
                            stacks[i] = aStack.copy();
                            chest.setStackInSlot(i, StackUtil.getNull());
                        }
                    }

                    //Set New Block
                    world.playEvent(2001, pos, Block.getStateId(world.getBlockState(pos)));
                    world.setBlockState(pos, this.end, 2);

                    //Copy Items into new Chest
                    TileEntity newTileHit = world.getTileEntity(pos);
                    if (newTileHit instanceof TileEntityInventoryBase) {
                        IItemHandlerModifiable newChest = ((TileEntityInventoryBase) newTileHit).slots;

                        for (int i = 0; i < stacks.length; i++) {
                            if (StackUtil.isValid(stacks[i])) {
                                if (newChest.getSlots() > i) {
                                    newChest.setStackInSlot(i, stacks[i].copy());
                                }
                            }
                        }
                    }

                    if (!player.capabilities.isCreativeMode) {
                        player.setHeldItem(hand, StackUtil.addStackSize(heldStack, -1));
                    }
                }
            }
            return EnumActionResult.SUCCESS;
        }
    }

    return super.onItemUse(player, world, pos, hand, facing, par8, par9, par10);
}

From source file:de.ellpeck.actuallyadditions.mod.util.AwfulUtil.java

private static List<Integer> getEmptySlotsRandomized(IItemHandlerModifiable inventory, Random rand) {
    List<Integer> list = Lists.newArrayList();

    for (int i = 0; i < inventory.getSlots(); ++i) {
        if (inventory.getStackInSlot(i).isEmpty()) {
            list.add(i);//w  ww  .  j ava2s . c  o m
        }
    }

    Collections.shuffle(list, rand);
    return list;
}