net.kubin.blocks.BlockJSONLoader.java Source code

Java tutorial

Introduction

Here is the source code for net.kubin.blocks.BlockJSONLoader.java

Source

/*******************************************************************************
 * Copyright 2014 TriBlade9 <triblade9@mail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package net.kubin.blocks;

import java.io.File;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import net.kubin.Side;
import net.kubin.items.ItemManager;
import net.kubin.math.MathHelper;
import net.kubin.math.Vec2f;
import net.kubin.math.Vec2i;
import net.kubin.math.Vec3f;
import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author martijncourteaux
 */
public class BlockJSONLoader {

    public static void parseJSON() throws Exception {
        String jsonFile = FileUtils.readFileToString(new File("res/blocks.json"));
        JSONObject jsonData = new JSONObject(jsonFile);

        /* Get NodeList of all the blocks */
        JSONArray blocksList = jsonData.getJSONArray("blocks");

        for (int i = 0; i < blocksList.length(); ++i) {
            JSONObject blockObject = blocksList.getJSONObject(i);
            BlockDef blockDef = parseBlockDef(blockObject);
            BlockManager.getInstance().addBlock(blockDef);
        }

        BlockManager.getInstance().load();
    }

    private static BlockDef parseBlockDef(JSONObject object) {
        short id = (short) object.getInt("id");
        String name = object.getString("name");

        System.out.println("Loading Block Definition: " + name);

        String colorString = object.getString("color");

        Integer transparency = 0;

        if (object.has("transparency")) {
            transparency = object.optInt("transparency");
        }

        Boolean fixed = true;

        if (object.has("fixed")) {
            fixed = object.optBoolean("fixed");
        }

        Boolean solid = true;

        if (object.has("solid")) {
            solid = object.optBoolean("solid");
        }

        DefaultBlockBrush blockBrush = new DefaultBlockBrush();

        Vec3f color;
        int colorInt = Integer.parseInt(colorString.substring(1), 16);
        int r = (colorInt >> 16) & 0xFF;
        int g = (colorInt >> 8) & 0xFF;
        int b = colorInt & 0xFF;

        color = new Vec3f(r / 255f, g / 255f, b / 255f);
        blockBrush.setGlobalColor(color);

        blockBrush.setInset(0);

        /* Load the brush into the brushstorage MUST BE HERE OR IT CAUSES NPE */
        BlockBrushStorage.registerBrush(name, blockBrush);

        /* Custom settings */
        BlockDef blockDef = new BlockDef(id, name);
        BlockManager bm = BlockManager.getInstance();

        blockDef.setFixed(fixed);
        blockDef.setSolid(solid);

        if (transparency > 0) {
            blockBrush.setAlphaBlending(true);
            blockDef.setTranslucent(true);
            transparency = MathHelper.clamp(transparency, 1, 8);

            blockBrush.setTexture(new Vec2f(transparency - 1, 1));
        } else {
            blockDef.setTranslucent(false);
            blockBrush.setTexture(new Vec2f(0, 0));
        }

        JSONObject properties = object.getJSONObject("properties");
        Set keyset = properties.keySet();

        for (Object key : keyset) {
            blockDef.setProperty((String) key, properties.getInt((String) key));
        }

        return blockDef;
    }

    private static Class<?> getSettingsType(BlockDef type, String name) {
        try {
            return type.getClass().getDeclaredField(name).getType();
        } catch (Exception ex) {
            return null;
        }
    }
}