|
| 1 | +package dev.lycanea.mwonmod.music; |
| 2 | + |
| 3 | +import net.minecraft.client.MinecraftClient; |
| 4 | +import net.minecraft.client.sound.SoundInstance; |
| 5 | +import net.minecraft.sound.SoundEvent; |
| 6 | +import net.minecraft.util.Identifier; |
| 7 | + |
| 8 | +public class CustomMusicManager { |
| 9 | + private static CustomSong currentSong = CustomSong.NONE; |
| 10 | + private static CustomSong playingSong = CustomSong.NONE; |
| 11 | + private static SoundInstance currentSound = null; |
| 12 | + |
| 13 | + public static void setCurrentSong(CustomSong song) { |
| 14 | + currentSong = song; |
| 15 | + } |
| 16 | + |
| 17 | + public static void tick(MinecraftClient client) { |
| 18 | +// client.options.getSoundVolumeOption(SoundCategory.MUSIC).setValue(0.0); |
| 19 | + |
| 20 | + if (currentSong != playingSong) { |
| 21 | + stopCurrentSong(client); |
| 22 | + |
| 23 | + if (currentSong != CustomSong.NONE) { |
| 24 | + playSong(client, currentSong); |
| 25 | + } |
| 26 | + |
| 27 | + playingSong = currentSong; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private static void playSong(MinecraftClient client, CustomSong song) { |
| 32 | + Identifier soundId = song.getSoundId(); |
| 33 | + if (soundId == null) return; |
| 34 | + |
| 35 | + SoundEvent soundEvent = SoundEvent.of(soundId); |
| 36 | + LoopingSoundInstance sound = new LoopingSoundInstance(soundEvent); |
| 37 | + currentSound = sound; |
| 38 | + |
| 39 | + client.getSoundManager().play(sound); |
| 40 | + } |
| 41 | + |
| 42 | + public static void stopCurrentSong(MinecraftClient client) { |
| 43 | + if (currentSound != null) { |
| 44 | + if (currentSound instanceof LoopingSoundInstance looping) { |
| 45 | + looping.stop(); |
| 46 | + } |
| 47 | + client.getSoundManager().stop(currentSound); |
| 48 | + currentSound = null; |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments