Skip to content

Instantly share code, notes, and snippets.

@smbarbour
Last active December 9, 2022 05:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smbarbour/3fc92f5f67d777d9ddc4f8ecff5176b9 to your computer and use it in GitHub Desktop.
Save smbarbour/3fc92f5f67d777d9ddc4f8ecff5176b9 to your computer and use it in GitHub Desktop.
ItemColors implementation
{
"variants": {
"normal": { "model": "examplemod:testOre" }
}
}
package com.example.examplemod;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.common.FMLLog;
/**
* Created by sbarbour on 4/7/16.
*/
public class ClientProxy extends CommonProxy {
private ModelResourceLocation testItemRL = new ModelResourceLocation("examplemod:testItem", "inventory");
private ModelResourceLocation testOreRL = new ModelResourceLocation("examplemod:testOre", "inventory");
@Override
public void registerRenderers() {
FMLLog.info("Client-side");
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(ExampleMod.testItem, 0, testItemRL);
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(ExampleMod.testItem, 1, testItemRL);
Minecraft.getMinecraft().getItemColors().registerItemColorHandler(new TestItem.ColorHandler(), ExampleMod.testItem);
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(Item.getItemFromBlock(ExampleMod.testOre), 0, testOreRL);
Minecraft.getMinecraft().getBlockColors().registerBlockColorHandler(new TestOre.ColorHandler(), ExampleMod.testOre);
Minecraft.getMinecraft().getItemColors().registerItemColorHandler(new TestOre.ColorHandler(), ExampleMod.testOre);
}
@Override
public void loadModels() {
ModelLoader.setCustomModelResourceLocation(ExampleMod.testOreItem, 0, testOreRL);
}
}
package com.example.examplemod;
import net.minecraftforge.fml.common.FMLLog;
/**
* Created by sbarbour on 4/7/16.
*/
public class CommonProxy {
public void registerRenderers(){
FMLLog.info("Server-side");
}
public void loadModels() {
}
}
package com.example.examplemod;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.FMLLog;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;
@Mod(modid = ExampleMod.MODID, version = ExampleMod.VERSION)
public class ExampleMod
{
public static final String MODID = "examplemod";
public static final String VERSION = "1.0";
@SidedProxy(clientSide = "com.example.examplemod.ClientProxy", serverSide = "com.example.examplemod.CommonProxy")
public static CommonProxy proxy;
public static Item testItem = new TestItem("testItem");
public static Block testOre = new TestOre("testOre");
public static Item testOreItem = new TestOreItem(testOre);
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
GameRegistry.register(testItem);
GameRegistry.register(testOreItem);
GameRegistry.register(testOre);
proxy.loadModels();
}
@EventHandler
public void postInit(FMLPostInitializationEvent event) {
FMLLog.bigWarning("Post-init");
proxy.registerRenderers();
}
}
{ "parent": "block/block",
"elements": [
{ "from": [ 0, 0, 0 ],
"to": [ 16, 16, 16 ],
"faces": {
"down": { "uv": [ 0, 0, 16, 16 ], "texture": "#base", "tintindex": 0, "cullface": "down" },
"up": { "uv": [ 0, 0, 16, 16 ], "texture": "#base", "tintindex": 0, "cullface": "up" },
"north": { "uv": [ 0, 0, 16, 16 ], "texture": "#base", "tintindex": 0, "cullface": "north" },
"south": { "uv": [ 0, 0, 16, 16 ], "texture": "#base", "tintindex": 0, "cullface": "south" },
"west": { "uv": [ 0, 0, 16, 16 ], "texture": "#base", "tintindex": 0, "cullface": "west" },
"east": { "uv": [ 0, 0, 16, 16 ], "texture": "#base", "tintindex": 0, "cullface": "east" }
}
},
{ "from": [ 0, 0, 0 ],
"to": [ 16, 16, 16 ],
"faces": {
"down": { "uv": [ 0, 0, 16, 16 ], "texture": "#overlay", "tintindex": 1, "cullface": "down" },
"up": { "uv": [ 0, 0, 16, 16 ], "texture": "#overlay", "tintindex": 1, "cullface": "up" },
"north": { "uv": [ 0, 0, 16, 16 ], "texture": "#overlay", "tintindex": 1, "cullface": "north" },
"south": { "uv": [ 0, 0, 16, 16 ], "texture": "#overlay", "tintindex": 1, "cullface": "south" },
"west": { "uv": [ 0, 0, 16, 16 ], "texture": "#overlay", "tintindex": 1, "cullface": "west" },
"east": { "uv": [ 0, 0, 16, 16 ], "texture": "#overlay", "tintindex": 1, "cullface": "east" }
}
}
],
"textures": {
"particle": "blocks/stone",
"base": "blocks/stone",
"overlay": "examplemod:blocks/oreFlecks"
}
}
{
"parent": "item/generated",
"textures": {
"layer0": "examplemod:items/testItem_layer0",
"layer1": "examplemod:items/testItem_layer1",
"layer2": "examplemod:items/testItem_layer2"
}
}
{
"parent": "examplemod:block/testOre",
"display": {
"thirdperson": {
"rotation": [ 10, -45, 170 ],
"translation": [ 0, 1.5, -2.75 ],
"scale": [ 0.375, 0.375, 0.375 ]
}
}
}
package com.example.examplemod;
import net.minecraft.client.renderer.color.IItemColor;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.awt.*;
import java.util.List;
public class TestItem extends Item {
public TestItem(String unlocalizedName) {
super();
this.setUnlocalizedName(unlocalizedName);
this.setCreativeTab(CreativeTabs.tabMisc);
this.setHasSubtypes(true);
this.setRegistryName("examplemod","testItem");
}
@Override
@SideOnly(Side.CLIENT)
public void getSubItems(Item itemIn, CreativeTabs tab, List<ItemStack> subItems)
{
for (int i = 0; i < 2; i++)
{
subItems.add(new ItemStack(this, 1, i));
}
}
public static class ColorHandler implements IItemColor {
@Override
public int getColorFromItemstack(ItemStack stack, int tintIndex) {
switch(stack.getMetadata()) {
case 0:
switch(tintIndex) {
case 0:
return -1;
case 1:
return Color.BLUE.getRGB();
case 2:
return Color.RED.getRGB();
}
case 1:
switch(tintIndex) {
case 0:
return -1;
case 1:
return Color.CYAN.getRGB();
case 2:
return Color.YELLOW.getRGB();
}
}
return -1;
}
}
}
package com.example.examplemod;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.color.IBlockColor;
import net.minecraft.client.renderer.color.IItemColor;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.fml.common.FMLLog;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.awt.*;
public class TestOre extends Block {
public TestOre(String unlocalizedName) {
super(Material.rock, Material.rock.getMaterialMapColor());
this.setRegistryName("examplemod",unlocalizedName);
this.setUnlocalizedName(unlocalizedName);
this.setCreativeTab(CreativeTabs.tabBlock);
}
@SideOnly(Side.CLIENT)
public BlockRenderLayer getBlockLayer() {
return BlockRenderLayer.CUTOUT_MIPPED;
}
public static class ColorHandler implements IBlockColor, IItemColor {
@Override
public int colorMultiplier(IBlockState state, IBlockAccess p_186720_2_, BlockPos pos, int tintIndex) {
int color = (tintIndex == 0 ? -1 : Color.CYAN.getRGB());
FMLLog.info("tintIndex: " + tintIndex + " - " + color);
return color;
}
@Override
public int getColorFromItemstack(ItemStack stack, int tintIndex) {
return (tintIndex == 0 ? -1 : Color.CYAN.getRGB());
}
}
}
package com.example.examplemod;
import net.minecraft.block.Block;
import net.minecraft.item.ItemBlock;
/**
* Created by sbarbour on 4/8/16.
*/
public class TestOreItem extends ItemBlock {
public TestOreItem(Block block) {
super(block);
setRegistryName("examplemod",block.getUnlocalizedName());
setUnlocalizedName(block.getUnlocalizedName());
}
}
@matshou
Copy link

matshou commented Oct 15, 2016

Thanks, this helped me a bit to learn how to implement it my own way.
If you're interested, you can see how I did here.

Some suggestions: _I believe it would be a better practice to use ModelLoader#setCustomModelResourceLocation to register model file locations. Also, you should register your colour handlers in the initialization not post-initialization phase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment