| 1 | package net.minecraft.client.sounds;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.Minecraft;
|
| 7 | import net.minecraft.client.gui.screens.LevelLoadingScreen;
|
| 8 | import net.minecraft.client.resources.sounds.SimpleSoundInstance;
|
| 9 | import net.minecraft.client.resources.sounds.Sound;
|
| 10 | import net.minecraft.client.resources.sounds.SoundInstance;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.sounds.Music;
|
| 13 | import net.minecraft.sounds.SoundEvent;
|
| 14 | import net.minecraft.sounds.SoundSource;
|
| 15 | import net.minecraft.util.Mth;
|
| 16 | import net.minecraft.util.RandomSource;
|
| 17 | import net.minecraft.util.StringRepresentable;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class MusicManager {
|
| 22 | private static final int STARTING_DELAY = 100;
|
| 23 | private final RandomSource random = RandomSource.create();
|
| 24 | private final Minecraft minecraft;
|
| 25 | private @Nullable SoundInstance currentMusic;
|
| 26 | private MusicManager.MusicFrequency gameMusicFrequency;
|
| 27 | private float currentGain = 1.0F;
|
| 28 | private int nextSongDelay = 100;
|
| 29 | private boolean toastShown = false;
|
| 30 |
|
| 31 | public MusicManager(final Minecraft minecraft) {
|
| 32 | this.minecraft = minecraft;
|
| 33 | this.gameMusicFrequency = minecraft.options.musicFrequency().get();
|
| 34 | }
|
| 35 |
|
| 36 | public void tick() {
|
| 37 | float volume = this.minecraft.getMusicVolume();
|
| 38 | if (this.currentMusic != null && this.currentGain != volume) {
|
| 39 | boolean stillPlaying = this.fadePlaying(volume);
|
| 40 | if (!stillPlaying) {
|
| 41 | return;
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | Music music = this.minecraft.getSituationalMusic();
|
| 46 | if (music == null) {
|
| 47 | this.nextSongDelay = Math.max(this.nextSongDelay, 100);
|
| 48 | } else {
|
| 49 | if (this.currentMusic != null) {
|
| 50 | if (canReplace(music, this.currentMusic)) {
|
| 51 | this.minecraft.getSoundManager().stop(this.currentMusic);
|
| 52 | this.nextSongDelay = Mth.nextInt(this.random, 0, music.minDelay() / 2);
|
| 53 | }
|
| 54 |
|
| 55 | if (!this.minecraft.getSoundManager().isActive(this.currentMusic)) {
|
| 56 | this.currentMusic = null;
|
| 57 | this.nextSongDelay = Math.min(this.nextSongDelay, this.gameMusicFrequency.getNextSongDelay(music, this.random));
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | this.nextSongDelay = Math.min(this.nextSongDelay, music.maxDelay());
|
| 62 | if (this.currentMusic == null && !(this.minecraft.gui.screen() instanceof LevelLoadingScreen) && --this.nextSongDelay <= 0) {
|
| 63 | this.startPlaying(music);
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | private static boolean canReplace(final Music music, final SoundInstance currentMusic) {
|
| 69 | return music.replaceCurrentMusic() && !music.sound().value().location().equals(currentMusic.getIdentifier());
|
| 70 | }
|
| 71 |
|
| 72 | public void startPlaying(final Music music) {
|
| 73 | SoundEvent soundEvent = music.sound().value();
|
| 74 | this.currentMusic = SimpleSoundInstance.forMusic(soundEvent);
|
| 75 | switch (this.minecraft.getSoundManager().play(this.currentMusic)) {
|
| 76 | case STARTED:
|
| 77 | this.minecraft.gui.toastManager().showNowPlayingToast();
|
| 78 | this.toastShown = true;
|
| 79 | break;
|
| 80 | case STARTED_SILENTLY:
|
| 81 | this.toastShown = false;
|
| 82 | }
|
| 83 |
|
| 84 | this.nextSongDelay = Integer.MAX_VALUE;
|
| 85 | }
|
| 86 |
|
| 87 | public void showNowPlayingToastIfNeeded() {
|
| 88 | if (!this.toastShown) {
|
| 89 | this.minecraft.gui.toastManager().showNowPlayingToast();
|
| 90 | this.toastShown = true;
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | public void stopPlaying(final Music music) {
|
| 95 | if (this.isPlayingMusic(music)) {
|
| 96 | this.stopPlaying();
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | public void stopPlaying() {
|
| 101 | if (this.currentMusic != null) {
|
| 102 | this.minecraft.getSoundManager().stop(this.currentMusic);
|
| 103 | this.currentMusic = null;
|
| 104 | this.minecraft.gui.toastManager().hideNowPlayingToast();
|
| 105 | }
|
| 106 |
|
| 107 | this.nextSongDelay = this.gameMusicFrequency.getNextSongDelay(this.minecraft.getSituationalMusic(), this.random) + 100;
|
| 108 | }
|
| 109 |
|
| 110 | private boolean fadePlaying(final float volume) {
|
| 111 | if (this.currentMusic == null) {
|
| 112 | return false;
|
| 113 | }
|
| 114 |
|
| 115 | if (this.currentGain == volume) {
|
| 116 | return true;
|
| 117 | }
|
| 118 |
|
| 119 | if (this.currentGain < volume) {
|
| 120 | this.currentGain = this.currentGain + Mth.clamp(this.currentGain, 5.0E-4F, 0.005F);
|
| 121 | if (this.currentGain > volume) {
|
| 122 | this.currentGain = volume;
|
| 123 | }
|
| 124 | } else {
|
| 125 | this.currentGain = 0.03F * volume + 0.97F * this.currentGain;
|
| 126 | if (Math.abs(this.currentGain - volume) < 1.0E-4F || this.currentGain < volume) {
|
| 127 | this.currentGain = volume;
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | this.currentGain = Mth.clamp(this.currentGain, 0.0F, 1.0F);
|
| 132 | if (this.currentGain <= 1.0E-4F) {
|
| 133 | this.stopPlaying();
|
| 134 | return false;
|
| 135 | } else {
|
| 136 | this.minecraft.getSoundManager().updateCategoryVolume(SoundSource.MUSIC, this.currentGain);
|
| 137 | return true;
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | public boolean isPlayingMusic(final Music music) {
|
| 142 | return this.currentMusic == null ? false : music.sound().value().location().equals(this.currentMusic.getIdentifier());
|
| 143 | }
|
| 144 |
|
| 145 | public @Nullable String getCurrentMusicTranslationKey() {
|
| 146 | if (this.currentMusic != null) {
|
| 147 | Sound sound = this.currentMusic.getSound();
|
| 148 | if (sound != null) {
|
| 149 | return sound.getLocation().toShortLanguageKey();
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | return null;
|
| 154 | }
|
| 155 |
|
| 156 | public void setMinutesBetweenSongs(final MusicManager.MusicFrequency musicFrequency) {
|
| 157 | this.gameMusicFrequency = musicFrequency;
|
| 158 | this.nextSongDelay = this.gameMusicFrequency.getNextSongDelay(this.minecraft.getSituationalMusic(), this.random);
|
| 159 | }
|
| 160 |
|
| 161 | @Environment(EnvType.CLIENT)
|
| 162 | public enum MusicFrequency implements StringRepresentable {
|
| 163 | DEFAULT("DEFAULT", "options.music_frequency.default", 20),
|
| 164 | FREQUENT("FREQUENT", "options.music_frequency.frequent", 10),
|
| 165 | CONSTANT("CONSTANT", "options.music_frequency.constant", 0);
|
| 166 |
|
| 167 | public static final Codec<MusicManager.MusicFrequency> CODEC = StringRepresentable.fromEnum(MusicManager.MusicFrequency::values);
|
| 168 | private final String name;
|
| 169 | private final int maxFrequency;
|
| 170 | private final Component caption;
|
| 171 |
|
| 172 | MusicFrequency(final String name, final String translationKey, final int maxFrequencyMinutes) {
|
| 173 | this.name = name;
|
| 174 | this.maxFrequency = maxFrequencyMinutes * 1200;
|
| 175 | this.caption = Component.translatable(translationKey);
|
| 176 | }
|
| 177 |
|
| 178 | private int getNextSongDelay(final @Nullable Music music, final RandomSource random) {
|
| 179 | if (music == null) {
|
| 180 | return this.maxFrequency;
|
| 181 | }
|
| 182 |
|
| 183 | if (this == CONSTANT) {
|
| 184 | return 100;
|
| 185 | }
|
| 186 |
|
| 187 | int minFrequency = Math.min(music.minDelay(), this.maxFrequency);
|
| 188 | int maxFrequency = Math.min(music.maxDelay(), this.maxFrequency);
|
| 189 | return Mth.nextInt(random, minFrequency, maxFrequency);
|
| 190 | }
|
| 191 |
|
| 192 | public Component caption() {
|
| 193 | return this.caption;
|
| 194 | }
|
| 195 |
|
| 196 | @Override
|
| 197 | public String getSerializedName() {
|
| 198 | return this.name;
|
| 199 | }
|
| 200 | }
|
| 201 | }
|
| 202 |
|