Player Mention

Discussion in 'Spigot Plugin Development' started by SmaxoS, Aug 8, 2018.

  1. Hi, I have this code:
    Code (Java):
    for(Player pl : Bukkit.getOnlinePlayers()) {

                if(msg.contains(pl.getName())) {
                          msg = msg.replace(pl.getName(), "§9" + pl.getName() + "§7");
                          pl.playSound(pl.getLocation(), Sound.BLOCK_ANVIL_DESTROY, 1.0F, 1.0F);
                }

    }
    but nick color in chat show only who I mention, other players cannot see that. Why?
     
  2. Try simply just doing
    msg.replace(pl.getName(), "§9" + pl.getName() + "§7");
    instead of doing msg = msg.replace(pl.getName(), "§9" + pl.getName() + "§7");
     
  3. Could you please send the code of the whole event, that way it will get much easier to help you.
     
    • Agree Agree x 1
  4. That will just return the content of the msg variable with the replace done, however the result will be ignored. That wouldn't change the value of the msg variable.
     
  5. here:
    Code (Java):
    @EventHandler
        public void onMention(AsyncPlayerChatEvent e) {
            String msg = e.getMessage();
            for(Player pl : Bukkit.getOnlinePlayers()) {
                if(msg.contains(pl.getName())) {
                    msg = msg.replace(pl.getName(), "§9" + pl.getName() + "§7");
                    pl.playSound(pl.getLocation(), Sound.BLOCK_ANVIL_DESTROY, 1.0F, 1.0F);
                    e.setFormat(e.getPlayer().getName() + " : §7" + msg);
                }
            }
        }
     
  6. Strahan

    Benefactor

    That code works fine for me. When I mention my name, I get the really annoying anvil sound.
     
  7. I think that doesn't work.
     
    • Agree Agree x 1
  8. Strahan

    Benefactor

    Agreed, simply calling .replace on it won't update the var itself.

    I'd also mention that it's better to do msg = msg.replace(pl.getName(), ChatColor.BLUE + pl.getName() + ChatColor.RESET);
     
  9. Yes, but when somebody mention somebody, only mentioned player see his colored nick, but I need to all players see colored nick.
     
  10. Use this code:
    Code (Java):
    e.setMessage(msg.replace(pl.getName(), "§9" + pl.getName() + "§7"));
     
    • Optimistic Optimistic x 1
  11. Strahan

    Benefactor

    Then something is wrong with your server, cuz with your code that's how it works for me.