AsyncChatEvent

Discussion in 'Plugin Development' started by DoggyCode™, Mar 19, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    DoggyCode™

    Hey Bukkit,

    I'm trying to make this mention plugin, here is what it is supposed to do:

    Code:
        @EventHandler(priority = EventPriority.HIGH)
        public void onChat(AsyncPlayerChatEvent event){
            Player p = event.getPlayer();
            String msg = event.getMessage().toLowerCase();
            for(Player players : Bukkit.getOnlinePlayers()){
                if(msg.contains(players.getName().toLowerCase())){
                    event.getRecipients().remove(players);
                    players.sendMessage(event.getFormat().replaceAll("%1$s", players.getName()).replaceAll("%2$s", event.getMessage().replaceAll(players.getName(), ChatColor.AQUA+"@"+players.getName())));
                    players.playSound(players.getLocation(), Sound.NOTE_PLING, 10.0f, 10.0f);
                }
            }
        }
    Basically, I'm trying to send the player who got mentioned the message, but with their name changed with a @TheirName. But the format thing doesn't work, it just prints out: "%1$s" for the name, and "%2$s" for the message: Any ideas?
    [​IMG]

    Any ways I can fix this?
     
  2. Offline

    Konato_K

    @DoggyCode™ Use String#format instead of replace all, that's what those tokens were meant for.
     
  3. Offline

    Assist

    @DoggyCode™
    Worth mentioning that the reason why your code doesn't work is because String#replaceAll takes a regular expression, not a CharSequence. "%1$s" is not a valid regex so it's ignored. You'd want to use String#replace instead.

    This code
    Code:
    String exampleString = "Hello %1$s";
    System.out.println(exampleString.replaceAll("%1$s", "world"));
    System.out.println(exampleString.replace("%1$s", "world"));
    provides this output
    Code:
    Hello %1$s
    Hello world
    As mentioned above, you can also use String#format to achieve the correct output.
     
    DoggyCode™ likes this.
  4. Offline

    DoggyCode™

    Thanks

    Hey! I'm back again, I got it working, but I'm still stuck with one thing.
    Code:
    @EventHandler(priority = EventPriority.HIGH)
        public void onChat(AsyncPlayerChatEvent event){
            Player p = event.getPlayer();
            String msg = event.getMessage().toLowerCase();
            for(Player players : Bukkit.getOnlinePlayers()){
                if(!(players == p)){
                    if(msg.contains(players.getName().toLowerCase())){
                        event.getRecipients().remove(players);
                        players.sendMessage(String.format(event.getFormat(), p.getName(), event.getMessage().replaceAll(players.getName(), ChatColor.AQUA + "@" + players.getName())));
                        players.playSound(players.getLocation(), Sound.NOTE_PLING, 100.0f, 1.0f);
                    }
                }
            }
        }
    As you can see, if a player mentions another player in their message, it will send a "PLING" to the player that was mentioned, plus a modified message where their name is AQUA:"@Highlighted". But my issue is, I need to somewhat reset the color after aqua, as the rest of the message is then colored it aqua. I want it back to the default format color.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 20, 2016
  5. Offline

    Ligachamp

    Just add "ChatColor.RESET" as last statement to the message string.
     
Thread Status:
Not open for further replies.

Share This Page