|
1 | 1 | package symbolics.division.meret.mixin.client; |
2 | 2 |
|
3 | | -import com.llamalad7.mixinextras.injector.ModifyExpressionValue; |
4 | 3 | import net.minecraft.client.Minecraft; |
| 4 | +import net.minecraft.client.player.LocalPlayer; |
| 5 | +import net.minecraft.client.sounds.MusicInfo; |
| 6 | +import net.minecraft.sounds.Music; |
5 | 7 | import org.spongepowered.asm.mixin.Mixin; |
| 8 | +import org.spongepowered.asm.mixin.Shadow; |
6 | 9 | import org.spongepowered.asm.mixin.injection.At; |
| 10 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 11 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
7 | 12 | import symbolics.division.meret.Meret; |
8 | 13 | import symbolics.division.meret.client.MeretClient; |
9 | 14 |
|
| 15 | +import java.util.Optional; |
| 16 | + |
10 | 17 | @Mixin(Minecraft.class) |
11 | 18 | public class MinecraftMixin { |
12 | | - @ModifyExpressionValue( |
| 19 | + @Shadow |
| 20 | + private LocalPlayer player; |
| 21 | + |
| 22 | + @Inject( |
13 | 23 | method = "getSituationalMusic", |
14 | | - at = @At(value = "INVOKE", target = "Lnet/minecraft/Optionull;map(Ljava/lang/Object;Ljava/util/function/Function;)Ljava/lang/Object;") |
| 24 | + at = @At( |
| 25 | + value = "INVOKE", |
| 26 | + target = "Lnet/minecraft/client/player/LocalPlayer;level()Lnet/minecraft/world/level/Level;", |
| 27 | + shift = At.Shift.BEFORE), |
| 28 | + cancellable = true |
15 | 29 | ) |
16 | | - public Object meretSituationalMusic(Object original) { |
17 | | - if (original != null) return original; // Preserve screen music. |
18 | | - Minecraft self = (Minecraft) (Object) this; |
19 | | - return MeretClient.getOverride(self.player).orElse(Meret.CONFIG.overrideAll ? MeretClient.EMPTY : null); |
| 30 | + private void meretSituationalMusic(CallbackInfoReturnable<MusicInfo> cir) { |
| 31 | + final Optional<Music> music = MeretClient.getOverride(this.player); |
| 32 | + |
| 33 | + // overrideAll prevents the regular level music from playing. |
| 34 | + if (music.isPresent() || Meret.CONFIG.overrideAll) { |
| 35 | + // A null music instance is safe. |
| 36 | + // The 1.21.6+ MusicManager shorts circuits null as a no-op, max time 100 frames, |
| 37 | + // and the same check has been mimicked in Music Moods. |
| 38 | + cir.setReturnValue(new MusicInfo(music.orElse(null))); |
| 39 | + } |
20 | 40 | } |
21 | 41 | } |
0 commit comments