Search in sources :

Example 1 with ModelResourceLocation

use of net.minecraft.client.renderer.block.model.ModelResourceLocation in project MinecraftForge by MinecraftForge.

the class ModelLoader method onPostBakeEvent.

/**
     * Internal, do not use.
     */
public void onPostBakeEvent(IRegistry<ModelResourceLocation, IBakedModel> modelRegistry) {
    IBakedModel missingModel = modelRegistry.getObject(MODEL_MISSING);
    Map<String, Integer> modelErrors = Maps.newHashMap();
    Set<ResourceLocation> printedBlockStateErrors = Sets.newHashSet();
    Multimap<ModelResourceLocation, IBlockState> reverseBlockMap = null;
    Multimap<ModelResourceLocation, String> reverseItemMap = null;
    if (enableVerboseMissingInfo) {
        reverseBlockMap = HashMultimap.create();
        for (Map.Entry<IBlockState, ModelResourceLocation> entry : blockModelShapes.getBlockStateMapper().putAllStateModelLocations().entrySet()) {
            reverseBlockMap.put(entry.getValue(), entry.getKey());
        }
        reverseItemMap = HashMultimap.create();
        for (Item item : GameData.getItemRegistry().typeSafeIterable()) {
            for (String s : getVariantNames(item)) {
                ModelResourceLocation memory = getInventoryVariant(s);
                reverseItemMap.put(memory, item.getRegistryName().toString());
            }
        }
    }
    for (Map.Entry<ResourceLocation, Exception> entry : loadingExceptions.entrySet()) {
        // ignoring pure ResourceLocation arguments, all things we care about pass ModelResourceLocation
        if (entry.getKey() instanceof ModelResourceLocation) {
            ModelResourceLocation location = (ModelResourceLocation) entry.getKey();
            IBakedModel model = modelRegistry.getObject(location);
            if (model == null || model == missingModel) {
                String domain = entry.getKey().getResourceDomain();
                Integer errorCountBox = modelErrors.get(domain);
                int errorCount = errorCountBox == null ? 0 : errorCountBox;
                errorCount++;
                if (errorCount < verboseMissingInfoCount) {
                    String errorMsg = "Exception loading model for variant " + entry.getKey();
                    if (enableVerboseMissingInfo) {
                        Collection<IBlockState> blocks = reverseBlockMap.get(location);
                        if (!blocks.isEmpty()) {
                            if (blocks.size() == 1) {
                                errorMsg += " for blockstate \"" + blocks.iterator().next() + "\"";
                            } else {
                                errorMsg += " for blockstates [\"" + Joiner.on("\", \"").join(blocks) + "\"]";
                            }
                        }
                        Collection<String> items = reverseItemMap.get(location);
                        if (!items.isEmpty()) {
                            if (!blocks.isEmpty())
                                errorMsg += " and";
                            if (items.size() == 1) {
                                errorMsg += " for item \"" + items.iterator().next() + "\"";
                            } else {
                                errorMsg += " for items [\"" + Joiner.on("\", \"").join(items) + "\"]";
                            }
                        }
                    }
                    if (entry.getValue() instanceof ItemLoadingException) {
                        ItemLoadingException ex = (ItemLoadingException) entry.getValue();
                        FMLLog.getLogger().error(errorMsg + ", normal location exception: ", ex.normalException);
                        FMLLog.getLogger().error(errorMsg + ", blockstate location exception: ", ex.blockstateException);
                    } else {
                        FMLLog.getLogger().error(errorMsg, entry.getValue());
                    }
                    ResourceLocation blockstateLocation = new ResourceLocation(location.getResourceDomain(), location.getResourcePath());
                    if (loadingExceptions.containsKey(blockstateLocation) && !printedBlockStateErrors.contains(blockstateLocation)) {
                        FMLLog.getLogger().error("Exception loading blockstate for the variant " + location + ": ", loadingExceptions.get(blockstateLocation));
                        printedBlockStateErrors.add(blockstateLocation);
                    }
                }
                modelErrors.put(domain, errorCount);
            }
            if (model == null) {
                modelRegistry.putObject(location, missingModel);
            }
        }
    }
    for (ModelResourceLocation missing : missingVariants) {
        IBakedModel model = modelRegistry.getObject(missing);
        if (model == null || model == missingModel) {
            String domain = missing.getResourceDomain();
            Integer errorCountBox = modelErrors.get(domain);
            int errorCount = errorCountBox == null ? 0 : errorCountBox;
            errorCount++;
            if (errorCount < verboseMissingInfoCount) {
                FMLLog.severe("Model definition for location %s not found", missing);
            }
            modelErrors.put(domain, errorCount);
        }
        if (model == null) {
            modelRegistry.putObject(missing, missingModel);
        }
    }
    for (Map.Entry<String, Integer> e : modelErrors.entrySet()) {
        if (e.getValue() >= verboseMissingInfoCount) {
            FMLLog.severe("Suppressed additional %s model loading errors for domain %s", e.getValue() - verboseMissingInfoCount, e.getKey());
        }
    }
    isLoading = false;
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) ModelResourceLocation(net.minecraft.client.renderer.block.model.ModelResourceLocation) MissingVariantException(net.minecraft.client.renderer.block.model.ModelBlockDefinition.MissingVariantException) Item(net.minecraft.item.Item) ModelResourceLocation(net.minecraft.client.renderer.block.model.ModelResourceLocation) ResourceLocation(net.minecraft.util.ResourceLocation) IBakedModel(net.minecraft.client.renderer.block.model.IBakedModel) Map(java.util.Map) TextureMap(net.minecraft.client.renderer.texture.TextureMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with ModelResourceLocation

use of net.minecraft.client.renderer.block.model.ModelResourceLocation in project MinecraftForge by MinecraftForge.

the class ModelLoader method setBucketModelDefinition.

/**
     * Helper method for registering all itemstacks for given item to map to universal bucket model.
     */
public static void setBucketModelDefinition(Item item) {
    ModelLoader.setCustomMeshDefinition(item, new ItemMeshDefinition() {

        @Override
        public ModelResourceLocation getModelLocation(@Nonnull ItemStack stack) {
            return ModelDynBucket.LOCATION;
        }
    });
    ModelBakery.registerItemVariants(item, ModelDynBucket.LOCATION);
}
Also used : ItemMeshDefinition(net.minecraft.client.renderer.ItemMeshDefinition) ModelResourceLocation(net.minecraft.client.renderer.block.model.ModelResourceLocation) ItemStack(net.minecraft.item.ItemStack)

Example 3 with ModelResourceLocation

use of net.minecraft.client.renderer.block.model.ModelResourceLocation in project MinecraftForge by MinecraftForge.

the class ModelLoader method setBucketModel.

private void setBucketModel(Item item) {
    for (String s : getVariantNames(item)) {
        ModelResourceLocation memory = getInventoryVariant(s);
        IModel model = stateModels.get(ModelDynBucket.LOCATION);
        if (model != null) {
            stateModels.put(memory, model);
        }
    }
}
Also used : ModelResourceLocation(net.minecraft.client.renderer.block.model.ModelResourceLocation)

Example 4 with ModelResourceLocation

use of net.minecraft.client.renderer.block.model.ModelResourceLocation in project MinecraftForge by MinecraftForge.

the class ModelLoader method loadItemModels.

@Override
protected void loadItemModels() {
    // register model for the universal bucket, if it exists
    if (FluidRegistry.isUniversalBucketEnabled()) {
        setBucketModelDefinition(ForgeModContainer.getInstance().universalBucket);
    }
    registerVariantNames();
    List<Item> items = Lists.newArrayList(Iterables.filter(Item.REGISTRY, new Predicate<Item>() {

        public boolean apply(Item item) {
            return item.getRegistryName() != null;
        }
    }));
    Collections.sort(items, new Comparator<Item>() {

        public int compare(Item i1, Item i2) {
            return i1.getRegistryName().toString().compareTo(i2.getRegistryName().toString());
        }
    });
    ProgressBar itemBar = ProgressManager.push("ModelLoader: items", items.size());
    for (Item item : items) {
        itemBar.step(item.getRegistryName().toString());
        for (String s : getVariantNames(item)) {
            ResourceLocation file = getItemLocation(s);
            ModelResourceLocation memory = getInventoryVariant(s);
            IModel model = ModelLoaderRegistry.getMissingModel();
            Exception exception = null;
            try {
                model = ModelLoaderRegistry.getModel(file);
            } catch (Exception normalException) {
                // try blockstate json if the item model is missing
                FMLLog.fine("Item json isn't found for '" + memory + "', trying to load the variant from the blockstate json");
                try {
                    model = ModelLoaderRegistry.getModel(memory);
                } catch (Exception blockstateException) {
                    exception = new ItemLoadingException("Could not load item model either from the normal location " + file + " or from the blockstate", normalException, blockstateException);
                }
            }
            if (exception != null) {
                storeException(memory, exception);
                model = ModelLoaderRegistry.getMissingModel(memory, exception);
            }
            stateModels.put(memory, model);
        }
    }
    ProgressManager.pop(itemBar);
    // replace vanilla bucket models if desired. done afterwards for performance reasons
    if (ForgeModContainer.replaceVanillaBucketModel) {
        // ensure the bucket model is loaded
        if (!stateModels.containsKey(ModelDynBucket.LOCATION)) {
            // load forges blockstate json for it
            try {
                registerVariant(getModelBlockDefinition(ModelDynBucket.LOCATION), ModelDynBucket.LOCATION);
            } catch (Exception exception) {
                FMLLog.getLogger().error("Could not load the forge bucket model from the blockstate", exception);
                return;
            }
        }
        // empty bucket
        for (String s : getVariantNames(Items.BUCKET)) {
            ModelResourceLocation memory = getInventoryVariant(s);
            IModel model = ModelLoaderRegistry.getModelOrMissing(new ResourceLocation(ForgeVersion.MOD_ID, "item/bucket"));
            // only on successful load, otherwise continue using the old model
            if (model != getMissingModel()) {
                stateModels.put(memory, model);
            }
        }
        setBucketModel(Items.WATER_BUCKET);
        setBucketModel(Items.LAVA_BUCKET);
        // milk bucket only replaced if some mod adds milk
        if (FluidRegistry.isFluidRegistered("milk")) {
            // can the milk be put into a bucket?
            Fluid milk = FluidRegistry.getFluid("milk");
            FluidStack milkStack = new FluidStack(milk, Fluid.BUCKET_VOLUME);
            IFluidHandler bucketHandler = FluidUtil.getFluidHandler(new ItemStack(Items.BUCKET));
            if (bucketHandler != null && bucketHandler.fill(milkStack, false) == Fluid.BUCKET_VOLUME) {
                setBucketModel(Items.MILK_BUCKET);
            }
        } else {
            // milk bucket if no milk fluid is present
            for (String s : getVariantNames(Items.MILK_BUCKET)) {
                ModelResourceLocation memory = getInventoryVariant(s);
                IModel model = ModelLoaderRegistry.getModelOrMissing(new ResourceLocation(ForgeVersion.MOD_ID, "item/bucket_milk"));
                // only on successful load, otherwise continue using the old model
                if (model != getMissingModel()) {
                    stateModels.put(memory, model);
                }
            }
        }
    }
}
Also used : FluidStack(net.minecraftforge.fluids.FluidStack) Fluid(net.minecraftforge.fluids.Fluid) ModelResourceLocation(net.minecraft.client.renderer.block.model.ModelResourceLocation) MissingVariantException(net.minecraft.client.renderer.block.model.ModelBlockDefinition.MissingVariantException) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) Predicate(com.google.common.base.Predicate) Item(net.minecraft.item.Item) ModelResourceLocation(net.minecraft.client.renderer.block.model.ModelResourceLocation) ResourceLocation(net.minecraft.util.ResourceLocation) ItemStack(net.minecraft.item.ItemStack) ProgressBar(net.minecraftforge.fml.common.ProgressManager.ProgressBar)

