From e6edee4c58fb6e9bce5eea2f400179e40d1615a3 Mon Sep 17 00:00:00 2001 From: ToadDev <748280+Toad-Dev@users.noreply.github.com> Date: Thu, 16 Dec 2021 01:56:36 -0800 Subject: [PATCH] Make SoundEngine#play mixin cancellable. No reason to call channel.play() twice. --- .../dan200/computercraft/client/sound/SpeakerManager.java | 8 +++++--- .../computercraft/fabric/mixin/MixinSoundEngine.java | 7 ++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/dan200/computercraft/client/sound/SpeakerManager.java b/src/main/java/dan200/computercraft/client/sound/SpeakerManager.java index ebc0f3bf5..4759b4176 100644 --- a/src/main/java/dan200/computercraft/client/sound/SpeakerManager.java +++ b/src/main/java/dan200/computercraft/client/sound/SpeakerManager.java @@ -20,14 +20,16 @@ public class SpeakerManager { private static final Map sounds = new ConcurrentHashMap<>(); - public static void playStreaming( SoundInstance soundInstance, Channel channel ) + // A return value of true cancels the event + public static boolean playStreaming( SoundInstance soundInstance, Channel channel ) { - if( !(soundInstance instanceof SpeakerSound) ) return; + if( !(soundInstance instanceof SpeakerSound) ) return false; SpeakerSound sound = (SpeakerSound) soundInstance; - if( sound.stream == null ) return; + if( sound.stream == null ) return false; channel.attachBufferStream( sound.stream ); channel.play(); + return true; } public static SpeakerInstance getSound( UUID source ) diff --git a/src/main/java/dan200/computercraft/fabric/mixin/MixinSoundEngine.java b/src/main/java/dan200/computercraft/fabric/mixin/MixinSoundEngine.java index 4893ca677..ead3e7c56 100644 --- a/src/main/java/dan200/computercraft/fabric/mixin/MixinSoundEngine.java +++ b/src/main/java/dan200/computercraft/fabric/mixin/MixinSoundEngine.java @@ -19,18 +19,19 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin( SoundEngine.class ) public class MixinSoundEngine { - // Used to capture the SoundInstance argument passed to SoundEngine#Play. Not a thread-safe way to do it but + // Used to capture the SoundInstance argument passed to SoundEngine#play. Not a thread-safe way to do it but // this code is only called from the render thread as far as I can tell. @Unique private static SoundInstance onPlaySoundInstanceCapture; @Inject( method = "lambda$play$8", - at = @At( "TAIL" ) + at = @At( "HEAD" ), + cancellable = true ) private static void onStreamingSourcePlay( AudioStream audioStream, Channel channel, CallbackInfo ci ) { - SpeakerManager.playStreaming( onPlaySoundInstanceCapture, channel ); + if( SpeakerManager.playStreaming( onPlaySoundInstanceCapture, channel ) ) ci.cancel(); } @Inject(