Search in sources :

Example 1 with Component

use of com.viaversion.viaversion.libs.kyori.adventure.text.Component in project ViaBackwards by ViaVersion.

the class TranslatableRewriter1_16 method processText.

@Override
public void processText(JsonElement value) {
    super.processText(value);
    if (value == null || !value.isJsonObject())
        return;
    // c o l o r s
    JsonObject object = value.getAsJsonObject();
    JsonPrimitive color = object.getAsJsonPrimitive("color");
    if (color != null) {
        String colorName = color.getAsString();
        if (!colorName.isEmpty() && colorName.charAt(0) == '#') {
            int rgb = Integer.parseInt(colorName.substring(1), 16);
            String closestChatColor = getClosestChatColor(rgb);
            object.addProperty("color", closestChatColor);
        }
    }
    JsonObject hoverEvent = object.getAsJsonObject("hoverEvent");
    if (hoverEvent != null) {
        // Let adventure handle all of that
        try {
            Component component = ChatRewriter.HOVER_GSON_SERIALIZER.deserializeFromTree(object);
            JsonObject processedHoverEvent = ((JsonObject) ChatRewriter.HOVER_GSON_SERIALIZER.serializeToTree(component)).getAsJsonObject("hoverEvent");
            // Remove new format
            processedHoverEvent.remove("contents");
            object.add("hoverEvent", processedHoverEvent);
        } catch (Exception e) {
            ViaBackwards.getPlatform().getLogger().severe("Error converting hover event component: " + object);
            e.printStackTrace();
        }
    }
}
Also used : JsonPrimitive(com.viaversion.viaversion.libs.gson.JsonPrimitive) JsonObject(com.viaversion.viaversion.libs.gson.JsonObject) Component(com.viaversion.viaversion.libs.kyori.adventure.text.Component)

Aggregations

JsonObject (com.viaversion.viaversion.libs.gson.JsonObject)1 JsonPrimitive (com.viaversion.viaversion.libs.gson.JsonPrimitive)1 Component (com.viaversion.viaversion.libs.kyori.adventure.text.Component)1