#looping sound on block entity

14 messages · Page 1 of 1 (latest)

daring wedge
#

I'm trying to find an approach to make a looping sound that plays based on conditions from a block entity.
I have a block that processes items much like a furnace that I would like to have a seamless sound loop that can stop and start as the conditions change. Before, I was using SoundInstance but as this is a client only class the mod was unable to be launched on a server. SoundInstance worked perfectly, but I can't figure out how to separate it from a block entity class because it's dependent on the item processing.
does anyone know an alternative approach to achieve this?

#

i had this rough setup before:

public void tickAudio() {
        if(!isNoisy() || world == null){
            return;
        }

        if(isPowered()){
            // TODO: start passive sound
        } else {
            // TODO: stop passive sound
            return;
        }


        if(hasItem()){
            // TODO: start active sound
        } else {
            // TODO: stop active sound
        }
    }

tickAudio() was called after a world.isClient() test but the only way i found to make SoundInstance work was to make it a class variable

#

but making it a class variable forsakes the world.isClient() test, leading to a crash

#

i know of no other way to seamlessly loop a sound event other than SoundInstance, and i know of no other way to update the sound instance than to use my BlockEntity thus why i'm here lol

dreamy hemlock
#

you could use an event system that is invoked. client mod initializer can register the listener

alternatively, you can return a different ticker for client
alternatively alternatively, a custom packet from S2C

daring wedge
#

how would i initalize the sound instance to loop going the event system route? different ticker is probably the easiest route idk why i didnt think of that

#

but i am curious about the event option

dreamy hemlock
#

make an event (standard fabric style event system)
in your client initializer, register a clientside listener to the event
do the clientside code in your listener

when the code runs on client, there will be a listener
when the code runs on server, there wont be a listener so there is nothing to run

#

(thats physical client vs physical server, you still need to sidedness check)

#

instead of triggering from the mixin, you just trigger it from your code

daring wedge
#

and as for the different ticker option, would i set that up in the Block class where i assign the ticker?

dreamy hemlock
#

yeah

daring wedge
#

i can give those a try tomorrow morning. thanks for the ideas!