How can I check if a player said another players name?

Discussion in 'Plugin Development' started by Blingdaddy1, Jan 29, 2014.

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

    Blingdaddy1

    Exactly like the title said, I wanted to know if there is a way to check if someone said another players name.
     
  2. Offline

    adam753

    Well there's no straightforward way. I guess you'd split the player's message by spaces, then check if any of the words they said are the name of an online player. Something like this (untested):

    Code:java
    1.  
    2. public void onPlayerChat(PlayerChatEvent event) {
    3. String[] words = event.getMessage().split(' '); //Split the message into an array of words
    4.  
    5. for(String word : words) { //For each word..
    6. Player p = Bukkit.getPlayer(word); //Get the player with that name
    7. if(p != null) { //If that player is online..
    8. //Do stuff here
    9. }
    10. }
    11. }
    12.  
     
  3. Offline

    Forseth11

    adam753 Blingdaddy1
    You could do that or you could keep a list of all player names and check if the string contains the name (With a for loop) Then you you need spaces you could do something like " " + name + " ".
    You would do this with message.contains().
     
  4. Offline

    Blingdaddy1

  5. Offline

    The_Doctor_123

  6. Offline

    adam753

    Forseth11 I suppose that would be easier, yes. The only disadvantage being that it would have a higher false-positive rate.
     
Thread Status:
Not open for further replies.

Share This Page