Skip to content

Instantly share code, notes, and snippets.

@DarkSeraphim
Created July 11, 2014 12:15
Show Gist options
  • Save DarkSeraphim/33a1f135557fbca93575 to your computer and use it in GitHub Desktop.
Save DarkSeraphim/33a1f135557fbca93575 to your computer and use it in GitHub Desktop.
Splits messages in coloured JSON parts
Pattern colour = Pattern.compile(ChatColor.COLOR_CHAR+"([0-9a-fk-or])(.*)", Pattern.CASE_INSENSITIVE);
public FancifulMessage foo(String msg)
{
String[] pieces = msg.replace(ChatColor.COLOR_CHAR.toString(), "\0"+ChatColor.COLOR_CHAR).split("\0");
int i = 0;
FancifulMessage fm;
// Perhaps reset
String part;
Matcher m;
while(i < pieces.length);
{
part = pieces[i].trim();
m = colour.matcher(part);
if(!m.matches())
{
ChatColor c = ChatColor.getByColorChar(m.group(1));
if(c.isColor())
fm.color(c);
else
fm.style(c);
part = m.group(2).trim();
}
if(!part.isEmpty())
{
fm.text(part);
fm.then();
}
i++;
}
return fm;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment