Java Code Examples for net.minecraftforge.fluids.FluidUtil#getFluidHandler()

The following examples show how to use net.minecraftforge.fluids.FluidUtil#getFluidHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlockTFStorage.java    From TofuCraftReload with MIT License 6 votes vote down vote up
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
    if (worldIn.isRemote) {

        return true;

    } else {
        ItemStack stack = playerIn.getHeldItem(hand);
        TileEntity tileentity = worldIn.getTileEntity(pos);
        if (tileentity instanceof TileEntityTFStorage) {
            IFluidHandlerItem handler = FluidUtil.getFluidHandler(ItemHandlerHelper.copyStackWithSize(stack, 1));

            if (handler != null) {
                FluidUtil.interactWithFluidHandler(playerIn, hand, tileentity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side));

                return true;
            } else {
                playerIn.openGui(TofuMain.instance, TofuGuiHandler.ID_STORAGE_MACHINE_GUI, worldIn, pos.getX(), pos.getY(), pos.getZ());
            }
        }
        return true;
    }
}
 
Example 2
Source File: BlockBarrelDistillation.java    From Sakura_mod with MIT License 6 votes vote down vote up
@Override
  public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
      if (world.isRemote) {
          return true;
      }
ItemStack stack = player.getHeldItem(hand);
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileEntityDistillation) {
    IFluidHandlerItem handler = FluidUtil.getFluidHandler(ItemHandlerHelper.copyStackWithSize(stack, 1));
    if (handler != null) {
        FluidUtil.interactWithFluidHandler(player, hand, tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side));
        return true;
    }
    player.openGui(SakuraMain.instance, SakuraGuiHandler.ID_DISTILLATION, world, pos.getX(), pos.getY(), pos.getZ());
    return true;
}
return true;
  }
 
Example 3
Source File: BlockBarrel.java    From Sakura_mod with MIT License 6 votes vote down vote up
@Override
  public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
      if (world.isRemote) {
          return true;
      }
ItemStack stack = player.getHeldItem(hand);
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileEntityBarrel) {
    IFluidHandlerItem handler = FluidUtil.getFluidHandler(ItemHandlerHelper.copyStackWithSize(stack, 1));
    if (handler != null) {
        FluidUtil.interactWithFluidHandler(player, hand, tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side));
        return true;
    }
    player.openGui(SakuraMain.instance, SakuraGuiHandler.ID_BARREL, world, pos.getX(), pos.getY(), pos.getZ());
    return true;
}
return true;
  }
 
Example 4
Source File: BlockTFBattery.java    From TofuCraftReload with MIT License 6 votes vote down vote up
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    if (!worldIn.isRemote) {
        ItemStack stack = playerIn.getHeldItem(hand);
        TileEntity tileentity = worldIn.getTileEntity(pos);
        if (tileentity instanceof TileEntityTofuBattery) {
            IFluidHandlerItem handler = FluidUtil.getFluidHandler(ItemHandlerHelper.copyStackWithSize(stack, 1));
            if (handler != null) {
                FluidUtil.interactWithFluidHandler(playerIn, hand, tileentity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, facing));
                return true;
            } else {
                playerIn.openGui(TofuMain.instance, TofuGuiHandler.ID_BATTERY_GUI, worldIn, pos.getX(), pos.getY(), pos.getZ());
            }
        }
    }
    return true;
}
 
Example 5
Source File: UpgradeCobbestone.java    From BetterChests with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static Predicate<ItemStack> getFluidPredicate(Fluid fluid) {
	return new Predicate<ItemStack>() {
		@Override
		public boolean test(ItemStack itemStack) {
			IFluidHandlerItem handler = FluidUtil.getFluidHandler(itemStack);
			if (handler == null) {
				return false;
			}
			for (IFluidTankProperties properties : handler.getTankProperties()) {
				FluidStack stack = properties.getContents();
				if (stack != null && stack.getFluid() == fluid) {
					return true;
				}
			}
			return false;
		}
	};
}
 
Example 6
Source File: MetaTileEntityPump.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void tryPumpFirstBlock() {
    BlockPos fluidBlockPos = fluidSourceBlocks.poll();
    if (fluidBlockPos == null) return;
    IBlockState blockHere = getWorld().getBlockState(fluidBlockPos);
    if (blockHere.getBlock() instanceof BlockLiquid ||
        blockHere.getBlock() instanceof IFluidBlock) {
        IFluidHandler fluidHandler = FluidUtil.getFluidHandler(getWorld(), fluidBlockPos, null);
        FluidStack drainStack = fluidHandler.drain(Integer.MAX_VALUE, false);
        if (drainStack != null && exportFluids.fill(drainStack, false) == drainStack.amount) {
            exportFluids.fill(drainStack, true);
            fluidHandler.drain(drainStack.amount, true);
            this.fluidSourceBlocks.remove(fluidBlockPos);
            energyContainer.changeEnergy(-GTValues.V[getTier()]);
        }
    }
}
 
Example 7
Source File: RecipeShapedFluid.java    From Wizardry with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv) {
	NonNullList<ItemStack> remains = ForgeHooks.defaultRecipeGetRemainingItems(inv);
	for (int i = 0; i < height * width; i++) {
		ItemStack stack = inv.getStackInSlot(i);
		NonNullList<Ingredient> matchedIngredients = this.input;
		if (matchedIngredients.get(i) instanceof IngredientFluidStack) {
			if (!stack.isEmpty()) {
				ItemStack copy = stack.copy();
				copy.setCount(1);
				remains.set(i, copy);
			}
			IFluidHandlerItem handler = FluidUtil.getFluidHandler(remains.get(i));
			if (handler != null) {
				FluidStack fluid = ((IngredientFluidStack) matchedIngredients.get(i)).getFluid();
				handler.drain(fluid.amount, true);
				remains.set(i, handler.getContainer());
			}
		}
	}
	return remains;
}
 
Example 8
Source File: GTTileTranslocatorFluid.java    From GT-Classic with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void onBufferTick() {
	BlockPos importPos = this.pos.offset(this.getFacing().getOpposite());
	BlockPos exportPos = this.pos.offset(this.getFacing());
	if (!world.isBlockLoaded(importPos) || !world.isBlockLoaded(exportPos)) {
		return;
	}
	IFluidHandler start = FluidUtil.getFluidHandler(world, importPos, getFacing());
	IFluidHandler end = FluidUtil.getFluidHandler(world, exportPos, getFacing().getOpposite());
	boolean canExport = start != null && end != null;
	if (canExport && filter == null) {
		FluidUtil.tryFluidTransfer(end, start, 500, true);
	}
	if (canExport && filter != null) {
		FluidStack fake = FluidUtil.tryFluidTransfer(end, start, 500, false);
		if (fake != null && (fake.getFluid().getName().equals(filter.getFluid().getName()))) {
			FluidUtil.tryFluidTransfer(end, start, 500, true);
		}
	}
}
 
Example 9
Source File: ContainerBucketFillHandler.java    From OpenModsLib with MIT License 5 votes vote down vote up
@SubscribeEvent
public void onBucketFill(FillBucketEvent evt) {
	if (evt.getResult() != Result.DEFAULT) return;

	if (evt.getEmptyBucket().getItem() != EMPTY_BUCKET) return;

	final RayTraceResult target = evt.getTarget();
	if (target == null || target.typeOfHit != RayTraceResult.Type.BLOCK) return;

	final TileEntity te = evt.getWorld().getTileEntity(target.getBlockPos());
	if (te == null) return;

	if (!canFill(evt.getWorld(), target.getBlockPos(), te)) { return; }

	if (te.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, target.sideHit)) {
		final IFluidHandler source = te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, target.sideHit);

		final FluidStack fluidInContainer = source.drain(Fluid.BUCKET_VOLUME, false);

		if (fluidInContainer != null) {
			final ItemStack filledBucket = getFilledBucket(fluidInContainer);
			if (!filledBucket.isEmpty()) {
				final IFluidHandlerItem container = FluidUtil.getFluidHandler(filledBucket);
				if (container != null) {
					final FluidStack fluidInBucket = container.drain(Integer.MAX_VALUE, false);
					if (fluidInBucket != null && fluidInBucket.isFluidStackIdentical(source.drain(fluidInBucket, false))) {
						source.drain(fluidInBucket, true);
						evt.setFilledBucket(filledBucket.copy());
						evt.setResult(Result.ALLOW);
					}
				}
			}
		}
	}
}
 
