Search in sources :

Example 51 with ISidedInventory

use of net.minecraft.inventory.ISidedInventory in project BloodMagic by WayofTime.

the class RitualEffectItemRouting method performEffect.

@Override
public void performEffect(IMasterRitualStone ritualStone) {
    String owner = ritualStone.getOwner();
    int currentEssence = SoulNetworkHandler.getCurrentEssence(owner);
    World world = ritualStone.getWorld();
    int x = ritualStone.getXCoord();
    int y = ritualStone.getYCoord();
    int z = ritualStone.getZCoord();
    if (world.getWorldTime() % 20 != 0) {
        return;
    }
    Map<Int3, IInventory> tileMap = new HashMap();
    List<IInventory> outputList = new ArrayList();
    for (// Check output foci chests, return if none available
    int i = 0; // Check output foci chests, return if none available
    i < 4; // Check output foci chests, return if none available
    i++) {
        Int3 outputFocusChest = this.getOutputBufferChestLocation(i);
        TileEntity outputFocusInv = world.getTileEntity(x + outputFocusChest.xCoord, y + outputFocusChest.yCoord, z + outputFocusChest.zCoord);
        if (outputFocusInv instanceof IInventory) {
            outputList.add((IInventory) outputFocusInv);
        }
    }
    if (outputList.isEmpty()) {
        return;
    }
    for (IInventory outputFocusInventory : outputList) {
        {
            OutputRoutingFocus outputFocus;
            ;
            RoutingFocusParadigm parad = new RoutingFocusParadigm();
            // Destination
            TileEntity outputChest = null;
            ForgeDirection inputDirection;
            {
                IInventory outputChestInventory = null;
                boolean lastItemWasFocus = true;
                for (int j = 0; j < outputFocusInventory.getSizeInventory(); j++) {
                    ItemStack keyStack = outputFocusInventory.getStackInSlot(j);
                    if (keyStack == null) {
                        continue;
                    }
                    if (keyStack.getItem() instanceof OutputRoutingFocus) {
                        if (!lastItemWasFocus) {
                            parad.clear();
                        }
                        outputFocus = (OutputRoutingFocus) keyStack.getItem();
                        parad.addRoutingFocusPosAndFacing(outputFocus.getPosAndFacing(keyStack));
                        parad.addLogic(outputFocus.getLogic(keyStack));
                        lastItemWasFocus = true;
                        continue;
                    } else {
                        lastItemWasFocus = false;
                    }
                    for (RoutingFocusPosAndFacing posAndFacing : parad.locationList) {
                        if (posAndFacing == null) {
                            continue;
                        }
                        inputDirection = posAndFacing.facing;
                        if (outputChest == null || !posAndFacing.location.equals(new Int3(outputChest.xCoord, outputChest.yCoord, outputChest.zCoord))) {
                            outputChest = world.getTileEntity(posAndFacing.location.xCoord, posAndFacing.location.yCoord, posAndFacing.location.zCoord);
                            if (outputChest instanceof IInventory) {
                                outputChestInventory = (IInventory) outputChest;
                            } else {
                                continue;
                            }
                        }
                        for (int i = 0; i < 4; i++) {
                            Int3 inputFocusChest = this.getInputBufferChestLocation(i);
                            TileEntity inputFocusInv = world.getTileEntity(x + inputFocusChest.xCoord, y + inputFocusChest.yCoord, z + inputFocusChest.zCoord);
                            if (inputFocusInv instanceof IInventory) {
                                for (// Iterate through foci inventory
                                int ji = 0; // Iterate through foci inventory
                                ji < ((IInventory) inputFocusInv).getSizeInventory(); // Iterate through foci inventory
                                ji++) {
                                    ItemStack inputFocusStack = ((IInventory) inputFocusInv).getStackInSlot(ji);
                                    if (inputFocusStack != null && inputFocusStack.getItem() instanceof InputRoutingFocus) {
                                        InputRoutingFocus inputFocus = (InputRoutingFocus) inputFocusStack.getItem();
                                        TileEntity inputChest = world.getTileEntity(inputFocus.xCoord(inputFocusStack), inputFocus.yCoord(inputFocusStack), inputFocus.zCoord(inputFocusStack));
                                        if (inputChest instanceof IInventory) {
                                            IInventory inputChestInventory = (IInventory) inputChest;
                                            ForgeDirection syphonDirection = inputFocus.getSetDirection(inputFocusStack);
                                            boolean[] canSyphonList = new boolean[inputChestInventory.getSizeInventory()];
                                            if (inputChest instanceof ISidedInventory) {
                                                int[] validSlots = ((ISidedInventory) inputChest).getAccessibleSlotsFromSide(syphonDirection.ordinal());
                                                for (int in : validSlots) {
                                                    canSyphonList[in] = true;
                                                }
                                            } else {
                                                for (int ni = 0; ni < inputChestInventory.getSizeInventory(); ni++) {
                                                    canSyphonList[ni] = true;
                                                }
                                            }
                                            for (int ni = 0; ni < inputChestInventory.getSizeInventory(); ni++) {
                                                if (canSyphonList[ni]) {
                                                    // Has a syphoned item linked, next need to find a destination
                                                    ItemStack syphonedStack = inputChestInventory.getStackInSlot(ni);
                                                    if (syphonedStack == null || (inputChestInventory instanceof ISidedInventory && !((ISidedInventory) inputChestInventory).canExtractItem(ni, syphonedStack, syphonDirection.ordinal()))) {
                                                        continue;
                                                    }
                                                    int size = syphonedStack.stackSize;
                                                    if (parad.doesItemMatch(keyStack, syphonedStack)) {
                                                        ItemStack newStack = null;
                                                        if (parad.maximumAmount <= 0) {
                                                            newStack = SpellHelper.insertStackIntoInventory(syphonedStack, outputChestInventory, inputDirection);
                                                        } else {
                                                            newStack = SpellHelper.insertStackIntoInventory(syphonedStack, outputChestInventory, inputDirection, parad.maximumAmount);
                                                        }
                                                        if (size == newStack.stackSize) {
                                                            continue;
                                                        }
                                                        int numberSyphoned = size - newStack.stackSize;
                                                        if (newStack != null && newStack.stackSize <= 0) {
                                                            size = newStack.stackSize;
                                                            newStack = null;
                                                        }
                                                        inputChestInventory.setInventorySlotContents(ni, newStack);
                                                    // break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : IInventory(net.minecraft.inventory.IInventory) HashMap(java.util.HashMap) Int3(WayofTime.alchemicalWizardry.api.Int3) ArrayList(java.util.ArrayList) InputRoutingFocus(WayofTime.alchemicalWizardry.common.items.routing.InputRoutingFocus) OutputRoutingFocus(WayofTime.alchemicalWizardry.common.items.routing.OutputRoutingFocus) RoutingFocusParadigm(WayofTime.alchemicalWizardry.api.RoutingFocusParadigm) World(net.minecraft.world.World) TileEntity(net.minecraft.tileentity.TileEntity) ISidedInventory(net.minecraft.inventory.ISidedInventory) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) ItemStack(net.minecraft.item.ItemStack) RoutingFocusPosAndFacing(WayofTime.alchemicalWizardry.api.RoutingFocusPosAndFacing)

Example 52 with ISidedInventory

use of net.minecraft.inventory.ISidedInventory in project BloodMagic by WayofTime.

the class SpellHelper method insertStackIntoInventory.

public static ItemStack insertStackIntoInventory(ItemStack stack, IInventory inventory, ForgeDirection dir, int limit) {
    if (stack == null) {
        return null;
    }
    boolean[] canBeInserted = new boolean[inventory.getSizeInventory()];
    if (inventory instanceof ISidedInventory) {
        int[] array = ((ISidedInventory) inventory).getAccessibleSlotsFromSide(dir.ordinal());
        for (int in : array) {
            canBeInserted[in] = ((ISidedInventory) inventory).canInsertItem(in, stack, dir.ordinal());
        }
    } else {
        for (int i = 0; i < canBeInserted.length; i++) {
            canBeInserted[i] = true;
        }
    }
    int numberMatching = 0;
    for (int i = 0; i < inventory.getSizeInventory(); i++) {
        if (!canBeInserted[i]) {
            continue;
        }
        ItemStack invStack = inventory.getStackInSlot(i);
        if (invStack != null && canCombine(stack, invStack)) {
            numberMatching += invStack.stackSize;
        }
    }
    if (numberMatching >= limit) {
        return stack;
    }
    int newLimit = limit - numberMatching;
    for (int i = 0; i < inventory.getSizeInventory(); i++) {
        if (!canBeInserted[i]) {
            continue;
        }
        int prevStackSize = stack.stackSize;
        ItemStack[] combinedStacks = combineStacks(stack, inventory.getStackInSlot(i), newLimit);
        stack = combinedStacks[0];
        inventory.setInventorySlotContents(i, combinedStacks[1]);
        newLimit -= (prevStackSize - stack.stackSize);
        if (newLimit <= 0 || stack.stackSize <= 0) {
            return stack;
        }
    }
    return stack;
}
Also used : ISidedInventory(net.minecraft.inventory.ISidedInventory) ItemStack(net.minecraft.item.ItemStack)

Example 53 with ISidedInventory

use of net.minecraft.inventory.ISidedInventory in project BloodMagic by WayofTime.

the class SpellHelper method canInsertStackFullyIntoInventory.

public static boolean canInsertStackFullyIntoInventory(ItemStack stack, IInventory inventory, ForgeDirection dir, boolean fillToLimit, int limit) {
    if (stack == null) {
        return true;
    }
    int itemsLeft = stack.stackSize;
    boolean[] canBeInserted = new boolean[inventory.getSizeInventory()];
    if (inventory instanceof ISidedInventory) {
        int[] array = ((ISidedInventory) inventory).getAccessibleSlotsFromSide(dir.ordinal());
        for (int in : array) {
            canBeInserted[in] = inventory.isItemValidForSlot(in, stack) && ((ISidedInventory) inventory).canInsertItem(in, stack, dir.ordinal());
        }
    } else {
        for (int i = 0; i < canBeInserted.length; i++) {
            canBeInserted[i] = inventory.isItemValidForSlot(i, stack);
        }
    }
    int numberMatching = 0;
    if (fillToLimit) {
        for (int i = 0; i < inventory.getSizeInventory(); i++) {
            if (!canBeInserted[i]) {
                continue;
            }
            ItemStack invStack = inventory.getStackInSlot(i);
            if (invStack != null && canCombine(stack, invStack)) {
                numberMatching += invStack.stackSize;
            }
        }
    }
    if (fillToLimit && limit < stack.stackSize + numberMatching) {
        return false;
    }
    for (int i = 0; i < inventory.getSizeInventory(); i++) {
        if (!canBeInserted[i]) {
            continue;
        }
        ItemStack invStack = inventory.getStackInSlot(i);
        boolean canCombine = canCombine(stack, invStack);
        if (canCombine) {
            if (invStack == null) {
                itemsLeft = 0;
            } else {
                itemsLeft -= (invStack.getMaxStackSize() - invStack.stackSize);
            }
        }
        if (itemsLeft <= 0) {
            return true;
        }
    }
    return false;
}
Also used : ISidedInventory(net.minecraft.inventory.ISidedInventory) ItemStack(net.minecraft.item.ItemStack)

Aggregations

ISidedInventory (net.minecraft.inventory.ISidedInventory)53 ItemStack (net.minecraft.item.ItemStack)36 IInventory (net.minecraft.inventory.IInventory)24 TileEntity (net.minecraft.tileentity.TileEntity)9 IItemHandler (net.minecraftforge.items.IItemHandler)7 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)5 ArrayList (java.util.ArrayList)3 World (net.minecraft.world.World)3 Pos (com.builtbroken.mc.lib.transform.vector.Pos)2 IBee (forestry.api.apiculture.IBee)2 IInventoryUtil (logisticspipes.interfaces.IInventoryUtil)2 ISlotUpgradeManager (logisticspipes.interfaces.ISlotUpgradeManager)2 TileEntityChest (net.minecraft.tileentity.TileEntityChest)2 EnumFacing (net.minecraft.util.EnumFacing)2 InvWrapper (net.minecraftforge.items.wrapper.InvWrapper)2 SidedInvWrapper (net.minecraftforge.items.wrapper.SidedInvWrapper)2 Int3 (WayofTime.alchemicalWizardry.api.Int3)1 RoutingFocusParadigm (WayofTime.alchemicalWizardry.api.RoutingFocusParadigm)1 RoutingFocusPosAndFacing (WayofTime.alchemicalWizardry.api.RoutingFocusPosAndFacing)1 IBloodAltar (WayofTime.alchemicalWizardry.api.tile.IBloodAltar)1