diff --git a/src/main/kotlin/com/enginemachiner/honkytones/Entity.kt b/src/main/kotlin/com/enginemachiner/honkytones/Entity.kt index 682b41b..6bb1486 100644 --- a/src/main/kotlin/com/enginemachiner/honkytones/Entity.kt +++ b/src/main/kotlin/com/enginemachiner/honkytones/Entity.kt @@ -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 ) diff --git a/src/main/kotlin/com/enginemachiner/honkytones/sound/AudioStreaming.kt b/src/main/kotlin/com/enginemachiner/honkytones/sound/AudioStreaming.kt index 73a07a4..f1e3083 100644 --- a/src/main/kotlin/com/enginemachiner/honkytones/sound/AudioStreaming.kt +++ b/src/main/kotlin/com/enginemachiner/honkytones/sound/AudioStreaming.kt @@ -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 @@ -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) { @@ -145,8 +150,8 @@ 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() { super.tick(); if ( isStopping() ) return; setNBTVolume() diff --git a/src/main/kotlin/com/enginemachiner/honkytones/sound/Sound.kt b/src/main/kotlin/com/enginemachiner/honkytones/sound/Sound.kt index 01e044e..044519b 100644 --- a/src/main/kotlin/com/enginemachiner/honkytones/sound/Sound.kt +++ b/src/main/kotlin/com/enginemachiner/honkytones/sound/Sound.kt @@ -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 @@ -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 @@ -91,17 +89,20 @@ 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() + } fun setPitch(f: Float) { pitch = f }