AsyncPlayerChatEvent

Discussion in 'Plugin Development' started by jacklin213, Jul 15, 2013.

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

    jacklin213

    Hi guys , im trying to get the message from the event like so

    Code:java
    1. @EventHandler
    2. public void onChat(AsyncPlayerChatEvent event){
    3. String rawMessage = event.getMessage();
    4.  
    5. }


    how do i loop through the message and see if it contains a specific string

    thanks in advance
    ~jacklin213
     
  2. Offline

    ZeusAllMighty11 Retired Staff

    Code:
    String[] message = rawMessage.split(" ");
    
     
  3. Offline

    Ewe Loon

    rawMessage.contains(arg0);
    rawMessage.indexOf(str)>0; // might need >=0

    theres 2 methods, you might also want to convert to lowercase unless it needs to be case sensitive
     
  4. Offline

    jacklin213

    is it possible to change the color of the string if it matches before the message is send out to chat
     
  5. Offline

    ZeusAllMighty11 Retired Staff

    Code:
    e.setMessage(e.getMessage().replace("mystring", "§e"mystring(red)");
     
  6. Offline

    Ewe Loon

    to change the color add "§" followed by a hexadecimal charactor "0"-"9" and "a"-"f" for the color
    "§" is typed by holding alt and typing "0167" on the numpad
    example "§f" = white "§e" = yellow

    oh, if you want to send colored text to the console use getServer().getConsoleSender().sendMessage(string);

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  7. Offline

    ZeusAllMighty11 Retired Staff

    § = alt+2+1 as well
     
  8. Offline

    jacklin213

    Code:
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event){
            String rawMessage = event.getMessage();
            String[] message = rawMessage.split(" ");
            for(int i = 0; i < message.length; i++){
                String txt = message[i];
                if (txt.startsWith("@")){
                    Bukkit.getServer().broadcastMessage("Working");
                    
                }
            }
        }
    this what i have atm and its not working
     
  9. Offline

    Ewe Loon

    add the following line after you get rawMessage
    Bukkit.getServer().broadcastMessage("Testing "+rawMessage);
    that will tell us if its getting that far
     
  10. Offline

    jacklin213

    nvm , i forgot to register the event :p but this new part isnt working now


    Code:java
    1. if (txt.startsWith("@")){
    2. txt.replace('@', ' ');
    3. Player player = Bukkit.getServer().getPlayer(txt);
    4. if (player != null){
    5. Bukkit.getServer().broadcastMessage("Working");
    6. }
    7. }
     
  11. Offline

    Ewe Loon

    player names dont start with a space so your code will be trying to get a player called " Ewe_Loon" if you type "@Ewe_Loon"
    txt.replace('@', ''); might work (i removed the space)
     
  12. Offline

    jacklin213

    invalid character constant.
    Im testing out txt.replace("@", ''); using string instead of character


    EDIT : Both of them do not replace the '@'
    EDIT 2 : Got it to work using this
    Code:
    if (txt.startsWith("@")){
                    txt.replace('@', ' ');
                    String[] pName = txt.split("@");
                    Player player = Bukkit.getServer().getPlayer(pName[1]);
                    if (player != null){
                        Bukkit.getServer().broadcastMessage("Working");
                    }
                }
    Not sure if this is a safe way though

    All i need to do now , is set that argument which has '@' in front of it to a different color, and i have no clue how to do that

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  13. Offline

    Ewe Loon

    to remove the first char off a String called "txt" use txt.substring(1);

    Code:
    if (txt.startsWith("@")){               
                    String pName = txt.substring(1);
                    Player player = Bukkit.getServer().getPlayer(pName);
                    if (player != null){
                        Bukkit.getServer().broadcastMessage("Working");
                    }
                }
     
  14. Offline

    jacklin213

    ..... oh , lol i ended up making another string of arrays somehow it works

    Code:
    String[] pName = txt.split("@");
                        try {
                            final Player player = Bukkit.getServer().getPlayerExact(pName[1]);
    whats the difference?
     
Thread Status:
Not open for further replies.

Share This Page