• If you have a mod, tool or prefab, please use the Resources section. Click Mods at the top of the forums.

How are IDs assigned to items and blocks?

Karlovsky120

New member
I've been scratching my head for a while now.

Since A17, items no longer have id attribute in items.xml.

In save files, they are still identified with a number, a short or int, can't remember exactly now.

I've been looking at the source code of the game, but I'm running around in circles. I cannot figure out how does the game decide which number to map to which specific item.

If anyone knows how this work, I'd really appreciate it!

 
The order of the block names in the blocks.xml defines their IDs (IDs lower than 256 are reserved for terrain blocks btw.).
Afaik same is true for items.

 
How does this work then?

What xmls are included in this? Items.xml and block.xml? What about item_modifiers.xml?

How do I count them? Do I start at the blocks with zero and them just increment it one by one? Where does sub 256 limit come in? Do I do just one section from zero and then continue from 256 from another?

Once I'm done with them, do I just continue with the items, or do they start as some number such as 4096 or something like that?

 
What xmls are included in this? Items.xml and block.xml? What about item_modifiers.xml?
I've no clue about item_modifiers.xml.

Do I start at the blocks with zero and them just increment it one by one?
Yes. 0 is the air block btw. Ids of the blocks.xml go from 0 to 2^15-1 (0 to 32767).

Where does sub 256 limit come in? Do I do just one section from zero and then continue from 256 from another?
VV7drwx.jpg


Not sure if the water blocks have to be at this position. There is a comment in the blocks.xml. Afaik there are no ID gaps in the items.xml.

Once I'm done with them, do I just continue with the items, or do they start as some number such as 4096 or something like that?
Items use a separate counter which start from 0.

 
Okay, given what you said and what I found in the source code, this is what I know:

There is a class called BlocksFromXml with a method that takes the blocks.xml and for each block in the xml creates a new block object in the game. There is also a ItemsFromXml class that does the same for the items.

Now, I don't really care for blocks as blocks, but as items you get from them.

There is a small method again that will copy certain block parameters and create item from them.

I think this is the workflow:

First, it loads/generates blocks from blocks.xml, I assume with IDs as you described. Then, it generates items from all of the blocks. Those items most likely have the same ID that the blocks they represent do.

Once that is done, it loads/generates items from items.xml, but I'm assuming the IDs here start with 32769, so they don't overlap with the items generated from the blocks. The block limit in the source code is 32768, as you said (IDs go from 0 to 32767), while the item limit is 2*block limit or 65536 (which I also found in the source code). I'm assuming they are read sequentially, but that's based solely on your word, since I cannot find that bit in the code.

That leaves me with a few things I'd like to clear up with someone more familiar with the code, if there are such people here.

Both methods that load the xmls into ItemClass and Block objects should not work at all. They return void, and the ItemClass/Block that they generated is never stored (referenced) by anything else in the method, meaning that it would be collected by garbage collector and deleted. Essentially, from what I can tell, while the methods parses xml the way it should, the result is not stored anywhere.

The second thing that's bothering me is that nowhere in the code I can't find where an item/block is explicitly given an id. There are a bunch of dictionaries that map item/block names to ids and vice versa and whatnot, but for none of them I couldn't figure out how they were created, or how IDs were mapped to item name or object. I would find that one dictionary is created by looking things up from another dictionary, while that other dictionary is created with the data from the first, which leaves me very confused.

If anyone would be kind enough to explain to me how any of this works, I would be really grateful.

 
Back
Top