| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import it.unimi.dsi.fastutil.objects.Object2IntArrayMap;
|
| 5 | import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.function.Function;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.Direction;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.sounds.SoundEvent;
|
| 12 | import net.minecraft.sounds.SoundEvents;
|
| 13 | import net.minecraft.sounds.SoundSource;
|
| 14 | import net.minecraft.tags.BlockTags;
|
| 15 | import net.minecraft.util.Mth;
|
| 16 | import net.minecraft.util.RandomSource;
|
| 17 | import net.minecraft.util.Util;
|
| 18 | import net.minecraft.world.entity.Entity;
|
| 19 | import net.minecraft.world.entity.InsideBlockEffectApplier;
|
| 20 | import net.minecraft.world.entity.projectile.Projectile;
|
| 21 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 22 | import net.minecraft.world.level.BlockGetter;
|
| 23 | import net.minecraft.world.level.Level;
|
| 24 | import net.minecraft.world.level.LevelAccessor;
|
| 25 | import net.minecraft.world.level.LevelReader;
|
| 26 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 27 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 28 | import net.minecraft.world.level.block.state.BlockState;
|
| 29 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 30 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 31 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 32 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 33 | import net.minecraft.world.level.block.state.properties.Tilt;
|
| 34 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 35 | import net.minecraft.world.level.material.FluidState;
|
| 36 | import net.minecraft.world.level.material.Fluids;
|
| 37 | import net.minecraft.world.level.redstone.Orientation;
|
| 38 | import net.minecraft.world.phys.BlockHitResult;
|
| 39 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 40 | import net.minecraft.world.phys.shapes.Shapes;
|
| 41 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 42 | import org.jspecify.annotations.Nullable;
|
| 43 |
|
| 44 | /**
|
| 45 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 46 | */
|
| 47 | public class BigDripleafBlock extends HorizontalDirectionalBlock implements SimpleWaterloggedBlock, BonemealableBlock {
|
| 48 | private static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 49 | private static final EnumProperty<Tilt> TILT = BlockStateProperties.TILT;
|
| 50 | private static final int NO_TICK = -1;
|
| 51 | private static final Object2IntMap<Tilt> DELAY_UNTIL_NEXT_TILT_STATE = Util.make(new Object2IntArrayMap<>(), map -> {
|
| 52 | map.defaultReturnValue(-1);
|
| 53 | map.put(Tilt.UNSTABLE, 10);
|
| 54 | map.put(Tilt.PARTIAL, 10);
|
| 55 | map.put(Tilt.FULL, 100);
|
| 56 | });
|
| 57 | private static final int MAX_GEN_HEIGHT = 5;
|
| 58 | private static final int ENTITY_DETECTION_MIN_Y = 11;
|
| 59 | private static final int LOWEST_LEAF_TOP = 13;
|
| 60 | private static final Map<Tilt, VoxelShape> SHAPE_LEAF = Maps.newEnumMap(
|
| 61 | Map.of(
|
| 62 | Tilt.NONE,
|
| 63 | Block.column(16.0, 11.0, 15.0),
|
| 64 | Tilt.UNSTABLE,
|
| 65 | Block.column(16.0, 11.0, 15.0),
|
| 66 | Tilt.PARTIAL,
|
| 67 | Block.column(16.0, 11.0, 13.0),
|
| 68 | Tilt.FULL,
|
| 69 | Shapes.empty()
|
| 70 | )
|
| 71 | );
|
| 72 | private final Function<BlockState, VoxelShape> shapes;
|
| 73 |
|
| 74 | /**
|
| 75 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 76 | */
|
| 77 | public BigDripleafBlock(final BlockBehaviour.Properties properties) {
|
| 78 | super(properties);
|
| 79 | this.registerDefaultState(this.stateDefinition.any().setValue(WATERLOGGED, false).setValue(FACING, Direction.NORTH).setValue(TILT, Tilt.NONE));
|
| 80 | this.shapes = this.makeShapes();
|
| 81 | }
|
| 82 |
|
| 83 | private Function<BlockState, VoxelShape> makeShapes() {
|
| 84 | Map<Direction, VoxelShape> stems = Shapes.rotateHorizontal(Block.column(6.0, 0.0, 13.0).move(0.0, 0.0, 0.25).optimize());
|
| 85 | return this.getShapeForEachState(state -> Shapes.or(SHAPE_LEAF.get(state.getValue(TILT)), stems.get(state.getValue(FACING))), WATERLOGGED);
|
| 86 | }
|
| 87 |
|
| 88 | public static void placeWithRandomHeight(final LevelAccessor level, final RandomSource random, final BlockPos stemBottomPos, final Direction facing) {
|
| 89 | int desiredHeight = Mth.nextInt(random, 2, 5);
|
| 90 | BlockPos.MutableBlockPos pos = stemBottomPos.mutable();
|
| 91 | int height = 0;
|
| 92 |
|
| 93 | while (height < desiredHeight && canPlaceAt(level, pos)) {
|
| 94 | height++;
|
| 95 | pos.move(Direction.UP);
|
| 96 | }
|
| 97 |
|
| 98 | int leafY = stemBottomPos.getY() + height - 1;
|
| 99 | pos.setY(stemBottomPos.getY());
|
| 100 |
|
| 101 | while (pos.getY() < leafY) {
|
| 102 | BigDripleafStemBlock.place(level, pos, level.getFluidState(pos), facing);
|
| 103 | pos.move(Direction.UP);
|
| 104 | }
|
| 105 |
|
| 106 | place(level, pos, level.getFluidState(pos), facing);
|
| 107 | }
|
| 108 |
|
| 109 | private static boolean canReplace(final BlockState oldState) {
|
| 110 | return oldState.isAir() || oldState.is(Blocks.WATER) || oldState.is(Blocks.SMALL_DRIPLEAF);
|
| 111 | }
|
| 112 |
|
| 113 | protected static boolean canPlaceAt(final LevelReader level, final BlockPos pos) {
|
| 114 | return canGrowInto(level, pos);
|
| 115 | }
|
| 116 |
|
| 117 | protected static boolean canGrowInto(final LevelReader level, final BlockPos pos) {
|
| 118 | BlockState state = level.getBlockState(pos);
|
| 119 | return level.isInsideBuildHeight(pos) && canReplace(state);
|
| 120 | }
|
| 121 |
|
| 122 | protected static boolean place(final LevelAccessor level, final BlockPos pos, final FluidState fluidState, final Direction facing) {
|
| 123 | BlockState newState = Blocks.BIG_DRIPLEAF.defaultBlockState().setValue(WATERLOGGED, fluidState.isSourceOfType(Fluids.WATER)).setValue(FACING, facing);
|
| 124 | return level.setBlockAndUpdate(pos, newState);
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | protected void onProjectileHit(final Level level, final BlockState state, final BlockHitResult blockHit, final Projectile projectile) {
|
| 129 | this.setTiltAndScheduleTick(state, level, blockHit.getBlockPos(), Tilt.FULL, SoundEvents.BIG_DRIPLEAF_TILT_DOWN);
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | protected FluidState getFluidState(final BlockState state) {
|
| 134 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 139 | BlockPos belowPos = pos.below();
|
| 140 | BlockState belowState = level.getBlockState(belowPos);
|
| 141 | return belowState.is(this) || belowState.is(Blocks.BIG_DRIPLEAF_STEM) || belowState.is(BlockTags.SUPPORTS_BIG_DRIPLEAF);
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | protected BlockState updateShape(
|
| 146 | final BlockState state,
|
| 147 | final LevelReader level,
|
| 148 | final ScheduledTickAccess ticks,
|
| 149 | final BlockPos pos,
|
| 150 | final Direction directionToNeighbour,
|
| 151 | final BlockPos neighbourPos,
|
| 152 | final BlockState neighbourState,
|
| 153 | final RandomSource random
|
| 154 | ) {
|
| 155 | if (directionToNeighbour == Direction.DOWN && !state.canSurvive(level, pos)) {
|
| 156 | return Blocks.AIR.defaultBlockState();
|
| 157 | }
|
| 158 |
|
| 159 | if (state.getValue(WATERLOGGED)) {
|
| 160 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 161 | }
|
| 162 |
|
| 163 | return directionToNeighbour == Direction.UP && neighbourState.is(this)
|
| 164 | ? Blocks.BIG_DRIPLEAF_STEM.withPropertiesOf(state)
|
| 165 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 166 | }
|
| 167 |
|
| 168 | @Override
|
| 169 | public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 170 | return canGrowInto(level, pos.above());
|
| 171 | }
|
| 172 |
|
| 173 | @Override
|
| 174 | public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 175 | return true;
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 180 | BlockPos abovePos = pos.above();
|
| 181 | if (canPlaceAt(level, abovePos)) {
|
| 182 | Direction facing = state.getValue(FACING);
|
| 183 | BigDripleafStemBlock.place(level, pos, state.getFluidState(), facing);
|
| 184 | place(level, abovePos, level.getBlockState(abovePos).getFluidState(), facing);
|
| 185 | }
|
| 186 | }
|
| 187 |
|
| 188 | @Override
|
| 189 | protected void entityInside(
|
| 190 | final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise
|
| 191 | ) {
|
| 192 | if (!level.isClientSide()) {
|
| 193 | if (state.getValue(TILT) == Tilt.NONE && canEntityTilt(pos, entity) && !level.hasNeighborSignal(pos)) {
|
| 194 | this.setTiltAndScheduleTick(state, level, pos, Tilt.UNSTABLE, null);
|
| 195 | }
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | @Override
|
| 200 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 201 | if (level.hasNeighborSignal(pos)) {
|
| 202 | resetTilt(state, level, pos);
|
| 203 | } else {
|
| 204 | Tilt tilt = state.getValue(TILT);
|
| 205 | if (tilt == Tilt.UNSTABLE) {
|
| 206 | this.setTiltAndScheduleTick(state, level, pos, Tilt.PARTIAL, SoundEvents.BIG_DRIPLEAF_TILT_DOWN);
|
| 207 | } else if (tilt == Tilt.PARTIAL) {
|
| 208 | this.setTiltAndScheduleTick(state, level, pos, Tilt.FULL, SoundEvents.BIG_DRIPLEAF_TILT_DOWN);
|
| 209 | } else if (tilt == Tilt.FULL) {
|
| 210 | resetTilt(state, level, pos);
|
| 211 | }
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | @Override
|
| 216 | protected void neighborChanged(
|
| 217 | final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
|
| 218 | ) {
|
| 219 | if (level.hasNeighborSignal(pos)) {
|
| 220 | resetTilt(state, level, pos);
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | private static void playTiltSound(final Level level, final BlockPos pos, final SoundEvent tiltSound) {
|
| 225 | float pitch = Mth.randomBetween(level.getRandom(), 0.8F, 1.2F);
|
| 226 | level.playSound(null, pos, tiltSound, SoundSource.BLOCKS, 1.0F, pitch);
|
| 227 | }
|
| 228 |
|
| 229 | private static boolean canEntityTilt(final BlockPos pos, final Entity entity) {
|
| 230 | return entity.onGround() && entity.position().y > pos.getY() + 0.6875F;
|
| 231 | }
|
| 232 |
|
| 233 | private void setTiltAndScheduleTick(final BlockState state, final Level level, final BlockPos pos, final Tilt tilt, final @Nullable SoundEvent sound) {
|
| 234 | setTilt(state, level, pos, tilt);
|
| 235 | if (sound != null) {
|
| 236 | playTiltSound(level, pos, sound);
|
| 237 | }
|
| 238 |
|
| 239 | int tickDelay = DELAY_UNTIL_NEXT_TILT_STATE.getInt(tilt);
|
| 240 | if (tickDelay != -1) {
|
| 241 | level.scheduleTick(pos, this, tickDelay);
|
| 242 | }
|
| 243 | }
|
| 244 |
|
| 245 | private static void resetTilt(final BlockState state, final Level level, final BlockPos pos) {
|
| 246 | setTilt(state, level, pos, Tilt.NONE);
|
| 247 | if (state.getValue(TILT) != Tilt.NONE) {
|
| 248 | playTiltSound(level, pos, SoundEvents.BIG_DRIPLEAF_TILT_UP);
|
| 249 | }
|
| 250 | }
|
| 251 |
|
| 252 | private static void setTilt(final BlockState state, final Level level, final BlockPos pos, final Tilt tilt) {
|
| 253 | Tilt previousTilt = state.getValue(TILT);
|
| 254 | level.setBlock(pos, state.setValue(TILT, tilt), 2);
|
| 255 | if (tilt.causesVibration() && tilt != previousTilt) {
|
| 256 | level.gameEvent(null, GameEvent.BLOCK_CHANGE, pos);
|
| 257 | }
|
| 258 | }
|
| 259 |
|
| 260 | @Override
|
| 261 | protected VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 262 | return SHAPE_LEAF.get(state.getValue(TILT));
|
| 263 | }
|
| 264 |
|
| 265 | @Override
|
| 266 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 267 | return this.shapes.apply(state);
|
| 268 | }
|
| 269 |
|
| 270 | @Override
|
| 271 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 272 | BlockState belowState = context.getLevel().getBlockState(context.getClickedPos().below());
|
| 273 | FluidState fluidState = context.getLevel().getFluidState(context.getClickedPos());
|
| 274 | boolean belowIsDripleafPart = belowState.is(Blocks.BIG_DRIPLEAF) || belowState.is(Blocks.BIG_DRIPLEAF_STEM);
|
| 275 | return this.defaultBlockState()
|
| 276 | .setValue(WATERLOGGED, fluidState.isSourceOfType(Fluids.WATER))
|
| 277 | .setValue(FACING, belowIsDripleafPart ? belowState.getValue(FACING) : context.getHorizontalDirection().getOpposite());
|
| 278 | }
|
| 279 |
|
| 280 | @Override
|
| 281 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 282 | builder.add(WATERLOGGED, FACING, TILT);
|
| 283 | }
|
| 284 | }
|
| 285 |
|