Jump to content

Recommended Posts

Posted (edited)

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());
	}
}

 

Edited by Kriptikz
  • Like 4
Posted
  On 2/12/2017 at 11:37 PM, diesieben07 said:

Not sure if you have a question or anything. All I can say is: kudos to you for actually doing research and not blindly copying outdated terrible tutorials. :)

Expand  

Thanks. I went through multiple iterations of this setup before I landed on this one. I edited my post to actually have a solid question. Basically I want a solid foundation before I start building and want to know if anything at all can be done better.

Posted (edited)

This is actually a good way to register blocks and items! I will probably switch to this in the future.

Also, is this 1.11 exclusive? 

Edited by Leomelonseeds
  • Like 1

Apparently I'm addicted to these forums and can't help but read all the posts. So if I somehow help you, please click the "Like This" button, it helps.

Posted

The later half of 1.10.2, at least.  I don't recall when it was first introduced.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

  • 6 months later...
Posted

What is the import for Utils.getLogger().info(); ? It doesn't matter which one I import, I get a syntax error

"Can't resolve method 'getLogger()' "

Else do I need to make a class called 'Utils' and create a 'getLogger()' method?

I have never used a Logger so this is new to me.

Posted (edited)
  On 8/31/2017 at 4:19 PM, JJ42001 said:

What is the import for Utils.getLogger().info(); ? It doesn't matter which one I import, I get a syntax error

"Can't resolve method 'getLogger()' "

Else do I need to make a class called 'Utils' and create a 'getLogger()' method?

I have never used a Logger so this is new to me.

Expand  

This is custom class and method. Logger is not necessary.
If you want to make a logger like many modders do, create an object of type Logger (make sure to import one from log4j library, not java) and initialize it with LogManager#getLogger("mod's id or name") (again, make sure to use log4j one). Everyone usually does that in main class, or in utils.
Use logger#info() to log information, logger#warn() to log a warning, etc.

Edited by MrBendelScrolls
  • Like 1
Posted

 

  On 9/1/2017 at 4:47 PM, JJ42001 said:

How would one go about rendering items with this method?

Expand  

Hold on, let me quote the OP. Wait just one second...

 

  On 2/12/2017 at 10:33 PM, Kriptikz said:
	@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");
	}

 

Expand  

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted (edited)

Yea, thats what I meant, I'll edit my other post.

I apologize for the confusion.

 

I'm still unsure how to resolve the issue though.

Edited by JJ42001
Posted
  On 9/1/2017 at 5:01 PM, JJ42001 said:

My model for the Steel Ingot is under: src/ main/ resources/ assets/ models/ item/ steel_ingot.json

 

And the texture: src/ main/ resources/ assets/ textures/ items/ steel_ingot.png

Expand  

You need your mod ID as a folder between "assets" and "textures" / "models"

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Log?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
  Reveal hidden contents

Here's the latest log.

Posted

Caused by: java.io.FileNotFoundException: isdevpds:models/item/steel_ingot.json
Caused by: java.io.FileNotFoundException: isdevpds:blockstates/steel_block.json
Caused by: java.io.FileNotFoundException: isdevpds:models/item/steel_block.json

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

This is a really clean and nice way to do registereing. I may switch to it myself. But would you need to add a new method for Items with Metadata? At least model/texture wise? Or edit those regiser models to cycle through meta data as well?

Posted
  On 9/3/2017 at 1:06 PM, HalestormXV said:

This is a really clean and nice way to do registereing. I may switch to it myself. But would you need to add a new method for Items with Metadata? At least model/texture wise? Or edit those regiser models to cycle through meta data as well?

Expand  

What you're looking at is the only way to register blocks and items right now. And it's the raw basics of it. There's no differentiation between how blocks and items need to be handled (variants, IStateMapper, custom MeshDefinition...)

 

If you want something with a little more nuance, check out my EasyRegistry classes:

https://github.com/Draco18s/ReasonableRealism/blob/1.12.1/src/main/java/com/draco18s/hardlib/EasyRegistry.java

https://github.com/Draco18s/ReasonableRealism/blob/1.12.1/src/main/java/com/draco18s/hardlib/client/ClientEasyRegistry.java

 

Note that the classes are both proxy and event handler.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.