Jump to content

Recommended Posts

Posted

Because i have been told not to use static initializers, i have come up with an other approach to register my stuff.

I'm wondering if this is a good 1?

 

So i have a ModBlocks class that has 2 methods: 1 for adding my blocks to a list and the other to iniatilize that

It looks like this:

public class ModBlocks {
	
	public static final List<Block> BLOCKS = new ArrayList<Block>();
	public static final List<ItemBlock> ITEMBLOCKS = new ArrayList<ItemBlock>();
	
	
	
	public static void addBlockToRegistryList(Block block) {
		
		BLOCKS.add(block);
		ITEMBLOCKS.add((ItemBlock) new ItemBlock(block).setRegistryName(block.getRegistryName()));
		
	}
	public static void init() {
	    addBlockToRegistryList(new BlockTerracotta(Material.ROCK, MapColor.WHITE_STAINED_HARDENED_CLAY, "white_terracotta_bricks"));
	    
}

Then i have a RegistryHandler class:

@EventBusSubscriber
public class RegistryHandler {
	
	@SubscribeEvent
	public static void onItemRegister(RegistryEvent.Register<Item> event) {
		
		event.getRegistry().registerAll(ModItems.ITEMS.toArray(new Item[0]));
	}
	
	@SubscribeEvent
	public static void onBlockRegister(RegistryEvent.Register<Block> event) {
		
		event.getRegistry().registerAll(ModBlocks.BLOCKS.toArray(new Block[0]));
	}
	
	@SubscribeEvent

	public static void registerItemBlocks(RegistryEvent.Register<Item> event) {

		event.getRegistry().registerAll(ModBlocks.ITEMBLOCKS.toArray(new ItemBlock[0]));

	}

}

And my ClientProxy:

@EventBusSubscriber
public class ClientProxy implements IProxy{
	
	private static final String DEFAULT_VARIANT = "inventory";

	

	@SubscribeEvent
	public static void onModelRegister(ModelRegistryEvent event) {
		
		for (Item item : ModItems.ITEMS) {
			
			registerItemModel(item);
		}
		
		for (Block block : ModBlocks.BLOCKS) {
			
			registerBlockModel(block);
		}
		
		
	}
	
