Skip to content

Commit

Permalink
Fix bucket interaction with tanks ignoring the new item fluid handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
founderio committed Dec 15, 2018
1 parent cdf9ced commit 7feee37
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
26 changes: 15 additions & 11 deletions common/net/teamio/taam/piping/PipeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
import net.teamio.taam.Log;
import net.teamio.taam.Taam;
import net.teamio.taam.util.FluidUtils;
Expand Down Expand Up @@ -145,25 +145,24 @@ public static boolean defaultPlayerInteraction(EntityPlayer player, IFluidTank t
Log.debug("Beginning fluid interaction.");
ItemStack playerStack = player.inventory.getCurrentItem();
ItemStack handlingStack = playerStack;
if (handlingStack == null) {
if (InventoryUtils.isEmpty(handlingStack)) {
return false;
}
boolean isPartialStack = false;
if (handlingStack.getCount() > 1) {
isPartialStack = true;
handlingStack = InventoryUtils.copyStack(handlingStack, 1);
}

IFluidHandler itemFH = FluidUtils.getFluidHandlerForItem(handlingStack);
IFluidHandlerItem itemFH = FluidUtils.getFluidHandlerForItem(handlingStack);

boolean success = false;
boolean modifyInventory = !player.capabilities.isCreativeMode;

if (itemFH != null) {
FluidStack inTank = tank.getFluid();
if (inTank != null) {
Log.debug("Attempting to fill {}x{} into item.", inTank.amount, inTank.getFluid());
// Fill into the item
int fill = itemFH.fill(inTank, true);
int fill = itemFH.fill(inTank, modifyInventory);
Log.debug("Filled {} into item.", fill);
if (fill > 0) {
// Drain from the tank
Expand All @@ -181,7 +180,7 @@ public static boolean defaultPlayerInteraction(EntityPlayer player, IFluidTank t
FluidStack toDrain = inTank.copy();
toDrain.amount = capa - inTank.amount;
// Drain maximum of that from the item
FluidStack drain = itemFH.drain(toDrain, true);
FluidStack drain = itemFH.drain(toDrain, modifyInventory);
if (drain != null && drain.amount > 0) {
Log.debug("Drained {}x{} from item.", drain.amount, drain.getFluid());
// Fill into the tank
Expand All @@ -199,7 +198,7 @@ public static boolean defaultPlayerInteraction(EntityPlayer player, IFluidTank t
} else {
Log.debug("Attempting to drain anything from item.");
// Drain maximum of tank capacity from the item
FluidStack drain = itemFH.drain(tank.getCapacity(), true);
FluidStack drain = itemFH.drain(tank.getCapacity(), modifyInventory);
if (drain != null && drain.amount > 0) {
Log.debug("Drained {}x{} from item.", drain.amount, drain.getFluid());
// Fill into the tank
Expand All @@ -215,9 +214,14 @@ public static boolean defaultPlayerInteraction(EntityPlayer player, IFluidTank t
}
}

if (success && isPartialStack) {
playerStack = InventoryUtils.adjustCount(playerStack, -1);
InventoryUtils.tryDropToInventory(player, handlingStack, player.getPosition());
if (success && modifyInventory) {
// Adjust stack that was clicked
playerStack.setCount(playerStack.getCount() - 1);
// Drop empty or filled container into inventory
handlingStack = itemFH.getContainer();
if(!InventoryUtils.isEmpty(handlingStack)) {
InventoryUtils.tryDropToInventory(player, handlingStack, player.getPosition());
}
}
return success;
}
Expand Down
18 changes: 9 additions & 9 deletions common/net/teamio/taam/util/FluidUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,43 @@
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;

public final class FluidUtils {
private FluidUtils() {
// Util Class
}

public static FluidStack getFluidFromItem(ItemStack stack) {
if(stack == null) {
if (stack == null) {
return null;
}
IFluidHandler fluidHandler = getFluidHandlerForItem(stack);
if(fluidHandler == null) {
if (fluidHandler == null) {
return null;
}
return fluidHandler.drain(Integer.MAX_VALUE, false);
}

public static IFluidHandler getFluidHandlerForItem(ItemStack stack) {
return stack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, EnumFacing.UP);
public static IFluidHandlerItem getFluidHandlerForItem(ItemStack stack) {
return stack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP);
}

public static IFluidHandler getFluidHandler(IBlockAccess world, BlockPos pos, EnumFacing side) {
return getFluidHandler(world.getTileEntity(pos), side);
}

// Deprecation because of the net.minecraftforge.fluids.IFluidHandler
@SuppressWarnings("deprecation")
public static IFluidHandler getFluidHandler(ICapabilityProvider tileEntity, EnumFacing side) {
if (tileEntity == null) {
return null;
}
IFluidHandler fluidHandler = tileEntity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side);
if (fluidHandler != null) return fluidHandler;

// Fallback if someone directly implemented the capability interface - not recommended, this is just for compatibility
if(fluidHandler == null && tileEntity instanceof IFluidHandler) {
fluidHandler = (IFluidHandler)tileEntity;
if (tileEntity instanceof IFluidHandler) {
return (IFluidHandler) tileEntity;
}
return fluidHandler;
return null;
}
}

0 comments on commit 7feee37

Please sign in to comment.