I'm brand new to modding and a noob programmer. I learned the old way to register blocks and items using GameRegistry.register() then I found out there is actually a new, better way to register blocks, items, and even models. The new way is to use RegistryEvents and I'm a sucker for new and better things so I tried to figure out how it works. My goal with this post is to see if anything I did can be done better and whether or not this is correct. Just because it works doesn't mean it's correct. I want to have a solid base before I build on it too much. Here is what I came up with:
RegistryEventHandler.java
@Mod.EventBusSubscriber
public class RegistryEventHandler
{
@SubscribeEvent
public static void registerBlocks(RegistryEvent.Register<Block> event)
{
event.getRegistry().registerAll(ModBlocks.BLOCKS);
Utils.getLogger().info("Registered blocks");
}
@SubscribeEvent
public static void registerItems(RegistryEvent.Register<Item> event)
{
event.getRegistry().registerAll(ModItems.ITEMS);
for (Block block : ModBlocks.BLOCKS)
{
event.getRegistry().register(new ItemBlock(block).setRegistryName(block.getRegistryName()));
}
Utils.getLogger().info("Registered items");
}
@SubscribeEvent
public static void registerModels(ModelRegistryEvent event)
{
for (Block block: ModBlocks.BLOCKS)
{
ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(block), 0, new ModelResourceLocation(block.getRegistryName(), "inventory"));
}
for (Item item: ModItems.ITEMS)
{
ModelLoader.setCustomModelResourceLocation(item, 0, new ModelResourceLocation(item.getRegistryName(), "inventory"));
}
Utils.getLogger().info("Registered models");
}
}
ModBlocks.java
public class ModBlocks
{
public static final Block[] BLOCKS = {
new BlockTinOre("tin_ore", Material.ROCK),
new BlockTinBlock("tin_block", Material.ROCK)
};
}
ModItems.java
public class ModItems
{
public static final Item[] ITEMS = {
new ItemTinIngot("tin_ingot")
};
}
BlockTinOre.java
public class BlockTinOre extends BlockBase
{
public BlockTinOre(String name, Material material)
{
super(name, material);
}
}
BlockTinBlock.java
public class BlockTinBlock extends BlockBase
{
public BlockTinBlock(String name, Material material)
{
super(name, material);
}
}
BlockBase.java
public class BlockBase extends Block
{
BlockBase(String name, Material material)
{
super(material);
this.setRegistryName(Reference.MODID, name);
this.setUnlocalizedName(this.getRegistryName().toString());
}
}
ItemTinIngot.java
public class ItemTinIngot extends ItemBase
{
public ItemTinIngot(String name)
{
super(name);
}
}
ItemBase.java
public class ItemBase extends Item
{
ItemBase(String name)
{
this.setRegistryName(new ResourceLocation(Reference.MODID, name));
this.setUnlocalizedName(this.getRegistryName().toString());
}
}