Skip to content

Commit

Permalink
Sound issues fixes.
Browse files Browse the repository at this point in the history
Fix when music player block is broken and it's sound doesn't fade out.
  • Loading branch information
EngineMachiner committed Oct 4, 2023
1 parent 1aff66a commit 1dd72c2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/com/enginemachiner/honkytones/Entity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ interface CanBeMuted {

fun isMuted(entity: Entity): Boolean {

if ( entity.isRemoved ) return false

if ( entity is MusicPlayerEntity ) {

val musicPlayer = world()!!.getBlockEntity( entity.blockPos )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import MarkErrorInputStream
import com.enginemachiner.honkytones.*
import com.enginemachiner.honkytones.CanBeMuted.Companion.isMuted
import com.enginemachiner.honkytones.blocks.musicplayer.MusicPlayer
import com.enginemachiner.honkytones.blocks.musicplayer.MusicPlayerBlockEntity
import com.squareup.okhttp.OkHttpClient
import com.squareup.okhttp.Request
import net.fabricmc.api.EnvType
Expand Down Expand Up @@ -103,9 +104,13 @@ class ExternalSound( private val musicPlayer: MusicPlayer ) : FadingSound("audio

private var nbtVolume = 1f; private var audioStream: AudioStream? = null

private var blockEntity: MusicPlayerBlockEntity? = null

private fun init() {

entity = musicPlayer.blockEntity!!.entity
blockEntity = musicPlayer.blockEntity

entity = blockEntity!!.entity

try { audioStream = getAudioStream() } catch (e: Exception) {

Expand Down Expand Up @@ -145,7 +150,7 @@ class ExternalSound( private val musicPlayer: MusicPlayer ) : FadingSound("audio

}

override fun fadeOut() { if ( !musicPlayer.blockEntity!!.repeatOnPlay ) super.fadeOut() else stop() }
override fun fadeOut() { if ( !blockEntity!!.repeatOnPlay ) super.fadeOut() else stop() }

override fun tick() {

Expand Down
16 changes: 8 additions & 8 deletions src/main/kotlin/com/enginemachiner/honkytones/sound/Sound.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ open class FadingSound( val path: String ) : MovingSoundInstance(
var entity: Entity? = null; var maxVolume = 1f
private var fadeIn = false; private var fadeOut = false
var shouldNetwork = true; var canReplay = true
private var timesStopped = 0; private var lastVolume: Float? = null
private var timesStopped = 0; private var volumeRate = maxVolume

private var isPlaying = false; protected var pos: Vec3d = Vec3d.ZERO

Expand All @@ -45,15 +45,13 @@ open class FadingSound( val path: String ) : MovingSoundInstance(

private fun getRate(): Float {

if ( lastVolume == null ) lastVolume = volume

return 0.125f * lastVolume!! / ( timesStopped + 1 )
return 0.125f * volumeRate / ( timesStopped + 1 )

}

private fun fadeInTick() {

if ( !fadeIn ) return
if ( !fadeIn || fadeOut ) return

val nextVolume = volume + getRate()
if ( nextVolume < maxVolume ) volume = nextVolume else fadeIn = false
Expand Down Expand Up @@ -91,15 +89,17 @@ open class FadingSound( val path: String ) : MovingSoundInstance(

getManager().stop(this); volume = maxVolume

if ( !canReplay ) setDone(); lastVolume = null
if ( !canReplay ) setDone()

}

fun fadeIn() { fadeIn = true; volume = 0f }
open fun fadeIn() { fadeIn = true; volume = 0f }

open fun fadeOut() {

fadeOut = true; if ( !canNetwork() || !shouldNetwork ) return
fadeOut = true; volumeRate = volume

if ( !canNetwork() || !shouldNetwork ) return

fadeOutOnClients()
}
Expand Down

2 comments on commit 1dd72c2

@EngineMachiner
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And fixes fade in.

@EngineMachiner
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fade in action is not networked so it should be specific if used specifically somewhere.

Please sign in to comment.