Jump to content

Get all blocks and items of a mod


ss7

Recommended Posts

Hello,

 

I'm trying to make a config file for my block and item id's. But unfortunately then i would need to make a var for each of my blocks containing the config id. And then i had to make this line of this code for each block:

 

config.get(Configuration.CATEGORY_BLOCKS, "test", 0);

 

And i thought: Why can't i just get all the blocks and items of my mod and iterate through all of them?

I found out that all blocks are added to a private Multimap in GameRegistry and i could access that map with reflection.

But i don't think that's a good way of doing this.

 

Do you guys have ideas how to do this?

 

Thanks in advance!

 

ss7

You sir are a god damn hero.

Link to comment
Share on other sites

Hello,

 

OK, i think i can't do that because i can't change the block id after i registered the block.

But anyways: Here is the code to get only blocks of a mod:

 

 

Field blocks;

	Multimap<ModContainer, BlockProxy> blockregistry = null;

	try
	{
		blocks = GameRegistry.class.getDeclaredField("blockRegistry");

		blocks.setAccessible(true);

		blockregistry = (Multimap<ModContainer, BlockProxy>)blocks.get(new GameRegistry());
	}

	catch (Exception ex)
	{
		ex.printStackTrace();
	}

	if (blockregistry.size() > 0)
	{
		for (ModContainer modcontainer : Loader.instance().getModList())
		{				
			if (modcontainer.getModId().equals("modid"))
			{					
				for (BlockProxy block : blockregistry.get(modcontainer))
				{
					System.out.println(block.getClass().getSimpleName());
				}
			}
		}
	}

 

 

ss7

You sir are a god damn hero.

Link to comment
Share on other sites

That's pretty cool - I was trying to figure out how to get the loaded mods :)

 

I know this isn't quite what you're looking for, but the way I get around creating variables for every single block is to create a single int modBlockIndex and increment it each time I initialize a Block:

private static int modBlockIndex;

public static Block yourBlock1, yourBlock2, etc;

// pre-init: get modBlockIndex start from config

// init:
yourBlock1 = new Block(modBlockIndex++);
yourBlock2 = new Block(modBlockIndex++);
etc.

This way your ids are still configurable as a group, but saves the hassle of all those extra variables. I do the same for items.

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.