instead of retrying pausing the sound again and again, is there no way to just call it…
- User
instead of retrying pausing the sound again and again, is there no way to just call it when the sound once the sound started playing? like from SoundEngineLoadEvent or using a mixin that listens when a sound is being played and if so, call the pause sound method. I am already using the following mixin:
package net.flameslight.ithasbeenrpgtweaks.datagen.mixin; import net.flameslight.ithasbeenrpgtweaks.datagen.client.soundgen.ClientMusicHandler; import net.flameslight.ithasbeenrpgtweaks.datagen.client.soundgen.SoundData; import net.minecraft.client.resources.sounds.SoundInstance; import net.minecraft.client.sounds.SoundEngine; import net.minecraft.sounds.SoundSource; import net.minecraft.util.Mth; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @OnlyIn(Dist.CLIENT) @Mixin(SoundEngine.class) public class SoundEngineMixin { // Temporary container for the SoundInstance currently being handled by play(...) private static final ThreadLocal<SoundInstance> CURRENT_PLAY_INSTANCE = new ThreadLocal<>(); // Capture the playing instance at the head of play(SoundInstance) @Inject(method = "play", at = @At("HEAD")) private void beforePlay(SoundInstance soundInstance, CallbackInfo ci) { CURRENT_PLAY_INSTANCE.set(soundInstance); } @Redirect( method = "play", at = @At( value = "INVOKE", // target is the calculateVolume(float, SoundSource) invocation used inside play(...) target = "Lnet/minecraft/client/sounds/SoundEngine;calculateVolume(FLnet/minecraft/sounds/SoundSource;)F" ) ) private float redirectCalculateVolume(SoundEngine engine, float vanillaVolumeArg, SoundSource source) { SoundInstance current = CURRENT_PLAY_INSTANCE.get(); if (current != null) { SoundData sd = ClientMusicHandler.getTrackedSound(current); if (sd != null) { return sd.getCurrentVolume(); } } // fallback: compute original value using private getVolume(source) float computed = ((SoundEngineInvoker) engine).invokeGetVolume(source) * vanillaVolumeArg; return Mth.clamp(computed, 0.0F, 1.0F); } @Redirect( method = "tickNonPaused", at = @At( value = "INVOKE", target = "Lnet/minecraft/client/sounds/SoundEngine;calculateVolume(Lnet/minecraft/client/resources/sounds/SoundInstance;)F" ) ) private float redirectCalculateVolumeInstance(SoundEngine engine, SoundInstance inst) { SoundData sd = ClientMusicHandler.getTrackedSound(inst); if (sd != null) { return sd.getCurrentVolume(); } // fallback: compute original behaviour inline: // original: return this.calculateVolume(inst.getVolume(), inst.getSource()); float vol = inst.getVolume(); SoundSource source = inst.getSource(); float computed = ((SoundEngineInvoker) engine).invokeGetVolume(source) * vol; return Mth.clamp(computed, 0.0f, 1.0f); } // Clear the ThreadLocal at the end of play(...) @Inject(method = "play", at = @At("RETURN")) private void afterPlay(SoundInstance soundInstance, CallbackInfo ci) { CURRENT_PLAY_INSTANCE.remove(); } @Inject(method = "stop(Lnet/minecraft/client/resources/sounds/SoundInstance;)V", at = @At("HEAD")) private void onStop(SoundInstance soundInstance, CallbackInfo ci) { ClientMusicHandler.removeTrackedSoundBySoundInstance(soundInstance); } } Assistant
You're already using a mixin to modify the
SoundEnginebehavior. You can leverage this mixin to pause the sound when it starts playing.One way to achieve this is by injecting a method call at the beginning of the
playmethod inSoundEngine. Here's how you can modify your existing mixin:@OnlyIn(Dist.CLIENT) @Mixin(SoundEngine.class) public class SoundEngineMixin { // ... existing code ... @Inject(method = "play", at = @At("HEAD")) private void beforePlay(SoundInstance soundInstance, CallbackInfo ci) { CURRENT_PLAY_INSTANCE.set(soundInstance); // Call the pause logic here ClientMusicHandler.pauseSoundIfNecessary(soundInstance); } // ... existing code ... }Then, in your
ClientMusicHandlerclass, add thepauseSoundIfNecessarymethod:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.