Java Code Examples for net.minecraftforge.items.ItemHandlerHelper#insertItemStacked()

The following examples show how to use net.minecraftforge.items.ItemHandlerHelper#insertItemStacked() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CartHopperBehaviourItems.java    From Signals with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean tryTransfer(IItemHandler from, IItemHandler to, List<Pair<TileEntity, EnumFacing>> filters){
    int totalExtracted = 0;

    for(int i = 0; i < from.getSlots(); i++) {
        ItemStack extracted = from.extractItem(i, MAX_TRANSFER_RATE - totalExtracted, true);
        if(!extracted.isEmpty() && passesFilters(extracted, filters)) {
            ItemStack leftover = ItemHandlerHelper.insertItemStacked(to, extracted, false);
            int leftoverCount = !leftover.isEmpty() ? leftover.getCount() : 0;

            int actuallyExtracted = extracted.getCount() - leftoverCount;
            if(actuallyExtracted > 0) {
                from.extractItem(i, actuallyExtracted, false);
                totalExtracted += actuallyExtracted;
                if(totalExtracted >= MAX_TRANSFER_RATE) break;
            }
        }
    }
    return totalExtracted > 0;
}
 
Example 2
Source File: MetaTileEntityItemCollector.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void moveItemsInEffectRange() {
    List<EntityItem> itemsInRange = getWorld().getEntitiesWithinAABB(EntityItem.class, areaBoundingBox);
    for (EntityItem entityItem : itemsInRange) {
        double distanceX = (areaCenterPos.getX() + 0.5) - entityItem.posX;
        double distanceZ = (areaCenterPos.getZ() + 0.5) - entityItem.posZ;
        double distance = MathHelper.sqrt(distanceX * distanceX + distanceZ * distanceZ);
        if(!itemFilter.testItemStack(entityItem.getItem())) {
            continue;
        }
        if (distance >= 0.7) {
            if(!entityItem.cannotPickup()) {
                double directionX = distanceX / distance;
                double directionZ = distanceZ / distance;
                entityItem.motionX = directionX * MOTION_MULTIPLIER * getTier();
                entityItem.motionZ = directionZ * MOTION_MULTIPLIER * getTier();
                entityItem.velocityChanged = true;
                entityItem.setPickupDelay(1);
            }
        } else {
            ItemStack itemStack = entityItem.getItem();
            ItemStack remainder = ItemHandlerHelper.insertItemStacked(exportItems, itemStack, false);
            if (remainder.isEmpty()) {
                entityItem.setDead();
            } else if (itemStack.getCount() > remainder.getCount()) {
                entityItem.setItem(remainder);
            }
        }
    }
    if (getTimer() % 5 == 0) {
        pushItemsIntoNearbyHandlers(getFrontFacing());
    }
}
 
Example 3
Source File: MetaTileEntityBlockBreaker.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void addToInventoryOrDropItems(List<ItemStack> drops) {
    EnumFacing outputFacing = getOutputFacing();
    double itemSpawnX = getPos().getX() + 0.5 + outputFacing.getFrontOffsetX();
    double itemSpawnY = getPos().getX() + 0.5 + outputFacing.getFrontOffsetX();
    double itemSpawnZ = getPos().getX() + 0.5 + outputFacing.getFrontOffsetX();
    for(ItemStack itemStack : drops) {
        ItemStack remainStack = ItemHandlerHelper.insertItemStacked(exportItems, itemStack, false);
        if(!remainStack.isEmpty()) {
            EntityItem entityitem = new EntityItem(getWorld(), itemSpawnX, itemSpawnY, itemSpawnZ, remainStack);
            entityitem.setDefaultPickupDelay();
            getWorld().spawnEntity(entityitem);
        }
    }
}
 
Example 4
Source File: MetaTileEntityCokeOven.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void finishCurrentRecipe() {
    this.maxProgressDuration = 0;
    this.currentProgress = 0;
    ItemHandlerHelper.insertItemStacked(exportItems, outputStack, false);
    this.exportFluids.fill(outputFluid, true);
    markDirty();
}
 
