1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-28 08:03:21 +00:00
CC-Tweaked/src/main/java/dan200/computercraft/shared/util/WaterloggableBlock.java
SquidDev f9e13ca67a Update CC: Tweaked to 1.13
Look, I originally had this split into several commits, but lots of
other cleanups got mixed in. I then backported some of the cleanups to
1.12, did other tidy ups there, and eventually the web of merges was
unreadable.

Yes, this is a horrible mess, but it's still nicer than it was. Anyway,
changes:

 - Flatten everything. For instance, there are now three instances of
   BlockComputer, two BlockTurtle, ItemPocketComputer. There's also no
   more BlockPeripheral (thank heavens) - there's separate block classes
   for each peripheral type.

 - Remove pretty much all legacy code. As we're breaking world
   compatibility anyway, we can remove all the code to load worlds from
   1.4 days.
 - The command system is largely rewriten to take advantage of 1.13's
   new system. It's very fancy!

 - WidgetTerminal now uses Minecraft's "GUI listener" system.

 - BREAKING CHANGE: All the codes in keys.lua are different, due to the
   move to LWJGL 3. Hopefully this won't have too much of an impact.

   I don't want to map to the old key codes on the Java side, as there
   always ends up being small but slight inconsistencies. IMO it's
   better to make a clean break - people should be using keys rather
   than hard coding the constants anyway.

 - commands.list now allows fetching sub-commands. The ROM has already
   been updated to allow fancy usage such as commands.time.set("noon").

 - Turtles, modems and cables can be waterlogged.
2019-04-02 20:59:48 +01:00

100 lines
3.4 KiB
Java

/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.util;
import net.minecraft.block.IBucketPickupHandler;
import net.minecraft.block.ILiquidContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.IFluidState;
import net.minecraft.init.Fluids;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.state.BooleanProperty;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import javax.annotation.Nonnull;
/**
* Represents a block which can be filled with water
*
* I'm fairly sure this exists on 1.14, but it's a useful convenience wrapper to have on 1.13.
*/
public interface WaterloggableBlock extends IBucketPickupHandler, ILiquidContainer
{
BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
/**
* Call from {@link net.minecraft.block.Block#getFluidState(IBlockState)}
*
* @param state The current state
* @return This waterlogged block's current fluid
*/
default IFluidState getWaterloggedFluidState( IBlockState state )
{
return state.get( WATERLOGGED ) ? Fluids.WATER.getStillFluidState( false ) : Fluids.EMPTY.getDefaultState();
}
@Nonnull
@Override
default Fluid pickupFluid( @Nonnull IWorld world, @Nonnull BlockPos pos, @Nonnull IBlockState state )
{
if( state.get( WATERLOGGED ) )
{
world.setBlockState( pos, state.with( WATERLOGGED, false ), 3 );
return Fluids.WATER;
}
else
{
return Fluids.EMPTY;
}
}
@Override
default boolean canContainFluid( @Nonnull IBlockReader world, @Nonnull BlockPos pos, @Nonnull IBlockState state, @Nonnull Fluid fluid )
{
return !state.get( WATERLOGGED ) && fluid == Fluids.WATER;
}
@Override
default boolean receiveFluid( @Nonnull IWorld world, @Nonnull BlockPos pos, @Nonnull IBlockState state, @Nonnull IFluidState fluid )
{
if( !canContainFluid( world, pos, state, fluid.getFluid() ) ) return false;
if( !world.isRemote() )
{
world.setBlockState( pos, state.with( WATERLOGGED, true ), 3 );
world.getPendingFluidTicks().scheduleTick( pos, fluid.getFluid(), fluid.getFluid().getTickRate( world ) );
}
return true;
}
/**
* Call from {@link net.minecraft.block.Block#updatePostPlacement(IBlockState, EnumFacing, IBlockState, IWorld, BlockPos, BlockPos)}
*
* @param state The current state
* @param world The position of this block
* @param pos The world this block exists in
*/
default void updateWaterloggedPostPlacement( IBlockState state, IWorld world, BlockPos pos )
{
if( state.get( WATERLOGGED ) )
{
world.getPendingFluidTicks().scheduleTick( pos, Fluids.WATER, Fluids.WATER.getTickRate( world ) );
}
}
default boolean getWaterloggedStateForPlacement( BlockItemUseContext context )
{
return context.getWorld().getFluidState( context.getPos() ).getFluid() == Fluids.WATER;
}
}