How to detect name

Discussion in 'Spigot Plugin Development' started by xXEmiXx, Jun 10, 2017.

  1. Hello, I want to make a name detector in the chat, but there is no way to put colors to the name, this I did:
    In Red what does not work

    tarjet.replace("§b§l"+p.getName() , p.getName());

    Code (Text):
    @EventHandler
        public void ChatPlayer(AsyncPlayerChatEvent e){
         
            String tarjet = e.getMessage();
         
            String player = e.getMessage();
         
            Player p = Bukkit.getPlayer(player);
         
            if(tarjet.contains(p.getName())){
                tarjet.replace("§b§l"+p.getName() , p.getName());
                p.playSound(p.getLocation(), Sound.NOTE_PLING, 3, 2);
            }
         
         
         
         
        }
     
  2. That won't work, you wanna convert " "§b§l"+p.getName() " to a blank colored player name.
    the message should be the player so if your message contains colors then the player won't be available ( p would be null )
    Also you could simply just get the event player instead of the message as player ( event.getPlayer() )

    Edit: also that would do almost nothing, if you wanna use the replaced value as message you have to do event.setMessage(String); again.
     
    #2 Phloxz, Jun 10, 2017
    Last edited: Jun 10, 2017
  3. Instead of Bukkit.getPlayer do e.getPlayer();
     
  4. Are you trying to make it be red for only that player or the whole server?
     
  5. [
    nvm I'm an idiot do

    Code (Text):
    for (Player p : Bukkit.getPlayers()) {
        String name = p.getName();
        If (e.getMessage().contains(name) {
            e.getMessage().replace("§4§l" + name, name);
        }
    }
     
     
  6. Naaah we don't wanna replace a message for everyone :p
    also even if you do that, you have to set the message again using event.setMessage(String);
     
  7. Something like that:
    ^033290AE0DC8716C4047FA2FF1E61AD39C0F83D8C5C26D2295^pimgpsh_fullsize_distr.png
     
  8. Would be something like:

    Code (Text):


    CHATEVENT

    String message = ChatColor.stripColor(event.getMessage());

    Player onlinePlayer = Bukkit.getPlayer(message);

    if(onlinePlayer != null){
      message = message.replace(onlinePlayer.getName(),ChatColor.RED + onlinePlayer.getName());
      event.setMessage(message);
    }

     
    Edit: And if you wanna change every name of a player that is online, i would recommend something like this:

    Code (Text):


    CHATEVENT

    String message = event.getMessage();

    for(Player recipient : event.getRecipients()){
      if(containsName(message,recipient.getName()){
        message = message.replace(recipient.getName(),ChatColor.RED + recipient.getName());
      }
    }

     event.setMessage(message);

    private boolean containsName(String message,String name){
      return message.contains(name);
    }


     
     
    • Like Like x 1
  9. <3 thanks!