Minecraft Version: Tested in 1.6.x and 1.7.x (use getTileEntity instead of getBlockTileEntity in 1.7.x)
MultiBlock structures are a nice way to make advance blocks without dealing with adding multiple recipes as an arbitrary way to make said block expensive. So, how does one go about creating their own MultiBlock? This tutorial will teach you how to make a basic 3x3x3 MultiBlock structure with a hollow center using a Master-Slave system.
This tutorial assumes you have basic knowledge of how to make a basic mod, creating TileEntities, and dealing with NBT data. If you don’t, I recommend reading/watching Mcjty's tutorials, they’re an excellent source of information, and pretty much the defacto place for learning how to mod.
Please note that there are multiple ways to create a MultiBlock structure and this method may not be the best. Also note that the way we will be making the MultiBlock will make it like the ones in RailCraft, in which all the data is stored in the bottom center block of the structure, so keep that in mind if you don’t want your structure to work that way.
Step 1: Create a basic TileEntity
To get started, where going to need a basic TileEntity, which I will be calling TileMultiBlock for this tutorial. I won’t go over what each method is for, but just note this will be our starting point.
We will also be needing a few variables, two boolean for the tile to see if it has a master and/or if it is the master of the structure and three integers for the coordinates of the master for the slaves to use. Make sure to add getters and setters for each one and have them saved in the Tile’s NBT data.
Step 2: Check and form MultiBlock
Next step is to make a method to check is the structure was properly formed. The easiest way is to make three for loops (for the x, y, and z coordinates) and check to see if there is a TileEntity at each coordinate and counting it if it is an instance of TileMultiBlock. Then counting them up to see if the right number of TileEntities are present and checking if the center is empty.
We also need a method to tell all those tiles that they now have a master and where it’s located at. You’ll also have to tell the bottom center block that it is the master. This method will only be used by the master once it detects that the structure is formed.
Step 3: Resetting the blocks
Now we need setup methods to check if the structure is invalidated and to reset all the parts. The master block can reuse the checkMultiBlockForm method for checking the structure, but the slave blocks should also constantly check that the master still exists in case the master is broken first, and for them to reset themselves if it no longer exists.
We also need a method for the master to use if checkMultiBlockForm returns false. Since we already have the reset method, we can just make a clone of setupStructure that resets stuff instead.
Step 4: Putting it all together
Now that we have all the parts we need ready, all that’s left is to make it actually run. What we basically need is for the each block to constantly check if the structure is formed until it is. Once it is, we only want it to check when there is a block change, in which the master will check if the structure is formed and the slaves check if the master still exists.
First, lets make sure our tiles check for the structure is formed and to make sure only the master block does work. All of this will be put in the updateEntity method.
Now, we need to have our master check if the structure is still formed or our slaves to check if the master is still in tact, which we will do in our onNeighborBlockChange in our block class.
And there ya go. Not too difficult when you look at it. Again, probably not the best way to do it, but it works. Remember that the master should be the only one doing work and saving NBT data, so remember that when you writing your code (I added comments for locations where it should be placed for a basic tile entity). If I’m missing anything, or if you don’t understand, leave a comment or contact me.
Since a few people have been having problems understanding this without an example, I made an example mod that is based of this tutorial. You can take a look for yourself here: https://github.com/Lomeli12/MultiBlock-Tutorial/
Edit 1: Small edit to take into account if a Tile is already a part of a mulitblock structure.
Edit 2: Fixed typos and missing TileEntity objects.
Edit 3: Fixed some things that may break the multiblock.
Edit 4: Thanks to Kihira for pointing out it’s I should only re-validate the structure on block neighbour change.
Edit 5: Moved my website to a new server and cms. Using Prism for highlighting.
Edit 6: Found a nice replacement for the code hightlighting/show hide =D
Edit 7: Wuppy doesn't exist anymore. =( Replaced with link to Mcjty's excellent tutorials.