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?