Example usage for net.minecraftforge.items IItemHandlerModifiable setStackInSlot

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

Introduction

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

Prototype

void setStackInSlot(int slot, @Nonnull ItemStack stack);

Source Link

Document

Overrides the stack in the given slot.

Usage

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

public void setBullets(ItemStack revolver, NonNullList<ItemStack> bullets, boolean ignoreExtendedMag) {
    IItemHandlerModifiable inv = (IItemHandlerModifiable) revolver
            .getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    assert inv != null;
    for (int i = 0; i < 18; i++)
        inv.setStackInSlot(i, ItemStack.EMPTY);
    if (ignoreExtendedMag && getUpgrades(revolver).getInteger("bullets") > 0)
        for (int i = 0; i < bullets.size(); i++)
            inv.setStackInSlot(i < 2 ? i : i + getUpgrades(revolver).getInteger("bullets"), bullets.get(i));
    else//w w w . j av a 2  s  .c o  m
        for (int i = 0; i < bullets.size(); i++)
            inv.setStackInSlot(i, bullets.get(i));
}

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;//from ww  w .  ja  v a2s.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.blocks.BlockGiantChest.java

@Override
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase entity,
        ItemStack stack) {//from  w  w  w .  j  a  v  a2  s. c  om
    if (stack.getTagCompound() != null) {
        TileEntity tile = world.getTileEntity(pos);
        if (tile instanceof TileEntityGiantChest) {
            NBTTagList list = stack.getTagCompound().getTagList("Items", 10);
            IItemHandlerModifiable inv = ((TileEntityGiantChest) tile).inv;

            for (int i = 0; i < list.tagCount(); i++) {
                NBTTagCompound compound = list.getCompoundTagAt(i);
                if (compound != null && compound.hasKey("id")) {
                    inv.setStackInSlot(i, new ItemStack(list.getCompoundTagAt(i)));
                }
            }
        }
    }

    super.onBlockPlacedBy(world, pos, state, entity, stack);
}

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 a v a2  s .  co m*/

                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.tile.TileEntityInventoryBase.java

public static void loadSlots(IItemHandlerModifiable slots, NBTTagCompound compound) {
    if (slots != null && slots.getSlots() > 0) {
        NBTTagList tagList = compound.getTagList("Items", 10);
        for (int i = 0; i < slots.getSlots(); i++) {
            NBTTagCompound tagCompound = tagList.getCompoundTagAt(i);
            slots.setStackInSlot(i, tagCompound != null && tagCompound.hasKey("id") ? new ItemStack(tagCompound)
                    : StackUtil.getNull());
        }/*from   w ww .  ja v  a 2 s .  c o  m*/
    }
}

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

public static void fillInventory(LootTable table, IItemHandlerModifiable inventory, Random rand,
        LootContext context) {//from   w  w  w  . j  a v a  2  s.c o m
    List<ItemStack> list = table.generateLootForPools(rand, context);
    List<Integer> list1 = getEmptySlotsRandomized(inventory, rand);
    shuffleItems(list, list1.size(), rand);

    for (ItemStack itemstack : list) {
        if (itemstack.isEmpty()) {
            inventory.setStackInSlot(list1.remove(list1.size() - 1), ItemStack.EMPTY);
        } else {
            inventory.setStackInSlot(list1.remove(list1.size() - 1), itemstack);
        }
    }
}

From source file:hellfirepvp.astralsorcery.common.util.ItemUtils.java

License:Open Source License

public static boolean consumeFromInventory(IItemHandlerModifiable handler, ItemStack toConsume,
        boolean simulate) {
    Map<Integer, ItemStack> contents = findItemsIndexedInInventory(handler, toConsume, false);
    if (contents.isEmpty())
        return false;

    int cAmt = toConsume.getCount();
    for (int slot : contents.keySet()) {
        ItemStack inSlot = contents.get(slot);
        int toRemove = cAmt > inSlot.getCount() ? inSlot.getCount() : cAmt;
        cAmt -= toRemove;// w w w .  java  2  s. co  m
        if (!simulate) {
            handler.setStackInSlot(slot, copyStackWithSize(inSlot, inSlot.getCount() - toRemove));
        }
        if (cAmt <= 0) {
            break;
        }
    }
    return cAmt <= 0;
}

From source file:vazkii.botania.common.block.tile.TileAltar.java

License:Open Source License

public static void tryToSetLastRecipe(EntityPlayer player, IItemHandlerModifiable inv,
        List<ItemStack> lastRecipe) {
    if (lastRecipe == null || lastRecipe.isEmpty() || player.worldObj.isRemote)
        return;//from w ww.j  a v  a  2  s .  c  om

    int index = 0;
    boolean didAny = false;
    for (ItemStack stack : lastRecipe) {
        if (stack == null)
            continue;

        for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
            ItemStack pstack = player.inventory.getStackInSlot(i);
            if (pstack != null && pstack.isItemEqual(stack) && ItemStack.areItemStackTagsEqual(stack, pstack)) {
                pstack.stackSize--;
                if (pstack.stackSize == 0)
                    player.inventory.setInventorySlotContents(i, null);

                ItemStack stackToPut = pstack.copy();
                stackToPut.stackSize = 1;
                inv.setStackInSlot(index, stackToPut);
                didAny = true;
                index++;
                break;
            }
        }
    }

    if (didAny) {
        if (inv instanceof TileAltar)
            player.worldObj.playSound(null, ((TileAltar) inv).getPos(), SoundEvents.ENTITY_GENERIC_SPLASH,
                    SoundCategory.BLOCKS, 0.1F, 10F);
        if (player instanceof EntityPlayerMP) {
            EntityPlayerMP mp = (EntityPlayerMP) player;
            mp.inventoryContainer.detectAndSendChanges();
        }
    }
}