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;
}
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;
}
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);
}
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;
}
Aggregations