Jump to content

How does Minecraft internally store blocks in memory?


Recommended Posts

Posted

I've been reading through the source because I'm interested in how Minecraft works from a programming point of view.

 

So far I've found Block.java and IBlockAccess.java, and they have methods such as "isAirBlock(x,y,z)" etc.

 

However I cannot find anywhere in the source where or how it keeps blocks in memory. For example, are the blocks kept in some kind of three dimensional array or list that is declared in "Chunk.java"?

 

The nearest I've been able to find is:

    /** List of ly/ff (BlockType) containing the already registered blocks. */
    public static final Block[] blocksList = new Block[4096];

 

However by the looks of it, that is just used for registering new block types.

Posted
public class Chunk
{

    /**
     * Used to store block IDs, block MSBs, Sky-light maps, Block-light maps, and metadata. Each entry corresponds to a
     * logical segment of 16x16x16 blocks, stacked vertically.
     */
    private ExtendedBlockStorage[] storageArrays;

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

i think the mcp team mush have derped, a chunk is 16x16x256 :P

 

A chunk is an array of ExtendedBlockStorage objects, each of which is 16x16x16.  So 16 of them. :D

(An ExtendedBlockStorage is a fancy class that stores blocks as two arrays: one for the least significant bits as a byte and one, complex data type that is effectively a byte with some other functions, for the most significant bits).

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

What draco said ^

 

+ if you look into world.setBlock methods, and follow it down into it's deepest level you will get to the extended block storage, that should give you a view of exactly how a block is set or get from the world :)

 

Also this blog may give insight into some of minecrafts internals:

http://greyminecraftcoder.blogspot.no/p/list-of-topics.html

If you guys dont get it.. then well ya.. try harder...

Posted

Also some helpful insight from XCompWiz:

 

I made a constructor in Forge to allow for block arrays of short ids to work.  The indexing is different from the standard chunk block array, so note that.

 

The main thing is that it is normally a byte array.  Byte only accept values from -128 to 127.  This means if you try to put in a value that's larger it will cut off the "top" of it.

 

The constructor I wrote uses a short array for the block id and another array for the metadata.  The indexing for these is reverse the vanilla way.

Vanilla indexes the arrays such that the y coordinate is the "lowest order bits", which is why world generation only supports up to y127.  I put the y coordinate on the higher order bits, with x and z (which are fixed constants due to the size of a chunk) on the lower bits.

I don't remember which of x or z is the lowest order of bits, but it should be possible to check from the constructor once you find it.

The operation to build the array index from coordinates should be (y << 8 | z << 4 | x).  That uses bit-shifting, so if you don't understand it I recommend looking that term up.

 

Vanilla uses (x << 11 | z << 7 | y).  Moving y to the higher order bits allows us to express values larger than 127.  You can use this to generate up to 255 easily.  In theory, it's possible to make the world height go up even further, but I've not tested this.  My chunk constructor wouldn't care, but the storage system might.  Note that all that needs to happen for the constructor to do this is to make the array you pass in bigger (it gets the y height of the array by array.length/256).

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

Thanks a lot draco! <3

 

Came as a result of someone needing blockIDs > 255 in their worldgen, so they posted on BinaryMage for some help and that was supplied, figured it was relevant here. :)

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.

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.