Jump to content

[1.7.2] Client side sounds not playing when connected to remote server


Recommended Posts

Posted

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?

Posted

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  ;D).

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.

Guest
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.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.