Jump to content

[Solved][1.9.4] Cleaner way to register blocks and itemblocks


American2050

Recommended Posts

I would like to know what would be a cleaner way to register blocks and itemblocks. I'm doing it in not such efficient way and it's always something that I leave for later, but now that I need to register a bunch of blocks, I guess it's time to ask, how to optimize this.

 

Here is an example of what I'm doing. I think I could somehow "loop" this using a for, but not sure what changes I should implement first.

 

public class ModCompressedBlocks {

public static final CompressBlock COMP_ROTFLESH_1 = new CompressBlock("rotten_flesh_1");
public static final ItemBlock COMP_ROTFLESH_1_ITEM = (ItemBlock) new ItemBlock(COMP_ROTFLESH_1).setRegistryName(COMP_ROTFLESH_1.getRegistryName());
public static final CompressBlock COMP_ROTFLESH_2 = new CompressBlock("rotten_flesh_2");
public static final ItemBlock COMP_ROTFLESH_2_ITEM = (ItemBlock) new ItemBlock(COMP_ROTFLESH_2).setRegistryName(COMP_ROTFLESH_2.getRegistryName());
public static final CompressBlock COMP_ROTFLESH_3 = new CompressBlock("rotten_flesh_3");
public static final ItemBlock COMP_ROTFLESH_3_ITEM = (ItemBlock) new ItemBlock(COMP_ROTFLESH_3).setRegistryName(COMP_ROTFLESH_3.getRegistryName());
public static final CompressBlock COMP_ROTFLESH_4 = new CompressBlock("rotten_flesh_4");
public static final ItemBlock COMP_ROTFLESH_4_ITEM = (ItemBlock) new ItemBlock(COMP_ROTFLESH_4).setRegistryName(COMP_ROTFLESH_4.getRegistryName());
public static final CompressBlock COMP_ROTFLESH_5 = new CompressBlock("rotten_flesh_5");
public static final ItemBlock COMP_ROTFLESH_5_ITEM = (ItemBlock) new ItemBlock(COMP_ROTFLESH_5).setRegistryName(COMP_ROTFLESH_5.getRegistryName());
public static final CompressBlock COMP_ROTFLESH_6 = new CompressBlock("rotten_flesh_6");
public static final ItemBlock COMP_ROTFLESH_6_ITEM = (ItemBlock) new ItemBlock(COMP_ROTFLESH_6).setRegistryName(COMP_ROTFLESH_6.getRegistryName());
public static final CompressBlock COMP_ROTFLESH_7 = new CompressBlock("rotten_flesh_7");
public static final ItemBlock COMP_ROTFLESH_7_ITEM = (ItemBlock) new ItemBlock(COMP_ROTFLESH_7).setRegistryName(COMP_ROTFLESH_7.getRegistryName());
public static final CompressBlock COMP_ROTFLESH_8 = new CompressBlock("rotten_flesh_8");
public static final ItemBlock COMP_ROTFLESH_8_ITEM = (ItemBlock) new ItemBlock(COMP_ROTFLESH_.setRegistryName(COMP_ROTFLESH_8.getRegistryName());
public static final CompressBlock COMP_ROTFLESH_9 = new CompressBlock("rotten_flesh_9");
public static final ItemBlock COMP_ROTFLESH_9_ITEM = (ItemBlock) new ItemBlock(COMP_ROTFLESH_9).setRegistryName(COMP_ROTFLESH_9.getRegistryName());


public static final CompressBlock COMP_GUNPOWDER_1 = new CompressBlock("gunpowder_1");
public static final ItemBlock COMP_GUNPOWDER_1_ITEM = (ItemBlock) new ItemBlock(COMP_GUNPOWDER_1).setRegistryName(COMP_GUNPOWDER_1.getRegistryName());
public static final CompressBlock COMP_GUNPOWDER_2 = new CompressBlock("gunpowder_2");
public static final ItemBlock COMP_GUNPOWDER_2_ITEM = (ItemBlock) new ItemBlock(COMP_GUNPOWDER_2).setRegistryName(COMP_GUNPOWDER_2.getRegistryName());
public static final CompressBlock COMP_GUNPOWDER_3 = new CompressBlock("gunpowder_3");
public static final ItemBlock COMP_GUNPOWDER_3_ITEM = (ItemBlock) new ItemBlock(COMP_GUNPOWDER_3).setRegistryName(COMP_GUNPOWDER_3.getRegistryName());
public static final CompressBlock COMP_GUNPOWDER_4 = new CompressBlock("gunpowder_4");
public static final ItemBlock COMP_GUNPOWDER_4_ITEM = (ItemBlock) new ItemBlock(COMP_GUNPOWDER_4).setRegistryName(COMP_GUNPOWDER_4.getRegistryName());
public static final CompressBlock COMP_GUNPOWDER_5 = new CompressBlock("gunpowder_5");
public static final ItemBlock COMP_GUNPOWDER_5_ITEM = (ItemBlock) new ItemBlock(COMP_GUNPOWDER_5).setRegistryName(COMP_GUNPOWDER_5.getRegistryName());
public static final CompressBlock COMP_GUNPOWDER_6 = new CompressBlock("gunpowder_6");
public static final ItemBlock COMP_GUNPOWDER_6_ITEM = (ItemBlock) new ItemBlock(COMP_GUNPOWDER_6).setRegistryName(COMP_GUNPOWDER_6.getRegistryName());
public static final CompressBlock COMP_GUNPOWDER_7 = new CompressBlock("gunpowder_7");
public static final ItemBlock COMP_GUNPOWDER_7_ITEM = (ItemBlock) new ItemBlock(COMP_GUNPOWDER_7).setRegistryName(COMP_GUNPOWDER_7.getRegistryName());
public static final CompressBlock COMP_GUNPOWDER_8 = new CompressBlock("gunpowder_8");
public static final ItemBlock COMP_GUNPOWDER_8_ITEM = (ItemBlock) new ItemBlock(COMP_GUNPOWDER_.setRegistryName(COMP_GUNPOWDER_8.getRegistryName());
public static final CompressBlock COMP_GUNPOWDER_9 = new CompressBlock("gunpowder_9");
public static final ItemBlock COMP_GUNPOWDER_9_ITEM = (ItemBlock) new ItemBlock(COMP_GUNPOWDER_9).setRegistryName(COMP_GUNPOWDER_9.getRegistryName());



public static void init() {

	GameRegistry.register(COMP_ROTFLESH_1);GameRegistry.register(COMP_ROTFLESH_1_ITEM);
	GameRegistry.register(COMP_ROTFLESH_2);GameRegistry.register(COMP_ROTFLESH_2_ITEM);
	GameRegistry.register(COMP_ROTFLESH_3);GameRegistry.register(COMP_ROTFLESH_3_ITEM);
	GameRegistry.register(COMP_ROTFLESH_4);GameRegistry.register(COMP_ROTFLESH_4_ITEM);
	GameRegistry.register(COMP_ROTFLESH_5);GameRegistry.register(COMP_ROTFLESH_5_ITEM);
	GameRegistry.register(COMP_ROTFLESH_6);GameRegistry.register(COMP_ROTFLESH_6_ITEM);
	GameRegistry.register(COMP_ROTFLESH_7);GameRegistry.register(COMP_ROTFLESH_7_ITEM);
	GameRegistry.register(COMP_ROTFLESH_;GameRegistry.register(COMP_ROTFLESH_8_ITEM);
	GameRegistry.register(COMP_ROTFLESH_9);GameRegistry.register(COMP_ROTFLESH_9_ITEM);


	GameRegistry.register(COMP_GUNPOWDER_1);GameRegistry.register(COMP_GUNPOWDER_1_ITEM);
	GameRegistry.register(COMP_GUNPOWDER_2);GameRegistry.register(COMP_GUNPOWDER_2_ITEM);
	GameRegistry.register(COMP_GUNPOWDER_3);GameRegistry.register(COMP_GUNPOWDER_3_ITEM);
	GameRegistry.register(COMP_GUNPOWDER_4);GameRegistry.register(COMP_GUNPOWDER_4_ITEM);
	GameRegistry.register(COMP_GUNPOWDER_5);GameRegistry.register(COMP_GUNPOWDER_5_ITEM);
	GameRegistry.register(COMP_GUNPOWDER_6);GameRegistry.register(COMP_GUNPOWDER_6_ITEM);
	GameRegistry.register(COMP_GUNPOWDER_7);GameRegistry.register(COMP_GUNPOWDER_7_ITEM);
	GameRegistry.register(COMP_GUNPOWDER_;GameRegistry.register(COMP_GUNPOWDER_8_ITEM);
	GameRegistry.register(COMP_GUNPOWDER_9);GameRegistry.register(COMP_GUNPOWDER_9_ITEM);

}

//This gets called on ClientPoxy
public static void registerRenders() {

	registerRender(COMP_ROTFLESH_1, COMP_ROTFLESH_1.getRegistryName().toString());
	registerRender(COMP_ROTFLESH_2, COMP_ROTFLESH_2.getRegistryName().toString());
	registerRender(COMP_ROTFLESH_3, COMP_ROTFLESH_3.getRegistryName().toString());
	registerRender(COMP_ROTFLESH_4, COMP_ROTFLESH_4.getRegistryName().toString());
	registerRender(COMP_ROTFLESH_5, COMP_ROTFLESH_5.getRegistryName().toString());
	registerRender(COMP_ROTFLESH_6, COMP_ROTFLESH_6.getRegistryName().toString());
	registerRender(COMP_ROTFLESH_7, COMP_ROTFLESH_7.getRegistryName().toString());
	registerRender(COMP_ROTFLESH_8, COMP_ROTFLESH_8.getRegistryName().toString());
	registerRender(COMP_ROTFLESH_9, COMP_ROTFLESH_9.getRegistryName().toString());

	registerRender(COMP_GUNPOWDER_1, COMP_GUNPOWDER_1.getRegistryName().toString());
	registerRender(COMP_GUNPOWDER_2, COMP_GUNPOWDER_2.getRegistryName().toString());
	registerRender(COMP_GUNPOWDER_3, COMP_GUNPOWDER_3.getRegistryName().toString());
	registerRender(COMP_GUNPOWDER_4, COMP_GUNPOWDER_4.getRegistryName().toString());
	registerRender(COMP_GUNPOWDER_5, COMP_GUNPOWDER_5.getRegistryName().toString());
	registerRender(COMP_GUNPOWDER_6, COMP_GUNPOWDER_6.getRegistryName().toString());
	registerRender(COMP_GUNPOWDER_7, COMP_GUNPOWDER_7.getRegistryName().toString());
	registerRender(COMP_GUNPOWDER_8, COMP_GUNPOWDER_8.getRegistryName().toString());
	registerRender(COMP_GUNPOWDER_9, COMP_GUNPOWDER_9.getRegistryName().toString());


}


public static void registerRender(Block block, String name) {

	RenderItem renderItem = Minecraft.getMinecraft().getRenderItem();
	renderItem.getItemModelMesher().register(Item.getItemFromBlock(block), 0, new ModelResourceLocation(name, "inventory"));


}

Link to comment
Share on other sites

Thanks you very much.

 

I kinda got the idea, and now it looks way way cleaner.

 

Still confused about how to use the ModelLoader.setCustomModelResourceLocation well, I kinda know how to use it, but I must be either missing something very basic, or doing something wrong, as when trying it, textures don't show.

 

Tried calling that from ClientProxy, and from preInit and nothing. Maybe I'm messing the structure on where it's looking for the textures.

 

Here is what I did (Commented what didn't worked) as it is not, it works and blocks renders.

 

public class ModCompressedBlocks {

public static final CompressBlock COMP_ROTFLESH_1 = new CompressBlock("rotten_flesh_1");
public static final CompressBlock COMP_ROTFLESH_2 = new CompressBlock("rotten_flesh_2");
public static final CompressBlock COMP_ROTFLESH_3 = new CompressBlock("rotten_flesh_3");
public static final CompressBlock COMP_ROTFLESH_4 = new CompressBlock("rotten_flesh_4");
public static final CompressBlock COMP_ROTFLESH_5 = new CompressBlock("rotten_flesh_5");
public static final CompressBlock COMP_ROTFLESH_6 = new CompressBlock("rotten_flesh_6");
public static final CompressBlock COMP_ROTFLESH_7 = new CompressBlock("rotten_flesh_7");
public static final CompressBlock COMP_ROTFLESH_8 = new CompressBlock("rotten_flesh_8");
public static final CompressBlock COMP_ROTFLESH_9 = new CompressBlock("rotten_flesh_9");

public static final CompressBlock COMP_GUNPOWDER_1 = new CompressBlock("gunpowder_1");
public static final CompressBlock COMP_GUNPOWDER_2 = new CompressBlock("gunpowder_2");
public static final CompressBlock COMP_GUNPOWDER_3 = new CompressBlock("gunpowder_3");
public static final CompressBlock COMP_GUNPOWDER_4 = new CompressBlock("gunpowder_4");
public static final CompressBlock COMP_GUNPOWDER_5 = new CompressBlock("gunpowder_5");
public static final CompressBlock COMP_GUNPOWDER_6 = new CompressBlock("gunpowder_6");
public static final CompressBlock COMP_GUNPOWDER_7 = new CompressBlock("gunpowder_7");
public static final CompressBlock COMP_GUNPOWDER_8 = new CompressBlock("gunpowder_8");
public static final CompressBlock COMP_GUNPOWDER_9 = new CompressBlock("gunpowder_9");

//This gets called on MainClass preInit
public static void init() {

	registerBlock(COMP_ROTFLESH_1);
	registerBlock(COMP_ROTFLESH_2);
	registerBlock(COMP_ROTFLESH_3);
	registerBlock(COMP_ROTFLESH_4);
	registerBlock(COMP_ROTFLESH_5);
	registerBlock(COMP_ROTFLESH_6);
	registerBlock(COMP_ROTFLESH_7);
	registerBlock(COMP_ROTFLESH_;
	registerBlock(COMP_ROTFLESH_9);

	registerBlock(COMP_GUNPOWDER_1);
	registerBlock(COMP_GUNPOWDER_2);
	registerBlock(COMP_GUNPOWDER_3);
	registerBlock(COMP_GUNPOWDER_4);
	registerBlock(COMP_GUNPOWDER_5);
	registerBlock(COMP_GUNPOWDER_6);
	registerBlock(COMP_GUNPOWDER_7);
	registerBlock(COMP_GUNPOWDER_;
	registerBlock(COMP_GUNPOWDER_9);

}


//This gets called on ClientPoxy
public static void registerRenders() {

	registerRender(COMP_ROTFLESH_1);
	registerRender(COMP_ROTFLESH_2);
	registerRender(COMP_ROTFLESH_3);
	registerRender(COMP_ROTFLESH_4);
	registerRender(COMP_ROTFLESH_5);
	registerRender(COMP_ROTFLESH_6);
	registerRender(COMP_ROTFLESH_7);
	registerRender(COMP_ROTFLESH_;
	registerRender(COMP_ROTFLESH_9);

	registerRender(COMP_GUNPOWDER_1);
	registerRender(COMP_GUNPOWDER_2);
	registerRender(COMP_GUNPOWDER_3);
	registerRender(COMP_GUNPOWDER_4);
	registerRender(COMP_GUNPOWDER_5);
	registerRender(COMP_GUNPOWDER_6);
	registerRender(COMP_GUNPOWDER_7);
	registerRender(COMP_GUNPOWDER_;
	registerRender(COMP_GUNPOWDER_9);

}


public static void registerBlock(CompressBlock block) {

	GameRegistry.register(block);
	GameRegistry.register(new ItemBlock(block).setRegistryName(block.getRegistryName()));

//		ModelResourceLocation modelLocation = new ModelResourceLocation(block.getRegistryName(), "inventory");
//		ModelLoader.setCustomModelResourceLocation(new ItemBlock(block), block.getIdFromBlock(block),  modelLocation);
}

public static void registerRender(Block block) {

	RenderItem renderItem = Minecraft.getMinecraft().getRenderItem();
	renderItem.getItemModelMesher().register(Item.getItemFromBlock(block), 0, new ModelResourceLocation(block.getRegistryName().toString(), "inventory"));

//		ModelResourceLocation modelLocation = new ModelResourceLocation(block.getRegistryName(), "inventory");
//		ModelLoader.setCustomModelResourceLocation(new ItemBlock(block), block.getIdFromBlock(block),  modelLocation);

}




}

Link to comment
Share on other sites

setCustomModelResourceLocation wants a metadata. but I derped, I called the wrong method :P I guess I could just put 0 there and it should do.

 

Thanks for the tip, gonna try to fix it and see how it goes.

 

Is it ok to call it just from ClientProxy for the render right?

 

 

Link to comment
Share on other sites

Thanks a lot, and thanks for the patience. I hope I didn't remove to much code :P

 

Well, that's why I called the registerRenders only from ClientProxy but using this new way to register the textures, apparently didn't work that way.

 

So, finally, now the code got reduced to this. Huge different, way more cleaner, and I hope it's ok :D At least it works, that's important.

 

public class ModCompressedBlocks {

public static final CompressBlock COMP_ROTFLESH_1 = new CompressBlock("rotten_flesh_1");
public static final CompressBlock COMP_ROTFLESH_2 = new CompressBlock("rotten_flesh_2");
public static final CompressBlock COMP_ROTFLESH_3 = new CompressBlock("rotten_flesh_3");
public static final CompressBlock COMP_ROTFLESH_4 = new CompressBlock("rotten_flesh_4");
public static final CompressBlock COMP_ROTFLESH_5 = new CompressBlock("rotten_flesh_5");
public static final CompressBlock COMP_ROTFLESH_6 = new CompressBlock("rotten_flesh_6");
public static final CompressBlock COMP_ROTFLESH_7 = new CompressBlock("rotten_flesh_7");
public static final CompressBlock COMP_ROTFLESH_8 = new CompressBlock("rotten_flesh_8");
public static final CompressBlock COMP_ROTFLESH_9 = new CompressBlock("rotten_flesh_9");

public static final CompressBlock COMP_GUNPOWDER_1 = new CompressBlock("gunpowder_1");
public static final CompressBlock COMP_GUNPOWDER_2 = new CompressBlock("gunpowder_2");
public static final CompressBlock COMP_GUNPOWDER_3 = new CompressBlock("gunpowder_3");
public static final CompressBlock COMP_GUNPOWDER_4 = new CompressBlock("gunpowder_4");
public static final CompressBlock COMP_GUNPOWDER_5 = new CompressBlock("gunpowder_5");
public static final CompressBlock COMP_GUNPOWDER_6 = new CompressBlock("gunpowder_6");
public static final CompressBlock COMP_GUNPOWDER_7 = new CompressBlock("gunpowder_7");
public static final CompressBlock COMP_GUNPOWDER_8 = new CompressBlock("gunpowder_8");
public static final CompressBlock COMP_GUNPOWDER_9 = new CompressBlock("gunpowder_9");

//This gets called on MainClass preInit
public static void init() {

	registerBlock(COMP_ROTFLESH_1);
	registerBlock(COMP_ROTFLESH_2);
	registerBlock(COMP_ROTFLESH_3);
	registerBlock(COMP_ROTFLESH_4);
	registerBlock(COMP_ROTFLESH_5);
	registerBlock(COMP_ROTFLESH_6);
	registerBlock(COMP_ROTFLESH_7);
	registerBlock(COMP_ROTFLESH_;
	registerBlock(COMP_ROTFLESH_9);

	registerBlock(COMP_GUNPOWDER_1);
	registerBlock(COMP_GUNPOWDER_2);
	registerBlock(COMP_GUNPOWDER_3);
	registerBlock(COMP_GUNPOWDER_4);
	registerBlock(COMP_GUNPOWDER_5);
	registerBlock(COMP_GUNPOWDER_6);
	registerBlock(COMP_GUNPOWDER_7);
	registerBlock(COMP_GUNPOWDER_;
	registerBlock(COMP_GUNPOWDER_9);

}

public static void registerBlock(CompressBlock block) {

	GameRegistry.register(block);
	GameRegistry.register(new ItemBlock(block).setRegistryName(block.getRegistryName()));

	ModelResourceLocation modelLocation = new ModelResourceLocation(block.getRegistryName(), "inventory");
	ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(block), block.getMetaFromState(block.getDefaultState()),  modelLocation);
}


}

Link to comment
Share on other sites

Clearly I did something wrong, as it now crashes when trying to launch Server and not Client.

 

java.lang.NoClassDefFoundError: net/minecraftforge/client/model/ModelLoader
at com.mramericanmike.mikedongles.init.ModCompressedBlocks.registerBlock(ModCompressedBlocks.java:68) ~[bin/:?]

 

So yes, clearly it's not ok to call it outside the ClientSide, but dunno why nothing renders when I call it from ClientSide.

 

Gonna do more testing or revert to my old method in the meanwhile ;)

Link to comment
Share on other sites

public static void registerBlock(CompressBlock block) {

 

GameRegistry.register(block);

GameRegistry.register(new ItemBlock(block).setRegistryName(block.getRegistryName()));

 

ModelResourceLocation modelLocation = new ModelResourceLocation(block.getRegistryName(), "inventory");

ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(block), block.getMetaFromState(block.getDefaultState()),  modelLocation);

}

 

dont do this server side

 

ModelResourceLocation modelLocation = new ModelResourceLocation(block.getRegistryName(), "inventory");

ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(block), block.getMetaFromState(block.getDefaultState()),  modelLocation);

 

Link to comment
Share on other sites

Show how you tried to call it from the client.

 

[[As I post this, I notice I'm still calling it from 'init'.... Could that be the problem?]]

 

On Main class:

@SidedProxy(clientSide = ModInfo.CLIENT_PROXY_CLASS, serverSide = ModInfo.SERVER_PROXY_CLASS)
public static IProxy proxy;	

    @EventHandler
    public void init(FMLInitializationEvent event)
    {
    	//Register Renders - This will call the right Side - Check ClientProxy for Items and Blocks render registration
    	proxy.registerRender();
    	proxy.otherInits();
    }

 

And on ClientProxy:

	@Override
public void registerRender()
{
	//Register Renders on Client Side Only
	ModItems.registerRenders();
	ModBlocks.registerRenders();

	ModCompressedBlocks.registerRenders();
}

 

PS: Right now, all that works, but because I'm using the old method, when I switch to the ModelLoader that is commented, it doesn't work.

 

public static void registerRenders() {

	registerRender(COMP_ROTFLESH_1);
	registerRender(COMP_ROTFLESH_2);
	registerRender(COMP_ROTFLESH_3);
	registerRender(COMP_ROTFLESH_4);
	registerRender(COMP_ROTFLESH_5);
	registerRender(COMP_ROTFLESH_6);
	registerRender(COMP_ROTFLESH_7);
	registerRender(COMP_ROTFLESH_;
	registerRender(COMP_ROTFLESH_9);

}


public static void registerRender(CompressBlock block){

	RenderItem renderItem = Minecraft.getMinecraft().getRenderItem();
	renderItem.getItemModelMesher().register(Item.getItemFromBlock(block), 0, new ModelResourceLocation(block.getRegistryName().toString(), "inventory"));

//		ModelResourceLocation modelLocation = new ModelResourceLocation(block.getRegistryName(), "inventory");
//		ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(block), block.getMetaFromState(block.getDefaultState()),  modelLocation);

}

 

 

Link to comment
Share on other sites

Ok, gonna implement ModelLoader for my items also and try it again ;) Will let you know if that fixes it.

 

Thanks you once again.

 

PS: Solved and changed the title to solved. Thanks 100000 for helping with this, I feel like the code if way cleaner now, and for sure more efficient. I was something I knew I had to do at some point ;)

Link to comment
Share on other sites

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.