Java Code Examples for net.minecraftforge.items.IItemHandlerModifiable#getStackInSlot()

The following examples show how to use net.minecraftforge.items.IItemHandlerModifiable#getStackInSlot() . 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: EnergyContainerBatteryBuffer.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public long changeEnergy(long energyToAdd) {
    boolean isDischarge = energyToAdd < 0L;
    energyToAdd = Math.abs(energyToAdd);
    long initialEnergyToAdd = energyToAdd;
    IItemHandlerModifiable inventory = getInventory();
    for (int i = 0; i < inventory.getSlots(); i++) {
        ItemStack batteryStack = inventory.getStackInSlot(i);
        IElectricItem electricItem = getBatteryContainer(batteryStack);
        if (electricItem == null) continue;
        long charged = chargeItem(electricItem, energyToAdd, getTier(), isDischarge);
        energyToAdd -= charged;
        if(energyToAdd == 0L) break;
    }
    long energyAdded = initialEnergyToAdd - energyToAdd;
    if(energyAdded > 0L) {
        notifyEnergyListener(false);
    }
    return energyAdded;
}
 
Example 2
Source File: EnergyContainerHandler.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
public boolean dischargeOrRechargeEnergyContainers(IItemHandlerModifiable itemHandler, int slotIndex) {
    ItemStack stackInSlot = itemHandler.getStackInSlot(slotIndex);
    if (stackInSlot.isEmpty()) {
        return false;
    }
    IElectricItem electricItem = stackInSlot.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
    if (electricItem == null || !electricItem.canProvideChargeExternally()) {
        return false;
    }
    int machineTier = GTUtility.getTierByVoltage(Math.max(getInputVoltage(), getOutputVoltage()));

    if (getEnergyCanBeInserted() > 0) {
        double chargePercent = getEnergyStored() / (getEnergyCapacity() * 1.0);
        if (chargePercent <= 0.5) {
            long dischargedBy = electricItem.discharge(getEnergyCanBeInserted(), machineTier, false, true, false);
            addEnergy(dischargedBy);
            return dischargedBy > 0L;

        } else if (chargePercent >= 0.9) {
            long chargedBy = electricItem.charge(getEnergyStored(), machineTier, false, false);
            removeEnergy(chargedBy);
            return chargedBy > 0L;
        }
    }
    return false;
}
 
Example 3
Source File: ItemHelper.java    From customstuff4 with GNU General Public License v3.0 6 votes vote down vote up
public static void removeInputsFromInventory(List<RecipeInput> inputs, IItemHandlerModifiable inv, int start, int numSlots)
{
    List<RecipeInput> remaining = Lists.newLinkedList(inputs);

    for (int i = start; i < start + numSlots; i++)
    {
        ItemStack stack = inv.getStackInSlot(i);
        for (Iterator<RecipeInput> iterator = remaining.iterator(); iterator.hasNext(); )
        {
            RecipeInput input = iterator.next();
            if (stackMatchesRecipeInput(stack, input, true))
            {
                extractInput(input, inv, i);

                iterator.remove();
                break;
            }
        }
    }
}
 
Example 4
Source File: EnergyContainerBatteryBuffer.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public long acceptEnergyFromNetwork(EnumFacing side, long voltage, long amperage) {
    long initialAmperage = amperage;
    if (side == null || inputsEnergy(side)) {
        if (voltage > getInputVoltage()) {
            GTUtility.doOvervoltageExplosion(metaTileEntity, voltage);
            return Math.min(amperage, getInputAmperage());
        }
        IItemHandlerModifiable inventory = getInventory();
        for (int i = 0; i < inventory.getSlots(); i++) {
            if (batterySlotsUsedThisTick.get(i)) continue;
            ItemStack batteryStack = inventory.getStackInSlot(i);
            IElectricItem electricItem = getBatteryContainer(batteryStack);
            if (electricItem == null) continue;
            if (chargeItemWithVoltageExact(electricItem, voltage, getTier(), true)) {
                chargeItemWithVoltageExact(electricItem, voltage, getTier(), false);
                inventory.setStackInSlot(i, batteryStack);
                this.batterySlotsUsedThisTick.set(i);
                if (--amperage == 0) break;
            }
        }
    }
    long amperageUsed = initialAmperage - amperage;
    if(amperageUsed > 0L) {
        notifyEnergyListener(false);
    }
    return amperageUsed;
}
 
