[1.11.x] Refine fluid placement utilities (#3509)

This commit is contained in:
mezz authored and LexManos committed 2016-12-05 20:17:47 -08:00
1 parent 160427b12c
commit 8eea766288
12 files changed
+615 -69

No files matched your search

@@ -37,7 +37,9 @@ import net.minecraft.world.World;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
import net.minecraftforge.fluids.capability.templates.VoidFluidHandler;
import net.minecraftforge.fluids.capability.wrappers.BlockLiquidWrapper;
import net.minecraftforge.fluids.capability.wrappers.BlockWrapper;
import net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
@@ -419,62 +421,6 @@ public class FluidUtil
return null;
}
/**
* Tries to place a fluid in the world in block form.
* Makes a fluid emptying sound when successful.
* Checks if water-like fluids should vaporize like in the nether.
*
* Modeled after {@link net.minecraft.item.ItemBucket#tryPlaceContainedLiquid(EntityPlayer, World, BlockPos)}
*
* @param player Player who places the fluid. May be null for blocks like dispensers.
* @param worldIn World to place the fluid in
* @param fluidStack The fluidStack to place.
* @param pos The position in the world to place the fluid block
* @return true if successful
*/
public static boolean tryPlaceFluid(@Nullable EntityPlayer player, World worldIn, FluidStack fluidStack, BlockPos pos)
{
if (worldIn == null || fluidStack == null || pos == null)
{
return false;
}
Fluid fluid = fluidStack.getFluid();
if (fluid == null || !fluid.canBePlacedInWorld())
{
return false;
}
// check that we can place the fluid at the destination
IBlockState destBlockState = worldIn.getBlockState(pos);
Material destMaterial = destBlockState.getMaterial();
boolean isDestNonSolid = !destMaterial.isSolid();
boolean isDestReplaceable = destBlockState.getBlock().isReplaceable(worldIn, pos);
if (!worldIn.isAirBlock(pos) && !isDestNonSolid && !isDestReplaceable)
{
return false; // Non-air, solid, unreplacable block. We can't put fluid here.
}
if (worldIn.provider.doesWaterVaporize() && fluid.doesVaporize(fluidStack))
{
fluid.vaporize(player, worldIn, pos, fluidStack);
}
else
{
if (!worldIn.isRemote && (isDestNonSolid || isDestReplaceable) && !destMaterial.isLiquid())
{
worldIn.destroyBlock(pos, true);
}
SoundEvent soundevent = fluid.getEmptySound(fluidStack);
worldIn.playSound(player, pos, soundevent, SoundCategory.BLOCKS, 1.0F, 1.0F);
IBlockState fluidBlockState = fluid.getBlock().getDefaultState();
worldIn.setBlockState(pos, fluidBlockState, 11);
}
return true;
}
/**
* Attempts to pick up a fluid in the world and put it in an empty container item.
*
@@ -511,4 +457,82 @@ public class FluidUtil
}
return FluidActionResult.FAILURE;
}
/**
* Tries to place a fluid in the world in block form and drains the container.
* Makes a fluid emptying 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 net.minecraft.item.ItemBucket#tryPlaceContainedLiquid(EntityPlayer, World, BlockPos)}
*
* @param player Player who places the fluid. May be null for blocks like dispensers.
* @param world World to place the fluid in
* @param pos The position in the world to place the fluid block
* @param container The fluid container holding the fluidStack to place
* @param resource The fluidStack to place
* @return the container's ItemStack with the remaining amount of fluid if the placement was successful, null otherwise
*/
@Nonnull
public static FluidActionResult tryPlaceFluid(@Nullable EntityPlayer player, World world, BlockPos pos, @Nonnull ItemStack container, FluidStack resource)
{
if (world == null || resource == null || pos == null)
{
return FluidActionResult.FAILURE;
}
Fluid fluid = resource.getFluid();
if (fluid == null || !fluid.canBePlacedInWorld())
{
return FluidActionResult.FAILURE;
}
// check that we can place the fluid at the destination
IBlockState destBlockState = world.getBlockState(pos);
Material destMaterial = destBlockState.getMaterial();
boolean isDestNonSolid = !destMaterial.isSolid();
boolean isDestReplaceable = destBlockState.getBlock().isReplaceable(world, pos);
if (!world.isAirBlock(pos) && !isDestNonSolid && !isDestReplaceable)
{
return FluidActionResult.FAILURE; // Non-air, solid, unreplacable block. We can't put fluid here.
}
if (world.provider.doesWaterVaporize() && fluid.doesVaporize(resource))
{
fluid.vaporize(player, world, pos, resource);
return tryEmptyContainer(container, new VoidFluidHandler(), Integer.MAX_VALUE, player, true);
}
else
{
if (!world.isRemote && (isDestNonSolid || isDestReplaceable) && !destMaterial.isLiquid())
{
world.destroyBlock(pos, true);
}
// Defer the placement to the fluid block
// Instead of actually "filling", the fluid handler method replaces the block
Block block = fluid.getBlock();
IFluidHandler handler;
if (block instanceof IFluidBlock)
{
handler = new FluidBlockWrapper((IFluidBlock) block, world, pos);
}
else if (block instanceof BlockLiquid)
{
handler = new BlockLiquidWrapper((BlockLiquid) block, world, pos);
}
else
{
handler = new BlockWrapper(block, world, pos);
}
FluidActionResult result = tryEmptyContainer(container, handler, Integer.MAX_VALUE, player, true);
if (result.isSuccess())
{
SoundEvent soundevent = fluid.getEmptySound(resource);
world.playSound(player, pos, soundevent, SoundCategory.BLOCKS, 1.0F, 1.0F);
}
return result;
}
}
}