Jump to content

[1.7.10] Container Issues


Cephrus

Recommended Posts

Sorry if this question is stupid, I'm a noob at containers.

 

I made an IInventory TileEntity and Container for it and I am experiencing some issues with them. I was following the forge Container guide on the wiki.

 

The main issue is that whenever I add over 9 slots to my inventory, picking up an item instantly returns it to the slot it came from. The inventory has no problem with the first 9, but as soon as I add the tenth, it stops working and doesn't allow items to be moved in its inventory. I am unsure of the cause of this issue.

 

Another weird issue I'm having is right clicking to halve the stack while in the inventory gives whatever the starting item was, and sometimes buttons (when I test with stone).

 

TileEntity

package net.cephlab.nm.core.research;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;

public class TileEntityResearchTable extends TileEntity implements IInventory
{
public String customName;
public ItemStack[] inventory = new ItemStack[13];

@Override
public int getSizeInventory()
{
	return 13;
}

@Override
public ItemStack getStackInSlot(int slot)
{
	return inventory[slot];
}

@Override
public ItemStack decrStackSize(int slot, int amount)
{
	ItemStack itemStack = getStackInSlot(slot);
        if (itemStack != null)
        {
            if (itemStack.stackSize <= amount)
            {
                setInventorySlotContents(slot, null);
            }
            else
            {
                itemStack = itemStack.splitStack(amount);
                if (itemStack.stackSize == 0)
                {
                    setInventorySlotContents(slot, null);
                }
            }
        }

        return itemStack;
}

@Override
public ItemStack getStackInSlotOnClosing(int slot)
{
	 if (inventory[slot] != null)
     {
		 ItemStack itemStack = inventory[slot];
         inventory[slot] = null;
         return itemStack;
     }
     else
     {
    	 return null;
     }
}

@Override
public void setInventorySlotContents(int slot, ItemStack stack)
{
	inventory[slot] = stack;
	if(stack != null && stack.stackSize > this.getInventoryStackLimit()) stack.stackSize = this.getInventoryStackLimit();

	this.markDirty();
}

@Override
    public void readFromNBT(NBTTagCompound tagCompound) 
{
            super.readFromNBT(tagCompound);
            
            NBTTagList tagList = tagCompound.getTagList("Inventory", 0);
            for (int i = 0; i < tagList.tagCount(); i++) 
            {
                    NBTTagCompound tag = (NBTTagCompound)tagList.getCompoundTagAt(i);
                    byte slot = tag.getByte("Slot");
                    if (slot >= 0 && slot < inventory.length) 
                    {
                            inventory[slot] = ItemStack.loadItemStackFromNBT(tag);
                    }
            }
    }

    @Override
    public void writeToNBT(NBTTagCompound tagCompound) 
    {
            super.writeToNBT(tagCompound);
                            
            NBTTagList itemList = new NBTTagList();
            for (int i = 0; i < inventory.length; i++) 
            {
                    ItemStack stack = inventory[i];
                    if (stack != null) 
                    {
                            NBTTagCompound tag = new NBTTagCompound();
                            tag.setByte("Slot", (byte) i);
                            stack.writeToNBT(tag);
                            itemList.appendTag(tag);
                    }
            }
            tagCompound.setTag("Inventory", itemList);
    }
    
    @Override
public String getInventoryName()
{
	return this.hasCustomInventoryName() ? customName : "researchTable";
}

@Override
public boolean hasCustomInventoryName()
{
	return false;
}

@Override
public int getInventoryStackLimit()
{
	return 64;
}

@Override
public boolean isUseableByPlayer(EntityPlayer player)
{
	return true;
}

@Override
public void openInventory() {}

@Override
public void closeInventory() {}

@Override
public boolean isItemValidForSlot(int slot, ItemStack stack)
{
	return true;
}
}

 

Container

package net.cephlab.nm.core.research;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class ContainerResearcher extends Container
{
protected TileEntityResearchTable te;

public ContainerResearcher(InventoryPlayer inv, TileEntityResearchTable tile)
{
	this.te = tile;

	this.addSlots();
	this.bindPlayerInventory(inv);
}

@Override
public boolean canInteractWith(EntityPlayer player)
{
	return te.isUseableByPlayer(player);
}

protected void addSlots()
{
	int tmp = 0;
	for (int i = 0; i < 3; i++) 
	{
            for (int j = 0; j < 3; j++) 
            {
            	tmp++;
            	if(tmp > 9) break;
                addSlotToContainer(new Slot(te, tmp, 62 + j * 18 + 35, 17 + i * 18 - 2));
            }
	}

	//addSlotToContainer(new Slot(te, 10, 62 + 18 / 3 + 4, 17 + 18 / 3 - );
}

protected void bindPlayerInventory(InventoryPlayer inventoryPlayer) 
{
        for (int i = 0; i < 3; i++)
        {
                for (int j = 0; j < 9; j++)
                {
                        addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18 + 9));
                }
        }

        for (int i = 0; i < 9; i++) 
        {
                addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142 + 9));
        }
}

 @Override
     public ItemStack transferStackInSlot(EntityPlayer player, int slot) 
 {
             ItemStack stack = null;
             Slot slotObject = (Slot) inventorySlots.get(slot);

             if (slotObject != null && slotObject.getHasStack()) 
             {
                     ItemStack stackInSlot = slotObject.getStack();
                     stack = stackInSlot.copy();

                     if (slot < te.getSizeInventory()) 
                     {
                             if (!this.mergeItemStack(stackInSlot, te.getSizeInventory(), 36 + te.getSizeInventory(), true)) 
                             {
                                     return null;
                             }
                     }
                     
                     else if (!this.mergeItemStack(stackInSlot, 0, te.getSizeInventory(), false)) 
                     {
                             return null;
                     }

                     if (stackInSlot.stackSize == 0) 
                     {
                             slotObject.putStack(null);
                     } else 
                     {
                             slotObject.onSlotChanged();
                     }

                     if (stackInSlot.stackSize == stack.stackSize) 
                     {
                             return null;
                     }
                     slotObject.onPickupFromSlot(player, stackInSlot);
             }
             return stack;
     }
}

 

Any help is appreciated.

Link to comment
Share on other sites

Hi

 

This background info and example code is for 1.8 but the concepts are exactly the same and might help you understand how it should work.

http://greyminecraftcoder.blogspot.com.au/2015/01/gui-containers.html

https://github.com/TheGreyGhost/MinecraftByExample  (MBE30, MBE31)

 

My main comment on your code is that it's got too many "magic numbers" in it for slots indices so it's very hard to tell where the problem is.

 

-TGG

 

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.