Search in sources :

Example 1 with ItemBase

use of com.builtbroken.mc.framework.item.ItemBase 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)

Aggregations

ItemBase (com.builtbroken.mc.framework.item.ItemBase)1 ItemNode (com.builtbroken.mc.framework.item.logic.ItemNode)1 JsonProcessorInjectionMap (com.builtbroken.mc.lib.json.loading.JsonProcessorInjectionMap)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Map (java.util.Map)1