Skip to content

Commit

Permalink
Merge pull request #196 from KitCat962/fix-sound-jank
Browse files Browse the repository at this point in the history
fix sound functions breaking when stopped naturally
  • Loading branch information
UnlikePaladin committed Mar 5, 2024
2 parents 599c403 + 5b82f5f commit 723cacc
Showing 1 changed file with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.client.sounds.SoundBufferLibrary;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundSource;

import org.figuramc.figura.avatar.Avatar;
import org.figuramc.figura.avatar.AvatarManager;
import org.figuramc.figura.lua.LuaWhitelist;
Expand Down Expand Up @@ -79,7 +80,8 @@ private float calculateVolume() {
@LuaWhitelist
@LuaMethodDoc("sound.play")
public LuaSound play() {
if (this.playing)
// Only skip logic when we truely know the sound is currently playing
if (this.playing && this.handle != null && !this.handle.isStopped())
return this;

if (!owner.soundsRemaining.use()) {
Expand All @@ -89,6 +91,12 @@ public LuaSound play() {

owner.noPermissions.remove(Permissions.SOUNDS);

// if we still have a handle reference but the handle has been released, remove the reference
// This occurs when sounds naturally stops
if (this.handle != null && this.handle.isStopped()) {
this.handle = null;
}

// if handle exists, the sound was previously played. Unpause it
if (handle != null) {
handle.execute(Channel::unpause);
Expand Down Expand Up @@ -185,8 +193,18 @@ public LuaSound play() {
@LuaWhitelist
@LuaMethodDoc("sound.is_playing")
public boolean isPlaying() {
if (handle != null)
handle.execute(channel -> this.playing = channel.playing());
if (handle != null) {
// If the handle was released via the sound stopping naturlly, force set to false.
// When a handle is stopped, it has no channel, so playing will not update.
if (handle.isStopped())
this.playing = false;
// set playing based on whether the sound is paused or not.
else
handle.execute(channel -> this.playing = channel.playing());
}
// If there is no handle, forcefully set it to false just incase
else
this.playing = false;
return this.playing;
}

Expand Down

0 comments on commit 723cacc

Please sign in to comment.