| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.util.Continuation;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.level.block.state.BlockState;
|
| 9 | import org.apache.commons.lang3.mutable.MutableInt;
|
| 10 |
|
| 11 | public interface ChangeOverTimeBlock<T extends Enum<T>> {
|
| 12 | int SCAN_DISTANCE = 4;
|
| 13 |
|
| 14 | Optional<BlockState> getNext(BlockState state);
|
| 15 |
|
| 16 | float getChanceModifier();
|
| 17 |
|
| 18 | default void changeOverTime(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 19 | float eachBlockOncePerDayChance = 0.05688889F;
|
| 20 | if (random.nextFloat() < 0.05688889F) {
|
| 21 | this.getNextState(state, level, pos, random).ifPresent(weatheredState -> level.setBlockAndUpdate(pos, weatheredState));
|
| 22 | }
|
| 23 | }
|
| 24 |
|
| 25 | T getAge();
|
| 26 |
|
| 27 | default Optional<BlockState> getNextState(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 28 | int ownAge = this.getAge().ordinal();
|
| 29 | MutableInt sameAgeCount = new MutableInt(0);
|
| 30 | MutableInt olderCount = new MutableInt(0);
|
| 31 | boolean aborted = level.findBlocksInManhattan(pos, 4)
|
| 32 | .filterPos(blockPos -> !blockPos.equals(pos))
|
| 33 | .filterState(this::isSameAgeingType)
|
| 34 | .forEachUntil((blockPos, var5x) -> {
|
| 35 | if (level.getBlockState(blockPos).getBlock() instanceof ChangeOverTimeBlock<?> neighborBlock) {
|
| 36 | int foundAge = neighborBlock.getAge().ordinal();
|
| 37 | if (foundAge < ownAge) {
|
| 38 | return Continuation.ABORT;
|
| 39 | }
|
| 40 |
|
| 41 | if (foundAge > ownAge) {
|
| 42 | olderCount.increment();
|
| 43 | } else {
|
| 44 | sameAgeCount.increment();
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | return Continuation.CONTINUE;
|
| 49 | });
|
| 50 | if (aborted) {
|
| 51 | return Optional.empty();
|
| 52 | }
|
| 53 |
|
| 54 | float chance = (float)(olderCount.intValue() + 1) / (olderCount.intValue() + sameAgeCount.intValue() + 1);
|
| 55 | float actualChance = chance * chance * this.getChanceModifier();
|
| 56 | return random.nextFloat() < actualChance ? this.getNext(state) : Optional.empty();
|
| 57 | }
|
| 58 |
|
| 59 | private boolean isSameAgeingType(final BlockState state) {
|
| 60 | return state.getBlock() instanceof ChangeOverTimeBlock<?> neighborBlock && this.getAge().getClass() == neighborBlock.getAge().getClass();
|
| 61 | }
|
| 62 | }
|
| 63 |
|