Search in sources :

Example 1 with IRenderState

use of com.builtbroken.mc.client.json.imp.IRenderState in project Engine by VoltzEngine-Project.

the class RenderJsonProcessor method process.

@Override
public RenderData process(JsonElement element) {
    final JsonObject object = element.getAsJsonObject();
    ensureValuesExist(object, "contentID", "states", "type");
    String contentID = object.get("contentID").getAsString();
    String overAllRenderType = object.get("type").getAsString();
    RenderData data;
    if (overAllRenderType.equalsIgnoreCase("tile")) {
        data = new TileRenderData(this, contentID, overAllRenderType);
        if (object.has("tileClass")) {
            try {
                ((TileRenderData) data).tileClass = (Class<? extends TileEntity>) Class.forName(object.get("tileClass").getAsString());
            } catch (Exception e) {
                throw new IllegalArgumentException("Failed to load class for name '" + object.get("tileClass").getAsString() + "'");
            }
        }
    } else {
        data = new RenderData(this, contentID, overAllRenderType);
    }
    JsonArray array = object.get("states").getAsJsonArray();
    for (JsonElement arrayElement : array) {
        if (arrayElement instanceof JsonObject) {
            JsonObject renderStateObject = arrayElement.getAsJsonObject();
            //For loop handling for the lazy
            if (renderStateObject.has("for")) {
                renderStateObject = renderStateObject.getAsJsonObject("for");
                ensureValuesExist(renderStateObject, "start", "end", "state");
                int start = renderStateObject.getAsJsonPrimitive("start").getAsInt();
                int end = renderStateObject.getAsJsonPrimitive("end").getAsInt();
                if (start >= end) {
                    throw new IllegalArgumentException("Start can not be greater than or equal to end for a for loop.");
                }
                JsonObject template = renderStateObject.getAsJsonObject("state");
                for (int i = start; i <= end; i++) {
                    JsonObject state = new JsonObject();
                    //Copy template and rename values as needed
                    for (Map.Entry<String, JsonElement> entry : template.entrySet()) {
                        if (entry.getValue() instanceof JsonPrimitive && ((JsonPrimitive) entry.getValue()).isString()) {
                            String s = entry.getValue().getAsString();
                            s = s.replace("%number%", "" + i);
                            state.add(entry.getKey(), new JsonPrimitive(s));
                        } else {
                            state.add(entry.getKey(), entry.getValue());
                        }
                    }
                    //Load state
                    handle(state, data, overAllRenderType);
                }
            } else {
                handle(renderStateObject, data, overAllRenderType);
            }
        }
    }
    //Handle post calls
    for (IRenderState state : data.renderStatesByName.values()) {
        //Handles post processor actions
        if (stateToProcessor.containsKey(state)) {
            stateToProcessor.get(state).postHandle(state, data);
        }
    }
    //Clear run data
    stateToProcessor.clear();
    //TODO ensure modelID exists if model state
    return data;
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) TileRenderData(com.builtbroken.mc.client.json.render.tile.TileRenderData) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) RenderData(com.builtbroken.mc.client.json.render.RenderData) TileRenderData(com.builtbroken.mc.client.json.render.tile.TileRenderData) JsonElement(com.google.gson.JsonElement) IRenderState(com.builtbroken.mc.client.json.imp.IRenderState) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with IRenderState

use of com.builtbroken.mc.client.json.imp.IRenderState in project Engine by VoltzEngine-Project.

the class RenderJsonProcessor method handle.

protected void handle(JsonObject renderStateObject, RenderData data, String overAllRenderType) {
    IRenderState renderState = null;
    //Data
    ensureValuesExist(renderStateObject, "id");
    //State ID
    JsonPrimitive stateIDElement = renderStateObject.getAsJsonPrimitive("id");
    String stateID = stateIDElement.getAsString();
    //Type override
    String subType = overAllRenderType;
    if (renderStateObject.has("renderType")) {
        subType = renderStateObject.get("renderType").getAsString();
    }
    //Check sub processors in order to get state
    for (RenderJsonSubProcessor processor : stateProcessors) {
        if (processor.canProcess(subType)) {
            renderState = processor.process(renderStateObject, stateID, overAllRenderType, subType);
            if (renderState != null) {
                stateToProcessor.put(renderState, processor);
                processor.process(renderState, renderStateObject);
                break;
            }
        }
    }
    //No state, crash TODO add config to ignore broken states
    if (renderState == null) {
        throw new RuntimeException("Failed to process render state for StateID[" + stateID + "] SubRenderType[" + subType + "] RenderType[" + overAllRenderType + "]");
    }
    //Add state to map
    data.add(stateID, renderState);
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) IRenderState(com.builtbroken.mc.client.json.imp.IRenderState)