Example 5 with ModelResourceLocation

use of net.minecraft.client.renderer.block.model.ModelResourceLocation in project MinecraftForge by MinecraftForge.

the class ModelLoaderRegistry method getModel.

/**
     * Primary method to get IModel instances.
     * ResourceLocation argument will be passed directly to the custom model loaders,
     * ModelResourceLocation argument will be loaded through the blockstate system.
     */
public static IModel getModel(ResourceLocation location) throws Exception {
    IModel model;
    if (cache.containsKey(location))
        return cache.get(location);
    for (ResourceLocation loading : loadingModels) {
        if (location.getClass() == loading.getClass() && location.equals(loading)) {
            throw new LoaderException("circular model dependencies, stack: [" + Joiner.on(", ").join(loadingModels) + "]");
        }
    }
    loadingModels.addLast(location);
    try {
        ResourceLocation actual = getActualLocation(location);
        ICustomModelLoader accepted = null;
        for (ICustomModelLoader loader : loaders) {
            try {
                if (loader.accepts(actual)) {
                    if (accepted != null) {
                        throw new LoaderException(String.format("2 loaders (%s and %s) want to load the same model %s", accepted, loader, location));
                    }
                    accepted = loader;
                }
            } catch (Exception e) {
                throw new LoaderException(String.format("Exception checking if model %s can be loaded with loader %s, skipping", location, loader), e);
            }
        }
        // no custom loaders found, try vanilla ones
        if (accepted == null) {
            if (VariantLoader.INSTANCE.accepts(actual)) {
                accepted = VariantLoader.INSTANCE;
            } else if (VanillaLoader.INSTANCE.accepts(actual)) {
                accepted = VanillaLoader.INSTANCE;
            }
        }
        if (accepted == null) {
            throw new LoaderException("no suitable loader found for the model " + location + ", skipping");
        }
        try {
            model = accepted.loadModel(actual);
        } catch (Exception e) {
            throw new LoaderException(String.format("Exception loading model %s with loader %s, skipping", location, accepted), e);
        }
        if (model == getMissingModel()) {
            throw new LoaderException(String.format("Loader %s returned missing model while loading model %s", accepted, location));
        }
        if (model == null) {
            throw new LoaderException(String.format("Loader %s returned null while loading model %s", accepted, location));
        }
        textures.addAll(model.getTextures());
    } finally {
        ResourceLocation popLoc = loadingModels.removeLast();
        if (popLoc != location) {
            throw new IllegalStateException("Corrupted loading model stack: " + popLoc + " != " + location);
        }
    }
    cache.put(location, model);
    for (ResourceLocation dep : model.getDependencies()) {
        getModelOrMissing(dep);
    }
    return model;
}
Also used : ModelResourceLocation(net.minecraft.client.renderer.block.model.ModelResourceLocation) ResourceLocation(net.minecraft.util.ResourceLocation)

Aggregations

ModelResourceLocation (net.minecraft.client.renderer.block.model.ModelResourceLocation)337 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)117 ResourceLocation (net.minecraft.util.ResourceLocation)101 Item (net.minecraft.item.Item)56 IBlockState (net.minecraft.block.state.IBlockState)55 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)54 IBakedModel (net.minecraft.client.renderer.block.model.IBakedModel)28 ArrayList (java.util.ArrayList)26 Block (net.minecraft.block.Block)23 StateMapperBase (net.minecraft.client.renderer.block.statemap.StateMapperBase)23 ItemStack (net.minecraft.item.ItemStack)16 EnumFacing (net.minecraft.util.EnumFacing)14 Map (java.util.Map)13 IProperty (net.minecraft.block.properties.IProperty)12 ItemMeshDefinition (net.minecraft.client.renderer.ItemMeshDefinition)11 DefaultStateMapper (net.minecraft.client.renderer.block.statemap.DefaultStateMapper)11 IModel (net.minecraftforge.client.model.IModel)11 Nonnull (javax.annotation.Nonnull)10 Side (net.minecraftforge.fml.relauncher.Side)10 ModelLoader (net.minecraftforge.client.model.ModelLoader)9