Page 1 of 1

Stripping a string of a color code? (Bukkit)

Posted: Fri May 25, 2012 11:22 am
by offluffy
I'm in the middle of writing a plugin to filter the chat (for personal use atm, since the other chat filter plugins just seem to suck). At the moment, it's working wonderfully, but it seems you can completely bypass the filter by putting a color code or some text before the word that needs to be filtered. So &ftest wouldn't be detected. I have a small bit of regex to detect it and any other text before the word. I've tried:
  • (([&|§])?([a-z0-9]?)*

    (([\\&|\\§][a-fk-o0-9])?[a-z0-9]?)*

    (([&|§][a-fk-o0-9])?[a-z0-9]?)*
I should note that the world is converted to lower case before it hits this part as well. And there's a separate part of the regex that catches symbols and such immediately after this part.

I intended for this to catch any color codes or various text (this is concatenated at the beginning of the regex string before it is compiled), but this isn't working. I tried it with and without escaping the & and § symbol, but no luck. I know the rest of the regex works fine, it's just this bit. I figure the color code is converted to another datatype before my plugin gets to it, and so the conventional &X won't catch it, but I'm just looking for a way to strip the color code off before it gets to the regex or some regex to detect whatever it uses to format the colors so that it doesn't bypass the filter. I guess most servers don't let people use color codes freely, but I still don't want this little gap here >.<

-- Edit --
Random thought, would a regex or something work if I jack up the event priority? I'm half-asleep right now and perhaps that's a dumb idea, but it's a thought.

Re: Stripping a string of a color code? (Bukkit)

Posted: Sat May 26, 2012 11:12 pm
by jacek
offluffy wrote:Random thought, would a regex or something work if I jack up the event priority? I'm half-asleep right now and perhaps that's a dumb idea, but it's a thought.
That is actually the best way to do it. Bukkit also provides a convenience method, stripColor()
String clean = ChatColo.stripColor(event.getMessage());
for example.

Re: Stripping a string of a color code? (Bukkit)

Posted: Sun May 27, 2012 1:16 am
by offluffy
Thanks .-. Wonder why I didn't try that XD Feel like an idiot now XDD

Re: Stripping a string of a color code? (Bukkit)

Posted: Sun May 27, 2012 11:30 am
by jacek
:D It's not that obvious that that method exists really.

Re: Stripping a string of a color code? (Bukkit)

Posted: Mon May 28, 2012 4:49 pm
by offluffy
Yeah, usually when I'm trying to find something new like this. I just put a dot and let Eclipse show me what the options are, which thankfully are a bit verbose XD But yeah, I wouldn't have thought about using the ChatColor object, so I guess it wouldn't have worked so well XD Thanks at any rate!

Re: Stripping a string of a color code? (Bukkit)

Posted: Tue May 29, 2012 2:37 pm
by offluffy
Well I have the event priority set to high, but I'm still not entirely sure what to search for with the regex. I'm trying to avoid stripping the color from the text because then I don't know how to replace the color as it should be, and I don't want the plugin to interfere with how the game renders colors and styles.

Re: Stripping a string of a color code? (Bukkit)

Posted: Tue May 29, 2012 6:57 pm
by jacek
offluffy wrote:Well I have the event priority set to high, but I'm still not entirely sure what to search for with the regex. I'm trying to avoid stripping the color from the text because then I don't know how to replace the color as it should be, and I don't want the plugin to interfere with how the game renders colors and styles.
You could take a look at the regex that the stipColor() method uses, the source is here https://github.com/Bukkit/Bukkit-Bleedi ... Color.java that might help ?

Re: Stripping a string of a color code? (Bukkit)

Posted: Tue May 29, 2012 7:06 pm
by offluffy
Thanks XD I'm no good and finding much of anything in java docs for anything. @.@ But this has a regex patter that might work, so I'll give it a shot.