| 1 | package net.minecraft.client.resources.sounds;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
|
| 4 | import java.util.Objects;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.player.LocalPlayer;
|
| 8 | import net.minecraft.client.sounds.SoundManager;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Holder;
|
| 11 | import net.minecraft.sounds.SoundEvent;
|
| 12 | import net.minecraft.sounds.SoundSource;
|
| 13 | import net.minecraft.util.Mth;
|
| 14 | import net.minecraft.util.RandomSource;
|
| 15 | import net.minecraft.world.attribute.AmbientAdditionsSettings;
|
| 16 | import net.minecraft.world.attribute.AmbientSounds;
|
| 17 | import net.minecraft.world.attribute.EnvironmentAttributeSystem;
|
| 18 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 19 | import net.minecraft.world.level.Level;
|
| 20 | import net.minecraft.world.level.LightLayer;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public class BiomeAmbientSoundsHandler implements AmbientSoundHandler {
|
| 25 | private static final int LOOP_SOUND_CROSS_FADE_TIME = 40;
|
| 26 | private static final float SKY_MOOD_RECOVERY_RATE = 0.001F;
|
| 27 | private final LocalPlayer player;
|
| 28 | private final SoundManager soundManager;
|
| 29 | private final RandomSource random;
|
| 30 | private final Object2ObjectArrayMap<Holder<SoundEvent>, BiomeAmbientSoundsHandler.LoopSoundInstance> loopSounds = new Object2ObjectArrayMap<>();
|
| 31 | private float moodiness;
|
| 32 | private @Nullable Holder<SoundEvent> previousLoopSound;
|
| 33 |
|
| 34 | public BiomeAmbientSoundsHandler(final LocalPlayer player, final SoundManager soundManager) {
|
| 35 | this.random = player.level().getRandom();
|
| 36 | this.player = player;
|
| 37 | this.soundManager = soundManager;
|
| 38 | }
|
| 39 |
|
| 40 | public float getMoodiness() {
|
| 41 | return this.moodiness;
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public void tick() {
|
| 46 | this.loopSounds.values().removeIf(AbstractTickableSoundInstance::isStopped);
|
| 47 | Level level = this.player.level();
|
| 48 | EnvironmentAttributeSystem environmentAttributes = level.environmentAttributes();
|
| 49 | AmbientSounds ambientSounds = environmentAttributes.getValue(EnvironmentAttributes.AMBIENT_SOUNDS, this.player.position());
|
| 50 | Holder<SoundEvent> currentLoopSound = ambientSounds.loop().orElse(null);
|
| 51 | if (!Objects.equals(currentLoopSound, this.previousLoopSound)) {
|
| 52 | this.previousLoopSound = currentLoopSound;
|
| 53 | this.loopSounds.values().forEach(BiomeAmbientSoundsHandler.LoopSoundInstance::fadeOut);
|
| 54 | if (currentLoopSound != null) {
|
| 55 | this.loopSounds.compute(currentLoopSound, (biomeKey, soundInstance) -> {
|
| 56 | if (soundInstance == null) {
|
| 57 | soundInstance = new BiomeAmbientSoundsHandler.LoopSoundInstance(currentLoopSound.value());
|
| 58 | this.soundManager.play(soundInstance);
|
| 59 | }
|
| 60 |
|
| 61 | soundInstance.fadeIn();
|
| 62 | return (BiomeAmbientSoundsHandler.LoopSoundInstance)soundInstance;
|
| 63 | });
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | for (AmbientAdditionsSettings additions : ambientSounds.additions()) {
|
| 68 | if (this.random.nextDouble() < additions.tickChance()) {
|
| 69 | this.soundManager.play(SimpleSoundInstance.forAmbientAddition(additions.soundEvent().value()));
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | ambientSounds.mood()
|
| 74 | .ifPresent(
|
| 75 | mood -> {
|
| 76 | int searchSpan = mood.blockSearchExtent() * 2 + 1;
|
| 77 | BlockPos blockSamplingPos = BlockPos.containing(
|
| 78 | this.player.getX() + this.random.nextInt(searchSpan) - mood.blockSearchExtent(),
|
| 79 | this.player.getEyeY() + this.random.nextInt(searchSpan) - mood.blockSearchExtent(),
|
| 80 | this.player.getZ() + this.random.nextInt(searchSpan) - mood.blockSearchExtent()
|
| 81 | );
|
| 82 | int skyBrightness = level.getBrightness(LightLayer.SKY, blockSamplingPos);
|
| 83 | if (skyBrightness > 0) {
|
| 84 | this.moodiness -= skyBrightness / 15.0F * 0.001F;
|
| 85 | } else {
|
| 86 | this.moodiness = this.moodiness - (float)(level.getBrightness(LightLayer.BLOCK, blockSamplingPos) - 1) / mood.tickDelay();
|
| 87 | }
|
| 88 |
|
| 89 | if (this.moodiness >= 1.0F) {
|
| 90 | double blockSampleX = blockSamplingPos.getX() + 0.5;
|
| 91 | double blockSampleY = blockSamplingPos.getY() + 0.5;
|
| 92 | double blockSampleZ = blockSamplingPos.getZ() + 0.5;
|
| 93 | double blockDirectionX = blockSampleX - this.player.getX();
|
| 94 | double blockDirectionY = blockSampleY - this.player.getEyeY();
|
| 95 | double blockDirectionZ = blockSampleZ - this.player.getZ();
|
| 96 | double blockDistance = Math.sqrt(blockDirectionX * blockDirectionX + blockDirectionY * blockDirectionY + blockDirectionZ * blockDirectionZ);
|
| 97 | double soundSourceDistance = blockDistance + mood.soundPositionOffset();
|
| 98 | SimpleSoundInstance moodSoundInstance = SimpleSoundInstance.forAmbientMood(
|
| 99 | mood.soundEvent().value(),
|
| 100 | this.random,
|
| 101 | this.player.getX() + blockDirectionX / blockDistance * soundSourceDistance,
|
| 102 | this.player.getEyeY() + blockDirectionY / blockDistance * soundSourceDistance,
|
| 103 | this.player.getZ() + blockDirectionZ / blockDistance * soundSourceDistance
|
| 104 | );
|
| 105 | this.soundManager.play(moodSoundInstance);
|
| 106 | this.moodiness = 0.0F;
|
| 107 | } else {
|
| 108 | this.moodiness = Math.max(this.moodiness, 0.0F);
|
| 109 | }
|
| 110 | }
|
| 111 | );
|
| 112 | }
|
| 113 |
|
| 114 | @Environment(EnvType.CLIENT)
|
| 115 | public static class LoopSoundInstance extends AbstractTickableSoundInstance {
|
| 116 | private int fadeDirection;
|
| 117 | private int fade;
|
| 118 |
|
| 119 | public LoopSoundInstance(final SoundEvent soundEvent) {
|
| 120 | super(soundEvent, SoundSource.AMBIENT, SoundInstance.createUnseededRandom());
|
| 121 | this.looping = true;
|
| 122 | this.delay = 0;
|
| 123 | this.volume = 1.0F;
|
| 124 | this.relative = true;
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | public void tick() {
|
| 129 | if (this.fade < 0) {
|
| 130 | this.stop();
|
| 131 | }
|
| 132 |
|
| 133 | this.fade = this.fade + this.fadeDirection;
|
| 134 | this.volume = Mth.clamp(this.fade / 40.0F, 0.0F, 1.0F);
|
| 135 | }
|
| 136 |
|
| 137 | public void fadeOut() {
|
| 138 | this.fade = Math.min(this.fade, 40);
|
| 139 | this.fadeDirection = -1;
|
| 140 | }
|
| 141 |
|
| 142 | public void fadeIn() {
|
| 143 | this.fade = Math.max(0, this.fade);
|
| 144 | this.fadeDirection = 1;
|
| 145 | }
|
| 146 | }
|
| 147 | }
|
| 148 |
|