Skip to content

Commit

Permalink
fix: water-resistant feature of iron pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
vvvbbbcz committed Sep 29, 2024
1 parent 292e63e commit 30f12c0
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/api/java/net/industrybase/api/pipe/PipeBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.SimpleWaterloggedBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
Expand All @@ -25,7 +28,8 @@
import java.util.HashMap;
import java.util.Map;

public abstract class PipeBlock extends BaseEntityBlock {
public abstract class PipeBlock extends BaseEntityBlock implements SimpleWaterloggedBlock {
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
public static final EnumMap<Direction, BooleanProperty> PROPERTIES = new EnumMap<>(ImmutableMap.of(
Direction.NORTH, BlockStateProperties.NORTH,
Direction.EAST, BlockStateProperties.EAST,
Expand All @@ -52,6 +56,7 @@ protected PipeBlock(Properties properties) {
for (Direction direction : Direction.values()) {
defaultState.setValue(PROPERTIES.get(direction), false);
}
defaultState.setValue(WATERLOGGED, false);
this.registerDefaultState(defaultState);

// calculate collision shapes for all states
Expand All @@ -60,6 +65,7 @@ protected PipeBlock(Properties properties) {
}
}

// TODO: implement Minecraft PipeBlock
private VoxelShape calculateShape(BlockState state, Map<Direction, VoxelShape> map) {
VoxelShape shape = CORE;
for (Direction direction : Direction.values()) {
Expand All @@ -70,17 +76,29 @@ private VoxelShape calculateShape(BlockState state, Map<Direction, VoxelShape> m
return shape;
}

@Override
protected boolean propagatesSkylightDown(BlockState pState, BlockGetter pReader, BlockPos pPos) {
return !pState.getValue(WATERLOGGED);
}

@Override
protected FluidState getFluidState(BlockState pState) {
return pState.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(pState);
}

@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
Level level = context.getLevel();
BlockPos pos = context.getClickedPos();
BlockState state = this.defaultBlockState();
FluidState fluidstate = level.getFluidState(pos);
for (Direction direction : Direction.values()) {
Level level = context.getLevel();
BlockPos facingPos = context.getClickedPos().relative(direction);
BlockPos facingPos = pos.relative(direction);
BlockState facingState = level.getBlockState(facingPos);
state = state.setValue(PROPERTIES.get(direction), this.canConnect(level, direction.getOpposite(), facingPos, facingState));
}
return state;
return state.setValue(WATERLOGGED, fluidstate.getType() == Fluids.WATER);
}

@Override
Expand All @@ -90,6 +108,9 @@ public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, Co

@Override
public BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos currentPos, BlockPos neighborPos) {
if (state.getValue(WATERLOGGED)) {
level.scheduleTick(currentPos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
}
return state.setValue(PROPERTIES.get(direction), this.canConnect(level, direction.getOpposite(), neighborPos, neighborState));
}

Expand All @@ -110,6 +131,7 @@ private boolean canConnect(LevelAccessor level, Direction facing, BlockPos pos,
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(PROPERTIES.values().toArray(new BooleanProperty[0]));
builder.add(WATERLOGGED);
}

@Override
Expand Down

0 comments on commit 30f12c0

Please sign in to comment.