Example 5
Source File: CoverConveyor.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected int moveInventoryItems(IItemHandler sourceInventory, IItemHandler targetInventory, int maxTransferAmount) {
    int itemsLeftToTransfer = maxTransferAmount;
    for (int srcIndex = 0; srcIndex < sourceInventory.getSlots(); srcIndex++) {
        ItemStack sourceStack = sourceInventory.extractItem(srcIndex, itemsLeftToTransfer, true);
        if (sourceStack.isEmpty()) {
            continue;
        }
        if (!itemFilterContainer.testItemStack(sourceStack)) {
            continue;
        }
        ItemStack remainder = ItemHandlerHelper.insertItemStacked(targetInventory, sourceStack, true);
        int amountToInsert = sourceStack.getCount() - remainder.getCount();

        if (amountToInsert > 0) {
            sourceStack = sourceInventory.extractItem(srcIndex, amountToInsert, false);
            if (!sourceStack.isEmpty()) {
                ItemHandlerHelper.insertItemStacked(targetInventory, sourceStack, false);
                itemsLeftToTransfer -= sourceStack.getCount();

                if (itemsLeftToTransfer == 0) {
                    break;
                }
            }
        }
    }
    return maxTransferAmount - itemsLeftToTransfer;
}
 
Example 6
Source File: CapabilityMinecartDestination.java    From Signals with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 
 * @param cart
 * @return true if there was a valid hopper (not necessarily if extracted an item)
 */
private boolean extractFuelFromHopper(EntityMinecart cart, BlockPos pos){
    boolean foundHopper = false;
    for(EnumFacing dir : EnumFacing.VALUES) {
        BlockPos neighbor = pos;
        for(int offsetTimes = 0; offsetTimes < (dir == EnumFacing.UP ? 2 : 1); offsetTimes++) {
            neighbor = neighbor.offset(dir);
            TileEntity te = cart.world.getTileEntity(neighbor);
            if(te instanceof TileEntityHopper) {
                EnumFacing hopperDir = cart.world.getBlockState(neighbor).getValue(BlockHopper.FACING);
                if(hopperDir.getOpposite() == dir) {
                    TileEntityHopper hopper = (TileEntityHopper)te;
                    for(int i = 0; i < hopper.getSizeInventory(); i++) {
                        ItemStack stack = hopper.getStackInSlot(i);
                        if(!stack.isEmpty() && getFuelInv().isItemValidForSlot(0, stack)) {
                            ItemStack inserted = stack.copy();
                            inserted.setCount(1);
                            ItemStack left = ItemHandlerHelper.insertItemStacked(getEngineItemHandler(), inserted, false);
                            if(left.isEmpty()) {
                                stack.shrink(1);
                                hopper.markDirty();
                                return true;
                            }
                        }
                    }
                    foundHopper = true;
                }
            }
        }
    }
    return foundHopper;
}
 
Example 7
Source File: CoverConveyor.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected int moveInventoryItems(IItemHandler sourceInventory, IItemHandler targetInventory, Map<Object, GroupItemInfo> itemInfos, int maxTransferAmount) {
    int itemsLeftToTransfer = maxTransferAmount;
    for (int i = 0; i < sourceInventory.getSlots(); i++) {
        ItemStack itemStack = sourceInventory.getStackInSlot(i);
        if(itemStack.isEmpty()) {
            continue;
        }
        Object matchSlotIndex = itemFilterContainer.matchItemStack(itemStack);
        if (matchSlotIndex == null || !itemInfos.containsKey(matchSlotIndex)) {
            continue;
        }

        GroupItemInfo itemInfo = itemInfos.get(matchSlotIndex);

        ItemStack extractedStack = sourceInventory.extractItem(i, Math.min(itemInfo.totalCount, itemsLeftToTransfer), true);

        ItemStack remainderStack = ItemHandlerHelper.insertItemStacked(targetInventory, extractedStack, true);
        int amountToInsert = extractedStack.getCount() - remainderStack.getCount();

        if (amountToInsert > 0) {
            extractedStack = sourceInventory.extractItem(i, amountToInsert, false);

            if(!extractedStack.isEmpty()) {

                ItemHandlerHelper.insertItemStacked(targetInventory, extractedStack, false);
                itemsLeftToTransfer -= extractedStack.getCount();
                itemInfo.totalCount -= extractedStack.getCount();

                if (itemInfo.totalCount == 0) {
                    itemInfos.remove(matchSlotIndex);
                    if(itemInfos.isEmpty()) {
                        break;
                    }
                }
                if(itemsLeftToTransfer == 0) {
                    break;
                }
            }
        }
    }
    return maxTransferAmount - itemsLeftToTransfer;
}
 
Example 8
Source File: InventoryUtils.java    From OpenModsLib with MIT License 4 votes vote down vote up
public static boolean canInsertStack(IItemHandler handler, @Nonnull ItemStack stack) {
	final ItemStack toInsert = ItemHandlerHelper.insertItemStacked(handler, stack, true);
	return toInsert.getCount() < stack.getCount();
}