Example usage for net.minecraftforge.items ItemHandlerHelper insertItem

List of usage examples for net.minecraftforge.items ItemHandlerHelper insertItem

Introduction

In this page you can find the example usage for net.minecraftforge.items ItemHandlerHelper insertItem.

Prototype

@Nonnull
    public static ItemStack insertItem(IItemHandler dest, @Nonnull ItemStack stack, boolean simulate) 

Source Link

Usage

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  w w w . j  a  va2  s.  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;
}

From source file:com.buuz135.industrial.tile.agriculture.AnimalResourceHarvesterTile.java

License:Open Source License

@Override
public float work() {
    if (WorkUtils.isDisabled(this.getBlockType()))
        return 0;
    List<EntityAnimal> animals = this.world.getEntitiesWithinAABB(EntityAnimal.class, getWorkingArea());
    boolean hasWorked = false;
    int fortune = getFortuneLevel();
    for (EntityAnimal living : animals) {
        if (!ItemStackUtils.isInventoryFull(outItems) && living instanceof IShearable && ((IShearable) living)
                .isShearable(new ItemStack(Items.SHEARS), this.world, living.getPosition())) {
            List<ItemStack> stacks = ((IShearable) living).onSheared(new ItemStack(Items.SHEARS), this.world,
                    null, fortune);//from w w w.j  ava2 s  . c o  m
            for (ItemStack stack : stacks) {
                ItemHandlerHelper.insertItem(outItems, stack, false);
            }
            hasWorked = true;
        }
        //Check if the animal is a Fluid Cow from moofluids. If the cow has a different
        //fluid than what is in the tank, skip it
        if (tank.getFluidAmount() <= 7000 && !(Loader.isModLoaded("moofluids") && tank.getFluidAmount() > 0
                && living instanceof EntityFluidCow
                && ((EntityFluidCow) living).getEntityFluid() != tank.getFluid().getFluid())) {
            FakePlayer player = IndustrialForegoing.getFakePlayer(this.world);
            player.setPosition(living.posX, living.posY, living.posZ);
            player.setHeldItem(EnumHand.MAIN_HAND, new ItemStack(Items.BUCKET));
            if (living.processInteract(player, EnumHand.MAIN_HAND)) {
                ItemStack stack = player.getHeldItem(EnumHand.MAIN_HAND);
                if (stack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null)) {
                    IFluidHandlerItem fluidHandlerItem = stack
                            .getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
                    tank.fill(fluidHandlerItem.drain(Integer.MAX_VALUE, true), true);
                    hasWorked = true;
                }
            }
        }
    }
    for (EntitySquid animal : this.world.getEntitiesWithinAABB(EntitySquid.class, getWorkingArea())) {
        if (!ItemStackUtils.isInventoryFull(outItems) && world.rand.nextBoolean() && world.rand.nextBoolean()
                && world.rand.nextBoolean() && world.rand.nextBoolean()) {
            ItemHandlerHelper.insertItem(outItems, new ItemStack(Items.DYE), false);
            hasWorked = true;
        }
    }
    return hasWorked ? 1 : 0;
}

From source file:com.buuz135.industrial.tile.agriculture.AnimalStockIncreaserTile.java

License:Open Source License

@Override
public float work() {
    if (WorkUtils.isDisabled(this.getBlockType()))
        return 0;

    AxisAlignedBB area = getWorkingArea();
    List<EntityAnimal> animals = this.world.getEntitiesWithinAABB(EntityAnimal.class, area);
    if (animals.size() == 0 || animals.size() > 35)
        return 0;
    //Removing from the list animals that can't breed
    animals.removeIf(entityAnimal -> entityAnimal.isChild() || entityAnimal.getGrowingAge() != 0
            || getFirstBreedingItem(entityAnimal).isEmpty() || entityAnimal.isInLove()
            || BlockRegistry.animalStockIncreaserBlock.entityBlacklist
                    .contains(EntityList.getKey(entityAnimal).toString()));
    for (EntityAnimal firstParent : animals) {
        for (EntityAnimal secondParent : animals) {
            if (!firstParent.equals(secondParent) && firstParent.getClass().equals(secondParent.getClass())) {
                ItemStack stack = getFirstBreedingItem(firstParent);
                Item item = stack.getItem();
                stack.setCount(stack.getCount() - 1);
                stack = getFirstBreedingItem(secondParent);
                if (stack.isEmpty()) {
                    ItemHandlerHelper.insertItem(inFeedItems, new ItemStack(item, 1), false);
                    return 0;
                }//from www.j  av  a 2  s .c  o m
                stack.setCount(stack.getCount() - 1);
                firstParent.setInLove(null);
                secondParent.setInLove(null);
                return 1;
            }
        }
    }
    return 0;
}

From source file:com.buuz135.industrial.tile.agriculture.CropRecolectorTile.java

License:Open Source License

private void insertItems(List<ItemStack> drops, ItemStackHandler outItems) {
    for (ItemStack stack : drops) {
        ItemHandlerHelper.insertItem(outItems, stack, false);
    }/*from  ww  w  .  java  2  s.c  o m*/
    if (sludge.getFluidAmount() < sludge.getCapacity())
        sludge.fill(new FluidStack(FluidsRegistry.SLUDGE,
                ((CropRecolectorBlock) this.getBlockType()).getSludgeOperation() * drops.size()), true);
}

From source file:com.buuz135.industrial.tile.agriculture.PlantInteractorTile.java

License:Open Source License

