Example #1
0
 public static boolean fillContainers(
     IFluidHandler fluidHandler,
     IInventory inv,
     int inputSlot,
     int outputSlot,
     Fluid fluidToFill) {
   if (fluidToFill == null) return false;
   ItemStack input = inv.getStackInSlot(inputSlot);
   ItemStack output = inv.getStackInSlot(outputSlot);
   FluidItemHelper.FillReturn fill =
       FluidItemHelper.fillContainer(input, new FluidStack(fluidToFill, PROCESS_VOLUME));
   if (fill.container != null && hasPlaceToPutContainer(output, fill.container)) {
     FluidStack drain = fluidHandler.drain(ForgeDirection.UNKNOWN, fill.amount, false);
     if (drain != null && drain.amount == fill.amount) {
       fill = FluidItemHelper.fillContainer(input, drain);
       if (fill.container != null && fill.amount == drain.amount) {
         fluidHandler.drain(ForgeDirection.UNKNOWN, fill.amount, true);
         storeContainer(inv, inputSlot, outputSlot, fill.container);
       }
       return true;
     }
   }
   return false;
 }
Example #2
0
  public static boolean handleRightClick(
      IFluidHandler tank, ForgeDirection side, EntityPlayer player, boolean fill, boolean drain) {
    if (player == null) return false;
    ItemStack current = player.inventory.getCurrentItem();
    if (current != null) {

      FluidItemHelper.DrainReturn drainReturn =
          FluidItemHelper.drainContainer(current, PROCESS_VOLUME);

      if (fill && drainReturn.fluidDrained != null) {
        int used = tank.fill(side, drainReturn.fluidDrained, true);

        if (used > 0) {
          drainReturn = FluidItemHelper.drainContainer(current, used);
          if (!player.capabilities.isCreativeMode) {
            player.inventory.setInventorySlotContents(
                player.inventory.currentItem, drainReturn.container);
            player.inventory.markDirty();
          }
          return true;
        }
      } else if (drain) {

        FluidStack available = tank.drain(side, PROCESS_VOLUME, false);
        if (available != null) {
          FluidItemHelper.FillReturn fillReturn = FluidItemHelper.fillContainer(current, available);
          if (fillReturn.amount > 0) {
            if (current.stackSize > 1) {
              if (!player.inventory.addItemStackToInventory(fillReturn.container)) return false;
              player.inventory.setInventorySlotContents(
                  player.inventory.currentItem, InvTools.depleteItem(current));
              player.inventory.markDirty();
            } else {
              player.inventory.setInventorySlotContents(
                  player.inventory.currentItem, fillReturn.container);
              player.inventory.markDirty();
            }

            tank.drain(side, fillReturn.amount, true);
            return true;
          }
        }
      }
    }
    return false;
  }