use of org.bukkit.ChatColor in project Glowstone by GlowstoneMC.
the class TitleCommand method convertJson.
/**
* Converts a valid JSON chat component to a basic colored string. This does not parse
* components like hover or click events. This returns null on parse failure.
*
* @param json the json chat component
* @return the colored string, or null
*/
public String convertJson(Map<String, Object> json) {
if (json == null || !json.containsKey("text") && !(json.get("text") instanceof String))
// We can't even parse this
return null;
ChatColor color = ChatColor.WHITE;
List<ChatColor> style = new ArrayList<>();
for (Object key : json.keySet()) {
if (!(key instanceof String))
continue;
String keyString = (String) key;
if (keyString.equalsIgnoreCase("color")) {
if (!(json.get("color") instanceof String))
return null;
color = toColor((String) json.get(keyString));
} else if (!keyString.equalsIgnoreCase("text")) {
if (toColor(keyString) == null)
return null;
style.add(toColor(keyString));
}
}
style.add(color);
String text = (String) json.get("text");
for (ChatColor c : style) {
text = c + text;
}
return text;
}
use of org.bukkit.ChatColor in project Glowstone by GlowstoneMC.
the class TextMessage method convert.
/**
* Convert from an old-style to a new-style chat message.
*
* @param text The The text of the message.
* @return The converted JSON structure.
*/
@SuppressWarnings("unchecked")
private static JSONObject convert(String text) {
// state
List<JSONObject> items = new LinkedList<>();
Set<ChatColor> formatting = EnumSet.noneOf(ChatColor.class);
StringBuilder current = new StringBuilder();
ChatColor color = null;
// work way through text, converting colors
for (int i = 0; i < text.length(); ++i) {
char ch = text.charAt(i);
if (ch != ChatColor.COLOR_CHAR) {
// no special handling
current.append(ch);
continue;
}
if (i == text.length() - 1) {
// ignore color character at end
continue;
}
// handle colors
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);
// convert list of items into structure
if (items.isEmpty()) {
// no items, return a blank message
JSONObject object = new JSONObject();
object.put("text", "");
return object;
} else if (items.size() == 1) {
// only one item, return it as-is
return items.get(0);
} else {
JSONObject object = items.get(0);
if (object.size() == 1) {
// only contains "text", no formatting, can reuse
object.put("extra", items.subList(1, items.size()));
} else {
// must put everything in the "extra" list
object = new JSONObject();
object.put("text", "");
object.put("extra", items);
}
return object;
}
}
Aggregations