minecraft-src/net/minecraft/client/sounds/MusicManager.java
2025-07-04 03:15:13 +03:00

143 lines
4.2 KiB
Java

package net.minecraft.client.sounds;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.client.resources.sounds.SoundInstance;
import net.minecraft.sounds.Music;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import org.jetbrains.annotations.Nullable;
/**
* The MusicManager class manages the playing of music in Minecraft.
*/
@Environment(EnvType.CLIENT)
public class MusicManager {
/**
* The delay before starting to play the next song.
*/
private static final int STARTING_DELAY = 100;
private final RandomSource random = RandomSource.create();
private final Minecraft minecraft;
@Nullable
private SoundInstance currentMusic;
private float currentGain = 1.0F;
/**
* The delay until the next song starts.
*/
private int nextSongDelay = 100;
public MusicManager(Minecraft minecraft) {
this.minecraft = minecraft;
}
/**
* Called every tick to manage the playing of music.
*/
public void tick() {
MusicInfo musicInfo = this.minecraft.getSituationalMusic();
float f = musicInfo.volume();
if (this.currentMusic != null && this.currentGain != f) {
boolean bl = this.fadePlaying(f);
if (!bl) {
return;
}
}
Music music = musicInfo.music();
if (music == null) {
this.nextSongDelay = Math.max(this.nextSongDelay, 100);
} else {
if (this.currentMusic != null) {
if (musicInfo.canReplace(this.currentMusic)) {
this.minecraft.getSoundManager().stop(this.currentMusic);
this.nextSongDelay = Mth.nextInt(this.random, 0, music.getMinDelay() / 2);
}
if (!this.minecraft.getSoundManager().isActive(this.currentMusic)) {
this.currentMusic = null;
this.nextSongDelay = Math.min(this.nextSongDelay, Mth.nextInt(this.random, music.getMinDelay(), music.getMaxDelay()));
}
}
this.nextSongDelay = Math.min(this.nextSongDelay, music.getMaxDelay());
if (this.currentMusic == null && this.nextSongDelay-- <= 0) {
this.startPlaying(musicInfo);
}
}
}
public void startPlaying(MusicInfo music) {
this.currentMusic = SimpleSoundInstance.forMusic(music.music().getEvent().value());
if (this.currentMusic.getSound() != SoundManager.EMPTY_SOUND) {
this.minecraft.getSoundManager().play(this.currentMusic);
this.minecraft.getSoundManager().setVolume(this.currentMusic, music.volume());
}
this.nextSongDelay = Integer.MAX_VALUE;
this.currentGain = music.volume();
}
/**
* Stops playing the specified {@linkplain Music} selector.
*
* @param music the {@linkplain Music} selector to stop playing
*/
public void stopPlaying(Music music) {
if (this.isPlayingMusic(music)) {
this.stopPlaying();
}
}
/**
* Stops playing the current {@linkplain Music} selector.
*/
public void stopPlaying() {
if (this.currentMusic != null) {
this.minecraft.getSoundManager().stop(this.currentMusic);
this.currentMusic = null;
}
this.nextSongDelay += 100;
}
private boolean fadePlaying(float volume) {
if (this.currentMusic == null) {
return false;
} else if (this.currentGain == volume) {
return true;
} else {
if (this.currentGain < volume) {
this.currentGain = this.currentGain + Mth.clamp(this.currentGain, 5.0E-4F, 0.005F);
if (this.currentGain > volume) {
this.currentGain = volume;
}
} else {
this.currentGain = 0.03F * volume + 0.97F * this.currentGain;
if (Math.abs(this.currentGain - volume) < 1.0E-4F || this.currentGain < volume) {
this.currentGain = volume;
}
}
this.currentGain = Mth.clamp(this.currentGain, 0.0F, 1.0F);
if (this.currentGain <= 1.0E-4F) {
this.stopPlaying();
return false;
} else {
this.minecraft.getSoundManager().setVolume(this.currentMusic, this.currentGain);
return true;
}
}
}
/**
* {@return {@code true} if the {@linkplain Music} selector is currently playing, {@code false} otherwise}
*
* @param selector the {@linkplain Music} selector to check for
*/
public boolean isPlayingMusic(Music selector) {
return this.currentMusic == null ? false : selector.getEvent().value().location().equals(this.currentMusic.getLocation());
}
}