Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.15.2] Playing music


Recommended Posts

1 hour ago, vemerion said:

You could register your own SoundEvents as detailed here, and then for example play those SoundEvents (your music) in a tick event at random.

I have already registered all the sound events, but i have never worked with tick events. Could you explain me how it works or give an example code?

Link to post
Share on other sites
39 minutes ago, kemika1girl said:

example code

This would play your music once every 1000 ticks (every 50 seconds):

private static int musicTimer = 1000;
	
	@SubscribeEvent
	public static void onTick(ClientTickEvent event) {
		if (event.phase == Phase.END && event.side == LogicalSide.CLIENT) {
			if (musicTimer-- <= 0 && Minecraft.getInstance() != null && Minecraft.getInstance().player != null) {
				musicTimer = 1000;
				Minecraft.getInstance().player.playSound(YOUR MUSIC HERE, 2f, 1f);
				Minecraft.getInstance().getMusicTicker().stop();
			}
		}
	}

 

Although I think the vanilla Minecraft music could potentially start while your music is playing, so you would have to handle that also.

Link to post
Share on other sites

@vemerion strange, but seems like nothing happens after i added the code.. did i do something wrong?

@Mod.EventBusSubscriber(modid = Atmospheric.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
@Mod("atmospheric")
public class Atmospheric {
    public static final String MOD_ID = "atmospheric";

    Minecraft minecraft = Minecraft.getInstance();
    private static int musicTimer = 10;

    @SubscribeEvent
    public static void onTick(TickEvent.ClientTickEvent event) {
        if (event.phase == TickEvent.Phase.END && event.side == LogicalSide.CLIENT) {
            if (musicTimer-- <= 0 && Minecraft.getInstance() != null && Minecraft.getInstance().player != null) {
                musicTimer = 10;
                Minecraft.getInstance().player.playSound(AtmosphericSoundEvents.SOLACE_1, 2f, 1f);
                Minecraft.getInstance().getMusicTicker().stop();
            }
        }
    }
}


 

Link to post
Share on other sites
11 minutes ago, kemika1girl said:

strange, but seems like nothing happens after i added the code.. did i do something wrong?

ClientTickEvent is fired on the Forge bus, try changing 

bus = Mod.EventBusSubscriber.Bus.MOD

to

bus = Mod.EventBusSubscriber.Bus.FORGE

 

Link to post
Share on other sites
  • 9 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.