Development Assistance Regular Expressions (regex) help

Discussion in 'Plugin Help/Development/Requests' started by metmad22, Jan 4, 2015.

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

    metmad22

    Hello everyone, I've been working on a plugin that highlights in bold player names longer or equal to 4 characters in-chat. But, whenever someone typed a sign for example "metmad!", my name would not be in bold. I used a regex (regular expressions) code to get rid of any non letter characters in front and after the name. I ran across an other bug. Since minecraft has colour codes, if I start my message with a colour code and a name ("&2metmad"), the plugin simply ignores the code and it sends a message in the colour specified.

    I made an other regex code to remove any '&' sign before a message so the colour does not work and the plugin detects the name. The regex code I tried using is the following:
    Code:
    event.setMessage(message.replaceAll("/(&[\\dA-FK-OR]){1,}/i", ""));
    What I am doing there, is i'm replacing the & in front of a name at the beginning of a message to remove it.

    Using that code, nothing is happening. Intact, if I type "&metmad" without using any colour code, I get this in chat "rmetmad". I'm not sure where that 'r' is coming from. Let's not forget '&m' is a strike code in minecraft. I also tried this on an other name. "&Gabe" and this did nothing at all.

    Is there a way I could remove the '&' sign and any colour code such as '&6, &f, &k, &9 etc' in front of a message right before a name is written?

    Thank you.​
     
  2. Offline

    xMakerx

    @metmad22

    Yeah, there is a way to remove the & signs. Use this:
    Code:
    String input = "&4This would be in red, until you strip the color.";
    ChatColor.stripColor(input);
    This method takes away the chat colors. I hope I helped you out!
     
  3. Offline

    metmad22

    @xMakerx

    I still have not tested it, but would that not strip the colours in all the message including the name which would be in the colour specified and bold? I don't want all the colours gone. I just want the plugin to check if the first word of a message is a player name starting with a colour to cancel the colour and replace it with the bold.

    I'll still try to use this though. Thanks!
     
  4. Offline

    RingOfStorms

  5. Offline

    metmad22

    @RingOfStorms

    Thank you for that site! That should help me somewhat. Also is that Java compatible?
     
  6. Offline

    xMakerx

    You're welcome. You could split where the message begins and where the player name ends, check the length, strip the color, add your color like so, and replace the character sequence with what you want. Try this:

    Code:
    String playerName = player.getDisplayName();
    String message = myMessage;
    
    String[] parts = message.split(playerName);
    String nameSection = parts[0];
    
    if(nameSection.length() >= 4) {
    nameSection = ChatColor.RED + ChatColor.BOLD + ChatColor.stripColor(nameSection);
    message.replace(playerName, nameSection);
    }
     
  7. Offline

    RingOfStorms

    regex is regex, there isn't really a difference between languages. You may need to escape some characters when putting it in as a string, but most IDE's do that automatically, or at least any good IDE does.
     
  8. Offline

    metmad22

    @RingOfStorms
    Alright. Thank you for the advice! :)


    @xMakerx
    I'll definitely try it out! Thank you for the help. I tried using a substring to check 0,1 which would get the first letter of the message (name), split it and remove the &. But it didn't quite go as planned :p. I'll definitely give this one a shot. Thanks!
     
    xMakerx likes this.
  9. Offline

    metmad22

    @xMakerx
    The code seems fine, but it's still not getting rid of my error.
    Code:
    Code:
     message = message.replace(word, getConfig().getString("Player name color") + word + ChatColor.WHITE);
    
                            String playerName = player.getDisplayName();
    
                            String[] parts = message.split(playerName);
                            String nameSection = parts[0];
    
                            if(nameSection.length() >= (getConfig().getInt("Word length")))
                            {
                                nameSection = getConfig().getString("Player name color") + ChatColor.stripColor(nameSection);
                                event.setMessage(message.replace(playerName, nameSection));
                            }
     
Thread Status:
Not open for further replies.

Share This Page