Search in sources :

Example 1 with Fluid

use of net.minecraft.world.level.material.Fluid in project MinecraftForge by MinecraftForge.

the class FluidUtil method tryPlaceFluid.

/**
 * Tries to place a fluid resource into the world as a block and drains the fluidSource.
 * Makes a fluid emptying or vaporization sound when successful.
 * Honors the amount of fluid contained by the used container.
 * Checks if water-like fluids should vaporize like in the nether.
 *
 * Modeled after {@link BucketItem#emptyContents(Player, Level, BlockPos, BlockHitResult)}
 *
 * @param player      Player who places the fluid. May be null for blocks like dispensers.
 * @param world       Level to place the fluid in
 * @param hand
 * @param pos         The position in the world to place the fluid block
 * @param fluidSource The fluid source holding the fluidStack to place
 * @param resource    The fluidStack to place.
 * @return true if the placement was successful, false otherwise
 */
public static boolean tryPlaceFluid(@Nullable Player player, Level world, InteractionHand hand, BlockPos pos, IFluidHandler fluidSource, FluidStack resource) {
    if (world == null || pos == null) {
        return false;
    }
    Fluid fluid = resource.getFluid();
    if (fluid == Fluids.EMPTY || !fluid.getAttributes().canBePlacedInWorld(world, pos, resource)) {
        return false;
    }
    if (fluidSource.drain(resource, IFluidHandler.FluidAction.SIMULATE).isEmpty()) {
        return false;
    }
    BlockPlaceContext context = new BlockPlaceContext(world, player, hand, player == null ? ItemStack.EMPTY : player.getItemInHand(hand), new BlockHitResult(Vec3.ZERO, Direction.UP, pos, false));
    // check that we can place the fluid at the destination
    BlockState destBlockState = world.getBlockState(pos);
    Material destMaterial = destBlockState.getMaterial();
    boolean isDestNonSolid = !destMaterial.isSolid();
    boolean isDestReplaceable = destBlockState.canBeReplaced(context);
    boolean canDestContainFluid = destBlockState.getBlock() instanceof LiquidBlockContainer && ((LiquidBlockContainer) destBlockState.getBlock()).canPlaceLiquid(world, pos, destBlockState, fluid);
    if (!world.isEmptyBlock(pos) && !isDestNonSolid && !isDestReplaceable && !canDestContainFluid) {
        // Non-air, solid, unreplacable block. We can't put fluid here.
        return false;
    }
    if (world.dimensionType().ultraWarm() && fluid.getAttributes().doesVaporize(world, pos, resource)) {
        FluidStack result = fluidSource.drain(resource, IFluidHandler.FluidAction.EXECUTE);
        if (!result.isEmpty()) {
            result.getFluid().getAttributes().vaporize(player, world, pos, result);
            return true;
        }
    } else {
        // This fluid handler places the fluid block when filled
        IFluidHandler handler;
        if (canDestContainFluid) {
            handler = new BlockWrapper.LiquidContainerBlockWrapper((LiquidBlockContainer) destBlockState.getBlock(), world, pos);
        } else {
            handler = getFluidBlockHandler(fluid, world, pos);
        }
        FluidStack result = tryFluidTransfer(handler, fluidSource, resource, true);
        if (!result.isEmpty()) {
            SoundEvent soundevent = resource.getFluid().getAttributes().getEmptySound(resource);
            world.playSound(player, pos, soundevent, SoundSource.BLOCKS, 1.0F, 1.0F);
            return true;
        }
    }
    return false;
}
Also used : LiquidBlockContainer(net.minecraft.world.level.block.LiquidBlockContainer) FluidBlockWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper) BlockWrapper(net.minecraftforge.fluids.capability.wrappers.BlockWrapper) SoundEvent(net.minecraft.sounds.SoundEvent) BlockState(net.minecraft.world.level.block.state.BlockState) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) Fluid(net.minecraft.world.level.material.Fluid) Material(net.minecraft.world.level.material.Material) BlockHitResult(net.minecraft.world.phys.BlockHitResult) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Example 2 with Fluid

use of net.minecraft.world.level.material.Fluid in project MinecraftForge by MinecraftForge.

the class FluidStack method loadFluidStackFromNBT.

/**
 * This provides a safe method for retrieving a FluidStack - if the Fluid is invalid, the stack
 * will return as null.
 */
public static FluidStack loadFluidStackFromNBT(CompoundTag nbt) {
    if (nbt == null) {
        return EMPTY;
    }
    if (!nbt.contains("FluidName", Tag.TAG_STRING)) {
        return EMPTY;
    }
    ResourceLocation fluidName = new ResourceLocation(nbt.getString("FluidName"));
    Fluid fluid = ForgeRegistries.FLUIDS.getValue(fluidName);
    if (fluid == null) {
        return EMPTY;
    }
    FluidStack stack = new FluidStack(fluid, nbt.getInt("Amount"));
    if (nbt.contains("Tag", Tag.TAG_COMPOUND)) {
        stack.tag = nbt.getCompound("Tag");
    }
    return stack;
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) Fluid(net.minecraft.world.level.material.Fluid)

