Search in sources :

Example 1 with ResourceLoaderContext

use of buildcraft.lib.client.model.ResourceLoaderContext in project BuildCraft by BuildCraft.

the class JsonGuiTypeRegistry method main.

// Simple test
public static void main(String[] args) throws IOException {
    String loc = "/assets/buildcraftbuilders/gui/filler.json";
    InputStream is = JsonGuiTypeRegistry.class.getResourceAsStream(loc);
    JsonObject obj;
    try (InputStreamReader isr = new InputStreamReader(is)) {
        obj = new Gson().fromJson(isr, JsonObject.class);
    }
    JsonGuiInfo info = new JsonGuiInfo(obj, DefaultContexts.createWithAll(), new ResourceLoaderContext());
    info.printOut(System.out::println);
}
Also used : ResourceLoaderContext(buildcraft.lib.client.model.ResourceLoaderContext) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson)

Example 2 with ResourceLoaderContext

use of buildcraft.lib.client.model.ResourceLoaderContext in project BuildCraft by BuildCraft.

the class BuildCraftJsonGui method load.

public final void load() {
    ResourceLoaderContext loadHistory = new ResourceLoaderContext();
    try (InputStreamReader reader = loadHistory.startLoading(jsonGuiDefinition)) {
        JsonObject obj = new Gson().fromJson(reader, JsonObject.class);
        JsonGuiInfo info = new JsonGuiInfo(obj, context, loadHistory);
        sizeX = (int) GenericExpressionCompiler.compileExpressionLong(info.sizeX, context).evaluate();
        sizeY = (int) GenericExpressionCompiler.compileExpressionLong(info.sizeY, context).evaluate();
        varData.setNodes(info.createTickableNodes());
        for (JsonGuiElement elem : info.elements) {
            String typeName = elem.properties.get("type");
            ElementType type = JsonGuiTypeRegistry.TYPES.get(typeName);
            if (type == null) {
                BCLog.logger.warn("Unknown type " + typeName);
            } else {
                IGuiElement e = type.deserialize(this, rootElement, info, elem);
                String parent = elem.properties.get("parent");
                IContainingElement p = properties.get("custom." + parent, IContainingElement.class);
                properties.put("custom." + elem.name, e);
                if (p == null) {
                    shownElements.add(e);
                } else {
                    p.getChildElements().add(e);
                    p.calculateSizes();
                }
            }
        }
    } catch (InvalidExpressionException iee) {
        throw new JsonSyntaxException("Failed to resolve the size of " + jsonGuiDefinition, iee);
    } catch (IOException e) {
        throw new Error(e);
    }
    loadHistory.finishLoading();
}
Also used : ResourceLoaderContext(buildcraft.lib.client.model.ResourceLoaderContext) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) IOException(java.io.IOException) IContainingElement(buildcraft.lib.gui.IContainingElement) JsonSyntaxException(com.google.gson.JsonSyntaxException) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) IGuiElement(buildcraft.lib.gui.IGuiElement)

Aggregations

ResourceLoaderContext (buildcraft.lib.client.model.ResourceLoaderContext)2 Gson (com.google.gson.Gson)2 JsonObject (com.google.gson.JsonObject)2 InputStreamReader (java.io.InputStreamReader)2 InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)1 IContainingElement (buildcraft.lib.gui.IContainingElement)1 IGuiElement (buildcraft.lib.gui.IGuiElement)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1