Search in sources :

Example 11 with ChatColor

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;
}
Also used : ArrayList(java.util.ArrayList) JSONObject(org.json.simple.JSONObject) ChatColor(org.bukkit.ChatColor)

Example 12 with ChatColor

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;
    }
}
Also used : JSONObject(org.json.simple.JSONObject) ChatColor(org.bukkit.ChatColor) LinkedList(java.util.LinkedList)

Aggregations

ChatColor (org.bukkit.ChatColor)12 JSONObject (org.json.simple.JSONObject)3 ArrayList (java.util.ArrayList)2 Player (org.bukkit.entity.Player)2 Team (org.bukkit.scoreboard.Team)2 DefaultPermission (fr.xephi.authme.permission.DefaultPermission)1 PermissionNode (fr.xephi.authme.permission.PermissionNode)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Set (java.util.Set)1 Element (net.aufdemrand.denizencore.objects.Element)1 Attribute (net.aufdemrand.denizencore.tags.Attribute)1 MaxMoneyException (net.ess3.api.MaxMoneyException)1 OfflinePlayer (org.bukkit.OfflinePlayer)1 EventHandler (org.bukkit.event.EventHandler)1 DisplaySlot (org.bukkit.scoreboard.DisplaySlot)1 Objective (org.bukkit.scoreboard.Objective)1 Score (org.bukkit.scoreboard.Score)1 Scoreboard (org.bukkit.scoreboard.Scoreboard)1