From 097c0c61302a21f2c7008733814dff48bc9b401a Mon Sep 17 00:00:00 2001 From: RainWarrior Date: Thu, 24 Dec 2015 06:06:38 +0300 Subject: [PATCH] Added an ability to register custom item variants, not ending with "#inventory". Should allow grouping multiple item models into 1 blockstate json. --- .../resources/model/ModelBakery.java.patch | 36 ++++++++++++++++++- .../client/model/ModelLoader.java | 29 ++++++++++++--- .../minecraftforge/debug/DynBucketTest.java | 7 ++-- .../debug/ForgeBlockStatesLoaderDebug.java | 1 - .../minecraftforge/debug/ModelFluidDebug.java | 7 ++-- 5 files changed, 68 insertions(+), 12 deletions(-) diff --git a/patches/minecraft/net/minecraft/client/resources/model/ModelBakery.java.patch b/patches/minecraft/net/minecraft/client/resources/model/ModelBakery.java.patch index 35547cd4e..f9f5a2c51 100644 --- a/patches/minecraft/net/minecraft/client/resources/model/ModelBakery.java.patch +++ b/patches/minecraft/net/minecraft/client/resources/model/ModelBakery.java.patch @@ -36,6 +36,24 @@ } protected List func_177596_a(Item p_177596_1_) +@@ -352,7 +358,7 @@ + + protected ResourceLocation func_177583_a(String p_177583_1_) + { +- ResourceLocation resourcelocation = new ResourceLocation(p_177583_1_); ++ ResourceLocation resourcelocation = new ResourceLocation(p_177583_1_.replaceAll("#.*", "")); + return new ResourceLocation(resourcelocation.func_110624_b(), "item/" + resourcelocation.func_110623_a()); + } + +@@ -395,7 +401,7 @@ + for (Entry entry : this.field_177615_s.entrySet()) + { + ResourceLocation resourcelocation = (ResourceLocation)entry.getValue(); +- ModelResourceLocation modelresourcelocation1 = new ModelResourceLocation((String)entry.getKey(), "inventory"); ++ ModelResourceLocation modelresourcelocation1 = net.minecraftforge.client.model.ModelLoader.getInventoryVariant(entry.getKey()); + ModelBlock modelblock1 = (ModelBlock)this.field_177611_h.get(resourcelocation); + + if (modelblock1 != null && modelblock1.func_178303_d()) @@ -453,6 +459,11 @@ private IBakedModel func_177578_a(ModelBlock p_177578_1_, ModelRotation p_177578_2_, boolean p_177578_3_) @@ -81,7 +99,7 @@ private void func_177597_h() { this.func_177574_i(); -@@ -722,4 +738,19 @@ +@@ -722,4 +738,35 @@ field_177617_q.field_178317_b = "class generation marker"; field_177616_r.field_178317_b = "block entity marker"; } @@ -90,6 +108,10 @@ + * FML Start + ***********************************************************/ + private static Map, Set> customVariantNames = Maps.newHashMap(); ++ /** ++ * @deprecated use registerItemVariants ++ */ ++ @Deprecated + public static void addVariantName(Item item, String... names) + { + if (customVariantNames.containsKey(item.delegate)) @@ -97,6 +119,18 @@ + else + customVariantNames.put(item.delegate, Sets.newHashSet(names)); + } ++ ++ public static void registerItemVariants(Item item, T... names) ++ { ++ if (!customVariantNames.containsKey(item.delegate)) ++ { ++ customVariantNames.put(item.delegate, Sets.newHashSet()); ++ } ++ for(ResourceLocation name : names) ++ { ++ customVariantNames.get(item.delegate).add(name.toString()); ++ } ++ } + /*********************************************************** + * FML End + ***********************************************************/ diff --git a/src/main/java/net/minecraftforge/client/model/ModelLoader.java b/src/main/java/net/minecraftforge/client/model/ModelLoader.java index 7aa3d5f77..c8edb57a5 100644 --- a/src/main/java/net/minecraftforge/client/model/ModelLoader.java +++ b/src/main/java/net/minecraftforge/client/model/ModelLoader.java @@ -188,7 +188,7 @@ public class ModelLoader extends ModelBakery for(String s : (List)getVariantNames(item)) { ResourceLocation file = getItemLocation(s); - ModelResourceLocation memory = new ModelResourceLocation(s, "inventory"); + ModelResourceLocation memory = getInventoryVariant(s); IModel model = null; try { @@ -216,7 +216,7 @@ public class ModelLoader extends ModelBakery // empty bucket for(String s : getVariantNames(Items.bucket)) { - ModelResourceLocation memory = new ModelResourceLocation(s, "inventory"); + ModelResourceLocation memory = getInventoryVariant(s); try { IModel model = getModel(new ResourceLocation("forge", "item/bucket")); @@ -247,7 +247,7 @@ public class ModelLoader extends ModelBakery // milk bucket if no milk fluid is present for(String s : getVariantNames(Items.milk_bucket)) { - ModelResourceLocation memory = new ModelResourceLocation(s, "inventory"); + ModelResourceLocation memory = getInventoryVariant(s); try { IModel model = getModel(new ResourceLocation("forge", "item/bucket_milk")); @@ -267,12 +267,21 @@ public class ModelLoader extends ModelBakery { for(String s : getVariantNames(item)) { - ModelResourceLocation memory = new ModelResourceLocation(s, "inventory"); + ModelResourceLocation memory = getInventoryVariant(s); IModel model = stateModels.get(ModelDynBucket.LOCATION); stateModels.put(memory, model); } } + public static ModelResourceLocation getInventoryVariant(String s) + { + if(s.contains("#")) + { + return new ModelResourceLocation(s); + } + return new ModelResourceLocation(s, "inventory"); + } + public IModel getModel(ResourceLocation location) throws IOException { if(!ModelLoaderRegistry.loaded(location)) loadAnyModel(location); @@ -855,6 +864,9 @@ public class ModelLoader extends ModelBakery private static final Map, IStateMapper> customStateMappers = Maps.newHashMap(); + /** + * Adds a custom IBlockState -> model variant logic. + */ public static void setCustomStateMapper(Block block, IStateMapper mapper) { customStateMappers.put(block.delegate, mapper); @@ -871,11 +883,20 @@ public class ModelLoader extends ModelBakery private static final Map, ItemMeshDefinition> customMeshDefinitions = com.google.common.collect.Maps.newHashMap(); private static final Map, Integer>, ModelResourceLocation> customModels = com.google.common.collect.Maps.newHashMap(); + /** + * Adds a simple mapping from Item + metadata to the model variant. + * Registers the variant with the ModelBakery too. + */ public static void setCustomModelResourceLocation(Item item, int metadata, ModelResourceLocation model) { customModels.put(Pair.of(item.delegate, metadata), model); + ModelBakery.registerItemVariants(item, model); } + /** + * Adds generic ItemStack -> model variant logic. + * You still need to manually call ModelBakery.registerItemVariants with all values that meshDefinition can return. + */ public static void setCustomMeshDefinition(Item item, ItemMeshDefinition meshDefinition) { customMeshDefinitions.put(item.delegate, meshDefinition); diff --git a/src/test/java/net/minecraftforge/debug/DynBucketTest.java b/src/test/java/net/minecraftforge/debug/DynBucketTest.java index defc191a3..593cb79fc 100644 --- a/src/test/java/net/minecraftforge/debug/DynBucketTest.java +++ b/src/test/java/net/minecraftforge/debug/DynBucketTest.java @@ -55,17 +55,18 @@ public class DynBucketTest return ModelDynBucket.LOCATION; } }); - ModelBakery.addVariantName(dynBucket, "forge:dynbucket"); + ModelBakery.registerItemVariants(dynBucket, new ResourceLocation("forge", "dynbucket")); + final ModelResourceLocation bottle = new ModelResourceLocation(new ResourceLocation("forge", "dynbottle"), "inventory"); ModelLoader.setCustomMeshDefinition(dynBottle, new ItemMeshDefinition() { @Override public ModelResourceLocation getModelLocation(ItemStack stack) { - return new ModelResourceLocation(new ResourceLocation("forge", "dynbottle"), "inventory"); + return bottle; } }); - ModelBakery.addVariantName(dynBottle, "forge:dynbottle"); + ModelBakery.registerItemVariants(dynBottle, bottle); } } diff --git a/src/test/java/net/minecraftforge/debug/ForgeBlockStatesLoaderDebug.java b/src/test/java/net/minecraftforge/debug/ForgeBlockStatesLoaderDebug.java index 603e3f62e..19f2b775b 100644 --- a/src/test/java/net/minecraftforge/debug/ForgeBlockStatesLoaderDebug.java +++ b/src/test/java/net/minecraftforge/debug/ForgeBlockStatesLoaderDebug.java @@ -89,7 +89,6 @@ public class ForgeBlockStatesLoaderDebug { Item customWallItem = Item.getItemFromBlock(blockCustomWall); ModelLoader.setCustomModelResourceLocation(customWallItem, 0, new ModelResourceLocation(ASSETS + "cobblestone_wall", "inventory")); ModelLoader.setCustomModelResourceLocation(customWallItem, 1, new ModelResourceLocation(ASSETS + "mossy_cobblestone_wall", "inventory")); - ModelBakery.addVariantName(customWallItem, ASSETS + "cobblestone_wall", ASSETS + "mossy_cobblestone_wall"); } // this block is never actually used, it's only needed for the error message on load to see the variant it maps to diff --git a/src/test/java/net/minecraftforge/debug/ModelFluidDebug.java b/src/test/java/net/minecraftforge/debug/ModelFluidDebug.java index 808da524d..5eb6b27b4 100644 --- a/src/test/java/net/minecraftforge/debug/ModelFluidDebug.java +++ b/src/test/java/net/minecraftforge/debug/ModelFluidDebug.java @@ -60,9 +60,10 @@ public class ModelFluidDebug Item fluid = Item.getItemFromBlock(TestFluidBlock.instance); Item gas = Item.getItemFromBlock(TestGasBlock.instance); Item milk = Item.getItemFromBlock(MilkFluidBlock.instance); - ModelBakery.addVariantName(fluid); - ModelBakery.addVariantName(gas); - ModelBakery.addVariantName(milk); + // no need to pass the locations here, since they'll be loaded by the block model logic. + ModelBakery.registerItemVariants(fluid); + ModelBakery.registerItemVariants(gas); + ModelBakery.registerItemVariants(milk); ModelLoader.setCustomMeshDefinition(fluid, new ItemMeshDefinition() { public ModelResourceLocation getModelLocation(ItemStack stack)