Example 3 with Fluid

use of net.minecraft.world.level.material.Fluid in project MinecraftForge by MinecraftForge.

the class FluidStack method readFromPacket.

public static FluidStack readFromPacket(FriendlyByteBuf buf) {
    Fluid fluid = buf.readRegistryId();
    int amount = buf.readVarInt();
    CompoundTag tag = buf.readNbt();
    if (fluid == Fluids.EMPTY)
        return EMPTY;
    return new FluidStack(fluid, amount, tag);
}
Also used : Fluid(net.minecraft.world.level.material.Fluid) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 4 with Fluid

use of net.minecraft.world.level.material.Fluid in project MinecraftForge by MinecraftForge.

the class FluidUtil method interactWithFluidHandler.

/**
 * 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 Player player, @Nonnull InteractionHand hand, @Nonnull IFluidHandler handler) {
    Preconditions.checkNotNull(player);
    Preconditions.checkNotNull(hand);
    Preconditions.checkNotNull(handler);
    ItemStack heldItem = player.getItemInHand(hand);
    if (!heldItem.isEmpty()) {
        return player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).map(playerInventory -> {
            FluidActionResult fluidActionResult = tryFillContainerAndStow(heldItem, handler, playerInventory, Integer.MAX_VALUE, player, true);
            if (!fluidActionResult.isSuccess()) {
                fluidActionResult = tryEmptyContainerAndStow(heldItem, handler, playerInventory, Integer.MAX_VALUE, player, true);
            }
            if (fluidActionResult.isSuccess()) {
                player.setItemInHand(hand, fluidActionResult.getResult());
                return true;
            }
            return false;
        }).orElse(false);
    }
    return false;
}
Also used : SoundSource(net.minecraft.sounds.SoundSource) IItemHandler(net.minecraftforge.items.IItemHandler) Items(net.minecraft.world.item.Items) FluidBlockWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper) BucketPickup(net.minecraft.world.level.block.BucketPickup) LiquidBlockContainer(net.minecraft.world.level.block.LiquidBlockContainer) Direction(net.minecraft.core.Direction) BlockState(net.minecraft.world.level.block.state.BlockState) BlockWrapper(net.minecraftforge.fluids.capability.wrappers.BlockWrapper) LazyOptional(net.minecraftforge.common.util.LazyOptional) ItemHandlerHelper(net.minecraftforge.items.ItemHandlerHelper) Fluid(net.minecraft.world.level.material.Fluid) Fluids(net.minecraft.world.level.material.Fluids) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) CapabilityFluidHandler(net.minecraftforge.fluids.capability.CapabilityFluidHandler) BlockHitResult(net.minecraft.world.phys.BlockHitResult) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) BucketPickupHandlerWrapper(net.minecraftforge.fluids.capability.wrappers.BucketPickupHandlerWrapper) Material(net.minecraft.world.level.material.Material) Player(net.minecraft.world.entity.player.Player) BlockPos(net.minecraft.core.BlockPos) Vec3(net.minecraft.world.phys.Vec3) SoundEvent(net.minecraft.sounds.SoundEvent) CapabilityItemHandler(net.minecraftforge.items.CapabilityItemHandler) IFluidHandlerItem(net.minecraftforge.fluids.capability.IFluidHandlerItem) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Block(net.minecraft.world.level.block.Block) BucketItem(net.minecraft.world.item.BucketItem) ItemStack(net.minecraft.world.item.ItemStack) InteractionHand(net.minecraft.world.InteractionHand) Level(net.minecraft.world.level.Level) ItemStack(net.minecraft.world.item.ItemStack)

Aggregations

Fluid (net.minecraft.world.level.material.Fluid)4 SoundEvent (net.minecraft.sounds.SoundEvent)2 BlockPlaceContext (net.minecraft.world.item.context.BlockPlaceContext)2 LiquidBlockContainer (net.minecraft.world.level.block.LiquidBlockContainer)2 BlockState (net.minecraft.world.level.block.state.BlockState)2 Material (net.minecraft.world.level.material.Material)2 BlockHitResult (net.minecraft.world.phys.BlockHitResult)2 IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)2 BlockWrapper (net.minecraftforge.fluids.capability.wrappers.BlockWrapper)2 FluidBlockWrapper (net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper)2 Preconditions (com.google.common.base.Preconditions)1 Optional (java.util.Optional)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 BlockPos (net.minecraft.core.BlockPos)1 Direction (net.minecraft.core.Direction)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 ResourceLocation (net.minecraft.resources.ResourceLocation)1 SoundSource (net.minecraft.sounds.SoundSource)1 InteractionHand (net.minecraft.world.InteractionHand)1