How to check if a player wrote something in chat?

Discussion in 'Plugin Development' started by shohouku, Apr 21, 2014.

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

    shohouku

    I'm checking the player name from the config.

    Then I'm checking if the player wrote something, if the player wrote something he will set that as his username.


    Is this the correct way to check if the player wrote a word?


    Code:java
    1. @EventHandler
    2. public void onPlayerChat(AsyncPlayerChatEvent event) {
    3. String message = event.getMessage();
    4.  
    5. Player p = event.getPlayer();
    6.  
    7. if(users.contains(p.getName())) {
    8. if(message.equalsIgnoreCase(message)) {
    9.  
    10.  
    11. }
    12. }
    13. }
     
  2. Offline

    Derpiee

    Code:java
    1.  
    2. if(message.equalsIgnoreCase(message))

    That's asking if the message they sent is the message they sent.
    You would want
    Code:java
    1.  
    2. if(message.equalsIgnoreCase("message you want to check for in here"))


    EDIT: If you meant how to check if the player sent a message then you don't need the message check, the event gets fired when a message is sent :)
     
  3. Offline

    shohouku


    How would I check if its a word thats not longer then 6 characters?

    I'm not trying to get a specific word, I'm trying to get the players message. The message that he sent will set the players new 'NICKNAME'. Which is not longer then 6 characters.

    Lol sorry I didn't explain this above :(


    updated code:

    Code:java
    1. @EventHandler
    2. public void onPlayerChat(AsyncPlayerChatEvent event) {
    3. String message = event.getMessage();
    4.  
    5. Player p = event.getPlayer();
    6.  
    7. if(users.contains(p.getName())) {
    8. if(message.equalsIgnoreCase(message)) {
    9. users2.set(p.getName(), message);
    10. //if player wrote something, set the name and set the name into the config
    11. im2.open(p);
    12.  
    13. }
    14. }
    15. }
     
  4. Offline

    Esophose

    shohouku
    Try the code below.
    Code:java
    1. if(!(message.length() > 6)){
    2. // Your code here
    3. }else{
    4. p.sendMessage("Nicknames can be no longer than 6 characters!");
    5. }


    If that doesn't work let me know, it should though :)
     
  5. Offline

    shohouku

    Okay so this is my new code,
    At the bottom of the code, I'm trying to do the AsyncPlayerReceiveNameTagEvent. I'm trying to get the players message that he sent and I'm trying to set his new nick name to it.

    How would I get the message off the config?
    Code:java
    1. @EventHandler
    2. public void onPlayerChat(AsyncPlayerChatEvent event) {
    3. String message = event.getMessage();
    4.  
    5. Player p = event.getPlayer();
    6.  
    7. if(users.contains(p.getName())) {
    8. if(!(message.length() > 6)){
    9. users2.set(p.getName(), message);
    10. //if player wrote something, set the name and set the name into the config
    11. im2.open(p);
    12. }else{
    13. p.sendMessage("Nicknames can be no longer than 6 characters!");
    14. }
    15.  
    16.  
    17. } else {
    18. //do nothing
    19. }
    20. }
    21. @EventHandler
    22. public void onNameTag(AsyncPlayerReceiveNameTagEvent event) {
    23. Player p = event.getPlayer();
    24.  
    25. if (event.getPlayer().getName().equals(users.contains(p.getName()))) {
    26. event.setTag("Notch"); //Set name from config
    27.  
    28. }
    29.  
    30. }
     
  6. Offline

    Esophose

Thread Status:
Not open for further replies.

Share This Page