Search in sources :

Example 1 with BlockModelState

use of com.bergerkiller.bukkit.common.map.util.BlockModelState in project BKCommonLib by bergerhealer.

the class MapResourcePack method loadBlockModel.

/**
 * Loads a block model, always fetching it from the resource pack instead of the cache
 *
 * @param blockRenderOptions of the block
 * @return the model, or <i>null</i> if not found
 */
protected final Model loadBlockModel(BlockRenderOptions blockRenderOptions) {
    if (blockRenderOptions.getBlockData().getType() == Material.AIR) {
        // air. No model.
        return new Model();
    }
    BlockRenderProvider oldProvider = this.currProvider;
    try {
        // Some blocks are handled by providers
        this.currProvider = BlockRenderProvider.get(blockRenderOptions.getBlockData());
        if (this.currProvider != null) {
            Model model = this.currProvider.createModel(this, blockRenderOptions);
            if (model != null) {
                return model;
            }
        }
        String blockName = blockRenderOptions.lookupModelName();
        // Find the blockstate
        BlockModelState state = this.openGsonObject(BlockModelState.class, ResourceType.BLOCKSTATES, blockName);
        // Find out the variant that is used
        List<BlockModelState.Variant> variants;
        if (state != null) {
            // Figure out from the blockstate what variant to use
            variants = state.findVariants(blockRenderOptions);
        } else {
            // Default variant based on block name
            BlockModelState.Variant variant = new BlockModelState.Variant();
            variant.modelName = blockName;
            variants = Arrays.asList(variant);
        }
        // If no variants are found, render nothing (AIR)
        if (variants.isEmpty()) {
            return new Model();
        }
        // Not multipart, then simply load the one variant
        if (variants.size() == 1) {
            return this.loadBlockVariant(variants.get(0), blockRenderOptions);
        }
        // Add all variant elements to the model
        Model result = new Model();
        boolean succ = true;
        for (BlockModelState.Variant variant : variants) {
            Model subModel = this.loadBlockVariant(variant, blockRenderOptions);
            if (subModel != null) {
                result.elements.addAll(subModel.elements);
            } else {
                succ = false;
            }
        }
        if (!succ && result.elements.isEmpty()) {
            return null;
        } else {
            return result;
        }
    } finally {
        // restore
        this.currProvider = oldProvider;
    }
}
Also used : BlockRenderProvider(com.bergerkiller.bukkit.common.internal.blocks.BlockRenderProvider) Model(com.bergerkiller.bukkit.common.map.util.Model) GeneratedModel(com.bergerkiller.bukkit.common.internal.resources.builtin.GeneratedModel) BlockModelState(com.bergerkiller.bukkit.common.map.util.BlockModelState)

Aggregations

BlockRenderProvider (com.bergerkiller.bukkit.common.internal.blocks.BlockRenderProvider)1 GeneratedModel (com.bergerkiller.bukkit.common.internal.resources.builtin.GeneratedModel)1 BlockModelState (com.bergerkiller.bukkit.common.map.util.BlockModelState)1 Model (com.bergerkiller.bukkit.common.map.util.Model)1