Example 3 with IRenderState

use of com.builtbroken.mc.client.json.imp.IRenderState in project Engine by VoltzEngine-Project.

the class TileRenderHandler method renderTileEntityAt.

@Override
public void renderTileEntityAt(TileEntity tile, double x, double y, double z, float f) {
    GL11.glPushMatrix();
    try {
        GL11.glTranslated(x + 0.5, y, z + 0.5);
        RenderData data = getRenderData(tile);
        if (data != null && data.renderType.equalsIgnoreCase("tile")) {
            //Try to get tile specific state
            String key = getRenderStateKey(tile);
            //Loop default keys
            for (String de : new String[] { key, "tile", "entity", "item.entity" }) {
                if (de != null) {
                    IRenderState state = data.getState(de);
                    if (state instanceof IModelState && ((IModelState) state).render(false)) {
                        break;
                    }
                }
            }
        }
    } catch (Exception e) {
        Engine.logger().error("TileRenderHandler: Error rendering " + tile, e);
    }
    GL11.glPopMatrix();
    //If BlockBase, iterate listeners
    if (tile.getBlockType() instanceof BlockBase) {
        ListenerIterator it = new ListenerIterator(tile.getWorldObj(), tile.xCoord, tile.yCoord, tile.zCoord, (BlockBase) tile.getBlockType(), "tilerender");
        while (it.hasNext()) {
            ITileEventListener next = it.next();
            if (next instanceof ITileRenderListener) {
                GL11.glPushMatrix();
                try {
                    ((ITileRenderListener) next).renderDynamic(tile, x, y, z, f);
                } catch (Exception e) {
                    Engine.logger().error("TileRenderHandler: Error calling listener[" + next + "] for  Tile[" + tile + "]", e);
                }
                GL11.glPopMatrix();
            }
        }
    }
}
Also used : ListenerIterator(com.builtbroken.mc.prefab.tile.listeners.ListenerIterator) BlockBase(com.builtbroken.mc.framework.block.BlockBase) RenderData(com.builtbroken.mc.client.json.render.RenderData) ITileEventListener(com.builtbroken.mc.api.tile.listeners.ITileEventListener) IRenderState(com.builtbroken.mc.client.json.imp.IRenderState) IModelState(com.builtbroken.mc.client.json.imp.IModelState) ITileRenderListener(com.builtbroken.mc.api.tile.listeners.client.ITileRenderListener)

Example 4 with IRenderState

use of com.builtbroken.mc.client.json.imp.IRenderState in project Engine by VoltzEngine-Project.

the class ItemBase method getIconFromState.

/**
     * Called to get the icon from the state
     *
     * @param data - render data
     * @param meta - metadata or damage of the item
     * @param pass - render pass, 0 by default
     * @return icon, can not be null or will crash
     */
protected IIcon getIconFromState(RenderData data, int meta, int pass) {
    if (data != null) {
        //Attempt to get meta
        IRenderState state = data.getState(RenderData.INVENTORY_RENDER_KEY + "." + meta);
        if (state != null) {
            state = data.getState(RenderData.INVENTORY_RENDER_KEY);
            if (state != null) {
                IIcon icon = state.getIcon(pass);
                if (icon != null) {
                    return icon;
                }
            }
        }
        //Attempt to do non-meta
        state = data.getState(RenderData.INVENTORY_RENDER_KEY);
        if (state != null) {
            IIcon icon = state.getIcon(0);
            if (icon != null) {
                return icon;
            }
        }
    }
    return getFallBackIcon();
}
Also used : IIcon(net.minecraft.util.IIcon) IRenderState(com.builtbroken.mc.client.json.imp.IRenderState)

Aggregations

IRenderState (com.builtbroken.mc.client.json.imp.IRenderState)4 RenderData (com.builtbroken.mc.client.json.render.RenderData)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 ITileEventListener (com.builtbroken.mc.api.tile.listeners.ITileEventListener)1 ITileRenderListener (com.builtbroken.mc.api.tile.listeners.client.ITileRenderListener)1 IModelState (com.builtbroken.mc.client.json.imp.IModelState)1 TileRenderData (com.builtbroken.mc.client.json.render.tile.TileRenderData)1 BlockBase (com.builtbroken.mc.framework.block.BlockBase)1 ListenerIterator (com.builtbroken.mc.prefab.tile.listeners.ListenerIterator)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 IIcon (net.minecraft.util.IIcon)1