@SuppressWarnings("unchecked") private static JSONObject convert(String text){
  List<JSONObject> items=new LinkedList<>();
  Set<ChatColor> formatting=EnumSet.noneOf(ChatColor.class);
  StringBuilder current=new StringBuilder();
  ChatColor color=null;
  for (int i=0; i < text.length(); ++i) {
    char ch=text.charAt(i);
    if (ch != ChatColor.COLOR_CHAR) {
      current.append(ch);
      continue;
    }
    if (i == text.length() - 1) {
      continue;
    }
    append(items,current,color,formatting);
    ChatColor code=ChatColor.getByChar(text.charAt(++i));
    if (code == ChatColor.RESET) {
      color=null;
      formatting.clear();
    }
 else     if (code.isFormat()) {
      formatting.add(code);
    }
 else {
      color=code;
      formatting.clear();
    }
  }
  append(items,current,color,formatting);
  if (items.isEmpty()) {
    JSONObject object=new JSONObject();
    object.put("text","");
    return object;
  }
 else   if (items.size() == 1) {
    return items.get(0);
  }
 else {
    JSONObject object=items.get(0);
    if (object.size() == 1) {
      object.put("extra",items.subList(1,items.size()));
    }
 else {
      object=new JSONObject();
      object.put("text","");
      object.put("extra",items);
    }
    return object;
  }
}

@ SuppressWarnings ( "unchecked" ) private static JSONObject convert ( String text ) { List < JSONObject > items = new LinkedList < > ( ) ; Set < ChatColor > formatting = EnumSet . noneOf ( ChatColor . class ) ; StringBuilder current = new StringBuilder ( ) ; ChatColor color = null ; for ( int i = 0 ; i < text . length ( ) ; ++ i ) { char ch = text . charAt ( i ) ; if ( ch != ChatColor . COLOR_CHAR ) { current . append ( ch ) ; continue ; } if ( i == text . length ( ) - 1 ) { continue ; } append ( items , current , color , formatting ) ; ChatColor code = ChatColor . getByChar ( text . charAt ( ++ i ) ) ; if ( code == ChatColor . RESET ) { color = null ; formatting . clear ( ) ; } else if ( code . isFormat ( ) ) { formatting . add ( code ) ; } else { color = code ; formatting . clear ( ) ; } } append ( items , current , color , formatting ) ; if ( items . isEmpty ( ) ) { JSONObject object = new JSONObject ( ) ; object . put ( "text" , "" ) ; return object ; } else if ( items . size ( ) == 1 ) { return items . get ( 0 ) ; } else { JSONObject object = items . get ( 0 ) ; if ( object . size ( ) == 1 ) { object . put ( "extra" , items . subList ( 1 , items . size ( ) ) ) ; } else { object = new JSONObject ( ) ; object . put ( "text" , "" ) ; object . put ( "extra" , items ) ; } return object ; } }
convert from an old - style to a new - style chat message .