Example 5
Source File: EnergyContainerBatteryBuffer.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public long getEnergyCapacity() {
    long energyCapacity = 0L;
    IItemHandlerModifiable inventory = getInventory();
    for (int i = 0; i < inventory.getSlots(); i++) {
        ItemStack batteryStack = inventory.getStackInSlot(i);
        IElectricItem electricItem = getBatteryContainer(batteryStack);
        if (electricItem == null) continue;
        energyCapacity += electricItem.getMaxCharge();
    }
    return energyCapacity;
}
 
Example 6
Source File: EnergyContainerBatteryBuffer.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public long getEnergyStored() {
    long energyStored = 0L;
    IItemHandlerModifiable inventory = getInventory();
    for (int i = 0; i < inventory.getSlots(); i++) {
        ItemStack batteryStack = inventory.getStackInSlot(i);
        IElectricItem electricItem = getBatteryContainer(batteryStack);
        if (electricItem == null) continue;
        energyStored += electricItem.getCharge();
    }
    return energyStored;
}
 
Example 7
Source File: EnergyContainerBatteryBuffer.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public long getInputAmperage() {
    long inputAmperage = 0L;
    IItemHandlerModifiable inventory = getInventory();
    for (int i = 0; i < inventory.getSlots(); i++) {
        ItemStack batteryStack = inventory.getStackInSlot(i);
        IElectricItem electricItem = getBatteryContainer(batteryStack);
        if (electricItem == null) continue;
        inputAmperage++;
    }
    return inputAmperage;
}
 
Example 8
Source File: MetaTileEntityChest.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void sortInventorySlotContents(IItemHandlerModifiable inventory) {
    //stack item stacks with equal items and compounds
    for (int i = 0; i < inventory.getSlots(); i++) {
        for (int j = i + 1; j < inventory.getSlots(); j++) {
            ItemStack stack1 = inventory.getStackInSlot(i);
            ItemStack stack2 = inventory.getStackInSlot(j);
            if (!stack1.isEmpty() && ItemStack.areItemsEqual(stack1, stack2) &&
                ItemStack.areItemStackTagsEqual(stack1, stack2)) {
                int maxStackSize = Math.min(stack1.getMaxStackSize(), inventory.getSlotLimit(i));
                int itemsCanAccept = Math.min(stack2.getCount(), maxStackSize - Math.min(stack1.getCount(), maxStackSize));
                if (itemsCanAccept > 0) {
                    stack1.grow(itemsCanAccept);
                    stack2.shrink(itemsCanAccept);
                }
            }
        }
    }
    //create itemstack pairs and sort them out by attributes
    ArrayList<ItemStack> inventoryContents = new ArrayList<>();
    for (int i = 0; i < inventory.getSlots(); i++) {
        ItemStack itemStack = inventory.getStackInSlot(i);
        if (!itemStack.isEmpty()) {
            inventory.setStackInSlot(i, ItemStack.EMPTY);
            inventoryContents.add(itemStack);
        }
    }
    inventoryContents.sort(GTUtility.createItemStackComparator());
    for (int i = 0; i < inventoryContents.size(); i++) {
        inventory.setStackInSlot(i, inventoryContents.get(i));
    }
}
 
Example 9
Source File: MetaTileEntityMultiFurnace.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected Recipe findRecipe(long maxVoltage, IItemHandlerModifiable inputs, IMultipleTankHandler fluidInputs) {
    int currentItemsEngaged = 0;
    int maxItemsLimit = 32 * heatingCoilLevel;
    ArrayList<CountableIngredient> recipeInputs = new ArrayList<>();
    ArrayList<ItemStack> recipeOutputs = new ArrayList<>();
    for (int index = 0; index < inputs.getSlots(); index++) {
        ItemStack stackInSlot = inputs.getStackInSlot(index);
        if (stackInSlot.isEmpty())
            continue;
        Recipe matchingRecipe = recipeMap.findRecipe(maxVoltage,
            Collections.singletonList(stackInSlot), Collections.emptyList(), 0);
        CountableIngredient inputIngredient = matchingRecipe == null ? null : matchingRecipe.getInputs().get(0);
        if (inputIngredient != null && (maxItemsLimit - currentItemsEngaged) >= inputIngredient.getCount()) {
            ItemStack outputStack = matchingRecipe.getOutputs().get(0).copy();
            int overclockAmount = Math.min(stackInSlot.getCount() / inputIngredient.getCount(),
                (maxItemsLimit - currentItemsEngaged) / inputIngredient.getCount());
            recipeInputs.add(new CountableIngredient(inputIngredient.getIngredient(),
                inputIngredient.getCount() * overclockAmount));
            if (!outputStack.isEmpty()) {
                outputStack.setCount(outputStack.getCount() * overclockAmount);
                recipeOutputs.add(outputStack);
            }
            currentItemsEngaged += inputIngredient.getCount() * overclockAmount;
        }

        if (currentItemsEngaged >= maxItemsLimit) break;
    }
    return recipeInputs.isEmpty() ? null : recipeMap.recipeBuilder()
        .inputsIngredients(recipeInputs)
        .outputs(recipeOutputs)
        .EUt(Math.max(1, 16 / heatingCoilDiscount))
        .duration((int) Math.max(1.0, 256 * (currentItemsEngaged / (maxItemsLimit * 1.0))))
        .build().getResult();
}
 
