Solved Play sound when username is mentioned in chat

Discussion in 'Spigot Plugin Development' started by LeeTheENTP, Oct 10, 2015.

  1. I'm trying to create a simple plugin where when a person's username is used in chat, it is highlighted and a sound is played only to that player. I'm working on the sound at the moment, but I'm having a hard time getting the sound to play.

    Here's what I have so far:

    Code (Text):

        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerChat(AsyncPlayerChatEvent chat)
        {
            message = chat.getMessage();
            for(Player p: players)
            {
                if(p.hasPermission("chatping.ping"))
                {
                    if(message.contains(p.getName()))
                    {
                        p.playSound(p.getLocation(), Sound.CAT_MEOW, 1.0F, 1.0F);
                    }
                }
            }
        }
    Where players is defined as Collection<? extends Player> players = Bukkit.getServer().getOnlinePlayers(), message is a String, and the permission defaults to true for debugging purposes.

    When I type my username in chat, nothing happens. No sound is played and there are no errors in the console.
     
  2. Did you register your event? Did you implement Listener?
     
  3. I'm implementing Listener, but what do you mean by registering my event?

    EDIT: I see, I did forget to register the even in the Main class. Let me go do that.
     
  4. If you have the event in a different class, go to your main class and write this:
    Code (Text):

        public void onEnable() {
            PluginManager pm = Bukkit.getServer().getPluginManager();
            pm.registerEvents(new EventName(), this);
        }
    }
    EventName is the class, this is the plugin.
    If you're making it in the main class, do (this,this) instead of (new EventName(), this);
    If you have multiple events, then it's recommended to do the PluginManager pm = Bukkit.getServer().getPluginManager();
    so it's easier to register events.
     
    • Useful Useful x 1
  5. Thanks for the help on that!

    Now, is there a simple way I can color the player's name in the chat message? I know of several ways to find the username, but I'm not sure how to color just the username.
     
  6. Thanks i was trying to look into how to register events in different classes i forgot thanks very much
     
  7. Im pretty sure its (Chatcolor.WhateverColorYouWant + playername)
     
  8. The only problem with that is that it replaces the entire message with the player's colored username. I'm looking to keep the message intact but with the username colored.
     
  9. Easiest way would be replacing the players name with the colored version using String#replace.
     
  10. before the message do a ChatColor.RESET
     
  11. Thanks, guys!
     
  12. One more thing - how can I set it so the username is only colored for the player that it matches, i.e. if CaptainMurica is mentioned in chat, only CaptainMurica will see his name colored?
     
  13. The way I would do this is to send the message manually to each player (looping through online players), if the player is mentioned send them a colored version otherwise send uncolored. There may be more effective way to do this, though.
     
  14. Problem with that is it removes chat formatting.
     
  15. Then you can add it back in manually; that's how I handled things when I made a chat plugin, but I was also much less experienced then, so take my advice on this topic with a grain of salt.
     
  16. I can't hard-code in chat formatting since every server does it differently.
     
  17. I used a config file with placeholders, but that was a standalone chat plugin. If I understand correctly you want your plugin to function alongside other chat plugins, which will require you to handle it differently.
     
  18. Is there a method in AsyncPlayerChatEvent that sends the message? If so, I could send it without the colored name to everyone but the player, then send it colored to the player, then cancel the event.
     
  19. I made a plugin exactly like this but with a configuration file for a server. I forget how I did this, but if I find the plugin I'll try to help. :)
     
  20. I can't tell if this is what you're asking, but you can use event.getMessage() [if I recall correctly] to get the message and event.getPlayer() to get the sender.