Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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?

  • 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  ;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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.