1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-09-01 01:57:55 +00:00

Make SoundEngine#play mixin cancellable.

No reason to call channel.play() twice.
This commit is contained in:
ToadDev
2021-12-16 01:56:36 -08:00
parent 6754c7bb69
commit e6edee4c58
2 changed files with 9 additions and 6 deletions

View File

@@ -20,14 +20,16 @@ public class SpeakerManager
{
private static final Map<UUID, SpeakerInstance> 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 )

View File

@@ -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(