AmbientLeavesBlockSoundPlayer .java

net.minecraft.world.level.block.sounds.AmbientLeavesBlockSoundPlayer (record, 76 lines): members: ambientSound, chance, satisfyingBlocks, noAmbientSound.

net.minecraft.world.level.block.soundsrecord76 linescommon3.2 KB

API Surface (4)

record AmbientLeavesBlockSoundPlayer(Optional<Holder<SoundEvent>> ambientSound, int chance, Optional<TagKey<Block>> satisfyingBlocks, int nearbySatisfyingBlocksRequired, int nearbySameLeavesRequired)
AmbientLeavesBlockSoundPlayer of(final Holder<SoundEvent> ambientSound, final TagKey<Block> satisfyingBlocks)
AmbientLeavesBlockSoundPlayer noAmbientSound()
void playAmbientLeavesSounds(final Level level, final BlockPos pos, final Block block, final RandomSource random)

Fields & Constants (5)

AMBIENT_SOUND_CHANCE
NEARBY_SATISFYING_BLOCKS_REQUIRED
NEARBY_SAME_LEAVES_REQUIRED
CODEC
NO_SOUND

String literals (5)

"ambient_sound"
"chance"
"satisfying_blocks"
"nearby_satisfying_blocks_required"
"nearby_same_leaves_required"

Full Source

AmbientLeavesBlockSoundPlayer.java76 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block.sounds;
2
3
import com.mojang.serialization.Codec;
4
import com.mojang.serialization.codecs.RecordCodecBuilder;
5
import java.util.Optional;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Direction;
8
import net.minecraft.core.Holder;
9
import net.minecraft.core.registries.Registries;
10
import net.minecraft.sounds.SoundEvent;
11
import net.minecraft.sounds.SoundSource;
12
import net.minecraft.tags.TagKey;
13
import net.minecraft.util.ExtraCodecs;
14
import net.minecraft.util.RandomSource;
15
import net.minecraft.world.level.Level;
16
import net.minecraft.world.level.block.Block;
17
import net.minecraft.world.level.block.state.BlockState;
18
19
public record AmbientLeavesBlockSoundPlayer(
20
	Optional<Holder<SoundEvent>> ambientSound,
21
	int chance,
22
	Optional<TagKey<Block>> satisfyingBlocks,
23
	int nearbySatisfyingBlocksRequired,
24
	int nearbySameLeavesRequired
25
) {
26
	private static final int AMBIENT_SOUND_CHANCE = 300;
27
	private static final int NEARBY_SATISFYING_BLOCKS_REQUIRED = 1;
28
	private static final int NEARBY_SAME_LEAVES_REQUIRED = 3;
29
	public static final Codec<AmbientLeavesBlockSoundPlayer> CODEC = RecordCodecBuilder.create(
30
		i -> i.group(
31
				SoundEvent.CODEC.optionalFieldOf("ambient_sound").forGetter(AmbientLeavesBlockSoundPlayer::ambientSound),
32
				ExtraCodecs.NON_NEGATIVE_INT.optionalFieldOf("chance", 300).forGetter(AmbientLeavesBlockSoundPlayer::chance),
33
				TagKey.codec(Registries.BLOCK).optionalFieldOf("satisfying_blocks").forGetter(AmbientLeavesBlockSoundPlayer::satisfyingBlocks),
34
				ExtraCodecs.NON_NEGATIVE_INT
35
					.optionalFieldOf("nearby_satisfying_blocks_required", 1)
36
					.forGetter(AmbientLeavesBlockSoundPlayer::nearbySatisfyingBlocksRequired),
37
				ExtraCodecs.NON_NEGATIVE_INT.optionalFieldOf("nearby_same_leaves_required", 3).forGetter(AmbientLeavesBlockSoundPlayer::nearbySameLeavesRequired)
38
			)
39
			.apply(i, AmbientLeavesBlockSoundPlayer::new)
40
	);
41
	private static final AmbientLeavesBlockSoundPlayer NO_SOUND = new AmbientLeavesBlockSoundPlayer(Optional.empty(), 0, Optional.empty(), 0, 0);
42
43
	public static AmbientLeavesBlockSoundPlayer of(final Holder<SoundEvent> ambientSound, final TagKey<Block> satisfyingBlocks) {
44
		return new AmbientLeavesBlockSoundPlayer(Optional.of(ambientSound), 300, Optional.of(satisfyingBlocks), 1, 3);
45
	}
46
47
	public static AmbientLeavesBlockSoundPlayer noAmbientSound() {
48
		return NO_SOUND;
49
	}
50
51
	public void playAmbientLeavesSounds(final Level level, final BlockPos pos, final Block block, final RandomSource random) {
52
		if (this.ambientSound.isPresent() && random.nextInt(this.chance) == 0) {
53
			TagKey<Block> satisfyingBlocks = this.satisfyingBlocks.orElse(null);
54
			int nearbyLogs = 0;
55
			int nearbyLeaves = 0;
56
57
			for (Direction direction : Direction.Plane.HORIZONTAL) {
58
				BlockPos neighborPos = pos.relative(direction);
59
				BlockState neighborState = level.getBlockState(neighborPos);
60
				if (satisfyingBlocks != null && neighborState.is(satisfyingBlocks)) {
61
					nearbyLogs++;
62
				}
63
64
				if (neighborState.is(block)) {
65
					nearbyLeaves++;
66
				}
67
68
				if (nearbyLogs == this.nearbySatisfyingBlocksRequired && nearbyLeaves == this.nearbySameLeavesRequired) {
69
					level.playLocalSound(pos.getX(), pos.getY(), pos.getZ(), this.ambientSound.get().value(), SoundSource.AMBIENT, 1.0F, 1.0F, false);
70
					return;
71
				}
72
			}
73
		}
74
	}
75
}
76

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