Get username from player

Discussion in 'Plugin Development' started by 2jesper2, Apr 25, 2018.

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

    2jesper2

    Hello,

    I'm new in the plugin developement and i've got a question. I am trying to make a mention plugin, so if someone says ur name in chat the name will change color but only for the player with that name. Now I need to get the name of the current player who's name is typed in chat. How can I get the name of the current player?

    Sorry for mistypes, posted via phone
     
  2. Offline

    Zombie_Striker

    @2jesper2
    Because this requires each player to see something different, you will need to send the messages individually.

    I would recommend you do the following:
    1. Listen to AsyncPlayerChatEvent. Set the priority equal to MONITOR
    2. For the message, check if the message contains any of the names of the players online.
    3. If there is, cancel the event, and create a for loop looping through all of the 'recipients'.
    4. For each of the recipients, send the player a message with the message the player types where the message replaces the player's name with the name with chatcolors.
     
  3. Offline

    2jesper2

    @Zombie_Striker

    I've found this code online, but it doesn't work. It doesn't show any color to the players name.

    Code:
    @EventHandler
        public void onChat(AsyncPlayerChatEvent e){
            for(Player on:Bukkit.getServer().getOnlinePlayers()){
                if(on.equals(e.getPlayer())) {
                    if(e.getMessage().contains(on.getName())){
                        e.setMessage(e.getMessage().replaceAll(on.getName(), ChatColor.GREEN+"@"+on.getName()+ChatColor.RESET));
                    }
                }
            }
        }
    
     
  4. Offline

    Zombie_Striker

    @2jesper2
    1. It does not cancel the event, so this just sends two messages; one with the color coded name and the other without.
    2. This only sends the message to the player who typed it in chat. It does do this for any of the other players. Remove the "on.equals(....)" line to let this apply to all players.
     
  5. Offline

    2jesper2

    @Zombie_Striker Thanks for your help!

    With the code below the name of a player becomes green, but everyone on the server can see the color. I want that only the player with that name can see his name get a color.

    This is the code:
    Code:
    @EventHandler
        public void onChat(AsyncPlayerChatEvent e){
            for(Player on:Bukkit.getServer().getOnlinePlayers()){
                if(e.getMessage().contains(on.getName())){
                    e.setMessage(e.getMessage().replaceAll(on.getName(), ChatColor.GREEN+"@"+on.getName()+ChatColor.RESET));
                   
                }
            }
        }
    
    I tried to cancel the event and send a chat message like this:
    Code:
    e.setCancelled(true);
    on.getName().chat(e.getMessage().replaceAll(on.getName(), ChatColor.GREEN+"@"+on.getName()+ChatColor.RESET));
    
    But when I use this it wil give a whole bunch of errors..
    Do you have any idea?
     
  6. Offline

    timtower Administrator Administrator Moderator

    @2jesper2 What errors do you get? On which lline do you get them?
     
  7. Offline

    2jesper2

    My whole console gets spammed with errors. Here are the errors:
    https://pastebin.com/q08Ppg5E

    Sorry i meant this:
    Code:
    e.setCancelled(true);
                    on.getPlayer().chat(e.getMessage().replaceAll(on.getName(), ChatColor.GREEN+"@"+on.getName()+ChatColor.RESET));
    
    That didn't work
     
  8. Offline

    timtower Administrator Administrator Moderator

    @2jesper2 Yeah, don't call chat, just don't use that method.
     
  9. Offline

    2jesper2

    What should i use?
     
  10. Offline

    timtower Administrator Administrator Moderator

    You should modify the message in the event.
     
  11. Offline

    2jesper2

    But if I use e.setMessage() alle the players will see the color change. I want it that if i call your name in chat, only you see your name change colors.
     
  12. Offline

    timtower Administrator Administrator Moderator

    @2jesper2 Then you remove the player that is being mentioned from the list of receivers.
    Then you use sendMessage to get the chat message to him.
    Calling chat from onChat will make an infinite loop
     
  13. Offline

    2jesper2

    Thanks for your help!
    But if I use sendMessage, the name of the sender isn't showed. Is there a way to solve this?
     
  14. Offline

    timtower Administrator Administrator Moderator

    @2jesper2 You need to use the format from the message and format the name and message into it.
     
  15. Offline

    2jesper2

    Thanks for your help! I got it working now the way you told me!
     
    timtower likes this.
Thread Status:
Not open for further replies.

Share This Page