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

The following examples show how to use net.minecraftforge.items.IItemHandlerModifiable#setStackInSlot() . 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: GTUtility.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void readItems(IItemHandlerModifiable handler, String tagName, NBTTagCompound tag) {
    if (tag.hasKey(tagName)) {
        NBTTagList tagList = tag.getTagList(tagName, Constants.NBT.TAG_COMPOUND);

        for (int i = 0; i < tagList.tagCount(); i++) {
            int slot = tagList.getCompoundTagAt(i).getInteger("Slot");

            if (slot >= 0 && slot < handler.getSlots()) {
                handler.setStackInSlot(slot, new ItemStack(tagList.getCompoundTagAt(i)));
            }
        }
    }
}
 
Example 2
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 3
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 4
Source File: LootTableHelper.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void fillInventory(IItemHandlerModifiable inventory, Random rand, List<ItemStack> lootList) {
    List<Integer> randomSlots = getEmptySlotsRandomized(inventory, rand);
    shuffleItems(lootList, randomSlots.size(), rand);

    for (ItemStack itemstack : lootList) {
        if (randomSlots.isEmpty()) {
            return;
        }
        if (itemstack.isEmpty()) {
            inventory.setStackInSlot(randomSlots.remove(randomSlots.size() - 1), ItemStack.EMPTY);
        } else {
            inventory.setStackInSlot(randomSlots.remove(randomSlots.size() - 1), itemstack);
        }
    }
}
 
Example 5
Source File: GTUtility.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void copyInventoryItems(IItemHandler src, IItemHandlerModifiable dest) {
    for (int i = 0; i < src.getSlots(); i++) {
        ItemStack itemStack = src.getStackInSlot(i);
        dest.setStackInSlot(i, itemStack.isEmpty() ? ItemStack.EMPTY : itemStack.copy());
    }
}
 
Example 6
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;
}