Example 10
Source File: EntityFluidCow.java    From Moo-Fluids with GNU General Public License v3.0 5 votes vote down vote up
private boolean attemptToHealCowWithFluidContainer(final ItemStack currentItemStack,
                                                   final EntityPlayer entityPlayer) {
  boolean cowHealed = false;
  if (!currentItemStack.isEmpty() && entityFluid != null) {
    IFluidHandlerItem fluidHandlerItem = FluidUtil.getFluidHandler(currentItemStack);
    if (fluidHandlerItem != null) {
      FluidStack containedFluid = fluidHandlerItem
              .drain(new FluidStack(entityFluid, Fluid.BUCKET_VOLUME), false);
      ItemStack emptyItemStack;
      if (containedFluid != null &&
              containedFluid.getFluid().getName().equalsIgnoreCase(entityFluid.getName())) {
        fluidHandlerItem.drain(new FluidStack(entityFluid, Fluid.BUCKET_VOLUME), false);
        emptyItemStack = fluidHandlerItem.getContainer();
        currentItemStack.shrink(1);
        if (currentItemStack.isEmpty()) {
          entityPlayer.inventory.setInventorySlotContents(
                  entityPlayer.inventory.currentItem,
                  emptyItemStack.copy());
        } else {
          ItemHandlerHelper.giveItemToPlayer(entityPlayer, emptyItemStack.copy());
        }
        heal(4F);
        cowHealed = true;
      }
    }
  }
  return cowHealed;
}
 
Example 11
Source File: EntityFluidCow.java    From Moo-Fluids with GNU General Public License v3.0 5 votes vote down vote up
private boolean attemptToGetFluidFromCow(final ItemStack currentItemStack,
                                         final EntityPlayer entityPlayer) {
  boolean canGetFluid = false;

  if (!currentItemStack.isEmpty() && entityFluid != null) {
    ItemStack filledItemStack = ItemHandlerHelper.copyStackWithSize(currentItemStack, 1);
    IFluidHandlerItem fluidHandlerItem = FluidUtil.getFluidHandler(filledItemStack);
    if (fluidHandlerItem != null) {
      if (fluidHandlerItem.fill(
              new FluidStack(entityFluid, Fluid.BUCKET_VOLUME), true) == Fluid.BUCKET_VOLUME) {

        filledItemStack = fluidHandlerItem.getContainer();

        currentItemStack.shrink(1);
        if (currentItemStack.isEmpty()) {
          entityPlayer.inventory.setInventorySlotContents(
              entityPlayer.inventory.currentItem,
              filledItemStack.copy());
        } else {
          ItemHandlerHelper.giveItemToPlayer(entityPlayer, filledItemStack.copy());
        }

        canGetFluid = true;
      }
    }
  }

  return canGetFluid;
}
 
Example 12
Source File: GTIFilters.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean matches(ItemStack stack) {
	if (FluidUtil.getFluidHandler(stack) == null) {
		return false;
	}
	if (this.tile.getTankInstance().getFluid() == null || this.tile.getTankInstance().getFluidAmount() == 0) {
		return true;
	}
	return this.tile.getTankInstance().getFluid().getFluid() == FluidUtil.getFluidContained(stack).getFluid();
}
 
Example 13
Source File: GTUtility.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Export a FluidStack from a TileEntityMachine tank to another tile.
 * 
 * @param machine - The TileEntityMachine which has the tank, provides World and
 *                BlockPos data.
 * @param tank    - the IC2Tank to try to export from.
 * @param side    - the EnumFacing to try to export fluids out of.
 * @param amount  - the amount of fluid to transfer
 */
public static void exportFluidFromMachineToSide(TileEntityMachine machine, IC2Tank tank, EnumFacing side,
		int amount) {
	BlockPos exportPos = machine.getPos().offset(side);
	if (!machine.getWorld().isBlockLoaded(exportPos)) {
		return;
	}
	IFluidHandler fluidTile = FluidUtil.getFluidHandler(machine.getWorld(), exportPos, side.getOpposite());
	boolean canExport = tank.getFluid() != null && fluidTile != null;
	if (canExport) {
		FluidUtil.tryFluidTransfer(fluidTile, tank, amount, true);
	}
}
 
Example 14
Source File: GTUtility.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Import a FluidStack from another tile into a TileEntityMachine tank.
 *
 * @param machine - The TileEntityMachine which has the tank, provides World and
 *                BlockPos data.
 * @param tank    - the IC2Tank to try to import into.
 * @param side    - the EnumFacing to try to import fluids from.
 * @param amount  - the amount of fluid to transfer
 */
