| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.tags.BlockTags;
|
| 6 | import net.minecraft.util.RandomSource;
|
| 7 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 8 | import net.minecraft.world.level.BlockGetter;
|
| 9 | import net.minecraft.world.level.LevelReader;
|
| 10 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 11 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 12 | import net.minecraft.world.level.block.state.BlockState;
|
| 13 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 14 | import net.minecraft.world.level.material.FluidState;
|
| 15 | import net.minecraft.world.level.material.Fluids;
|
| 16 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 17 | import net.minecraft.world.phys.shapes.Shapes;
|
| 18 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 19 |
|
| 20 | /**
|
| 21 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 22 | */
|
| 23 | public class IronBarsBlock extends CrossCollisionBlock {
|
| 24 | /**
|
| 25 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 26 | */
|
| 27 | public IronBarsBlock(final BlockBehaviour.Properties properties) {
|
| 28 | super(2.0F, 16.0F, 2.0F, 16.0F, 16.0F, properties);
|
| 29 | this.registerDefaultState(
|
| 30 | this.stateDefinition.any().setValue(NORTH, false).setValue(EAST, false).setValue(SOUTH, false).setValue(WEST, false).setValue(WATERLOGGED, false)
|
| 31 | );
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 36 | BlockGetter level = context.getLevel();
|
| 37 | BlockPos pos = context.getClickedPos();
|
| 38 | FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
|
| 39 | BlockPos north = pos.north();
|
| 40 | BlockPos south = pos.south();
|
| 41 | BlockPos west = pos.west();
|
| 42 | BlockPos east = pos.east();
|
| 43 | BlockState northState = level.getBlockState(north);
|
| 44 | BlockState southState = level.getBlockState(south);
|
| 45 | BlockState westState = level.getBlockState(west);
|
| 46 | BlockState eastState = level.getBlockState(east);
|
| 47 | return this.defaultBlockState()
|
| 48 | .setValue(NORTH, this.attachsTo(northState, northState.isFaceSturdy(level, north, Direction.SOUTH)))
|
| 49 | .setValue(SOUTH, this.attachsTo(southState, southState.isFaceSturdy(level, south, Direction.NORTH)))
|
| 50 | .setValue(WEST, this.attachsTo(westState, westState.isFaceSturdy(level, west, Direction.EAST)))
|
| 51 | .setValue(EAST, this.attachsTo(eastState, eastState.isFaceSturdy(level, east, Direction.WEST)))
|
| 52 | .setValue(WATERLOGGED, replacedFluidState.is(Fluids.WATER));
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | protected BlockState updateShape(
|
| 57 | final BlockState state,
|
| 58 | final LevelReader level,
|
| 59 | final ScheduledTickAccess ticks,
|
| 60 | final BlockPos pos,
|
| 61 | final Direction directionToNeighbour,
|
| 62 | final BlockPos neighbourPos,
|
| 63 | final BlockState neighbourState,
|
| 64 | final RandomSource random
|
| 65 | ) {
|
| 66 | if (state.getValue(WATERLOGGED)) {
|
| 67 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 68 | }
|
| 69 |
|
| 70 | return directionToNeighbour.getAxis().isHorizontal()
|
| 71 | ? state.setValue(
|
| 72 | PROPERTY_BY_DIRECTION.get(directionToNeighbour),
|
| 73 | this.attachsTo(neighbourState, neighbourState.isFaceSturdy(level, neighbourPos, directionToNeighbour.getOpposite()))
|
| 74 | )
|
| 75 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | protected VoxelShape getVisualShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 80 | return Shapes.empty();
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | protected boolean skipRendering(final BlockState state, final BlockState neighborState, final Direction direction) {
|
| 85 | if (neighborState.is(this)
|
| 86 | || neighborState.is(BlockTags.BARS) && state.is(BlockTags.BARS) && neighborState.hasProperty(PROPERTY_BY_DIRECTION.get(direction.getOpposite()))) {
|
| 87 | if (!direction.getAxis().isHorizontal()) {
|
| 88 | return true;
|
| 89 | }
|
| 90 |
|
| 91 | if (state.getValue(PROPERTY_BY_DIRECTION.get(direction)) && neighborState.getValue(PROPERTY_BY_DIRECTION.get(direction.getOpposite()))) {
|
| 92 | return true;
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | return super.skipRendering(state, neighborState, direction);
|
| 97 | }
|
| 98 |
|
| 99 | public final boolean attachsTo(final BlockState state, final boolean faceSolid) {
|
| 100 | return !isExceptionForConnection(state) && faceSolid || state.getBlock() instanceof IronBarsBlock || state.is(BlockTags.WALLS);
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 105 | builder.add(NORTH, EAST, WEST, SOUTH, WATERLOGGED);
|
| 106 | }
|
| 107 | }
|
| 108 |
|