-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathAetherMusicManager.java
233 lines (214 loc) · 10.4 KB
/
AetherMusicManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package com.aetherteam.aether.client;
import com.aetherteam.aether.AetherConfig;
import com.aetherteam.aether.AetherTags;
import com.aetherteam.aether.api.AetherMenus;
import com.aetherteam.aether.client.event.hooks.GuiHooks;
import com.aetherteam.aether.client.sound.MusicSoundInstance;
import com.aetherteam.aether.entity.AetherBossMob;
import com.aetherteam.aether.mixin.mixins.client.accessor.BossHealthOverlayAccessor;
import com.aetherteam.cumulus.api.Menus;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.LerpingBossEvent;
import net.minecraft.client.gui.screens.WinScreen;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.client.resources.sounds.SoundInstance;
import net.minecraft.client.sounds.MusicManager;
import net.minecraft.client.sounds.SoundManager;
import net.minecraft.core.Holder;
import net.minecraft.sounds.Music;
import net.minecraft.sounds.Musics;
import net.minecraft.tags.BiomeTags;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.biome.Biome;
import javax.annotation.Nullable;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* This class is used to replace the menu screen music when applicable, and replace the creative mode music when in an Aether biome.
*/
public class AetherMusicManager {
private static final int FADE_LIMIT = 50;
private static final RandomSource random = RandomSource.create();
private static final Minecraft minecraft = Minecraft.getInstance();
private static final MusicManager musicManager = Minecraft.getInstance().getMusicManager();
@Nullable
private static SoundInstance currentMusic;
private static int nextSongDelay = 100;
private static Integer fade = null;
/**
* [CODE COPY] - {@link MusicManager#tick()}.<br><br>
* Modified to have a {@link Music} null check.
*/
public static void tick() {
Music music = getSituationalMusic();
if (currentMusic instanceof MusicSoundInstance musicSoundInstance) {
if (musicSoundInstance.isBossMusic()) {
if (music == null || !isAetherBossMusic(music)) {
if (fade == null) {
fade = 0;
}
musicSoundInstance.setVolume((float) Math.exp(-(fade / (FADE_LIMIT / 3.0))));
fade++;
if (fade >= FADE_LIMIT) {
fade = null;
minecraft.getSoundManager().stop(currentMusic);
currentMusic = null;
nextSongDelay = Math.min(Integer.MAX_VALUE, Mth.nextInt(random, AetherConfig.CLIENT.music_backup_min_delay.get(), AetherConfig.CLIENT.music_backup_max_delay.get() / 2)); // End boss music
}
}
} else {
if (music != null && isAetherBossMusic(music)) {
if (fade == null) {
fade = 0;
}
musicSoundInstance.setVolume((float) Math.exp(-(fade / (FADE_LIMIT / 3.0))));
fade++;
if (fade >= FADE_LIMIT) {
fade = null;
minecraft.getSoundManager().stop(currentMusic);
currentMusic = null;
nextSongDelay = FADE_LIMIT / 3; // Start boss music
}
}
}
}
if (music != null) {
if (currentMusic != null) {
if (fade == null) {
if (!music.getEvent().value().getLocation().equals(currentMusic.getLocation()) && music.replaceCurrentMusic()) {
minecraft.getSoundManager().stop(currentMusic); // Non-copy, cancels vanilla music if Aether music starts
nextSongDelay = Mth.nextInt(random, 0, music.getMinDelay() / 2);
}
if (!minecraft.getSoundManager().isActive(currentMusic)) {
currentMusic = null;
nextSongDelay = Math.min(nextSongDelay, Mth.nextInt(random, music.getMinDelay(), music.getMaxDelay()));
}
}
}
nextSongDelay = Math.min(nextSongDelay, music.getMaxDelay());
if (currentMusic == null && nextSongDelay-- <= 0) {
startPlaying(music);
}
} else {
if (currentMusic == null || !minecraft.getSoundManager().isActive(currentMusic)) {
currentMusic = null;
if (nextSongDelay-- <= 0) {
nextSongDelay = Math.min(Integer.MAX_VALUE, Mth.nextInt(random, AetherConfig.CLIENT.music_backup_min_delay.get(), AetherConfig.CLIENT.music_backup_max_delay.get()));
}
}
}
}
/**
* [CODE COPY] - {@link MusicManager#startPlaying(Music)}.
*/
public static void startPlaying(Music music) {
musicManager.stopPlaying(); // Non-copy, cancels vanilla music if Aether music starts
if (isAetherBossMusic(music)) {
currentMusic = MusicSoundInstance.forBossMusic(music.getEvent().value());
} else {
currentMusic = MusicSoundInstance.forMusic(music.getEvent().value());
}
if (currentMusic.getSound() != SoundManager.EMPTY_SOUND) {
minecraft.getSoundManager().play(currentMusic);
}
nextSongDelay = Integer.MAX_VALUE;
}
/**
* [CODE COPY] - {@link MusicManager#stopPlaying()}.
*/
public static void stopPlaying() {
if (currentMusic != null) {
minecraft.getSoundManager().stop(currentMusic); // Non-copy, cancels vanilla music if Aether music starts
currentMusic = null;
}
nextSongDelay += 100;
}
@Nullable
public static SoundInstance getCurrentMusic() {
return currentMusic;
}
/**
* Determines when to play different music.
*
* @return The {@link Music} to play.
*/
@Nullable
public static <T extends LivingEntity & AetherBossMob<?>> Music getSituationalMusic() {
if (!(minecraft.screen instanceof WinScreen)) {
if (isAetherWorldPreviewEnabled()) { // Play Aether menu music when the Aether menu world preview is enabled.
return AetherMenus.THE_AETHER.get().getMusic();
} else if (isVanillaWorldPreviewEnabled()) { // Play Minecraft menu music when the Minecraft menu world preview is enabled.
return Menus.MINECRAFT.get().getMusic();
} else if (minecraft.player != null) { // Otherwise replace creative music with biome music in the Aether.
if (isAetherBossMusicActive()) {
T boss = getBossFromFight();
if (boss != null && boss.getHealth() > 0) {
Music bossMusic = boss.getBossMusic();
if (bossMusic != null) {
return boss.getBossMusic();
}
}
} else {
Holder<Biome> holder = minecraft.player.level().getBiome(minecraft.player.blockPosition());
if (isCreative(holder, minecraft.player)) {
return (holder.value().getBackgroundMusic().orElse(Musics.GAME));
}
}
}
}
return null;
}
public static boolean isAetherBossMusic(Music music) {
return music.getEvent().is(AetherTags.SoundEvents.BOSS_MUSIC);
}
public static boolean isAetherBossMusicActive() {
return !AetherConfig.CLIENT.disable_aether_boss_music.get() && !getAetherBossFights().isEmpty() && minecraft.gui.getBossOverlay().shouldPlayMusic();
}
public static Map<UUID, LerpingBossEvent> getAetherBossFights() {
return ((BossHealthOverlayAccessor) minecraft.gui.getBossOverlay()).getEvents().entrySet().stream().filter((entry) -> GuiHooks.isAetherBossBar(entry.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
public static <T extends LivingEntity & AetherBossMob<?>> T getBossFromFight() {
for (Map.Entry<UUID, LerpingBossEvent> event : getAetherBossFights().entrySet()) {
UUID eventUUID = event.getKey();
int entityId = GuiHooks.BOSS_EVENTS.get(eventUUID);
Entity entity = minecraft.player.level().getEntity(entityId);
if (entity instanceof LivingEntity && entity instanceof AetherBossMob<?>) {
return (T) entity;
}
}
return null;
}
/**
* @return Whether the world preview is enabled for an Aether menu, and the music isn't cancelled through {@link AetherConfig.Client#disable_aether_world_preview_menu_music}.
*/
public static boolean isAetherWorldPreviewEnabled() {
return AetherMenuUtil.isAetherMenu() && isWorldPreviewEnabled() && !AetherConfig.CLIENT.disable_aether_world_preview_menu_music.get();
}
/**
* @return Whether the world preview is enabled for a Minecraft menu, and the music isn't cancelled through {@link AetherConfig.Client#disable_vanilla_world_preview_menu_music}.
*/
public static boolean isVanillaWorldPreviewEnabled() {
return AetherMenuUtil.isMinecraftMenu() && isWorldPreviewEnabled() && !AetherConfig.CLIENT.disable_vanilla_world_preview_menu_music.get();
}
/**
* @return Whether the world preview is enabled, according to {@link WorldDisplayHelper#isActive()}, and if the player exists.
*/
public static boolean isWorldPreviewEnabled() {
return minecraft.player != null && WorldDisplayHelper.isActive();
}
/**
* [CODE COPY] - {@link Minecraft#getSituationalMusic()}.<br><br>
* Based on vanilla creative music checks, but also checks if the biome plays Aether music.
*/
public static boolean isCreative(Holder<Biome> holder, Player player) {
return player.level().dimension() != Level.END && player.level().dimension() != Level.NETHER && holder.is(AetherTags.Biomes.AETHER_MUSIC)
&& !musicManager.isPlayingMusic(Musics.UNDER_WATER) && (!player.isUnderWater() || !holder.is(BiomeTags.PLAYS_UNDERWATER_MUSIC))
&& player.getAbilities().instabuild && player.getAbilities().mayfly;
}
}