From 8c04651301d86edc02ac10a15af243869c87ac51 Mon Sep 17 00:00:00 2001 From: Vincent Lee Date: Thu, 15 Aug 2019 11:03:57 -0700 Subject: [PATCH] Add hook to load custom mrls/rls not attached to a block or item. (#6010) --- .../renderer/model/ModelBakery.java.patch | 30 ++- .../client/model/ModelLoader.java | 194 ++---------------- 2 files changed, 39 insertions(+), 185 deletions(-) diff --git a/patches/minecraft/net/minecraft/client/renderer/model/ModelBakery.java.patch b/patches/minecraft/net/minecraft/client/renderer/model/ModelBakery.java.patch index cb92ea9de8..99a4277372 100644 --- a/patches/minecraft/net/minecraft/client/renderer/model/ModelBakery.java.patch +++ b/patches/minecraft/net/minecraft/client/renderer/model/ModelBakery.java.patch @@ -9,7 +9,20 @@ private final Map field_217851_H = Maps.newHashMap(); private final Map field_217852_I = Maps.newHashMap(); private final AtlasTexture.SheetData field_217853_J; -@@ -288,7 +288,7 @@ +@@ -142,6 +142,12 @@ + + p_i51735_4_.func_219895_b("special"); + this.func_217843_a(new ModelResourceLocation("minecraft:trident_in_hand#inventory")); ++ for (ResourceLocation rl : getSpecialModels()) { ++ // Same as func_217843_a but without restricting to MRL's ++ IUnbakedModel iunbakedmodel = this.func_209597_a(rl); ++ this.field_217849_F.put(rl, iunbakedmodel); ++ this.field_217851_H.put(rl, iunbakedmodel); ++ } + p_i51735_4_.func_219895_b("textures"); + Set set = Sets.newLinkedHashSet(); + Set set1 = this.field_217851_H.values().stream().flatMap((p_217838_2_) -> { +@@ -288,7 +294,7 @@ { lvt_13_5_ = this.field_177598_f.func_199004_b(resourcelocation1).stream().map((p_217839_1_) -> { try (InputStream inputstream = p_217839_1_.func_199027_b()) { @@ -18,7 +31,7 @@ return pair2; } catch (Exception exception1) { throw new ModelBakery.BlockStateDefinitionException(String.format("Exception loading blockstate definition: '%s' in resourcepack: '%s': %s", p_217839_1_.func_199029_a(), p_217839_1_.func_199026_d(), exception1.getMessage())); -@@ -404,7 +404,12 @@ +@@ -404,7 +410,12 @@ @Nullable public IBakedModel func_217845_a(ResourceLocation p_217845_1_, ISprite p_217845_2_) { @@ -32,7 +45,7 @@ if (this.field_217850_G.containsKey(triple)) { return this.field_217850_G.get(triple); } else { -@@ -412,11 +417,11 @@ +@@ -412,11 +423,11 @@ if (iunbakedmodel instanceof BlockModel) { BlockModel blockmodel = (BlockModel)iunbakedmodel; if (blockmodel.func_178310_f() == field_177606_o) { @@ -46,3 +59,14 @@ this.field_217850_G.put(triple, ibakedmodel); return ibakedmodel; } +@@ -471,6 +482,10 @@ + return this.field_225367_M; + } + ++ public Set getSpecialModels() { ++ return java.util.Collections.emptySet(); ++ } ++ + @OnlyIn(Dist.CLIENT) + static class BlockStateDefinitionException extends RuntimeException { + public BlockStateDefinitionException(String p_i49526_1_) { diff --git a/src/main/java/net/minecraftforge/client/model/ModelLoader.java b/src/main/java/net/minecraftforge/client/model/ModelLoader.java index 5542c73368..020cbc02de 100644 --- a/src/main/java/net/minecraftforge/client/model/ModelLoader.java +++ b/src/main/java/net/minecraftforge/client/model/ModelLoader.java @@ -145,194 +145,24 @@ public final class ModelLoader extends ModelBakery ModelLoaderRegistry.clearModelCache(manager); } - // TODO lots of commented code here - we need to reevaluate what exactly this class needs to do in the new system, which is difficult with no mappings. So let's get everything compiling first. - // This code has mostly been ported to 1.13 - /* - @Nonnull - @Override - public IRegistry setupModelRegistry() - { - Map map = Maps.newHashMap(); + private static Set specialModels = new HashSet<>(); - if (ClientModLoader.isErrored()) // skip loading models if we're just going to show a fatal error screen - return bakedRegistry; - - isLoading = true; - - missingModel = ModelLoaderRegistry.getMissingModel(); - stateModels.put(MODEL_MISSING, missingModel); - - final Set textures = Sets.newHashSet(ModelLoaderRegistry.getTextures()); - textures.remove(MissingTextureSprite.getLocation()); - textures.addAll(LOCATIONS_BUILTIN_TEXTURES); - - textureMap.stitch(resourceManager, textures); - - STATE_CONTAINER_OVERRIDES.forEach((p_209602_2_, p_209602_3_) -> { - p_209602_3_.getValidStates().forEach((p_209601_3_) -> { - this.getUnbakedModel(map, BlockModelShapes.getModelLocation(p_209602_2_, p_209601_3_)); - }); - }); - - for(ResourceLocation resourcelocation : Item.REGISTRY.getKeys()) { - this.getUnbakedModel(map, new ModelResourceLocation(resourcelocation, "inventory")); - } - - this.getUnbakedModel(map, new ModelResourceLocation("minecraft:trident_in_hand#inventory")); - Set set = Sets.newLinkedHashSet(); - Set set1 = (Set)map.values().stream().flatMap((p_209595_2_) -> { - return p_209595_2_.getTextures(this::getUnbakedModel, set).stream(); - }).collect(Collectors.toSet()); - set1.addAll(LOCATIONS_BUILTIN_TEXTURES); - set.forEach((p_209588_0_) -> { - LOGGER.warn("Unable to resolve texture reference: {}", (Object)p_209588_0_); - }); - this.textureMap.stitch(this.resourceManager, set1); - map.forEach((p_209599_1_, p_209599_2_) -> { - IBakedModel ibakedmodel = p_209599_2_.bake(this::getUnbakedModel, this.textureMap::getSprite, ModelRotation.X0_Y0, false); - if (ibakedmodel != null) { - this.bakedRegistry.putObject(p_209599_1_, ibakedmodel); - } - - }); - return this.bakedRegistry; - - IBakedModel missingBaked = missingModel.bake(defaultModelGetter(), defaultTextureGetter(), missingModel.getDefaultState(), false, DefaultVertexFormats.ITEM); - Map bakedModels = Maps.newHashMap(); - HashMultimap models = HashMultimap.create(); - Multimaps.invertFrom(Multimaps.forMap(stateModels), models); - - ProgressBar bakeBar = ProgressManager.push("ModelLoader: baking", models.keySet().size()); - - for(IUnbakedModel model : models.keySet()) - { - String modelLocations = "[" + Joiner.on(", ").join(models.get(model)) + "]"; - bakeBar.step(modelLocations); - if(model == getMissingModel()) - { - bakedModels.put(model, missingBaked); - } - else - { - try - { - bakedModels.put(model, model.bake(defaultModelGetter(), defaultTextureGetter(), model.getDefaultState(), false, DefaultVertexFormats.ITEM)); - } - catch (Exception e) - { - LOGGER.error("Exception baking model for location(s) {}:", modelLocations, e); - bakedModels.put(model, missingBaked); - } - } - } - - ProgressManager.pop(bakeBar); - - for (Entry e : stateModels.entrySet()) - { - bakedRegistry.putObject(e.getKey(), bakedModels.get(e.getValue())); - } - return bakedRegistry; - } - - protected void loadBlocks() - { - List blocks = StreamSupport.stream(Block.REGISTRY.spliterator(), false) - .filter(block -> block.getRegistryName() != null) - .sorted(Comparator.comparing(b -> b.getRegistryName().toString())) - .collect(Collectors.toList()); - ProgressBar blockBar = ProgressManager.push("ModelLoader: blocks", blocks.size()); - - for(Block block : blocks) - { - blockBar.step(block.getRegistryName().toString()); - for(ResourceLocation location : mapper.getBlockstateLocations(block)) - { - loadBlock(mapper, block, location); - } - } - ProgressManager.pop(blockBar); + /** + * Indicate to vanilla that it should load and bake the given model, even if no blocks or + * items use it. This is useful if e.g. you have baked models only for entity renderers. + * Call during {@link net.minecraftforge.client.event.ModelRegistryEvent} + * @param rl The model, either {@link ModelResourceLocation} to point to a blockstate variant, + * or plain {@link ResourceLocation} to point directly to a json in the models folder. + */ + public static void addSpecialModel(ResourceLocation rl) { + specialModels.add(rl); } @Override - protected void registerVariant(@Nullable ModelBlockDefinition definition, ModelResourceLocation location) - { - IUnbakedModel model; - try - { - model = ModelLoaderRegistry.getModel(location); - } - catch(Exception e) - { - storeException(location, e); - model = ModelLoaderRegistry.getMissingModel(location, e); - } - stateModels.put(location, model); + public Set getSpecialModels() { + return specialModels; } - @Override - protected void registerMultipartVariant(ModelBlockDefinition definition, Collection locations) - { - for (ModelResourceLocation location : locations) - { - multipartDefinitions.put(location, definition); - registerVariant(null, location); - } - } - - private void storeException(ResourceLocation location, Exception exception) - { - loadingExceptions.put(location, exception); - } - - - @Override - protected void loadItemModels() - { - registerVariantNames(); - - List items = StreamSupport.stream(Item.REGISTRY.spliterator(), false) - .filter(item -> item.getRegistryName() != null) - .sorted(Comparator.comparing(i -> i.getRegistryName().toString())) - .collect(Collectors.toList()); - - 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); - IUnbakedModel model = ModelLoaderRegistry.getMissingModel(); - Exception exception = null; - try - { - model = ModelLoaderRegistry.getModel(memory); - } - catch (Exception blockstateException) - { - try - { - model = ModelLoaderRegistry.getModel(file); - ModelLoaderRegistry.addAlias(memory, file); - } - catch (Exception normalException) - { - 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); - } -*/ /** * Hooked from ModelBakery, allows using MRLs that don't end with "inventory" for items. */