Jump to content

[1.11.2] Getting local sound to play on some kind of timer


Recommended Posts

Hello MinecraftForge forum users,


I have been struggling like crazy to get an item that's simple in theory to exist, but it appears the constraints of what you can and cannot do within Minecraft is a bit inconsistent or unexpected for me.

I'm hoping those with more experience can help me out, and thank you for taking the time to help if you do!


I'm trying to create a piece of armor that will play music for the user while its being worn for the local user only. I have tried handling this problem using the EventHandler and in the ModArmor classes of my mod and I can't find a best way of doing this.


Here is my, what I thought to have been my closest attempt yet:

    public void onUpdate(ItemStack itemStack, World world, Entity entity, int slot, boolean inHand)
        if (checkForPartyGlasses((EntityPlayer)entity)&&entity instanceof EntityPlayer)
    			System.out.println("Play music");
    			entity.playSound(ModMusic.partyMusic, 100, 1);

I took some inspiration from my coding in Verilog to make some type of clock counter. The item itself has a partyCount property initialized to 0 (I'm also assuming that this is unique for all players with this item in a server scenario).

I am checking for both 0 and 1 to start playing because of what I presume to be multi threading causing the actual playSound method not to call when the print statement for play music to not. 


The main idea here is that I want to have some kind of counter, counting the number of ticks that have passed and after a certain duration have passed, it will replay the sound again.



Where I read onUpdate is called every tick is FALSE. It is called much more frequently and variable to the number of PartyGlasses items you have, also showing that the this.partyCount is only unique to all instances of PartyGlasses as opposed to each individual item instance. With one item it's called about 45-60 times a second. 

In the line:


The game will crash entirely even though it is being called, at most, twice per full song cycle. I am unsure why this is illegal to be called in the ModArmor class. 

What I am trying to do:

I would like for the song to be played every 618 ticks (20/second) if the player is wearing the glasses and only to the player.


Thanks for reading, and any help/direction is greatly appreciated!


COMPLETE SIDE QUESTION: Is there an easy way to have animated armor textures (ie. using .mcmeta files like for item textures)?

Link to comment
Share on other sites

	public void onClientTick(ClientTickEvent event){
			System.out.println("Play music here!");
			System.out.println("Reset at "+secondCounter);
			secondCounter = 0;
		if(event.phase == Phase.START){
			System.out.println("We are in phase start at "+secondCounter+" ticks.");
		if(event.phase == Phase.END){
			System.out.println("We are in phase end at "+secondCounter+" ticks.");
		if(event.phase != Phase.END&&event.phase != Phase.START){
			System.out.println("We are in an unknown phase.");

So from what I'm seeing from playing around with this here is, that there are 40 ticks per second (which was just me being dumb), Minecraft is always calling the onClientTick even when the game itself isn't "playing" (like at the menus), and these START and END phases are the only two phases in which is goes through unless forced into another custom phase. They kind of act like a rising/falling edge of clock and the frequency is changing from START to END every tick. I know this is bad for the game itself, creating lag from flushing the print buffer so timing isn't perfect, but close enough. I will try fudging some code in here and return to discuss potential issues/successes. Thanks a bunch diesieben07!


Link to comment
Share on other sites

So I would need to create a music ticker to call the playMusic() method. The question becomes how do I call this if I shouldn't utilize the timing generated from the ClientTickEvent method? The client tick event itself cannot determine whether or not a player is wearing armor either, where as a playerOnTick event can. I would imagine I can put the music ticker play music method call in the checkForPartyGlasses block of the onPlayerTick method, but wouldn't this be called, like a lot of times and calling playMusic seems to stop the current music if its the same as the one its trying to play to start it again?

Link to comment
Share on other sites

1 hour ago, diesieben07 said:

Again, look at the MusicTicker class.


Of course it can.

I would love to see how a ClientTickEvent itself could fetch information about a specific player's armor slot contents.

Being it has no field data for the player itself.

Link to comment
Share on other sites

5 minutes ago, Heen said:


Which means it's called on the client-side. On the client-side, you can use the Minecraft class which contains a lot of information about the game, including the player and the world he's in.

  • Like 1

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.


1.12 -> 1.13 primer by williewillus.


1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.



Link to comment
Share on other sites

2 minutes ago, larsgerrits said:

Which means it's called on the client-side. On the client-side, you can use the Minecraft class which contains a lot of information about the game, including the player and the world he's in.

That's fairly straight forward, I was just confirming that the tickevent itself holds no information regarding the player. Thanks for the clarification though, I am fairly new to modding and am still trying to determine which side can handle what.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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 Terms of Use.