Implement fluid item capabilities without item swapping

This commit is contained in:
mezz authored and LexManos committed 2016-11-16 16:17:21 -08:00
1 parent 6f1a44a405
commit eb6b3ea228
15 files changed
+437 -227

No files matched your search

@@ -36,6 +36,7 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
import net.minecraftforge.fluids.capability.wrappers.BlockLiquidWrapper;
import net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper;
import net.minecraftforge.items.IItemHandler;
@@ -53,37 +54,51 @@ public class FluidUtil
* First it tries to fill the container item from the fluid handler,
* if that action fails then it tries to drain the container item into the fluid handler.
*
* Returns true if interaction was successful.
* Returns false if interaction failed.
* @param stack The filled or empty fluid container.
* Will not be modified directly, if modifications are necessary a modified copy is returned in the result.
* @param fluidHandler The fluid handler to interact with.
* @param player The player doing the interaction between the item and fluid handler.
* @return a {@link FluidActionResult} holding the result and resulting container.
*/
public static boolean interactWithFluidHandler(ItemStack stack, IFluidHandler fluidHandler, EntityPlayer player)
@Nonnull
public static FluidActionResult interactWithFluidHandler(@Nonnull ItemStack stack, IFluidHandler fluidHandler, EntityPlayer player)
{
if (stack == null || fluidHandler == null || player == null)
if (stack.func_190926_b() || fluidHandler == null || player == null)
{
return false;
return FluidActionResult.FAILURE;
}
IItemHandler playerInventory = new InvWrapper(player.inventory);
return tryFillContainerAndStow(stack, fluidHandler, playerInventory, Integer.MAX_VALUE, player) ||
tryEmptyContainerAndStow(stack, fluidHandler, playerInventory, Integer.MAX_VALUE, player);
FluidActionResult fillResult = tryFillContainerAndStow(stack, fluidHandler, playerInventory, Integer.MAX_VALUE, player);
if (fillResult.isSuccess())
{
return fillResult;
}
else
{
return tryEmptyContainerAndStow(stack, fluidHandler, playerInventory, Integer.MAX_VALUE, player);
}
}
/**
* Fill a container from the given fluidSource.
*
* @param container The container to be filled. Will not be modified.
* Separate handling must be done to reduce the stack size, stow containers, etc, on success.
* See {@link #tryFillContainerAndStow(ItemStack, IFluidHandler, IItemHandler, int, EntityPlayer)}.
* @param fluidSource The fluid handler to be drained.
* @param maxAmount The largest amount of fluid that should be transferred.
* @param player The player to make the filling noise. Pass null for no noise.
* @param doFill true if the container should actually be filled, false if it should be simulated.
* @return The filled container or null if the liquid couldn't be taken from the tank.
* @return a {@link FluidActionResult} holding the filled container if successful.
*/
@Nonnull
public static ItemStack tryFillContainer(@Nonnull ItemStack container, IFluidHandler fluidSource, int maxAmount, @Nullable EntityPlayer player, boolean doFill)
public static FluidActionResult tryFillContainer(@Nonnull ItemStack container, IFluidHandler fluidSource, int maxAmount, @Nullable EntityPlayer player, boolean doFill)
{
container = container.copy(); // do not modify the input
container.func_190920_e(1);
IFluidHandler containerFluidHandler = getFluidHandler(container);
ItemStack containerCopy = container.copy(); // do not modify the input
containerCopy.func_190920_e(1);
IFluidHandlerItem containerFluidHandler = getFluidHandler(containerCopy);
if (containerFluidHandler != null)
{
FluidStack simulatedTransfer = tryFluidTransfer(containerFluidHandler, fluidSource, maxAmount, false);
@@ -102,30 +117,33 @@ public class FluidUtil
{
containerFluidHandler.fill(simulatedTransfer, true);
}
return container;
ItemStack resultContainer = containerFluidHandler.getContainer();
return new FluidActionResult(resultContainer);
}
}
return null;
return FluidActionResult.FAILURE;
}
/**
* Takes a filled container and tries to empty it into the given tank.
*
* @param container The filled container. Will not be modified.
* Separate handling must be done to reduce the stack size, stow containers, etc, on success.
* See {@link #tryEmptyContainerAndStow(ItemStack, IFluidHandler, IItemHandler, int, EntityPlayer)}.
* @param fluidDestination The fluid handler to be filled by the container.
* @param maxAmount The largest amount of fluid that should be transferred.
* @param player Player for making the bucket drained sound. Pass null for no noise.
* @param doDrain true if the container should actually be drained, false if it should be simulated.
* @return The empty container if successful, null if the fluid handler couldn't be filled.
* NOTE The empty container will have a stackSize of 0 when a filled container is consumable,
* i.e. it has a "null" empty container but has successfully been emptied.
* @return a {@link FluidActionResult} holding the empty container if the fluid handler was filled.
* NOTE If the container is consumable, the empty container will be null on success.
*/
@Nonnull
public static ItemStack tryEmptyContainer(@Nonnull ItemStack container, IFluidHandler fluidDestination, int maxAmount, @Nullable EntityPlayer player, boolean doDrain)
public static FluidActionResult tryEmptyContainer(@Nonnull ItemStack container, IFluidHandler fluidDestination, int maxAmount, @Nullable EntityPlayer player, boolean doDrain)
{
container = container.copy(); // do not modify the input
container.func_190920_e(1);
IFluidHandler containerFluidHandler = getFluidHandler(container);
ItemStack containerCopy = container.copy(); // do not modify the input
containerCopy.func_190920_e(1);
IFluidHandlerItem containerFluidHandler = getFluidHandler(containerCopy);
if (containerFluidHandler != null)
{
FluidStack simulatedTransfer = tryFluidTransfer(fluidDestination, containerFluidHandler, maxAmount, false);
@@ -144,10 +162,12 @@ public class FluidUtil
{
containerFluidHandler.drain(simulatedTransfer, true);
}
return container;
ItemStack resultContainer = containerFluidHandler.getContainer();
return new FluidActionResult(resultContainer);
}
}
return null;
return FluidActionResult.FAILURE;
}
/**
@@ -157,62 +177,65 @@ public class FluidUtil
* If the inventory does not accept it, it will be given to the player or dropped at the players feet.
* If player is null in this case, the action will be aborted.
*
* @param container The Fluid Container Itemstack to fill. This stack WILL be modified on success.
* @param container The Fluid Container ItemStack to fill.
* Will not be modified directly, if modifications are necessary a modified copy is returned in the result.
* @param fluidSource The fluid source to fill from
* @param inventory An inventory where any additionally created item (filled container if multiple empty are present) are put
* @param maxAmount Maximum amount of fluid to take from the tank.
* @param player The player that gets the items the inventory can't take. Can be null, only used if the inventory cannot take the filled stack.
* @return True if the container was filled successfully and stowed, false otherwise.
* @param player The player that gets the items the inventory can't take.
* Can be null, only used if the inventory cannot take the filled stack.
* @return a {@link FluidActionResult} holding the result and the resulting container. The resulting container is empty on failure.
*/
public static boolean tryFillContainerAndStow(@Nonnull ItemStack container, IFluidHandler fluidSource, IItemHandler inventory, int maxAmount, @Nullable EntityPlayer player)
@Nonnull
public static FluidActionResult tryFillContainerAndStow(@Nonnull ItemStack container, IFluidHandler fluidSource, IItemHandler inventory, int maxAmount, @Nullable EntityPlayer player)
{
if (container.func_190926_b())
{
return false;
return FluidActionResult.FAILURE;
}
if (player != null && player.capabilities.isCreativeMode)
{
ItemStack filledReal = tryFillContainer(container, fluidSource, maxAmount, player, true);
if (filledReal != null)
FluidActionResult filledReal = tryFillContainer(container, fluidSource, maxAmount, player, true);
if (filledReal.isSuccess())
{
return true;
return new FluidActionResult(container); // creative mode: item does not change
}
}
else if (container.func_190916_E() == 1) // don't need to stow anything, just fill and edit the container stack
else if (container.func_190916_E() == 1) // don't need to stow anything, just fill the container stack
{
ItemStack filledReal = tryFillContainer(container, fluidSource, maxAmount, player, true);
if (filledReal != null)
FluidActionResult filledReal = tryFillContainer(container, fluidSource, maxAmount, player, true);
if (filledReal.isSuccess())
{
container.deserializeNBT(filledReal.serializeNBT());
return true;
return filledReal;
}
}
else
{
ItemStack filledSimulated = tryFillContainer(container, fluidSource, maxAmount, player, false);
if (filledSimulated != null)
FluidActionResult filledSimulated = tryFillContainer(container, fluidSource, maxAmount, player, false);
if (filledSimulated.isSuccess())
{
// check if we can give the itemStack to the inventory
ItemStack remainder = ItemHandlerHelper.insertItemStacked(inventory, filledSimulated, true);
if (remainder == null || player != null)
ItemStack remainder = ItemHandlerHelper.insertItemStacked(inventory, filledSimulated.getResult(), true);
if (remainder.func_190926_b() || player != null)
{
ItemStack filledReal = tryFillContainer(container, fluidSource, maxAmount, player, true);
remainder = ItemHandlerHelper.insertItemStacked(inventory, filledReal, false);
FluidActionResult filledReal = tryFillContainer(container, fluidSource, maxAmount, player, true);
remainder = ItemHandlerHelper.insertItemStacked(inventory, filledReal.getResult(), false);
// give it to the player or drop it at their feet
if (remainder != null && player != null)
if (!remainder.func_190926_b() && player != null)
{
ItemHandlerHelper.giveItemToPlayer(player, remainder);
}
container.func_190918_g(1);
return true;
ItemStack containerCopy = container.copy();
containerCopy.func_190918_g(1);
return new FluidActionResult(containerCopy);
}
}
}
return false;
return FluidActionResult.FAILURE;
}
/**
@@ -222,72 +245,64 @@ public class FluidUtil
* If the inventory does not accept the emptied container, it will be given to the player or dropped at the players feet.
* If player is null in this case, the action will be aborted.
*
* @param container The filled Fluid Container Itemstack to empty. This stack WILL be modified on success.
* @param container The filled Fluid Container Itemstack to empty.
* Will not be modified directly, if modifications are necessary a modified copy is returned in the result.
* @param fluidDestination The fluid destination to fill from the fluid container.
* @param inventory An inventory where any additionally created item (filled container if multiple empty are present) are put
* @param maxAmount Maximum amount of fluid to take from the tank.
* @param player The player that gets the items the inventory can't take. Can be null, only used if the inventory cannot take the filled stack.
* @return True if the container was filled successfully and stowed, false otherwise.
* @return a {@link FluidActionResult} holding the result and the resulting container. The resulting container is empty on failure.
*/
public static boolean tryEmptyContainerAndStow(@Nonnull ItemStack container, IFluidHandler fluidDestination, IItemHandler inventory, int maxAmount, @Nullable EntityPlayer player)
@Nonnull
public static FluidActionResult tryEmptyContainerAndStow(@Nonnull ItemStack container, IFluidHandler fluidDestination, IItemHandler inventory, int maxAmount, @Nullable EntityPlayer player)
{
if (container.func_190926_b())
{
return false;
return FluidActionResult.FAILURE;
}
if (player != null && player.capabilities.isCreativeMode)
{
ItemStack emptiedReal = tryEmptyContainer(container, fluidDestination, maxAmount, player, true);
if (emptiedReal != null)
FluidActionResult emptiedReal = tryEmptyContainer(container, fluidDestination, maxAmount, player, true);
if (emptiedReal.isSuccess())
{
return true;
return new FluidActionResult(container); // creative mode: item does not change
}
}
else if (container.func_190916_E() == 1) // don't need to stow anything, just fill and edit the container stack
{
ItemStack emptiedReal = tryEmptyContainer(container, fluidDestination, maxAmount, player, true);
if (emptiedReal.func_190926_b())
FluidActionResult emptiedReal = tryEmptyContainer(container, fluidDestination, maxAmount, player, true);
if (emptiedReal.isSuccess())
{
container.func_190918_g(1);
return emptiedReal;
}
else
{
container.deserializeNBT(emptiedReal.serializeNBT());
}
return true;
}
else
{
ItemStack emptiedSimulated = tryEmptyContainer(container, fluidDestination, maxAmount, player, false);
if (emptiedSimulated.func_190926_b())
{
tryEmptyContainer(container, fluidDestination, maxAmount, player, true);
container.func_190918_g(1);
return true;
}
else
FluidActionResult emptiedSimulated = tryEmptyContainer(container, fluidDestination, maxAmount, player, false);
if (emptiedSimulated.isSuccess())
{
// check if we can give the itemStack to the inventory
ItemStack remainder = ItemHandlerHelper.insertItemStacked(inventory, emptiedSimulated, true);
if (remainder == null || player != null)
ItemStack remainder = ItemHandlerHelper.insertItemStacked(inventory, emptiedSimulated.getResult(), true);
if (remainder.func_190926_b() || player != null)
{
ItemStack emptiedReal = tryEmptyContainer(container, fluidDestination, maxAmount, player, true);
remainder = ItemHandlerHelper.insertItemStacked(inventory, emptiedReal, false);
FluidActionResult emptiedReal = tryEmptyContainer(container, fluidDestination, maxAmount, player, true);
remainder = ItemHandlerHelper.insertItemStacked(inventory, emptiedReal.getResult(), false);
// give it to the player or drop it at their feet
if (remainder != null && player != null)
if (!remainder.func_190926_b() && player != null)
{
ItemHandlerHelper.giveItemToPlayer(player, remainder);
}
container.func_190918_g(1);
return true;
ItemStack containerCopy = container.copy();
containerCopy.func_190918_g(1);
return new FluidActionResult(containerCopy);
}
}
}
return false;
return FluidActionResult.FAILURE;
}
/**
@@ -308,11 +323,19 @@ public class FluidUtil
int fillableAmount = fluidDestination.fill(drainable, false);
if (fillableAmount > 0)
{
FluidStack drained = fluidSource.drain(fillableAmount, doTransfer);
if (drained != null)
if (doTransfer)
{
drained.amount = fluidDestination.fill(drained, doTransfer);
return drained;
FluidStack drained = fluidSource.drain(fillableAmount, true);
if (drained != null)
{
drained.amount = fluidDestination.fill(drained, true);
return drained;
}
}
else
{
drainable.amount = fillableAmount;
return drainable;
}
}
}
@@ -320,23 +343,25 @@ public class FluidUtil
}
/**
* Helper method to get an IFluidHandler for an itemStack.
* Helper method to get an {@link IFluidHandlerItem} for an itemStack.
*
* The itemStack passed in here WILL be modified, the IFluidHandler acts on it directly.
* The itemStack passed in here WILL be modified, the {@link IFluidHandlerItem} acts on it directly.
* Some {@link IFluidHandlerItem} will change the item entirely, always use {@link IFluidHandlerItem#getContainer()}
* after using the fluid handler to get the resulting item back.
*
* Note that the itemStack MUST have a stackSize of 1 if you want to fill or drain it.
* You can't fill or drain a whole stack at once, if you do then liquid is multiplied or destroyed.
* You can't fill or drain multiple items at once, if you do then liquid is multiplied or destroyed.
*
* Vanilla buckets will be converted to universal buckets if they are enabled.
*
* Returns null if the itemStack passed in does not have a fluid handler.
*/
@Nullable
public static IFluidHandler getFluidHandler(ItemStack itemStack)
public static IFluidHandlerItem getFluidHandler(@Nonnull ItemStack itemStack)
{
if (itemStack != null && itemStack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null))
if (itemStack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null))
{
return itemStack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null);
return itemStack.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
}
else
{
@@ -348,13 +373,13 @@ public class FluidUtil
* Helper method to get the fluid contained in an itemStack
*/
@Nullable
public static FluidStack getFluidContained(ItemStack container)
public static FluidStack getFluidContained(@Nonnull ItemStack container)
{
if (container != null)
if (!container.func_190926_b())
{
container = container.copy();
container.func_190920_e(1);
IFluidHandler fluidHandler = FluidUtil.getFluidHandler(container);
IFluidHandlerItem fluidHandler = FluidUtil.getFluidHandler(container);
if (fluidHandler != null)
{
return fluidHandler.drain(Integer.MAX_VALUE, false);
@@ -453,18 +478,20 @@ public class FluidUtil
/**
* Attempts to pick up a fluid in the world and put it in an empty container item.
*
* @param emptyContainer The empty container to fill. Will not be modified.
* @param emptyContainer The empty container to fill.
* Will not be modified directly, if modifications are necessary a modified copy is returned in the result.
* @param playerIn The player filling the container. Optional.
* @param worldIn The world the fluid is in.
* @param pos The position of the fluid in the world.
* @param side The side of the fluid that is being drained.
* @return a filled container if it was successful. returns null on failure.
* @return a {@link FluidActionResult} holding the result and the resulting container.
*/
@Nonnull
public static ItemStack tryPickUpFluid(@Nonnull ItemStack emptyContainer, @Nullable EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side)
public static FluidActionResult tryPickUpFluid(@Nonnull ItemStack emptyContainer, @Nullable EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side)
{
if (emptyContainer.func_190926_b() || worldIn == null || pos == null) {
return ItemStack.field_190927_a;
if (emptyContainer.func_190926_b() || worldIn == null || pos == null)
{
return FluidActionResult.FAILURE;
}
IBlockState state = worldIn.getBlockState(pos);
@@ -475,9 +502,13 @@ public class FluidUtil
IFluidHandler targetFluidHandler = FluidUtil.getFluidHandler(worldIn, pos, side);
if (targetFluidHandler != null)
{
return FluidUtil.tryFillContainer(emptyContainer, targetFluidHandler, Integer.MAX_VALUE, playerIn, true);
FluidActionResult fluidActionResult = FluidUtil.tryFillContainer(emptyContainer, targetFluidHandler, Integer.MAX_VALUE, playerIn, true);
if (fluidActionResult.isSuccess())
{
return fluidActionResult;
}
}
}
return null;
return FluidActionResult.FAILURE;
}
}