Jump to content

[1.12.1] [UNSOLVED] Registering blocks/items and models


Recommended Posts

Posted

Hi,

 

I've got the following code I've used so far for handling all varius aspects of registering my blocks and the models for its. This code is for blocks only, but I've got some similiar code for items, too.

 

My problem is now, I'm currently porting some things from my mod over to 1.12.1 and 1.11.2 with using the new RegistryEvents, but I don't want to lose  the flexibility of the system I've got below.

Note: This system was also used in my old mod for orginizing stuff. This means I've used it in my old mod also to register everything with sub-folders. For example I've got those paths when a block had multiply variants and still want to have it as an

optional part in my 1.12 code with the RegistryEvents.

Quote

assets.primevalforest.models.item.stoneBrick

assets.primevalforest.textures.blocks.rocks

	/**
	 * Provides the functionality of automatically registering the block and
	 * setting the registry name and unlocalized name. It also registered
	 * the block JSON renderer's and the item block with it's JSON renderer's.
	 * 
	 * @param block The block to register
	 * @param name The unique name of the block
	 * @param itemFactory  A function that creates the ItemBlock instance, or null if no ItemBlock should be created
	 * @param withMeta Should the meta data of the block also be registered?
	 * @return The block
	 */
	public static Block register(Block block, String name, @Nullable Function<Block, ItemBlock> itemFactory, boolean withMeta) {
		block.setUnlocalizedName(Constants.MOD_ID.toLowerCase() + "." + name);
		block.setRegistryName(name);
		GameRegistry.register(block);
		
		if(itemFactory != null) {
			final ItemBlock itemBlock = itemFactory.apply(block);
			GameRegistry.register(itemBlock.setRegistryName(block.getRegistryName()));
			
			if(block instanceof IItemOreDict)
				((IItemOreDict)block).initOreDict();
			
			if(itemBlock instanceof IItemOreDict)
				((IItemOreDict)itemBlock).initOreDict();
			
			if(FMLCommonHandler.instance().getEffectiveSide().isClient())
				BlockRegistry.registerRenderer(block, withMeta);
		}
		
		return block;
	}
	
	/**
	 * Provides the functionality of automatically registering the block 
	 * JSON renderer's and the item block with it's JSON renderer's.
	 * 
	 * @param block The block to register
	 * @param withMeta Should the meta data of the block also be registered?
	 * @return If the registration of the JSON renderer was successful.
	 */
	@SideOnly(Side.CLIENT)
	private static boolean registerRenderer(Block block, boolean withMeta) {
		Iterator<IBlockState> it = block.getBlockState().getValidStates().iterator();
		boolean flag = true;
		
		while(it.hasNext()) {
			IBlockState state = it.next();
			
			if(state == null) {
				JustAnotherEnergy.getLogger().error("Skipping block rendering registration for {}", state);
				flag = false;
				break;
			}
			
			int meta = block.getMetaFromState(state);
			RenderUtil.renderBlock(block, withMeta ? meta : 0);
		}
		return flag;
	}
	
		/**
	 * Provides the functionality of automatically registering the block and
	 * setting the registry name and unlocalized name. It also registered
	 * the block JSON renderer's and the item block with it's JSON renderer's.
	 * <p>
	 * This method tells Minecraft that the model json's can be found in a sub-folder.
	 * 
	 * @param block The block to register
	 * @param name The unique name of the block
	 * @param itemFactory  A function that creates the ItemBlock instance, or null if no ItemBlock should be created
	 * @param withMeta Should the meta data of the block also be registered?
	 * @param variants The different variants in which block comes
	 * @return The block
	 */
	public static <T extends IType> Block register(Block block, String name, @Nullable Function<Block, ItemBlock> itemFactory, boolean withMeta, @Nullable T[] variants) {
		block.setUnlocalizedName(Constants.MOD_ID.toLowerCase() + "." + name);
		block.setRegistryName(name);
		GameRegistry.register(block);
		
		if(itemFactory != null) {
			final ItemBlock itemBlock = itemFactory.apply(block);
			GameRegistry.register(itemBlock.setRegistryName(block.getRegistryName()));
			
			if(FMLCommonHandler.instance().getEffectiveSide().isClient()) {
				Iterator<IBlockState> it = block.getBlockState().getValidStates().iterator();
				
				while(it.hasNext()) {
					IBlockState state = it.next();
					
					if(state == null) {
						JustAnotherEnergy.getLogger().error("Skipping block rendering registration for {}", state);
						break;
					}
					
					for(T variant : variants) {
						RenderUtil.renderBlock(BlockRegistry.getResourcePath(name), block, variant.getID(), variant.getName());
					}
				}
			}
		}
		
		return block;
	}

 

Thx in advance.

Bektor

Developer of Primeval Forest.

Posted (edited)

GameRegistry.register is private in 1.12, so I don't know how you're using it. You should be using the Registry events.

 

If you want to have a one-line registration and handle everything all at once regardless of how you want to register the block, 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.

Edited by Draco18s

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
2 minutes ago, Draco18s said:

GameRegistry.register is private in 1.12, so I don't know how you're using it. You should be using the Registry events.

 

I'm not using GameRegistry.register. The code is from 1.10.2 and my goal is to have a system which does something similiar in 1.12 using Registry events. I know how to register blocks and

items with the registry events, but I don't know how to do it in a flexible way like my old code allowed it.

 

4 minutes ago, Draco18s said:

If you want to have a one-line registration and handle everything all at once regardless of how you want to register the block, 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.

Ok,I'm looking into it. ;) 

Developer of Primeval Forest.

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.