@Override
public float work() {
    if (WorkUtils.isDisabled(this.getBlockType()))
        return 0;
    if (ItemStackUtils.isInventoryFull(outItems))
        return 0;
    hasWorked = false;//from  w  w w  .ja v a2  s.co  m
    List<BlockPos> blockPos = BlockUtils.getBlockPosInAABB(getWorkingArea());
    if (pointer >= blockPos.size() / (BlockRegistry.plantInteractorBlock.getHeight() + 1))
        pointer = 0;
    if (pointer < blockPos.size()) {
        BlockPos pointerPos = blockPos.get(pointer);
        for (int i = 0; i < BlockRegistry.plantInteractorBlock.getHeight() + 1; ++i) {
            BlockPos tempPos = new BlockPos(pointerPos.getX(), pointerPos.getY() + i, pointerPos.getZ());
            if (!BlockUtils.canBlockBeBroken(this.world, tempPos))
                continue;
            IBlockState tempState = this.world.getBlockState(tempPos);
            if (tempState.getBlock() instanceof IPlantable || tempState.getBlock() instanceof IGrowable) {
                FakePlayer player = IndustrialForegoing.getFakePlayer(this.world, tempPos.up());
                player.inventory.clear();
                WORKING_TILES.add(this);
                tempState.getBlock().onBlockActivated(this.world, tempPos, tempState, player,
                        EnumHand.MAIN_HAND, EnumFacing.UP, 0, 0, 0);
                ForgeHooks.onRightClickBlock(player, EnumHand.MAIN_HAND, tempPos, EnumFacing.UP,
                        new Vec3d(0, 0, 0));
                for (ItemStack stack : player.inventory.mainInventory) {
                    if (!stack.isEmpty()) {
                        ItemHandlerHelper.insertItem(outItems, stack, false);
                        sludge.fill(new FluidStack(FluidsRegistry.SLUDGE, 10 * stack.getCount()), true);
                        hasWorked = true;
                    }
                }
                player.inventory.clear();
                WORKING_TILES.remove(this);
            }
        }
    }
    ++pointer;
    return hasWorked ? 1 : 0.1f;
}

From source file:com.buuz135.industrial.tile.agriculture.PlantInteractorTile.java

License:Open Source License

public void harvestDrops(BlockEvent.HarvestDropsEvent event) {
    for (ItemStack stack : event.getDrops()) {
        ItemHandlerHelper.insertItem(outItems, stack, false);
        sludge.fill(new FluidStack(FluidsRegistry.SLUDGE, 10 * stack.getCount()), true);
    }/*from   w  w  w  . jav a 2  s  . co m*/
    event.getDrops().clear();
    hasWorked = true;
}

From source file:com.buuz135.industrial.tile.agriculture.SewageCompostSolidifierTile.java

License:Open Source License

@Override
protected float performWork() {
    if (WorkUtils.isDisabled(this.getBlockType()))
        return 0;

    ItemStack stack = new ItemStack(ItemRegistry.fertilizer, 1);
    if (sewage.getFluid() != null && sewage.drain(2000, false).amount == 2000
            && ItemHandlerHelper.insertItem(outFertilizer, stack, true).isEmpty()) {
        sewage.drain(2000, true);/*  www  .  j  av a2s.  c o  m*/
        ItemHandlerHelper.insertItem(outFertilizer, stack, false);
        return 1;
    }

    return 0;
}

From source file:com.buuz135.industrial.tile.agriculture.SludgeRefinerTile.java

License:Open Source License

@Override
protected float performWork() {
    if (WorkUtils.isDisabled(this.getBlockType()))
        return 0;

    if (tank.getFluid() != null && tank.getFluidAmount() >= 1000) {
        ItemStackWeightedItem itemStack = getRandomItem();
        if (ItemHandlerHelper.insertItem(outItems, itemStack.getStack(), true).isEmpty()) {
            tank.drain(1000, true);/*from  w  ww  . j av  a  2s  . c o  m*/
            ItemHandlerHelper.insertItem(outItems, itemStack.getStack().copy(), false);
            return 1;
        }
    }

    return 0;
}

From source file:com.buuz135.industrial.tile.agriculture.SporesRecreatorTile.java

License:Open Source License

@Override
protected float performWork() {
    ItemStack stack = getFirstItem();/*from w w w .j  a va 2 s.  c  o  m*/
    if (!stack.isEmpty() && waterTank.getFluidAmount() >= 500
            && ItemHandlerHelper.insertItem(output, stack.copy(), true).isEmpty()) {
        ItemStack out = stack.copy();
        out.setCount(2);
        ItemHandlerHelper.insertItem(output, out, false);
        waterTank.drain(500, true);
        stack.setCount(stack.getCount() - 1);
        return 1;
    }
    return 0;
}

From source file:com.buuz135.industrial.tile.agriculture.WaterResourcesCollectorTile.java

License:Open Source License

@Override
public float work() {
    if (WorkUtils.isDisabled(this.getBlockType()))
        return 0;
    if (this.world.rand.nextBoolean() && this.world.rand.nextBoolean())
        return 1;
    List<BlockPos> blockPos = BlockUtils.getBlockPosInAABB(getWorkingArea());
    boolean allWaterSources = true;
    for (BlockPos pos : blockPos) {
        IBlockState state = this.world.getBlockState(pos);
        if (!(state.getBlock().equals(FluidRegistry.WATER.getBlock())
                && state.getBlock().getMetaFromState(state) == 0))
            allWaterSources = false;/*from www  .  ja v a 2s. co m*/
    }
    if (allWaterSources) {
        LootContext.Builder lootcontext = new LootContext.Builder((WorldServer) this.world);
        List<ItemStack> items = this.world.getLootTableManager()
                .getLootTableFromLocation(LootTableList.GAMEPLAY_FISHING)
                .generateLootForPools(this.world.rand, lootcontext.build());
        for (ItemStack stack : items) {
            ItemHandlerHelper.insertItem(outFish, stack.copy(), false);
        }
        return 1;
    }
    return 0;
}