Hi!
I'm trying to create a shard for one of my items where you cut it and it gives you it's shard's, I want the texture to change based on the amount in the stack, for example, if the ItemStack limit was 2 then if you held there was only 1 in the stack then the texture would show 1 shard but if there was 2 in the texture would change to show 2 shards.
What code do I have to use to make different variants for the same item and how do I get the item to change variants?
Edit: Nevermind I figured out how!
Here is how I did it
In my common proxy, I added
public void registerItemVariant(Item item, ItemMeshDefinition meshDefinition, ModelResourceLocation... names) {}
and in my client proxy, I added
@Override
public void registerItemVariantRenderer(Item item, ItemMeshDefinition meshDefinition, ModelResourceLocation... names) {
ModelBakery.registerItemVariants(item, names);
ModelLoader.setCustomMeshDefinition(item, meshDefinition);
}
Next, I created a new class called ItemMeshDefinitionHandler and did this
public class ItemMeshDefinitionHandler implements ItemMeshDefinition {
@Override
public ModelResourceLocation getModelLocation(ItemStack stack) {
if(stack.getItem() == ItemInit.DIAMOND_SHARD) {
if(stack.getCount() == 1) {
return new ModelResourceLocation(stack.getItem().getRegistryName() + "_1", "inventory");
} else if (stack.getCount() == 2) {
return new ModelResourceLocation(stack.getItem().getRegistryName() + "_2", "inventory");
} else {
return new ModelResourceLocation(stack.getItem().getRegistryName() + "_2", "inventory");
}
} else if(stack.getItem() == ItemInit.COAL_SHARD) {
if(stack.getCount() == 1) {
return new ModelResourceLocation(stack.getItem().getRegistryName() + "_1", "inventory");
} else if (stack.getCount() == 2) {
return new ModelResourceLocation(stack.getItem().getRegistryName() + "_2", "inventory");
} else {
return new ModelResourceLocation(stack.getItem().getRegistryName() + "_2", "inventory");
}
}
return null;
}
}
Then in a new class named ItemShard and extended my ItemBase class (In my ItemBase class it implements IHasModel) and did this
public class ItemShard extends ItemBase {
public String name;
public ItemShard(String name, CreativeTabs creativeTab) {
super(name, creativeTab);
this.name = name;
setMaxStackSize(8);
}
@Override
public void registerModels() {
Main.proxy.registerItemVariantRenderer(this, new ItemMeshDefinitionHandler(), new ModelResourceLocation(Reference.MOD_ID + ":" + name + "_1", "inventory"), new ModelResourceLocation(Reference.MOD_ID + ":" + name + "_2", "inventory"));
}
}
Then finally I added the item to my ItemInit
public static final Item DIAMOND_SHARD = new ItemShard("diamond_shard", Main.CreativeTab);