Search in sources :

Example 71 with JsonObject

use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project Engine by VoltzEngine-Project.

the class JsonConverterNBT method handle.

protected void handle(JsonObject object, NBTTagCompound nbt, int depth) {
    for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
        if (entry.getValue() instanceof JsonObject) {
            NBTTagCompound tag = new NBTTagCompound();
            //TODO add depth limit
            handle((JsonObject) entry.getValue(), tag, depth++);
            nbt.setTag(entry.getKey(), tag);
        } else if (entry.getValue() instanceof JsonPrimitive) {
            JsonPrimitive primitive = (JsonPrimitive) entry.getValue();
            if (primitive.isNumber()) {
                String[] split = entry.getKey().split(":");
                String key = split[0];
                String type = split[1].toLowerCase();
                if (type.equals("int") || type.equals("integer")) {
                    nbt.setInteger(key, primitive.getAsInt());
                } else if (type.equals("byte")) {
                    nbt.setByte(key, primitive.getAsByte());
                } else if (type.equals("short")) {
                    nbt.setShort(key, primitive.getAsShort());
                } else if (type.equals("double")) {
                    nbt.setDouble(key, primitive.getAsDouble());
                } else if (type.equals("float")) {
                    nbt.setFloat(key, primitive.getAsFloat());
                } else if (type.equals("long")) {
                    nbt.setLong(key, primitive.getAsLong());
                } else {
                    throw new RuntimeException("Unknown number type for " + type + " while reading " + object);
                }
            } else if (primitive.isBoolean()) {
                nbt.setBoolean(entry.getKey(), primitive.getAsBoolean());
            } else if (primitive.isString()) {
                nbt.setString(entry.getKey(), primitive.getAsString());
            }
        } else if (entry.getValue() instanceof JsonArray) {
            JsonArray array = (JsonArray) entry.getValue();
            if (array.size() > 0) {
                JsonElement element = array.get(0);
                if (element instanceof JsonPrimitive) {
                    String[] split = entry.getKey().split(":");
                    String key = split[0];
                    String type = split[1].toLowerCase();
                    if (type.equals("int") || type.equals("integer")) {
                        int[] ar = new int[array.size()];
                        for (int i = 0; i < array.size(); i++) {
                            JsonPrimitive p = array.get(i).getAsJsonPrimitive();
                            ar[i] = p.getAsInt();
                        }
                        nbt.setIntArray(key, ar);
                    } else if (type.equals("byte")) {
                        byte[] ar = new byte[array.size()];
                        for (int i = 0; i < array.size(); i++) {
                            JsonPrimitive p = array.get(i).getAsJsonPrimitive();
                            ar[i] = p.getAsByte();
                        }
                        nbt.setByteArray(key, ar);
                    } else {
                        throw new RuntimeException("Unsupported type of " + type + " for array read");
                    }
                } else if (element instanceof JsonObject) {
                    NBTTagList list = new NBTTagList();
                    for (int i = 0; i < array.size(); i++) {
                        NBTTagCompound tag = new NBTTagCompound();
                        handle((JsonObject) array.get(i), tag, depth++);
                        list.appendTag(tag);
                    }
                    nbt.setTag(entry.getKey(), list);
                }
            }
        } else {
            throw new RuntimeException("Unknown type to convert to NBT -> " + entry.getValue());
        }
    }
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) Map(java.util.Map)

Example 72 with JsonObject

use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project Engine by VoltzEngine-Project.

the class JsonLoader method loadJsonElement.

/**
     * Loads the data from the element passed in and creates {@link JsonEntry} for processing
     * later on.
     *
     * @param file    - file the element was read from
     * @param element - the element to process
     * @param entries - list to populate with new entries
     */
