Search in sources :

Example 1 with IInventorySlots

use of binnie.core.machines.inventory.IInventorySlots in project Binnie by ForestryMC.

the class TransferRequest method transfer.

public TransferResult transfer(@Nullable EntityPlayer player, boolean doAdd) {
    ItemStack item = this.returnItem;
    if (item.isEmpty() || this.destination == null) {
        return TransferResult.FAILURE;
    }
    if (this.transferLiquids && this.destination instanceof ITankMachine && item.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null)) {
        ITankMachine tankMachine = (ITankMachine) this.destination;
        IFluidHandler fluidHandler = tankMachine.getHandler(targetTanks);
        if (fluidHandler != null) {
            ItemStack singleCopy = ItemHandlerHelper.copyStackWithSize(item, 1);
            FluidActionResult fluidActionResult;
            if (this.origin != null && doAdd) {
                IItemHandler itemHandler = new InvWrapper(this.origin);
                fluidActionResult = FluidUtil.tryEmptyContainerAndStow(singleCopy, fluidHandler, itemHandler, Fluid.BUCKET_VOLUME, player, doAdd);
                if (!fluidActionResult.isSuccess()) {
                    fluidActionResult = FluidUtil.tryFillContainerAndStow(singleCopy, fluidHandler, itemHandler, Fluid.BUCKET_VOLUME, player, doAdd);
                }
            } else {
                fluidActionResult = FluidUtil.tryEmptyContainer(singleCopy, fluidHandler, Fluid.BUCKET_VOLUME, player, doAdd);
                if (!fluidActionResult.isSuccess()) {
                    fluidActionResult = FluidUtil.tryFillContainer(singleCopy, fluidHandler, Fluid.BUCKET_VOLUME, player, doAdd);
                }
            }
            if (fluidActionResult.isSuccess()) {
                if (item.getCount() == 1) {
                    return new TransferResult(fluidActionResult.result);
                } else {
                    ItemStack itemCopy = item.copy();
                    itemCopy.shrink(1);
                    return new TransferResult(fluidActionResult.result, itemCopy);
                }
            }
            return TransferResult.FAILURE;
        }
    }
    if (!item.isEmpty()) {
        for (final int slot : this.targetSlots) {
            if (this.destination.isItemValidForSlot(slot, item) || this.ignoreReadOnly) {
                if (this.destination instanceof IInventorySlots) {
                    InventorySlot inventorySlot = ((IInventorySlots) this.destination).getSlot(slot);
                    if (inventorySlot != null && inventorySlot.isRecipe()) {
                        continue;
                    }
                }
                ItemStack stackInSlot = this.destination.getStackInSlot(slot);
                if (!stackInSlot.isEmpty()) {
                    if (item.isStackable()) {
                        final ItemStack merged = stackInSlot.copy();
                        final NonNullList<ItemStack> newStacks = mergeStacks(item.copy(), merged.copy());
                        item = newStacks.get(0);
                        if (!areItemsEqual(merged, newStacks.get(1))) {
                            this.insertedSlots.add(new TransferSlot(slot, this.destination));
                        }
                        if (doAdd) {
                            this.destination.setInventorySlotContents(slot, newStacks.get(1));
                        }
                        if (item.isEmpty()) {
                            return new TransferResult(ItemStack.EMPTY);
                        }
                    }
                }
            }
        }
    }
    if (!item.isEmpty()) {
        for (final int slot : this.targetSlots) {
            if (this.destination.isItemValidForSlot(slot, item) || this.ignoreReadOnly) {
                if (this.destination instanceof IInventorySlots) {
                    InventorySlot inventorySlot = ((IInventorySlots) this.destination).getSlot(slot);
                    if (inventorySlot != null && inventorySlot.isRecipe()) {
                        continue;
                    }
                }
                if (this.destination.getStackInSlot(slot).isEmpty()) {
                    this.insertedSlots.add(new TransferSlot(slot, this.destination));
                    if (doAdd) {
                        this.destination.setInventorySlotContents(slot, item.copy());
                    }
                    return new TransferResult(ItemStack.EMPTY);
                }
            }
        }
    }
    this.setReturnItem(item);
    return new TransferResult(item);
}
Also used : ITankMachine(binnie.core.machines.power.ITankMachine) IInventorySlots(binnie.core.machines.inventory.IInventorySlots) IItemHandler(net.minecraftforge.items.IItemHandler) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) InventorySlot(binnie.core.machines.inventory.InventorySlot) ItemStack(net.minecraft.item.ItemStack) FluidActionResult(net.minecraftforge.fluids.FluidActionResult) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Aggregations

IInventorySlots (binnie.core.machines.inventory.IInventorySlots)1 InventorySlot (binnie.core.machines.inventory.InventorySlot)1 ITankMachine (binnie.core.machines.power.ITankMachine)1 ItemStack (net.minecraft.item.ItemStack)1 FluidActionResult (net.minecraftforge.fluids.FluidActionResult)1 IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)1 IItemHandler (net.minecraftforge.items.IItemHandler)1 InvWrapper (net.minecraftforge.items.wrapper.InvWrapper)1