Stripping a string of a color code? (Bukkit)

Any help topics that don't fit in the current categories can go here.
Post Reply
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

Stripping a string of a color code? (Bukkit)

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

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

Post 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.
Image
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

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

Post by offluffy »

Thanks .-. Wonder why I didn't try that XD Feel like an idiot now XDD
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

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

Post by jacek »

:D It's not that obvious that that method exists really.
Image
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

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

Post 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!
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

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

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

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

Post 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 ?
Image
User avatar
offluffy
Posts: 65
Joined: Tue Mar 20, 2012 7:04 am

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

Post 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.
Post Reply