public static void loadJsonElement(String file, JsonElement element, HashMap<String, List<JsonEntry>> entries) {
    if (element.isJsonObject()) {
        JsonObject object = element.getAsJsonObject();
        String author = null;
        String helpSite = null;
        if (object.has("author")) {
            JsonObject authorData = object.get("author").getAsJsonObject();
            author = authorData.get("name").getAsString();
            if (authorData.has("site")) {
                helpSite = authorData.get("site").getAsString();
            }
        }
        for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
            if (!entry.getKey().equalsIgnoreCase("author")) {
                String key = entry.getKey();
                if (key.contains(":")) {
                    key = key.split(":")[0];
                }
                JsonEntry jsonEntry = new JsonEntry(key, file, entry.getValue());
                jsonEntry.author = author;
                jsonEntry.authorHelpSite = helpSite;
                List<JsonEntry> list = entries.get(jsonEntry.jsonKey);
                if (list == null) {
                    list = new ArrayList();
                }
                list.add(jsonEntry);
                entries.put(jsonEntry.jsonKey, list);
            }
        }
    }
}
Also used : JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 73 with JsonObject

use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project Engine by VoltzEngine-Project.

the class JsonItemProcessor method process.

@Override
public boolean process(JsonElement element, List<IJsonGenObject> objectList) {
    debugPrinter.start("ItemProcessor", "Processing entry", Engine.runningAsDev);
    JsonObject itemData = element.getAsJsonObject();
    ensureValuesExist(itemData, "name");
    ItemBase item;
    if (itemData.has("itemClass")) {
        String className = itemData.get("itemClass").getAsString();
        debugPrinter.log("ItemClass: " + className);
        try {
            Class clazz = Class.forName(className);
            item = (ItemBase) clazz.newInstance();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("Failed to find item class for '" + className + "'", e);
        } catch (InstantiationException e) {
            throw new RuntimeException("Failed to create item for class '" + className + "'", e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Failed to access item constructor for class '" + className + "'", e);
        } catch (Exception e) {
            throw new RuntimeException("Unexpected error creating node from '" + className + "'", e);
        }
    } else if (itemData.has("nodeClass")) {
        String className = itemData.get("nodeClass").getAsString();
        debugPrinter.log("NodeClass: " + className);
        try {
            Class clazz = Class.forName(className);
            ItemNode node = (ItemNode) clazz.newInstance();
            item = new ItemBase(node);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("Failed to find class for '" + className + "' in order to construct item node", e);
        } catch (InstantiationException e) {
            throw new RuntimeException("Failed to create constructor for class '" + className + "'", e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Failed to access node constructor for class '" + className + "'", e);
        } catch (Exception e) {
            throw new RuntimeException("Unexpected error creating node from '" + className + "'", e);
        }
    } else {
        ensureValuesExist(itemData, "id", "mod");
        String id = itemData.getAsJsonPrimitive("id").getAsString();
        String mod = itemData.getAsJsonPrimitive("mod").getAsString();
        String name = itemData.getAsJsonPrimitive("name").getAsString();
        item = new ItemBase(id, mod, name);
        debugPrinter.log("Name: " + name);
        debugPrinter.log("Mod: " + mod);
        debugPrinter.log("ID: " + id);
    }
    //Handles loading node data for item
    for (Map.Entry<String, JsonElement> entry : itemData.entrySet()) {
        if (itemPropDataHandler.handle(item.node, entry.getKey().toLowerCase(), entry.getValue())) {
            if (Engine.runningAsDev) {
                debugPrinter.log("Injected Key: " + entry.getKey());
            }
        }
    }
    objectList.add(item);
    debugPrinter.end("Done...");
    return true;
}
Also used : ItemBase(com.builtbroken.mc.framework.item.ItemBase) JsonObject(com.google.gson.JsonObject) ItemNode(com.builtbroken.mc.framework.item.logic.ItemNode) JsonElement(com.google.gson.JsonElement) JsonProcessorInjectionMap(com.builtbroken.mc.lib.json.loading.JsonProcessorInjectionMap) Map(java.util.Map)

Example 74 with JsonObject

use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project Engine by VoltzEngine-Project.

the class JsonRecipeReplacementProcessor method process.

@Override
public boolean process(JsonElement element, List<IJsonGenObject> entries) {
    JsonObject jsonData = element.getAsJsonObject();
    //Ensure the basics exist
    JsonProcessor.ensureValuesExist(jsonData, "type", "item", "craftingType");
    String type = jsonData.getAsJsonPrimitive("type").getAsString();
    String craftingType = jsonData.getAsJsonPrimitive("craftingType").getAsString();
    String item = jsonData.getAsJsonPrimitive("item").getAsString();
    JsonRecipeReplacementData replacementDataEntry = new JsonRecipeReplacementData(this, item, craftingType, true);
    //Remove and replace call
    if ("replace".equalsIgnoreCase(type)) {
        for (Map.Entry<String, JsonElement> entry : jsonData.entrySet()) {
            //Ignore fields we use for the replacement data, only grab extra fields
            if (!entry.getKey().equalsIgnoreCase("type") && !entry.getKey().equalsIgnoreCase("item") && !entry.getKey().equalsIgnoreCase("craftingType")) {
                replacementDataEntry.subProcessingData.put(entry.getKey(), entry.getValue());
            }
        }
    } else if (!"remove".equalsIgnoreCase(type)) {
        throw new RuntimeException("Unknown replacement type '" + type + "'");
    }
    replacementData.add(replacementDataEntry);
    return true;
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) Map(java.util.Map)

Example 75 with JsonObject

use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project Engine by VoltzEngine-Project.

the class JsonWorldOreGenProcessor method process.

public JsonWorldOreGenData process(Block block, int meta, String ore, JsonElement element) {
    JsonObject genData = element.getAsJsonObject();
    ensureValuesExist(genData, "type", "minY", "maxY", "branchSize", "chunkLimit");
    //Get ore block
    Object oreBlock;
    if (block == null) {
        ensureValuesExist(genData, "block");
        oreBlock = genData.getAsJsonPrimitive("block").getAsString();
    } else {
        oreBlock = new ItemStack(block, 1, meta >= 0 ? meta : 0);
    }
    String oreName = ore;
    if (oreName == null) {
        ensureValuesExist(genData, "oreName");
        oreName = genData.getAsJsonPrimitive("oreName").getAsString();
    }
    String type = genData.getAsJsonPrimitive("type").getAsString();
    int min = genData.getAsJsonPrimitive("minY").getAsInt();
    int max = genData.getAsJsonPrimitive("maxY").getAsInt();
    int branch = genData.getAsJsonPrimitive("branchSize").getAsInt();
    int chunk = genData.getAsJsonPrimitive("chunkLimit").getAsInt();
    if (min > 255 || min < 0 || max > 255 || max < 0 || max < min) {
        throw new RuntimeException("Min & Max value for ore generators must be between 0-255, and max must be equal to or greater than min");
    }
    if (branch <= 0 || chunk <= 0) {
        throw new RuntimeException("Branch size per generation and chunk generation limit must be greater than zero.");
    }
    if (type.equalsIgnoreCase("StoneOre")) {
        return new JsonWorldOreGenData(this, oreBlock, oreName, min, max, branch, chunk);
    } else {
        throw new RuntimeException("Failed to ID world ore gen type of " + type);
    }
}
Also used : JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) IJsonGenObject(com.builtbroken.mc.lib.json.imp.IJsonGenObject) ItemStack(net.minecraft.item.ItemStack)

Aggregations

JsonObject (com.google.gson.JsonObject)5111 JsonArray (com.google.gson.JsonArray)1162 JsonElement (com.google.gson.JsonElement)1084 JsonParser (com.google.gson.JsonParser)710 Test (org.junit.Test)491 IOException (java.io.IOException)471 JsonPrimitive (com.google.gson.JsonPrimitive)387 ArrayList (java.util.ArrayList)376 Gson (com.google.gson.Gson)369 HashMap (java.util.HashMap)324 Map (java.util.Map)269 Test (org.testng.annotations.Test)208 Test (org.junit.jupiter.api.Test)201 File (java.io.File)146 List (java.util.List)142 InputStreamReader (java.io.InputStreamReader)135 JsonParseException (com.google.gson.JsonParseException)121 GsonBuilder (com.google.gson.GsonBuilder)93 JsonSyntaxException (com.google.gson.JsonSyntaxException)88 Nonnull (javax.annotation.Nonnull)87