Skip to content

Commit de35084

Browse files
committed
Fix #18: make pipes waterloggable
1 parent 36745b7 commit de35084

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/main/java/dev/technici4n/moderndynamics/pipe/PipeBlock.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,27 @@
2121
import com.google.common.base.Preconditions;
2222
import java.util.List;
2323
import net.minecraft.core.BlockPos;
24+
import net.minecraft.core.Direction;
2425
import net.minecraft.world.InteractionHand;
2526
import net.minecraft.world.InteractionResult;
2627
import net.minecraft.world.entity.player.Player;
2728
import net.minecraft.world.item.ItemStack;
29+
import net.minecraft.world.item.context.BlockPlaceContext;
2830
import net.minecraft.world.level.BlockGetter;
2931
import net.minecraft.world.level.Level;
32+
import net.minecraft.world.level.LevelAccessor;
3033
import net.minecraft.world.level.block.Block;
3134
import net.minecraft.world.level.block.EntityBlock;
35+
import net.minecraft.world.level.block.SimpleWaterloggedBlock;
3236
import net.minecraft.world.level.block.entity.BlockEntity;
3337
import net.minecraft.world.level.block.entity.BlockEntityTicker;
3438
import net.minecraft.world.level.block.entity.BlockEntityType;
3539
import net.minecraft.world.level.block.state.BlockState;
40+
import net.minecraft.world.level.block.state.StateDefinition;
41+
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
42+
import net.minecraft.world.level.block.state.properties.BooleanProperty;
43+
import net.minecraft.world.level.material.FluidState;
44+
import net.minecraft.world.level.material.Fluids;
3645
import net.minecraft.world.level.material.Material;
3746
import net.minecraft.world.level.storage.loot.LootContext;
3847
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
@@ -41,7 +50,8 @@
4150
import net.minecraft.world.phys.shapes.VoxelShape;
4251
import org.jetbrains.annotations.Nullable;
4352

44-
public class PipeBlock extends Block implements EntityBlock {
53+
public class PipeBlock extends Block implements EntityBlock, SimpleWaterloggedBlock {
54+
private static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
4555

4656
public final String id;
4757
private PipeItem item;
@@ -50,6 +60,7 @@ public class PipeBlock extends Block implements EntityBlock {
5060
public PipeBlock(String id) {
5161
super(Properties.of(Material.METAL).noOcclusion().isRedstoneConductor((state, world, pos) -> false));
5262
this.id = id;
63+
this.registerDefaultState(this.defaultBlockState().setValue(WATERLOGGED, false));
5364
}
5465

5566
public PipeItem getItem() {
@@ -77,6 +88,35 @@ public void setBlockEntityProvider(BlockEntityType<PipeBlockEntity> blockEntityT
7788
this.blockEntityType = blockEntityType;
7889
}
7990

91+
@Override
92+
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
93+
super.createBlockStateDefinition(builder);
94+
builder.add(WATERLOGGED);
95+
}
96+
97+
@Nullable
98+
@Override
99+
public BlockState getStateForPlacement(BlockPlaceContext context) {
100+
BlockPos pos = context.getClickedPos();
101+
FluidState fluidState = context.getLevel().getFluidState(pos);
102+
return this.defaultBlockState().setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER);
103+
}
104+
105+
@Override
106+
public FluidState getFluidState(BlockState state) {
107+
return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
108+
}
109+
110+
@Override
111+
public BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos currentPos,
112+
BlockPos neighborPos) {
113+
if (state.getValue(WATERLOGGED)) {
114+
level.scheduleTick(currentPos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
115+
}
116+
117+
return super.updateShape(state, direction, neighborState, level, currentPos, neighborPos);
118+
}
119+
80120
@Override
81121
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
82122
var drops = super.getDrops(state, builder);

0 commit comments

Comments
 (0)