diff options
-rw-r--r-- | src/main/java/gregtech/api/gui/GT_Container_BasicMachine.java | 98 |
1 files changed, 71 insertions, 27 deletions
diff --git a/src/main/java/gregtech/api/gui/GT_Container_BasicMachine.java b/src/main/java/gregtech/api/gui/GT_Container_BasicMachine.java index da528d67fc..665be8b8ce 100644 --- a/src/main/java/gregtech/api/gui/GT_Container_BasicMachine.java +++ b/src/main/java/gregtech/api/gui/GT_Container_BasicMachine.java @@ -12,9 +12,7 @@ import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.IFluidContainerItem; -import net.minecraftforge.fluids.IFluidTank; -import static gregtech.api.enums.GT_Values.NI; import static gregtech.api.metatileentity.implementations.GT_MetaTileEntity_BasicMachine.OTHER_SLOT_COUNT; /** @@ -198,12 +196,16 @@ public class GT_Container_BasicMachine extends GT_Container_BasicTank { machine.mItemTransfer = !machine.mItemTransfer; return null; case 2: - tResultStack = pickupFluid(machine.getDrainableStack(), aPlayer); - if (machine.getDrainableStack().amount == 0) + if (aMouseclick > 1) + return null; + tResultStack = pickupFluid(machine.getDrainableStack(), aPlayer, aMouseclick == 0); + if (machine.getDrainableStack() != null && machine.getDrainableStack().amount == 0) machine.setDrainableStack(null); return tResultStack; default: if (aSlotIndex == OTHER_SLOT_COUNT + 1 + machine.mInputSlotCount + machine.mOutputItems.length) { + if (aMouseclick > 1) + return null; // input fluid slot ItemStack tStackHeld = aPlayer.inventory.getItemStack(); ItemStack tStackSizedOne = GT_Utility.copyAmount(1, tStackHeld); @@ -214,13 +216,13 @@ public class GT_Container_BasicMachine extends GT_Container_BasicTank { if (tFluidHeld == null) // both null -> no op return null; - return fillFluid(machine, aPlayer, tFluidHeld); + return fillFluid(machine, aPlayer, tFluidHeld, aMouseclick == 0); } else { if (tFluidHeld != null) { // both nonnull. actually both pickup and fill is reasonable, but I'll go with fill here - return fillFluid(machine, aPlayer, tFluidHeld); + return fillFluid(machine, aPlayer, tFluidHeld, aMouseclick == 1); } else { - tResultStack = pickupFluid(tInputFluid, aPlayer); + tResultStack = pickupFluid(tInputFluid, aPlayer, aMouseclick == 0); if (tInputFluid.amount == 0) machine.setFillableStack(null); return tResultStack; @@ -232,11 +234,12 @@ public class GT_Container_BasicMachine extends GT_Container_BasicTank { } } - private ItemStack pickupFluid(FluidStack aTankStack, EntityPlayer aPlayer) { + private ItemStack pickupFluid(FluidStack aTankStack, EntityPlayer aPlayer, boolean aProcessFullStack) { if (aTankStack == null) return null; ItemStack tStackHeld = aPlayer.inventory.getItemStack(); ItemStack tStackSizedOne = GT_Utility.copyAmount(1, tStackHeld); if (tStackSizedOne == null) return null; + int tOriginalFluidAmount = aTankStack.amount; ItemStack tFilled = GT_Utility.fillFluidContainer(aTankStack, tStackSizedOne, true, false); if (tFilled == null && tStackSizedOne.getItem() instanceof IFluidContainerItem) { IFluidContainerItem tContainerItem = (IFluidContainerItem) tStackSizedOne.getItem(); @@ -247,47 +250,88 @@ public class GT_Container_BasicMachine extends GT_Container_BasicTank { } } if (tFilled != null) { - reduceStackSizeInHandByOne(aPlayer); - GT_Utility.addItemToPlayerInventory(aPlayer, tFilled); + if (aProcessFullStack) { + int tFilledAmount = tOriginalFluidAmount - aTankStack.amount; + /* + work out how many more items we can fill + the round down behavior will left over a fraction of a cell worth of fluid + the user then get to decide what to do with it + it will not be too fancy if it spills out partially filled cells + */ + int tAdditionalParallel = Math.min(tStackHeld.stackSize, aTankStack.amount / tFilledAmount); + aTankStack.amount -= tFilledAmount * tAdditionalParallel; + tFilled.stackSize += tAdditionalParallel; + } + replaceCursorItemStack(aPlayer, tFilled); } return tFilled; } - private ItemStack fillFluid(IFluidTank aTank, EntityPlayer aPlayer, FluidStack aFluidHeld) { + private ItemStack fillFluid(GT_MetaTileEntity_BasicMachine aMachine, EntityPlayer aPlayer, FluidStack aFluidHeld, boolean aProcessFullStack) { + // we are not using aMachine.fill() here any more, so we need to check for fluid type here ourselves + if (aMachine.getFillableStack() != null && !aMachine.getFillableStack().isFluidEqual(aFluidHeld)) + return null; ItemStack tStackHeld = aPlayer.inventory.getItemStack(); ItemStack tStackSizedOne = GT_Utility.copyAmount(1, tStackHeld); if (tStackSizedOne == null) return null; - int tFilled = aTank.fill(aFluidHeld, false); - if (tFilled == 0) // filled nothing + int tFreeSpace = aMachine.getCapacity() - (aMachine.getFillableStack() != null ? aMachine.getFillableStack().amount : 0); + if (tFreeSpace <= 0) + // no space left return null; + + // find out how much fluid can be taken + // some cells cannot be partially filled ItemStack tStackEmptied = null; - if (tFilled == aFluidHeld.amount) + int tAmountTaken = 0; + if (tFreeSpace == aFluidHeld.amount) { // fully accepted - try take it from item now // IFluidContainerItem is intentionally not checked here. it will be checked later tStackEmptied = GT_Utility.getContainerForFilledItem(tStackSizedOne, false); - if (tStackEmptied == null && tStackHeld.getItem() instanceof IFluidContainerItem) { - IFluidContainerItem container = (IFluidContainerItem) tStackHeld.getItem(); - FluidStack tDrained = container.drain(tStackSizedOne, tFilled, true); - if (tDrained != null && tDrained.amount > 0) + tAmountTaken = aFluidHeld.amount; + } + if (tStackEmptied == null && tStackSizedOne.getItem() instanceof IFluidContainerItem) { + // either partially accepted, or is IFluidContainerItem + IFluidContainerItem container = (IFluidContainerItem) tStackSizedOne.getItem(); + FluidStack tDrained = container.drain(tStackSizedOne, tFreeSpace, true); + if (tDrained != null && tDrained.amount > 0) { // something is actually drained - change the cell and drop it to player tStackEmptied = tStackSizedOne; + tAmountTaken = tDrained.amount; + } } if (tStackEmptied == null) - // somehow the cell refuse to take that amount of fluid, no op then + // somehow the cell refuse to give out that amount of fluid, no op then return null; - aTank.fill(aFluidHeld, true); - GT_Utility.addItemToPlayerInventory(aPlayer, tStackEmptied); - reduceStackSizeInHandByOne(aPlayer); + + // find out how many fill can we do + // same round down behavior as above + // however here the fluid stack is not changed at all, so the exact code will slightly differ + int tParallel = aProcessFullStack ? Math.min(tFreeSpace / tAmountTaken, tStackHeld.stackSize) : 1; + if (aMachine.getFillableStack() == null) { + FluidStack tNewFillableStack = aFluidHeld.copy(); + tNewFillableStack.amount = tAmountTaken * tParallel; + aMachine.setFillableStack(tNewFillableStack); + } else { + aMachine.getFillableStack().amount += tAmountTaken * tParallel; + } + tStackEmptied.stackSize = tParallel; + replaceCursorItemStack(aPlayer, tStackEmptied); return tStackEmptied; } - private void reduceStackSizeInHandByOne(EntityPlayer aPlayer) { - ItemStack tStackHeld = aPlayer.inventory.getItemStack(); - tStackHeld.stackSize -= 1; - if (tStackHeld.stackSize == 0) - aPlayer.inventory.setItemStack(NI); + private void replaceCursorItemStack(EntityPlayer aPlayer, ItemStack tStackResult) { + if (aPlayer.inventory.getItemStack().stackSize == tStackResult.stackSize) { + // every cell is mutated. it could just stay on the cursor. + aPlayer.inventory.setItemStack(tStackResult); + } else { + // some cells not mutated. The mutated cells must go into the inventory + // or drop into the world if there isn't enough space. + ItemStack tStackHeld = aPlayer.inventory.getItemStack(); + tStackHeld.stackSize -= tStackResult.stackSize; + GT_Utility.addItemToPlayerInventory(aPlayer, tStackResult); + } } @Override |