public static void importFluidFromSideToMachine(TileEntityMachine machine, IC2Tank tank, EnumFacing side,
		int amount) {
	BlockPos importPos = machine.getPos().offset(side);
	if (!machine.getWorld().isBlockLoaded(importPos)) {
		return;
	}
	IFluidHandler fluidTile = FluidUtil.getFluidHandler(machine.getWorld(), importPos, side.getOpposite());
	if (fluidTile != null && tank.getFluidAmount() < tank.getCapacity()) {
		FluidUtil.tryFluidTransfer(tank, fluidTile, amount, true);
	}
}
 
Example 15
Source File: MetaTileEntityPump.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void checkFluidBlockAt(BlockPos pumpHeadPos, BlockPos checkPos) {
    IBlockState blockHere = getWorld().getBlockState(checkPos);
    boolean shouldCheckNeighbours = isStraightInPumpRange(checkPos);

    if (blockHere.getBlock() instanceof BlockLiquid ||
        blockHere.getBlock() instanceof IFluidBlock) {
        IFluidHandler fluidHandler = FluidUtil.getFluidHandler(getWorld(), checkPos, null);
        FluidStack drainStack = fluidHandler.drain(Integer.MAX_VALUE, false);
        if (drainStack != null && drainStack.amount > 0) {
            this.fluidSourceBlocks.add(checkPos);
        }
        shouldCheckNeighbours = true;
    }

    if (shouldCheckNeighbours) {
        int maxPumpRange = getMaxPumpRange();
        for (EnumFacing facing : EnumFacing.VALUES) {
            BlockPos offsetPos = checkPos.offset(facing);
            if (offsetPos.distanceSq(pumpHeadPos) > maxPumpRange * maxPumpRange)
                continue; //do not add blocks outside bounds
            if (!fluidSourceBlocks.contains(offsetPos) &&
                !blocksToCheck.contains(offsetPos)) {
                this.blocksToCheck.add(offsetPos);
            }
        }
    }
}
 
Example 16
Source File: BlockCampfirePot.java    From Sakura_mod with MIT License 5 votes vote down vote up
@Override
  public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
      if (worldIn.isRemote) {
          return true;
      }
ItemStack stack = playerIn.getHeldItem(hand);
TileEntity tile = worldIn.getTileEntity(pos);
if (hand == EnumHand.MAIN_HAND) {
    if (tile instanceof TileEntityCampfirePot) {
        TileEntityCampfirePot tileEntityCampfire = (TileEntityCampfirePot) tile;
        IFluidHandlerItem handler = FluidUtil.getFluidHandler(ItemHandlerHelper.copyStackWithSize(stack, 1));
        if (handler != null) {
            FluidUtil.interactWithFluidHandler(playerIn, hand, tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, facing));
            return true;
        }
        
        if (WorldUtil.isItemFuel(stack)) {
            tileEntityCampfire.setBurningTime(tileEntityCampfire.getBurningTime() + TileEntityFurnace.getItemBurnTime(stack));
            setState(true, worldIn, pos);
			if(stack.getItem().hasContainerItem(stack)) stack = stack.getItem().getContainerItem(stack);
				else stack.shrink(1);
            return true;
        }

        if (stack.getItem() == Items.FLINT_AND_STEEL) {
            tileEntityCampfire.setBurningTime(tileEntityCampfire.getBurningTime() + 10000);
            setState(true, worldIn, pos);
            stack.damageItem(1, playerIn);
            return true;
        }

        playerIn.openGui(SakuraMain.instance, SakuraGuiHandler.ID_CAMPFIREPOT, worldIn, pos.getX(), pos.getY(), pos.getZ());
        return true;
    }
}

return true;
  }
 
Example 17
Source File: GTIFilters.java    From GT-Classic with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public boolean matches(ItemStack stack) {
	return !stack.isEmpty() && FluidUtil.getFluidHandler(stack) != null;
}
 
Example 18
Source File: FluidContainerSlotWidget.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public boolean canPutStack(ItemStack stack) {
    IFluidHandlerItem fluidHandlerItem = FluidUtil.getFluidHandler(stack);
    return fluidHandlerItem != null && (!requireFilledContainer || fluidHandlerItem.getTankProperties()[0].getContents() != null);
}