Search in sources :

Example 1 with JsonPrimitive

use of com.viaversion.viaversion.libs.gson.JsonPrimitive 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)

Example 2 with JsonPrimitive

use of com.viaversion.viaversion.libs.gson.JsonPrimitive in project ViaBackwards by ViaVersion.

the class BackwardsMappings method mapIdentifiers.

// Has lots of compat layers, so we can't use the default Via method
private static void mapIdentifiers(int[] output, JsonObject newIdentifiers, JsonObject oldIdentifiers, JsonObject mapping) {
    Object2IntMap<String> newIdentifierMap = MappingDataLoader.indexedObjectToMap(oldIdentifiers);
    for (Map.Entry<String, JsonElement> entry : newIdentifiers.entrySet()) {
        String key = entry.getValue().getAsString();
        int value = newIdentifierMap.getInt(key);
        short hardId = -1;
        if (value == -1) {
            JsonPrimitive replacement = mapping.getAsJsonPrimitive(key);
            int propertyIndex;
            if (replacement == null && (propertyIndex = key.indexOf('[')) != -1) {
                replacement = mapping.getAsJsonPrimitive(key.substring(0, propertyIndex));
            }
            if (replacement != null) {
                if (replacement.getAsString().startsWith("id:")) {
                    String id = replacement.getAsString().replace("id:", "");
                    hardId = Short.parseShort(id);
                    value = newIdentifierMap.getInt(oldIdentifiers.getAsJsonPrimitive(id).getAsString());
                } else {
                    value = newIdentifierMap.getInt(replacement.getAsString());
                }
            }
            if (value == -1) {
                if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
                    if (replacement != null) {
                        ViaBackwards.getPlatform().getLogger().warning("No key for " + entry.getValue() + "/" + replacement.getAsString() + " :( ");
                    } else {
                        ViaBackwards.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( ");
                    }
                }
                continue;
            }
        }
        output[Integer.parseInt(entry.getKey())] = hardId != -1 ? hardId : (short) value;
    }
}
Also used : JsonPrimitive(com.viaversion.viaversion.libs.gson.JsonPrimitive) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) Object2IntMap(com.viaversion.viaversion.libs.fastutil.objects.Object2IntMap) HashMap(java.util.HashMap) Int2ObjectMap(com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectMap) Map(java.util.Map) Int2ObjectOpenHashMap(com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap)

Example 3 with JsonPrimitive

use of com.viaversion.viaversion.libs.gson.JsonPrimitive in project ViaBackwards by ViaVersion.

the class ChatItemRewriter method toClient.

public static void toClient(JsonElement element, UserConnection user) {
    if (element instanceof JsonObject) {
        JsonObject obj = (JsonObject) element;
        if (obj.has("hoverEvent")) {
            if (obj.get("hoverEvent") instanceof JsonObject) {
                JsonObject hoverEvent = (JsonObject) obj.get("hoverEvent");
                if (hoverEvent.has("action") && hoverEvent.has("value")) {
                    String type = hoverEvent.get("action").getAsString();
                    if (type.equals("show_item") || type.equals("show_entity")) {
                        JsonElement value = hoverEvent.get("value");
                        if (value.isJsonArray()) {
                            JsonArray newArray = new JsonArray();
                            int index = 0;
                            for (JsonElement valueElement : value.getAsJsonArray()) {
                                if (valueElement.isJsonPrimitive() && valueElement.getAsJsonPrimitive().isString()) {
                                    String newValue = index + ":" + valueElement.getAsString();
                                    newArray.add(new JsonPrimitive(newValue));
                                }
                            }
                            hoverEvent.add("value", newArray);
                        }
                    }
                }
            }
        } else if (obj.has("extra")) {
            toClient(obj.get("extra"), user);
        }
    } else if (element instanceof JsonArray) {
        JsonArray array = (JsonArray) element;
        for (JsonElement value : array) {
            toClient(value, user);
        }
    }
}
Also used : JsonArray(com.viaversion.viaversion.libs.gson.JsonArray) JsonPrimitive(com.viaversion.viaversion.libs.gson.JsonPrimitive) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) JsonObject(com.viaversion.viaversion.libs.gson.JsonObject)

Example 4 with JsonPrimitive

use of com.viaversion.viaversion.libs.gson.JsonPrimitive in project ViaBackwards by ViaVersion.

the class VBMappingDataLoader method mapIdentifiers.

public static void mapIdentifiers(int[] output, JsonObject oldIdentifiers, JsonObject newIdentifiers, JsonObject diffIdentifiers, boolean warnOnMissing) {
    Object2IntMap<String> newIdentifierMap = MappingDataLoader.indexedObjectToMap(newIdentifiers);
    for (Map.Entry<String, JsonElement> entry : oldIdentifiers.entrySet()) {
        String key = entry.getValue().getAsString();
        int mappedId = newIdentifierMap.getInt(key);
        if (mappedId == -1) {
            if (diffIdentifiers != null) {
                // Search in diff mappings
                JsonPrimitive diffValueJson = diffIdentifiers.getAsJsonPrimitive(key);
                String diffValue = diffValueJson != null ? diffValueJson.getAsString() : null;
                int dataIndex;
                if (diffValue == null && (dataIndex = key.indexOf('[')) != -1 && (diffValueJson = diffIdentifiers.getAsJsonPrimitive(key.substring(0, dataIndex))) != null) {
                    // Check for wildcard mappings
                    diffValue = diffValueJson.getAsString();
                    // Keep original properties if value ends with [
                    if (diffValue.endsWith("[")) {
                        diffValue += key.substring(dataIndex + 1);
                    }
                }
                if (diffValue != null) {
                    mappedId = newIdentifierMap.getInt(diffValue);
                }
            }
            if (mappedId == -1) {
                // Nothing found :(
                if (warnOnMissing && !Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
                    ViaBackwards.getPlatform().getLogger().warning("No key for " + entry.getValue() + " :( ");
                }
                continue;
            }
        }
        output[Integer.parseInt(entry.getKey())] = mappedId;
    }
}
Also used : JsonPrimitive(com.viaversion.viaversion.libs.gson.JsonPrimitive) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) Object2IntMap(com.viaversion.viaversion.libs.fastutil.objects.Object2IntMap) HashMap(java.util.HashMap) Int2ObjectMap(com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectMap) Map(java.util.Map) Int2ObjectOpenHashMap(com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap)

Aggregations

JsonPrimitive (com.viaversion.viaversion.libs.gson.JsonPrimitive)4 JsonElement (com.viaversion.viaversion.libs.gson.JsonElement)3 Int2ObjectMap (com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectMap)2 Int2ObjectOpenHashMap (com.viaversion.viaversion.libs.fastutil.ints.Int2ObjectOpenHashMap)2 Object2IntMap (com.viaversion.viaversion.libs.fastutil.objects.Object2IntMap)2 JsonObject (com.viaversion.viaversion.libs.gson.JsonObject)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 JsonArray (com.viaversion.viaversion.libs.gson.JsonArray)1 Component (com.viaversion.viaversion.libs.kyori.adventure.text.Component)1