Add a FluidUtil method to interact with an arbitrary IFluidHandler (#4080)

This commit is contained in:
Ben Staddon 2017-06-26 02:22:52 +01:00 committed by mezz
parent 690c9bb76a
commit 51aa029bb1
1 changed files with 31 additions and 16 deletions

View File

@ -71,31 +71,46 @@ public class FluidUtil
*/
public static boolean interactWithFluidHandler(@Nonnull EntityPlayer player, @Nonnull EnumHand hand, @Nonnull World world, @Nonnull BlockPos pos, @Nullable EnumFacing side)
{
Preconditions.checkNotNull(player);
Preconditions.checkNotNull(hand);
Preconditions.checkNotNull(world);
Preconditions.checkNotNull(pos);
IFluidHandler blockFluidHandler = getFluidHandler(world, pos, side);
return blockFluidHandler != null && interactWithFluidHandler(player, hand, blockFluidHandler);
}
/**
* Used to handle the common case of a player holding a fluid item and right-clicking on a fluid handler.
* First it tries to fill the item from the handler,
* if that action fails then it tries to drain the item into the handler.
* Automatically updates the item in the player's hand and stashes any extra items created.
*
* @param player The player doing the interaction between the item and fluid handler.
* @param hand The player's hand that is holding an item that should interact with the fluid handler.
* @param handler The fluid handler.
* @return true if the interaction succeeded and updated the item held by the player, false otherwise.
*/
public static boolean interactWithFluidHandler(@Nonnull EntityPlayer player, @Nonnull EnumHand hand, @Nonnull IFluidHandler handler)
{
Preconditions.checkNotNull(player);
Preconditions.checkNotNull(hand);
Preconditions.checkNotNull(handler);
ItemStack heldItem = player.getHeldItem(hand);
if (!heldItem.isEmpty())
{
IFluidHandler blockFluidHandler = getFluidHandler(world, pos, side);
if (blockFluidHandler != null)
IItemHandler playerInventory = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
if (playerInventory != null)
{
IItemHandler playerInventory = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
if (playerInventory != null)
FluidActionResult fluidActionResult = tryFillContainerAndStow(heldItem, handler, playerInventory, Integer.MAX_VALUE, player);
if (!fluidActionResult.isSuccess())
{
FluidActionResult fluidActionResult = tryFillContainerAndStow(heldItem, blockFluidHandler, playerInventory, Integer.MAX_VALUE, player);
if (!fluidActionResult.isSuccess())
{
fluidActionResult = tryEmptyContainerAndStow(heldItem, blockFluidHandler, playerInventory, Integer.MAX_VALUE, player);
}
fluidActionResult = tryEmptyContainerAndStow(heldItem, handler, playerInventory, Integer.MAX_VALUE, player);
}
if (fluidActionResult.isSuccess())
{
player.setHeldItem(hand, fluidActionResult.getResult());
return true;
}
if (fluidActionResult.isSuccess())
{
player.setHeldItem(hand, fluidActionResult.getResult());
return true;
}
}
}