Skip to content
Permalink
Browse files

Added an ability to register custom item variants, not ending with "#…

…inventory". Should allow grouping multiple item models into 1 blockstate json.
  • Loading branch information...
RainWarrior committed Dec 24, 2015
1 parent a54b939 commit 097c0c61302a21f2c7008733814dff48bc9b401a
@@ -36,6 +36,24 @@
}

protected List<String> 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<String, ResourceLocation> 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,13 +108,29 @@
+ * FML Start
+ ***********************************************************/
+ private static Map<net.minecraftforge.fml.common.registry.RegistryDelegate<Item>, Set<String>> customVariantNames = Maps.newHashMap();
+ /**
+ * @deprecated use registerItemVariants
+ */
+ @Deprecated
+ public static void addVariantName(Item item, String... names)
+ {
+ if (customVariantNames.containsKey(item.delegate))
+ customVariantNames.get(item.delegate).addAll(Lists.newArrayList(names));
+ else
+ customVariantNames.put(item.delegate, Sets.newHashSet(names));
+ }
+
+ public static <T extends ResourceLocation> void registerItemVariants(Item item, T... names)
+ {
+ if (!customVariantNames.containsKey(item.delegate))
+ {
+ customVariantNames.put(item.delegate, Sets.<String>newHashSet());
+ }
+ for(ResourceLocation name : names)
+ {
+ customVariantNames.get(item.delegate).add(name.toString());
+ }
+ }
+ /***********************************************************
+ * FML End
+ ***********************************************************/
@@ -188,7 +188,7 @@ private void loadItems()
for(String s : (List<String>)getVariantNames(item))
{
ResourceLocation file = getItemLocation(s);
ModelResourceLocation memory = new ModelResourceLocation(s, "inventory");
ModelResourceLocation memory = getInventoryVariant(s);
IModel model = null;
try
{
@@ -216,7 +216,7 @@ private void loadItems()
// 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 @@ private void loadItems()
// 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 @@ private void setBucketModel(Item item)
{
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 void onPostBakeEvent(IRegistry<ModelResourceLocation, IBakedModel> modelR

private static final Map<RegistryDelegate<Block>, 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 static void onRegisterAllBlocks(BlockModelShapes shapes)
private static final Map<RegistryDelegate<Item>, ItemMeshDefinition> customMeshDefinitions = com.google.common.collect.Maps.newHashMap();
private static final Map<Pair<RegistryDelegate<Item>, 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);
@@ -55,17 +55,18 @@ public ModelResourceLocation getModelLocation(ItemStack stack)
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);
}
}

@@ -89,7 +89,6 @@ public void preInitClient(FMLPreInitializationEvent event)
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
@@ -60,9 +60,10 @@ public void preInit(FMLPreInitializationEvent event)
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)

0 comments on commit 097c0c6

Please sign in to comment.
You can’t perform that action at this time.