| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.tags.BlockTags;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 9 | import net.minecraft.world.level.BlockGetter;
|
| 10 | import net.minecraft.world.level.Level;
|
| 11 | import net.minecraft.world.level.LevelReader;
|
| 12 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 13 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 14 | import net.minecraft.world.level.block.state.BlockState;
|
| 15 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 16 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 17 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 18 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 19 | import net.minecraft.world.level.material.FluidState;
|
| 20 | import net.minecraft.world.level.material.Fluids;
|
| 21 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 22 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 23 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | /**
|
| 27 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 28 | */
|
| 29 | public class SeaPickleBlock extends VegetationBlock implements SimpleWaterloggedBlock, BonemealableBlock {
|
| 30 | public static final int MAX_PICKLES = 4;
|
| 31 | public static final IntegerProperty PICKLES = BlockStateProperties.PICKLES;
|
| 32 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 33 | private static final VoxelShape SHAPE_ONE = Block.column(4.0, 0.0, 6.0);
|
| 34 | private static final VoxelShape SHAPE_TWO = Block.column(10.0, 0.0, 6.0);
|
| 35 | private static final VoxelShape SHAPE_THREE = Block.column(12.0, 0.0, 6.0);
|
| 36 | private static final VoxelShape SHAPE_FOUR = Block.column(12.0, 0.0, 7.0);
|
| 37 |
|
| 38 | /**
|
| 39 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 40 | */
|
| 41 | public SeaPickleBlock(final BlockBehaviour.Properties properties) {
|
| 42 | super(properties);
|
| 43 | this.registerDefaultState(this.stateDefinition.any().setValue(PICKLES, 1).setValue(WATERLOGGED, true));
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 48 | BlockState state = context.getLevel().getBlockState(context.getClickedPos());
|
| 49 | if (state.is(this)) {
|
| 50 | return state.setValue(PICKLES, Math.min(4, state.getValue(PICKLES) + 1));
|
| 51 | }
|
| 52 |
|
| 53 | FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
|
| 54 | boolean isWaterSource = replacedFluidState.is(Fluids.WATER);
|
| 55 | return super.getStateForPlacement(context).setValue(WATERLOGGED, isWaterSource);
|
| 56 | }
|
| 57 |
|
| 58 | public static boolean isDead(final BlockState state) {
|
| 59 | return !state.getValue(WATERLOGGED);
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | protected boolean mayPlaceOn(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 64 | return !state.getCollisionShape(level, pos).getFaceShape(Direction.UP).isEmpty() || state.isFaceSturdy(level, pos, Direction.UP);
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 69 | BlockPos belowPos = pos.below();
|
| 70 | return this.mayPlaceOn(level.getBlockState(belowPos), level, belowPos);
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | protected BlockState updateShape(
|
| 75 | final BlockState state,
|
| 76 | final LevelReader level,
|
| 77 | final ScheduledTickAccess ticks,
|
| 78 | final BlockPos pos,
|
| 79 | final Direction directionToNeighbour,
|
| 80 | final BlockPos neighbourPos,
|
| 81 | final BlockState neighbourState,
|
| 82 | final RandomSource random
|
| 83 | ) {
|
| 84 | if (!state.canSurvive(level, pos)) {
|
| 85 | return Blocks.AIR.defaultBlockState();
|
| 86 | }
|
| 87 |
|
| 88 | if (state.getValue(WATERLOGGED)) {
|
| 89 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 90 | }
|
| 91 |
|
| 92 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | protected boolean canBeReplaced(final BlockState state, final BlockPlaceContext context) {
|
| 97 | return !context.isSecondaryUseActive() && context.getItemInHand().is(this.asItem()) && state.getValue(PICKLES) < 4
|
| 98 | ? true
|
| 99 | : super.canBeReplaced(state, context);
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 104 | return switch (state.getValue(PICKLES)) {
|
| 105 | case 2 -> SHAPE_TWO;
|
| 106 | case 3 -> SHAPE_THREE;
|
| 107 | case 4 -> SHAPE_FOUR;
|
| 108 | default -> SHAPE_ONE;
|
| 109 | };
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | protected FluidState getFluidState(final BlockState state) {
|
| 114 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 119 | builder.add(PICKLES, WATERLOGGED);
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 124 | return !isDead(state) && level.getBlockState(pos.below()).is(BlockTags.CORAL_BLOCKS);
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 129 | return true;
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 134 | int span = 5;
|
| 135 | int zSpan = 1;
|
| 136 | int height = 2;
|
| 137 | int count = 0;
|
| 138 | int xStart = pos.getX() - 2;
|
| 139 | int zOffSet = 0;
|
| 140 |
|
| 141 | for (int x = 0; x < 5; x++) {
|
| 142 | for (int z = 0; z < zSpan; z++) {
|
| 143 | int endY = 2 + pos.getY() - 1;
|
| 144 |
|
| 145 | for (int startY = endY - 2; startY < endY; startY++) {
|
| 146 | BlockPos position = new BlockPos(xStart + x, startY, pos.getZ() - zOffSet + z);
|
| 147 | if (!position.equals(pos) && random.nextInt(6) == 0 && level.getBlockState(position).is(Blocks.WATER)) {
|
| 148 | BlockState belowState = level.getBlockState(position.below());
|
| 149 | if (belowState.is(BlockTags.CORAL_BLOCKS)) {
|
| 150 | level.setBlockAndUpdate(position, Blocks.SEA_PICKLE.defaultBlockState().setValue(PICKLES, random.nextInt(4) + 1));
|
| 151 | }
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | if (count < 2) {
|
| 157 | zSpan += 2;
|
| 158 | zOffSet++;
|
| 159 | } else {
|
| 160 | zSpan -= 2;
|
| 161 | zOffSet--;
|
| 162 | }
|
| 163 |
|
| 164 | count++;
|
| 165 | }
|
| 166 |
|
| 167 | level.setBlock(pos, state.setValue(PICKLES, 4), 2);
|
| 168 | }
|
| 169 |
|
| 170 | @Override
|
| 171 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 172 | return false;
|
| 173 | }
|
| 174 | }
|
| 175 |
|