| 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.util.Mth;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.item.ItemStack;
|
| 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.LightLayer;
|
| 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.IntegerProperty;
|
| 18 | import net.minecraft.world.level.redstone.Orientation;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 |
|
| 21 | public class FrostedIceBlock extends IceBlock {
|
| 22 | public static final int MAX_AGE = 3;
|
| 23 | public static final IntegerProperty AGE = BlockStateProperties.AGE_3;
|
| 24 | private static final int NEIGHBORS_TO_AGE = 4;
|
| 25 | private static final int NEIGHBORS_TO_MELT = 2;
|
| 26 |
|
| 27 | public FrostedIceBlock(final BlockBehaviour.Properties properties) {
|
| 28 | super(properties);
|
| 29 | this.registerDefaultState(this.stateDefinition.any().setValue(AGE, 0));
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
|
| 34 | level.scheduleTick(pos, this, Mth.nextInt(level.getRandom(), 60, 120));
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 39 | if (random.nextInt(3) == 0 || this.fewerNeigboursThan(level, pos, 4)) {
|
| 40 | int brightness = level.dimension() == Level.END ? level.getBrightness(LightLayer.BLOCK, pos) : level.getMaxLocalRawBrightness(pos);
|
| 41 | if (brightness > 11 - state.getValue(AGE) - state.getLightDampening() && this.slightlyMelt(state, level, pos)) {
|
| 42 | BlockPos.MutableBlockPos neighborPos = new BlockPos.MutableBlockPos();
|
| 43 |
|
| 44 | for (Direction direction : Direction.values()) {
|
| 45 | neighborPos.setWithOffset(pos, direction);
|
| 46 | BlockState neighbour = level.getBlockState(neighborPos);
|
| 47 | if (neighbour.is(this) && !this.slightlyMelt(neighbour, level, neighborPos)) {
|
| 48 | level.scheduleTick(neighborPos, this, Mth.nextInt(random, 20, 40));
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | return;
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | level.scheduleTick(pos, this, Mth.nextInt(random, 20, 40));
|
| 57 | }
|
| 58 |
|
| 59 | private boolean slightlyMelt(final BlockState state, final Level level, final BlockPos pos) {
|
| 60 | int age = state.getValue(AGE);
|
| 61 | if (age < 3) {
|
| 62 | level.setBlock(pos, state.setValue(AGE, age + 1), 2);
|
| 63 | return false;
|
| 64 | } else {
|
| 65 | this.melt(state, level, pos);
|
| 66 | return true;
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | protected void neighborChanged(
|
| 72 | final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
|
| 73 | ) {
|
| 74 | if (block.defaultBlockState().is(this) && this.fewerNeigboursThan(level, pos, 2)) {
|
| 75 | this.melt(state, level, pos);
|
| 76 | }
|
| 77 |
|
| 78 | super.neighborChanged(state, level, pos, block, orientation, movedByPiston);
|
| 79 | }
|
| 80 |
|
| 81 | private boolean fewerNeigboursThan(final BlockGetter level, final BlockPos pos, final int limit) {
|
| 82 | int result = 0;
|
| 83 | BlockPos.MutableBlockPos neighborPos = new BlockPos.MutableBlockPos();
|
| 84 |
|
| 85 | for (Direction direction : Direction.values()) {
|
| 86 | neighborPos.setWithOffset(pos, direction);
|
| 87 | if (level.getBlockState(neighborPos).is(this)) {
|
| 88 | if (++result >= limit) {
|
| 89 | return false;
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | return true;
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 99 | builder.add(AGE);
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) {
|
| 104 | return ItemStack.EMPTY;
|
| 105 | }
|
| 106 | }
|
| 107 |
|