Minecraft Modding: Blocks With GUIs

Introduction


Making blocks with GUIs can be confusing because there are several things that have to work together.  More importantly, you may not need all the complexity depending on what you are trying to do.

Do You Need An IInventory?


An IInventory is used to hold information about any stuff (items or block items) that are contained inside the block. For example, when you put fuel into a furnace it is going into that block's inventory. In other words, if you have custom slots in your GUI you will need an IInventory.

The player inventory is separate and the container helps combine (if needed) the player inventory with your custom IInventory.

Tip: Many people have the tile entity implement IInventory although that is not necessary.  You can also create an IInventory class separately and in fact there are cases where you might have an IInventory but no tile entity (see discussion below).

Do You Need A Container?


If your block will have an IInventory or if you want the GUI to interact with the player inventory then you should use a Container.  The Container will then help collect the inventories and sync them between client and server using an IGuiHandler.

The IGuiHandler


Warning: If you use a container then you must also have an IGuiHandler registered, otherwise your GUI will have very weird behavior (things disappear from slots, etc.)

The IGuiHandler helps create networking packets to sync up the containers on the client and server.

The IGuiHandler needs a unique GUI ID passed to it as a parameter, so I suggest creating an enum to help manage these IDs.

Do You Need A Tile Entity?


A tile entity is useful if the block has to "remember" complex data.  As you probably know, a block can store a little bit of data (4 bits) called "metadata".  So a block like a BlockFurnace uses that to remember which way the front of the block is facing.  In 1.8 the metadata is mapped into properties.

So if you had a very simple amount of things to remember (up to 16 things) then you could store that in metadata instead of using a tile entity.

In particular a tile entity is useful if you want the GUI to continue to do something even when it isn't open. For example a furnace will continue to cook something even if you're not in the GUI.

Examples where no tile entity is needed:
  • A simple block that just displays some information in the GUI, like maybe a sign that displays your mod's credits or help info.
  • A block where the processing happens instantaneously like a crafting table.
Examples where a tile entity is recommended:
  • A block where processing happens over time like cooking in a furnace.

Detailed Tutorials


Once you understand what kind of block GUI you're doing, you can check out my detailed tutorials for each type:

4 comments:

  1. Thanks for these tutorials! They are very helpful!

    I'm wondering when you'll be posting the "Block with immediate GUI result" tutorial. I want to implementing this sort of thing in my mod, and I'm having a bit of trouble tweaking the material in "Block that processes over time" for that purpose.

    Thanks for all of your help!

    ReplyDelete
    Replies
    1. Yeah, I guess it's about time for me to post that. Let me see if I can get something started on it today.

      Delete
    2. I look forward to your tutorial! My mod is just waiting for that, and after you post it I'll be able to release version 1!

      Delete
    3. Okay, I posted it. I didn't have a lot of time to really explain everything, and I hope to post a video soon to better illustrate the result. But hopefully it gives you enough idea to get you started.

      Delete