Advanced Modding - Tile Entity with Inventory and GUI


I want to show you how to add a GUI to our Tile Entity with inventory.


4/10 - Moderately Easy


Forge Version

This Tutorial was created with Forge for Minecraft 1.8. If anything doesn't work with other versions, please contact me!

Adding the GUI to the TileEntity

In the last tutorials we prepared everything to create a real Tile Entity with an inventory and a GUI.

Now, we are going to put everything together.

We need the Tile Entity with Inventory from the corresponding tutorial. Also, we need a GUI Handler.

The GUI Container that we created in the Tutorial about GUIs with inventory is exactly the one that we need. The only thing left to do is the GUI itself.

It's class extends GuiContainer and is located in com.bedrockminer.tutorial.client.gui (in my example at least).
package com.bedrockminer.tutorial.client.gui;

import net.minecraft.client.gui.inventory.GuiContainer;

public class GuiModTileEntity extends GuiContainer {


GuiContainer requires a constructor with a Container as argument. Our Container, ContainerModTileEntity, is the container we need to add here. Personally, I prefer to create the container in the constructor, but you also can add the container as an argument and create it in the GUI handler.

In this case it's useful to create the container in the constructor, because we need to save references to the player's inventory and the TileEntity for later.
private IInventory playerInv;
private ModTileEntity te;

public GuiModTileEntity(IInventory playerInv, ModTileEntity te) {
    super(new ContainerModTileEntity(playerInv, te));

    this.playerInv = playerInv;
    this.te = te;

    this.xSize = 176;
    this.ySize = 166;

As you can see I also set the GUI's size in the constructor.

Now, we need to override both rendering methods, drawGuiContainerForeground- and BackgroundLayer.

We'll use the background layer for the background texture and the foreground layer for the inventory title.
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {

protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {

In the method for the background we need to add the rendering code for our background texture. In this example, I use the same texture as the Minecraft dispenser.

One thing is important about this texture: It must be 256*256 pixels large, although our GUI is smaller. This is caused by the way the GUI methods work. You probably read something about this in the tutorial about drawing GUIs with vanilla methods. (If you use other methods like Miner's Basic, you don't necessarily need a 256*256 texture).

However, this is the texture I'll use: (I painted the background pink so you can see where the image ends)

Image file - Show

This image needs to be placed in the resource folder. I placed mine in assets/tutorial/textures/gui/container/ and named it mod_tile_entity.png.

You folder path can be different (the modid must be different!), you only need to enter the same path in the rendering code.

The rendering code for a simple image needs three methods:

The color is set to white, the image is bound to the renderer and the image is rendered.

For my example, the methods look like this:
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
    GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f); ResourceLocation("tutorial:textures/gui/container/mod_tile_entity.png"));
    this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize);

Two things are important.

First, you need to ensure that the resource location is correct, it must contain your image path.

Second, (obviously) the textured part on the image needs to match the size of the GUI (xSize, ySize). Otherwise it would look a bit weird.

The third method here simply draws the texture from the GUI's (and the texture's) top left corner with the x and y size.

Now we need to add the inventory titles in the other drawing method.

Here I want the TileEntity's title to be centered and the player's inventory's title to be leftbound.

A centered string is really easy: We just need to measure it's length and subtract half of it from the GUI's center coordinate  (176 / 2 = 88). That's what this method does.

Remember: in drawGuiContainerForegroundLayer, the origin 0|0 is at the top left corner of the GUI, not of the screen.
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
    String s = this.te.getDisplayName().getUnformattedText();
    this.fontRendererObj.drawString(s, 88 - this.fontRendererObj.getStringWidth(s) / 2, 6, 4210752);            //#404040
    this.fontRendererObj.drawString(this.playerInv.getDisplayName().getUnformattedText(), 8, 72, 4210752);      //#404040

By the way: the color code for the Strings is a gray color with the hexadecimal code #404040.

Also, the exact coordinates of a thing in a GUI can either be found via exact planning or just by testing.

This method uses the method getDisplayName of the IInventory interface to get the inventory's name. This method returns an IChatComponent, so we need to use the method getUnformattedString to receive a String for the rendering methods.

The last thing to do is to add the GUI to the GUI handler, then we're finished.

In the GUI handler we add an ID for the GUI and create if-statements for each method to return instances of our classes when the ID is requested.
public static final int MOD_TILE_ENTITY_GUI = 0;

public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        return new ContainerModTileEntity(player.inventory, (ModTileEntity) world.getTileEntity(new BlockPos(x, y, z)));

    return null;

public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        return new GuiModTileEntity(player.inventory, (ModTileEntity) world.getTileEntity(new BlockPos(x, y, z)));

    return null;

Now the GUI can be opened! If we add the opening method to the block's onBlockActivated method, it opens on right-click.
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ) {
    if (!world.isRemote) {
        player.openGui(Main.instance, ModGuiHandler.MOD_TILE_ENTITY_GUI, world, pos.getX(), pos.getY(), pos.getZ());
    return true;

A few worls left to say abount this method:

We add a check if the world is not remote (-> server side only) because the GUI handler automatically opens the GUI on client side as well.

The onBlockActivated method returns true to stop further actions like placing a block adjacent to the TileEntity. If this should be done, the player must sneak before clicking. (You know this behaviour from everything in Minecraft that has an inventory).

Now we are really finished. We've created a TileEntity with an inventory and a GUI!

You can download the code used in this tutorial as a .zip file from here.

Recommended tutorials to continue with

Take a look at the tutorial overview and decide what you want to do next.

Comments and Questions:

If you want to report modding problems, please make sure to include the code in a pastebin link or something else! Don't just write "It doesn't work", otherwise your post will be deleted. For more complicated problems, please use the troubleshooter form.