1. I'm making a kill counter and I'm wondering how to implement Regex with the ClientChatMessages.
    Someone in this thread recommended using the regex pattern
    "\w{1,16} [ a-zA-Z0-9']+ (by|of|to|for) "+Minecraft.getMinecraft().thePlayer.getName()
    I'm wondering where I would put that pattern, because I've tried doing it in the picture below, and it didn't work. If anyone could help that would be great.

    Attached Files:

    • bhj.png
      File size:
      8.7 KB
  2. the .contains function checks literally for that text in it, and wont parse regex.

    To use regex in java, you have to compile your regex to a pattern, create a matcher from that pattern with your input string, then check if it matches.

    The Javadocs of this should help you on your way:
  3. In addition to post #2 above, the kill messages contain formatting codes (§x), so you will need to strip them from the string before using the regex or include them in the regex.
  4. Thanks for the help.
    Also, which function would parse regex? And what would I put in that input?
  5. There's a number of ways you can handle your regex here, you're best of using the java.util.Regex library built into the JDK small example:

    Pattern p = Parttern.compile("INSERT YOUR REGEX HERE");
    //matcher will take your regex above and match it to your input string
    Matcher m = p.matcher(event.getMessage().getUnformattedText());
    if(m.matches()) {
      //add your counter stuff here
  6. Would that go in the ClientChatReceivedEvent? Also thanks for the advice. :)

    Attached Files:

  7. Yep that should work fine, don't forget to do as mentioned above to strip down the chat colour otherwise you may get false results.
  8. When I was testing it out in singleplayer I still didn't get it to work. I'm not sure what's wrong. Heres a picture of everything if it's any help. I'm not worried about the chat color at the moment. example.png
    Last edited: Dec 29, 2019
  9. What is the input string? As in, what are you wanting to read from chat?
  10. I want it to read things such as:
  11. I think the main reason it doesnt match currently is because there is a . or ! in the message you try to match, but not in your regex.

    I believe if you use m.find() instead of m.matches() (Matches tries to match the entire input, whilst find just tries to find a matching part of the input) , it should work.

    This does however cause some other issues, such as if a player says the exact phrase. You can, instead of the above suggestion, also add in your regex to check for either a "." or a "!" at the end

    As a suggestion, try to print out the event.getMessage.getUnformattedText() so you know the input too, it will be easier to find why it is not working

Share This Page