Example usage for net.minecraftforge.items IItemHandlerModifiable getSlots

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

Introduction

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

Prototype

int getSlots();

Source Link

Document

Returns the number of slots available

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 a2 s  .c o  m

    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  ww .j  a va2  s  . c o 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 w  w .j a  v a  2  s. c  o  m
    }
}

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  a  v a 2  s . c  om
        }
    }

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