Posted May 21, 201411 yr I have a small mod that I want to be client-side only. I want the mod to play a short sound when some matching text is seen in the player's chat. I have the mod working when in single player mode (even when it is "opened to LAN") but when I connect to a server on another host the sound is not heard. I have a class which receives ClientChatReceivedEvent events. In the event handler method I play the sound if the text matches what I am looking for: public class ChatEventHandler { net.minecraft.world.World worldClient; @SubscribeEvent @SideOnly(Side.Client) public void onChatMessage(ClientChatReceivedEvent) { // check for matching text if(event.message.getUnformattedText.beginsWith("blabla")) { EntityClientPlayer player = Minecraft.getMinecraft().thePlayer; if(worldClient != null) worldClient.playSoundAtEntity(player, "mymod:chatsound", 1.0f, 1.0f); else player.worldObj.playSoundAtEntity(player, "mymod:chatsound", 1.0f, 1.0f); } } @SubscribeEvent @SideOnly(Side.Client) public void onWorldEvent(WorldEvent.Load event) { if((event.world != null) && !event.world.isRemote) worldClient = event.world; } } When the onWorldEvent handler is called and the worldClient member gets set with a world that is not remote then the sound plays. That is what happens when in single player mode. When I connect to a remote server worldClient never gets set and Minecrat.thePlayer.worldObj is used to play the sound. That world instance is remote and that is (I believe) where the problem is. So is there a consistent way to play a short sound on the client?
May 22, 201411 yr When the world is not remote, that means you are on the server. If you just want the sound to play on the client, there is a player.playSound method that you should use - no one but the player in question will hear the sound. http://i.imgur.com/NdrFdld.png[/img]
May 22, 201411 yr Author When the world is not remote, that means you are on the server. If you just want the sound to play on the client, there is a player.playSound method that you should use - no one but the player in question will hear the sound. Thank you coolAlias. EntityPlayerSP.playSound is what I needed to get it working. In this case I only want the one player hearing the sound, so that method is perfect. I will send you some karma (assuming I can figure out how ).
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.