Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.21] Select Music Event #862

Merged
merged 22 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
66545fe
Select Music Event
Zepalesque Apr 26, 2024
73b60ac
Optimized patches and make a small fix
Zepalesque Apr 26, 2024
9ff4ba6
Optimize patches and revamp documentation
Zepalesque Apr 27, 2024
a0524cb
Simplify music event javadocs a bit
Zepalesque Apr 27, 2024
a2bc17c
forgot to do formatting and stuff
Zepalesque Apr 27, 2024
640ee7f
fix: avoid unintended behavior where the music delay may go below 0
Zepalesque May 4, 2024
90ac34e
Merge branch '1.21.x' into select-music-event
Zepalesque Jun 16, 2024
e927bfb
Merge branch '1.21.x' into select-music-event
Zepalesque Jun 17, 2024
1a1c33d
feat: Rework event, with new documentation and tweaked behavior
Zepalesque Jun 17, 2024
551a73f
Patches (not sure why others were changed? I haven't made any changes…
Zepalesque Jun 17, 2024
b91bd8f
Merge branch '1.21.x' into select-music-event
Zepalesque Jun 17, 2024
aaa2521
improv: optimize patches
Zepalesque Jun 18, 2024
6310a70
fix: remove unintentional patch issues from merge
Zepalesque Jun 18, 2024
96ad5c1
fix: overrideMusic method which cancels the event, and remove if logi…
Zepalesque Jun 18, 2024
f1b2067
docs: update documentation
Zepalesque Jun 18, 2024
ccc5ba9
fix: fixes
Zepalesque Jun 18, 2024
327c0a4
refactor: tiny change; make spacing consistent with vanilla
Zepalesque Jun 18, 2024
95aa305
Merge branch '1.21.x' into select-music-event
Zepalesque Jun 20, 2024
6b1c286
Merge branch '1.21.x' into select-music-event
Zepalesque Jun 20, 2024
8785b87
Merge branch '1.21.x' into select-music-event
Zepalesque Jun 28, 2024
e239b6f
Merge branch '1.21.x' into select-music-event
TelepathicGrunt Jul 7, 2024
37823eb
Fix typo in comment
TelepathicGrunt Jul 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions patches/net/minecraft/client/sounds/MusicManager.java.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--- a/net/minecraft/client/sounds/MusicManager.java
+++ b/net/minecraft/client/sounds/MusicManager.java
@@ -24,7 +_,15 @@
}

public void tick() {
- Music music = this.minecraft.getSituationalMusic();
+ Music music = net.neoforged.neoforge.client.ClientHooks.selectMusic(this.minecraft.getSituationalMusic(), this.currentMusic);
Zepalesque marked this conversation as resolved.
Show resolved Hide resolved
+ if (music == null) {
Zepalesque marked this conversation as resolved.
Show resolved Hide resolved
+ if (this.currentMusic != null) {
+ this.stopPlaying();
+ }
+ this.nextSongDelay = 0;
+ return;
+ }
+
if (this.currentMusic != null) {
if (!music.getEvent().value().getLocation().equals(this.currentMusic.getLocation()) && music.replaceCurrentMusic()) {
this.minecraft.getSoundManager().stop(this.currentMusic);
9 changes: 9 additions & 0 deletions src/main/java/net/neoforged/neoforge/client/ClientHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ReloadableResourceManager;
import net.minecraft.sounds.Music;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
Expand Down Expand Up @@ -164,6 +165,7 @@
import net.neoforged.neoforge.client.event.RenderTooltipEvent;
import net.neoforged.neoforge.client.event.ScreenEvent;
import net.neoforged.neoforge.client.event.ScreenshotEvent;
import net.neoforged.neoforge.client.event.SelectMusicEvent;
import net.neoforged.neoforge.client.event.TextureAtlasStitchedEvent;
import net.neoforged.neoforge.client.event.ToastAddEvent;
import net.neoforged.neoforge.client.event.ViewportEvent;
Expand Down Expand Up @@ -392,6 +394,13 @@ public static SoundInstance playSound(SoundEngine manager, SoundInstance sound)
return e.getSound();
}

@Nullable
public static Music selectMusic(Music situational, @Nullable SoundInstance playing) {
SelectMusicEvent e = new SelectMusicEvent(situational, playing);
NeoForge.EVENT_BUS.post(e);
return e.getMusic();
}

public static void drawScreen(Screen screen, GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
guiGraphics.pose().pushPose();
guiLayers.forEach(layer -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.client.event;

import net.minecraft.client.resources.sounds.SoundInstance;
import net.minecraft.sounds.Music;
import net.neoforged.bus.api.Event;
import net.neoforged.bus.api.ICancellableEvent;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.LogicalSide;
import net.neoforged.neoforge.common.NeoForge;
import org.jetbrains.annotations.Nullable;

/**
* Fired when the {@link net.minecraft.client.sounds.MusicManager} checks what situational music should be used. This fires before the music begins playing.<br>
* If the music is set to {@code null} by a modder, it will cancel any music that was already playing.<br>
* <br>
* Note that the higher priority you make your event listener, the earlier the music will be set.<br>
* Because of this, if you want your music to take precedence over others (perhaps you want to have seperate nighttime music for a biome for instance) then you may want it to have a lower priority.<br>
* <br>
* To make your music instantly play rather than waiting for the playing music to stop, set the music to one that {@linkplain Music#replaceCurrentMusic() is set to replace the current music.}<br>
* <br>
* Higher priorities would likely be better suited for biome-based or dimension-based musics, whereas lower priority is likely good for specific structures or situations.<br>
* <br>
* This event is {@linkplain ICancellableEvent cancellable}, and does not {@linkplain HasResult have a result}.<br>
* If the event is canceled, then whatever the latest music set was will be used as the music.
* <br>
* This event is fired on the {@linkplain NeoForge#EVENT_BUS main Forge event bus},<br>
* only on the {@linkplain LogicalSide#CLIENT logical client}.
*
*/
public class SelectMusicEvent extends Event implements ICancellableEvent {
private @Nullable Music music;
private final Music originalMusic;
private final @Nullable SoundInstance playingMusic;

public SelectMusicEvent(Music music, @Nullable SoundInstance playingMusic) {
this.music = music;
this.originalMusic = music;
this.playingMusic = playingMusic;
}

/**
* {@return the original situational music that was selected}
*/
public Music getOriginalMusic() {
return originalMusic;
}

/**
* {@return the current track that the {@link net.minecraft.client.sounds.MusicManager} is playing, or {@code null} if there is none}
*/
@Nullable
public SoundInstance getPlayingMusic() {
return playingMusic;
}

/**
* {@return the Music to be played, or {@code null} if any playing music should be cancelled}
*/
@Nullable
public Music getMusic() {
return music;
}

/**
* Changes the situational music. If this is set to {@code null}, any currently playing music will be cancelled.<br>
* If this <i>was</i> {@code null} but on the next tick isn't, the music given will be immediately played.<br>
* <br>
*/
public void setMusic(@Nullable Music newMusic) {
this.music = newMusic;
}

/**
* Sets the music and then cancels the event so that other listeners will not be invoked.<br>
* Note that listeners using {@link SubscribeEvent#receiveCanceled()} will still be able to override this, but by default they will not
*/
public void overrideMusic(@Nullable Music newMusic) {
this.music = newMusic;
this.setCanceled(true);
}
}
Loading