Tutorial 13. Custom Inventory

Before you read, make sure you have seen the previous tutorial

Code is on github

Ready for some difficult stuff?
Ye? Cool. Let’s go.

A custom inventory = A TileEntity with a GUI, and in that TileEntity, you can store stuff.
Therefor, make sure you have watched TutorialTileEntity and TutorialGUI

For this tutorial, i will make a new Tile Entity, and a new GUI class.
Mostly will be the same.

So, add a new block.

All of this is already explained.
The only thing different, is that we extend Container. Containers can hold items (inventories). Note that we open a GUI with id 1!

Few differences here.
We extends GUIContainer. This just because its easier, and made for containers :3
Also, we have to call our super class now. GUIContainer needs that. Therefor, we need our TileEntity. I will tell you how to get that in a second. Also, set your gui sizes again.


My GUI:

myguimygui

So, as i said, we need our TileEntity in our GUI class. In our GUIHandler, we get our TE-Coordinates. With that, we can create a TE Object, which we can give to our GUI ánd Inventory (created in a bit). We set the id for this gui to 1. Note, Inventories are Server side.


For the new stuff: Lets create an Inventory.

Now, create the constructor. We get an Inventory, and our TE. To store our TE in this class, we make an object of that

Now, lets bind the standard player’s inventory.

The first for-loop, with i<9, is the players hotbar.
We create a basic slot for that, with inventoryPlayer, an id, the xCoord of the slot, and the yCoord of the slot.
We do the same for the player’s normal inventory. To make sure our ID is right, we postincrement that every time.

To fix shiftClicking, add this:

 

Now, lets add our own slots, and override canInteract with

To make it easier, we will use a 2nd ID param for our custom slots.
As you see, we cant use the normal Slots. We have to create our own.

Not too hard i’d say. The thing here to note, is that i set it to only accept my custom item.

Last and hardest thing, our container. Since we implement IInventory, we need to override a lot of stuff.
I think, if you read the comments, it should be pretty clear what they do.

DONE.
almost…

In commonProxy, register your TE

 

Finally…

Back to List

Next tutorial


2 Responses to Tutorial 13. Custom Inventory

  1. Mark says:

    the addToSlotContainer method in InventoryTest class from Container is protected; how can i use that method?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">