Jump to content

IInventory vs ItemStackHandler for Containers with GUI


TheGreyGhost

Recommended Posts

Howdy Folks.

I'm creating a container with GUI similar to vanilla chest.

Previously I've used IInventory to do that but I hear phrases like "that's deprecated" and "use IItemHandler instead".

 

ItemStackHandler I understand fine, but it doesn't seem to be directly compatible with GUI.

 

What's the "Forge way" to code a chest similar to Vanilla?

 

i.e. currently I'm doing composition to adapt the ItemStackHandler to an IInventory, sometimes in a pretty clumsy way

public class TileEntityInventoryBasic extends TileEntity implements IInventory {
	// Create and initialize the item variable that will store store the item
	private final int NUMBER_OF_SLOTS = 9;

  private final ItemStackHandler itemStackHandler = new ItemStackHandler(NUMBER_OF_SLOTS);

	public TileEntityInventoryBasic()
	{
    super(StartupCommon.tileEntityInventoryBasic);
		clear();
	}

	/* The following are some IInventory methods you are required to override */

	// Gets the number of slots in the inventory
	@Override
	public int getSizeInventory() {
		return itemStackHandler.getSlots();
	}

  // returns true if all of the slots in the inventory are empty
	@Override
	public boolean isEmpty()
  {
    for (int i = 0; i < itemStackHandler.getSlots(); ++i) {
      if (!itemStackHandler.getStackInSlot(i).isEmpty()) {  // isEmpty()
        return false;
      }
    }
    return true;
  }

  // Gets the stack in the given slot
	@Override
	public ItemStack getStackInSlot(int slotIndex) {
		return itemStackHandler.getStackInSlot(slotIndex);
	}

  /**
	 * Removes some of the units from itemstack in the given slot, and returns as a separate itemstack
 	 * @param slotIndex the slot number to remove the item from
	 * @param count the number of units to remove
	 * @return a new itemstack containing the units removed from the slot
	 */
	@Override
	public ItemStack decrStackSize(int slotIndex, int count) {
    ItemStack extractedItemStack = itemStackHandler.extractItem(slotIndex, count, false);
		if (!extractedItemStack.isEmpty()) {
      markDirty();
    }
	}

(etc)

and the reason I think I need to do this is because Slot needs an IInventory

public class ContainerBasic extends Container {

	// Stores a reference to the tile entity instance for later use
	private TileEntityInventoryBasic tileEntityInventoryBasic;

		for (int x = 0; x < TE_INVENTORY_SLOT_COUNT; x++) {
			int slotNumber = x;
			addSlot(new Slot(tileEntityInventoryBasic, slotNumber, TILE_INVENTORY_XPOS + SLOT_X_SPACING * x, TILE_INVENTORY_YPOS));
   /// slot requires an IInventory.                                                    
		}
	}

 

Am I missing something?

 

-TGG

 

 

 

 

 

 

Link to comment
Share on other sites

2 hours ago, TheGreyGhost said:

implements IInventory

And remove this entirely.

  • Thanks 1

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.

Link to comment
Share on other sites

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.