Example 10
Source File: InventoryUtils.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void sortInventoryWithinRange(IItemHandlerModifiable inv, SlotRange range)
{
    List<ItemTypeByName> blocks = new ArrayList<ItemTypeByName>();
    List<ItemTypeByName> items = new ArrayList<ItemTypeByName>();
    final int lastSlot = Math.min(range.lastInc, inv.getSlots() - 1);

    // Get the different items that are present in the inventory
    for (int i = range.first; i <= lastSlot; i++)
    {
        ItemStack stack = inv.getStackInSlot(i);

        if (stack.isEmpty() == false)
        {
            ItemTypeByName type = new ItemTypeByName(stack);

            if (stack.getItem() instanceof ItemBlock)
            {
                if (blocks.contains(type) == false)
                {
                    blocks.add(type);
                }
            }
            else if (items.contains(type) == false)
            {
                items.add(type);
            }
        }
    }

    Collections.sort(blocks);
    Collections.sort(items);

    int slots = sortInventoryWithinRangeByTypes(inv, blocks, range);
    sortInventoryWithinRangeByTypes(inv, items, new SlotRange(range.first + slots, range.lastExc - (range.first + slots)));
}
 
Example 11
Source File: InventoryUtils.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static int sortInventoryWithinRangeByTypes(IItemHandlerModifiable inv, List<ItemTypeByName> types, SlotRange range)
{
    int slot = range.first;
    int slots = 0;

    for (ItemTypeByName type : types)
    {
        ItemStack stack = type.getStack();

        while (true)
        {
            final int max = inv instanceof IItemHandlerSize ? ((IItemHandlerSize) inv).getItemStackLimit(slot, stack) : Math.min(inv.getSlotLimit(slot), stack.getMaxStackSize());
            //System.out.printf("sorting for: %s - slot: %d, max: %d\n", stack.toString(), slot, max);

            if (slot >= range.lastInc)
            {
                //System.out.printf("slot >= range.lastInc\n");
                return slots;
            }

            SlotRange rangeTmp = new SlotRange(slot, range.lastExc - slot);
            stack = collectItemsFromInventoryFromSlotRange(inv, stack, rangeTmp, max, false, false);
            //System.out.printf("collected stack: %s from range: %s\n", stack, rangeTmp.toString());

            if (stack.isEmpty())
            {
                break;
            }

            ItemStack stackTmp = inv.getStackInSlot(slot);

            // There is a stack in the slot that we are moving items to, try to move the stack towards the end of the inventory
            if (stackTmp.isEmpty() == false)
            {
                //System.out.printf("existing stack: %s\n", inv.getStackInSlot(slot).toString());
                rangeTmp = new SlotRange(slot + 1, range.lastExc - (slot + 1));
                stackTmp = tryInsertItemStackToInventoryWithinSlotRange(inv, stackTmp, rangeTmp);
                //System.out.printf("tried moving stack to range: %s - remaining: %s\n", rangeTmp.toString(), stackTmp);

                // Failed to move the stack - this shouldn't happen, we are in trouble now!
                if (stackTmp.isEmpty() == false)
                {
                    //System.out.printf("failed moving existing stack, panic mode!\n");
                    // Try to return all the items currently being worked on and then bail out
                    tryInsertItemStackToInventoryWithinSlotRange(inv, stackTmp, range);
                    tryInsertItemStackToInventoryWithinSlotRange(inv, stack, range);
                    return slots;
                }
            }

            //System.out.printf("setting stack: %s to slot: %d - slots: %d\n", stack, slot, slots + 1);
            // Put the stack (collected starting from this slot towards the end of the inventory) into this slot
            inv.setStackInSlot(slot, stack);

            /*if (inv instanceof IItemHandlerModifiable)
            {
                ((IItemHandlerModifiable)inv).setStackInSlot(slot, stack);
            }
            else
            {
                tryToEmptySlot(inv, slots, 128);
                inv.insertItem(slot, stack, false);
            }*/

            slot++;
            slots++;
        }
    }

    return slots;
}