Search in sources :

Example 1 with RelayingBakedModel

use of crazypants.enderio.base.render.model.RelayingBakedModel in project EnderIO by SleepyTrousers.

the class SmartModelAttacher method bakeModels.

@SuppressWarnings({ "rawtypes", "unchecked" })
@SubscribeEvent
@SideOnly(Side.CLIENT)
public static void bakeModels(@Nonnull ModelBakeEvent event) {
    for (RegistrationHolder holder : blocks) {
        Block block = holder.block;
        Map<IBlockState, ModelResourceLocation> locations = event.getModelManager().getBlockModelShapes().getBlockStateMapper().getVariants(block);
        final IProperty holder_property = holder.property;
        final Comparable holder_defaultsValue = holder.defaultsValue;
        final IBakedModel missingModel = event.getModelManager().getMissingModel();
        if (holder_property != null && block.getDefaultState().getPropertyKeys().contains(holder_property) && holder_defaultsValue != null) {
            IBlockState defaultState = block.getDefaultState().withProperty(holder_property, holder_defaultsValue);
            ModelResourceLocation defaultMrl = locations.get(defaultState);
            if (defaultMrl == null) {
                throw new RuntimeException("Model for state " + defaultState + " failed to load from " + defaultMrl + ". " + debugOutput(event.getModelRegistry(), defaultMrl, missingModel));
            }
            IBakedModel defaultBakedModel = event.getModelRegistry().getObject(defaultMrl);
            if (defaultBakedModel == null) {
                Log.warn("ModelBakeEvent: cannot register smart model over null model for " + holder.block + ". See model errors below.");
                continue;
            }
            RelayingBakedModel model = new RelayingBakedModel(defaultBakedModel);
            ModelResourceLocation itemMrl = new ModelResourceLocation(defaultMrl.getResourceDomain() + ":" + defaultMrl.getResourcePath() + "#inventory");
            event.getModelRegistry().putObject(itemMrl, model);
            if (defaultBakedModel == missingModel) {
                // This is normal on the first pass. We register our synthetic models above anyway to avoid model loading errors.
                continue;
            }
            for (Entry<IBlockState, ModelResourceLocation> entry : locations.entrySet()) {
                final ModelResourceLocation entryMrl = NullHelper.notnullF(entry.getValue(), "BlockModelShapes contains null keys");
                final IBlockState entryBlockstate = entry.getKey();
                final IBakedModel existingModel = event.getModelRegistry().getObject(entryMrl);
                if (existingModel == null || existingModel == missingModel) {
                    event.getModelRegistry().putObject(entryMrl, defaultBakedModel);
                } else if (entryBlockstate.getValue(holder_property) == holder.autoValue) {
                    event.getModelRegistry().putObject(entryMrl, model);
                }
            }
        } else {
            IBlockState defaultState = block.getDefaultState();
            ModelResourceLocation defaultMrl = locations.get(defaultState);
            if (defaultMrl == null) {
                throw new RuntimeException("Model for state " + defaultState + " failed to load from " + defaultMrl + ". " + debugOutput(event.getModelRegistry(), defaultMrl, missingModel));
            }
            IBakedModel defaultBakedModel = event.getModelRegistry().getObject(defaultMrl);
            if (defaultBakedModel == null || defaultBakedModel == missingModel) {
                continue;
            }
            if (!holder.itemOnly) {
                for (ModelResourceLocation mrl0 : locations.values()) {
                    final ModelResourceLocation mrl = NullHelper.notnullF(mrl0, "BlockModelShapes contains null keys");
                    IBakedModel model = event.getModelRegistry().getObject(mrl);
                    if (model == null || model == missingModel) {
                    } else {
                        event.getModelRegistry().putObject(mrl, new RelayingBakedModel(NullHelper.first(model, defaultBakedModel)));
                    }
                }
            }
            ModelResourceLocation itemMrl = new ModelResourceLocation(defaultMrl.getResourceDomain() + ":" + defaultMrl.getResourcePath() + "#inventory");
            final IBakedModel model = event.getModelRegistry().getObject(itemMrl);
            if (model == null || model == missingModel) {
                event.getModelRegistry().putObject(itemMrl, new RelayingBakedModel(defaultBakedModel));
            } else {
                event.getModelRegistry().putObject(itemMrl, new RelayingBakedModel(model));
            }
        }
    }
    OverlayHolder.collectOverlayQuads(event);
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) RelayingBakedModel(crazypants.enderio.base.render.model.RelayingBakedModel) IProperty(net.minecraft.block.properties.IProperty) ITintedBlock(crazypants.enderio.base.render.ITintedBlock) Block(net.minecraft.block.Block) ModelResourceLocation(net.minecraft.client.renderer.block.model.ModelResourceLocation) IBakedModel(net.minecraft.client.renderer.block.model.IBakedModel) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent) SideOnly(net.minecraftforge.fml.relauncher.SideOnly)

Aggregations

ITintedBlock (crazypants.enderio.base.render.ITintedBlock)1 RelayingBakedModel (crazypants.enderio.base.render.model.RelayingBakedModel)1 Block (net.minecraft.block.Block)1 IProperty (net.minecraft.block.properties.IProperty)1 IBlockState (net.minecraft.block.state.IBlockState)1 IBakedModel (net.minecraft.client.renderer.block.model.IBakedModel)1 ModelResourceLocation (net.minecraft.client.renderer.block.model.ModelResourceLocation)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)1