SpreadingSnowyBlock .java

net.minecraft.world.level.block.SpreadingSnowyBlock (class): members: baseBlock, randomTick. imports: LightEngine, FluidTags, Registry, LevelReader.

net.minecraft.world.level.blockclass67 linescommonextends SnowyBlock2.6 KB

API Surface (1)

void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random)

Fields & Constants (1)

baseBlock

Full Source

SpreadingSnowyBlock.java67 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import java.util.Optional;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.Direction;
6
import net.minecraft.core.Registry;
7
import net.minecraft.core.registries.Registries;
8
import net.minecraft.resources.ResourceKey;
9
import net.minecraft.server.level.ServerLevel;
10
import net.minecraft.tags.FluidTags;
11
import net.minecraft.util.RandomSource;
12
import net.minecraft.world.level.LevelReader;
13
import net.minecraft.world.level.block.state.BlockBehaviour;
14
import net.minecraft.world.level.block.state.BlockState;
15
import net.minecraft.world.level.lighting.LightEngine;
16
17
public abstract class SpreadingSnowyBlock extends SnowyBlock {
18
	private final ResourceKey<Block> baseBlock;
19
20
	protected SpreadingSnowyBlock(final BlockBehaviour.Properties properties, final ResourceKey<Block> baseBlock) {
21
		super(properties);
22
		this.baseBlock = baseBlock;
23
	}
24
25
	private static boolean canStayAlive(final BlockState state, final LevelReader level, final BlockPos pos) {
26
		BlockPos above = pos.above();
27
		BlockState aboveState = level.getBlockState(above);
28
		if (aboveState.is(Blocks.SNOW) && aboveState.getValue(SnowLayerBlock.LAYERS) == 1) {
29
			return true;
30
		}
31
32
		if (aboveState.getFluidState().isFull()) {
33
			return false;
34
		}
35
36
		int lightDampeningTopFace = LightEngine.getLightDampeningInto(state, aboveState, Direction.UP, aboveState.getLightDampening());
37
		return lightDampeningTopFace < 15;
38
	}
39
40
	private static boolean canPropagate(final BlockState state, final LevelReader level, final BlockPos pos) {
41
		BlockPos above = pos.above();
42
		return canStayAlive(state, level, pos) && !level.getFluidState(above).is(FluidTags.WATER);
43
	}
44
45
	@Override
46
	protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
47
		Registry<Block> blocks = level.registryAccess().lookupOrThrow(Registries.BLOCK);
48
		Optional<Block> baseBlock = blocks.getOptional(this.baseBlock);
49
		if (!baseBlock.isEmpty()) {
50
			if (!canStayAlive(state, level, pos)) {
51
				level.setBlockAndUpdate(pos, baseBlock.get().defaultBlockState());
52
			} else {
53
				if (level.getMaxLocalRawBrightness(pos.above()) >= 9) {
54
					BlockState defaultBlockState = this.defaultBlockState();
55
56
					for (int i = 0; i < 4; i++) {
57
						BlockPos testPos = pos.offset(random.nextInt(3) - 1, random.nextInt(5) - 3, random.nextInt(3) - 1);
58
						if (level.getBlockState(testPos).is(baseBlock.get()) && canPropagate(defaultBlockState, level, testPos)) {
59
							level.setBlockAndUpdate(testPos, defaultBlockState.setValue(SNOWY, isSnowySetting(level.getBlockState(testPos.above()))));
60
						}
61
					}
62
				}
63
			}
64
		}
65
	}
66
}
67

Decompiled Minecraft 26.3 source of SpreadingSnowyBlock (net/minecraft/world/level/block/SpreadingSnowyBlock.java) with Mojang official mappings, stored verbatim on the site.