Search in sources :

Example 1 with BlockRenderProvider

use of com.bergerkiller.bukkit.common.internal.blocks.BlockRenderProvider 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().isType(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 = "block/" + 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)

Example 2 with BlockRenderProvider

use of com.bergerkiller.bukkit.common.internal.blocks.BlockRenderProvider in project BKCommonLib by bergerhealer.

the class BlockDataImpl method getRenderOptions.

@Override
public BlockRenderOptions getRenderOptions(World world, int x, int y, int z) {
    if (!this.hasRenderOptions) {
        return new BlockRenderOptions(this, "");
    }
    CommonListener.BLOCK_PHYSICS_FIRED = false;
    Object stateData;
    if (world == null || BLOCK_UPDATE_STATE_BLACKLIST.contains(this.getType())) {
        // TODO: We should call updateState() with an IBlockAccess that returns all Air.
        // Right now, it will return the options of the last-modified block
        stateData = this.data.getRaw();
    } else {
        // This refreshes the state (cached) to reflect a particular Block
        stateData = BlockHandle.T.updateState.raw.invoke(this.block.getRaw(), this.data.getRaw(), HandleConversion.toWorldHandle(world), BlockPositionHandle.T.constr_x_y_z.raw.newInstance(x, y, z));
    }
    BlockRenderOptions options;
    if (stateData == null) {
        // Not sure if this can happen; but we handle it!
        options = new BlockRenderOptions(this, new HashMap<String, String>(0));
    } else {
        // Serialize all tokens into String key-value pairs
        Map<IBlockStateHandle, Comparable<?>> states = IBlockDataHandle.T.getStates.invoke(stateData);
        Map<String, String> statesStr = new HashMap<String, String>(states.size());
        for (Map.Entry<IBlockStateHandle, Comparable<?>> state : states.entrySet()) {
            String key = state.getKey().getKeyToken();
            String value = state.getKey().getValueToken(state.getValue());
            statesStr.put(key, value);
        }
        options = new BlockRenderOptions(this, statesStr);
    }
    // Add additional options not provided by the server
    // This handles the display parameters for blocks like Water and Lava
    BlockRenderProvider renderProvider = BlockRenderProvider.get(this);
    if (renderProvider != null) {
        renderProvider.addOptions(options, world, x, y, z);
    }
    // This offers performance benefits
    if (options.isEmpty()) {
        this.hasRenderOptions = false;
    }
    // Block physics events ruin things, if they occur, disable the type and log it
    if (CommonListener.BLOCK_PHYSICS_FIRED) {
        CommonListener.BLOCK_PHYSICS_FIRED = false;
        BLOCK_UPDATE_STATE_BLACKLIST.add(this.getType());
        Logging.LOGGER.warning("[BlockData] Block physics are occurring when reading state of " + CommonLegacyMaterials.getMaterialName(this.getType()) + " data=" + this.toString() + " options=" + options);
    }
    return options;
}
Also used : BlockRenderProvider(com.bergerkiller.bukkit.common.internal.blocks.BlockRenderProvider) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) IBlockStateHandle(com.bergerkiller.generated.net.minecraft.world.level.block.state.properties.IBlockStateHandle) HashMap(java.util.HashMap) Map(java.util.Map) ConvertingMap(com.bergerkiller.mountiplex.conversion.util.ConvertingMap) IdentityHashMap(java.util.IdentityHashMap) EnumMap(java.util.EnumMap)

Aggregations

BlockRenderProvider (com.bergerkiller.bukkit.common.internal.blocks.BlockRenderProvider)2 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 IBlockStateHandle (com.bergerkiller.generated.net.minecraft.world.level.block.state.properties.IBlockStateHandle)1 ConvertingMap (com.bergerkiller.mountiplex.conversion.util.ConvertingMap)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 Map (java.util.Map)1