	@Override
	public void PreInit(FMLPreInitializationEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void Init(FMLInitializationEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void PostInit(FMLPostInitializationEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ServerStarting(FMLServerStartingEvent event) {
		// TODO Auto-generated method stub
		
	}

	public static void registerItemModel(Item item) {
		
		ModelLoader.setCustomModelResourceLocation(item, 0, new ModelResourceLocation(item.getRegistryName(), DEFAULT_VARIANT));
		
	}
    public static void registerBlockModel(Block block) {
		
		ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(block), 0, new ModelResourceLocation(block.getRegistryName(), DEFAULT_VARIANT));
		
	}

}

and my main class:

@Mod(modid = References.MOD_ID, name = References.NAME, version = References.VERSION)
public class Wtemod {
	
	@Instance
	public static Wtemod instance;
	
	@SidedProxy(clientSide = References.CLIENT_PROXY_CLASS, serverSide = References.SERVER_PROXY_CLASS)
	public static IProxy proxy;
	
	@EventHandler
	public void PreInit(FMLPreInitializationEvent event)
	{
		//entities & networking
	}
	
	@EventHandler
	public void Init(FMLInitializationEvent event)
	{
		//registry events
		ModBlocks.init();
		ModRecipes.init();
	}
	
	@EventHandler
	public void PostInit(FMLPostInitializationEvent event)
	{
		//inter-mod stuff
	}
	@EventHandler
    public void serverStarting(FMLServerStartingEvent event)
    {
		//server commands registering
    }

	
}

 

Posted

Alright this is absolutely not working, because the subscribe event triggers before the ModBlocks.init();

So i changed my class to this:

@EventBusSubscriber
public class ModBlocks {
	
	public static final List<Block> BLOCKS = new ArrayList<Block>();
	public static final List<ItemBlock> ITEMBLOCKS = new ArrayList<ItemBlock>();
	
	
	public static void addBlockToRegistryList(Block block) {
		
		BLOCKS.add(block);
		ITEMBLOCKS.add((ItemBlock) new ItemBlock(block).setRegistryName(block.getRegistryName()));
		
		System.out.println(block.getRegistryName() + "has been registered");
		
	}
	
	@SubscribeEvent
	public static void onBlockRegister(RegistryEvent.Register<Block> event) {
		
		addBlockToRegistryList(new BlockTerracotta(Material.ROCK, MapColor.WHITE_STAINED_HARDENED_CLAY, "white_terracotta_bricks"));
	    addBlockToRegistryList(new BlockTerracotta(Material.ROCK, MapColor.ORANGE_STAINED_HARDENED_CLAY, "orange_terracotta_bricks"));
	    addBlockToRegistryList(new BlockTerracotta(Material.ROCK, MapColor.MAGENTA_STAINED_HARDENED_CLAY, "magenta_terracotta_bricks"));
	    addBlockToRegistryList(new BlockTerracotta(Material.ROCK, MapColor.LIGHT_BLUE_STAINED_HARDENED_CLAY, "light_blue_terracotta_bricks"));
	    addBlockToRegistryList(new BlockTerracotta(Material.ROCK, MapColor.YELLOW_STAINED_HARDENED_CLAY, "yellow_terracotta_bricks"));
	    addBlockToRegistryList(new BlockTerracotta(Material.ROCK, MapColor.LIME_STAINED_HARDENED_CLAY, "lime_terracotta_bricks"));
	    addBlockToRegistryList(new BlockTerracotta(Material.ROCK, MapColor.PINK_STAINED_HARDENED_CLAY, "pink_terracotta_bricks"));
	    addBlockToRegistryList(new BlockTerracotta(Material.ROCK, MapColor.GRAY_STAINED_HARDENED_CLAY, "gray_terracotta_bricks"));
	    addBlockToRegistryList(new BlockTerracotta(Material.ROCK, MapColor.SILVER_STAINED_HARDENED_CLAY, "silver_terracotta_bricks"));
	    addBlockToRegistryList(new BlockTerracotta(Material.ROCK, MapColor.CYAN_STAINED_HARDENED_CLAY, "cyan_terracotta_bricks"));
	    addBlockToRegistryList(new BlockTerracotta(Material.ROCK, MapColor.PURPLE_STAINED_HARDENED_CLAY, "purple_terracotta_bricks"));
	    addBlockToRegistryList(new BlockTerracotta(Material.ROCK, MapColor.BLUE_STAINED_HARDENED_CLAY, "blue_terracotta_bricks"));
	    addBlockToRegistryList(new BlockTerracotta(Material.ROCK, MapColor.BROWN_STAINED_HARDENED_CLAY, "brown_terracotta_bricks"));
	    addBlockToRegistryList(new BlockTerracotta(Material.ROCK, MapColor.GREEN_STAINED_HARDENED_CLAY, "green_terracotta_bricks"));
	    addBlockToRegistryList(new BlockTerracotta(Material.ROCK, MapColor.RED_STAINED_HARDENED_CLAY, "red_terracotta_bricks"));
	    addBlockToRegistryList(new BlockTerracotta(Material.ROCK, MapColor.BLACK_STAINED_HARDENED_CLAY, "black_terracotta_bricks"));
		
		
		event.getRegistry().registerAll(ModBlocks.BLOCKS.toArray(new Block[0]));
		
	}
	
	@SubscribeEvent
	public static void registerItemBlocks(RegistryEvent.Register<Item> event) {

		event.getRegistry().registerAll(ModBlocks.ITEMBLOCKS.toArray(new ItemBlock[0]));
		
	}

}

This is working fine, but i'm not sure putting the addBlockToRegistryList inside the onBlockRegister is the correct place.

It had to be within a subscribe event or it won't work.

So i was wondering if there is a subscribevent where i can put it?

 

Posted (edited)

Yes i know i can do this as simple as your example.

Main reason was to have clean code, but it takes as much as space like the simple way.

On the other hand, having all blocks and items stored in a list can be usefull some day.

Meh….That sounds stupid when i read it again ?

I guess i just wanted to make it fancy, but you are so right about this.

 

EDIT: It does takes less space to register blocks and itemblocks. both are  done in 1 method, So registering block and it's itemblock takes only 1 line instead of 2 lines each time.

Or can this be done otherwise?

Edited by winnetrie
Posted
  On 4/9/2019 at 8:12 PM, diesieben07 said:

Oh, if only there was a data structure that kept track of registered things... You know, some kind of, i don't know, Registry, maybe?

Expand  

Yes ….haha i know, that's why i said this: 

 

  On 4/9/2019 at 7:40 PM, winnetrie said:

Meh….That sounds stupid when i read it again ?

Expand  

 

Ow yes i could loop trough the registry and look for blocks with my modid, then register the itemblock for it...ofcourse.

Thank you

Posted
  On 4/9/2019 at 8:35 PM, winnetrie said:

btw to get a block from the registry do i call this?:

Block.getBlockFromName(References.MOD_ID + ":" + "white_terracotta_bricks");

 

I ask, because i only know this way.

There is also 

Block.REGISTRY.getObjectById(id) 

But i don't know the id, only the name

Expand  

Don't use ID, you can just have a static instance of the Block in your registry class.

Posted (edited)

He just linked you the source-code for the class that specifically holds all registries... perhaps you should use that class?

ForgeRegistries.BLOCKS.getValue(key) to get a specific block, where key is the block's RegistryName,
If you want to get all your mod's blocks at once, you would need to iterate over the registry first. (personally I'd use a lambda stream with a filter matching each blocks's RegistryName's getResourceDomain (mod-id), then save that in a collection somewhere)

 

 

  On 4/9/2019 at 8:57 PM, Big_Bad_E said:

Don't use ID, you can just have a static instance of the Block in your registry class.

Expand  

Whilst not using raw ID's is correct (they can be different on different servers!) NEVER use static instances. The whole RegistryEvent was made to remove this practice.
Use the ObjectHolder annotation instead if you must have an associated field.
 

Edited by Matryoshika

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Posted (edited)

Alright i'm done rewriting the code. Result looks like this:

ModRegistry class:

  Reveal hidden contents

ClientProxy class:

  Reveal hidden contents

 

BlockStainedClay class:

  Reveal hidden contents

ModRecipes class: 

  Reveal hidden contents

And also a Utilities class:

  Reveal hidden contents

Can i improve this more or is this a good starting template?

Edited by winnetrie
Posted (edited)
  On 4/9/2019 at 10:40 PM, diesieben07 said:

A minor thing: In your Utilities class you can use the ResourceLocation constructor that takes 2 String instead of constructing it with concatenation. 

 

And start using json recipes.

Expand  

Oh, alright. I'll change it later. Going to sleep now.

 

I am using .json recipes for crafting btw. Can we also do that with smelting recipes? 

EDIT: Sorry, but i can't find anything about json smelting recipes for 1.12.2.

Even in the minecraft recipe folder, there is none to find.

Edited by winnetrie

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.