Jump to content

[1.7.2] How to make container don't drop items after close and...


Sokaya

Recommended Posts

Container:

package sokaya.client.gui;

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

public class KociolekContainer extends Container {

protected IInventory tileEntity;

public KociolekContainer(InventoryPlayer inventoryPlayer, IInventory tileEntity2){
	tileEntity = tileEntity2;

	addSlotToContainer(new Slot(inventoryPlayer, 0, 17, 27));
	addSlotToContainer(new Slot(inventoryPlayer, 1, 17, 45));
	addSlotToContainer(new Slot(inventoryPlayer, 2, 35, 36));
	bindPlayerInventory(inventoryPlayer);
}

/*@Override
public void onContainerClosed(EntityPlayer par1EntityPlayer)
{
	super.onContainerClosed(par1EntityPlayer);
}*/

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


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

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

 

TileEntity:

package sokaya.tileentities;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
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 TileEntityKociolek extends TileEntity implements IInventory{
private final String name = "Kociolek";
private final String tagName = "kociolekCreate";
public static final int INV_SIZE = 3;
private ItemStack[] inventory = new ItemStack[iNV_SIZE];

public TileEntityKociolek(){}

@Override
public int getSizeInventory()
{
	return inventory.length;
}

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

@Override
public ItemStack decrStackSize(int var1, int var2)
{
	ItemStack stack = getStackInSlot(var1);
	if (stack != null)
	{
		if (stack.stackSize > var2)
		{
			stack = stack.splitStack(var2);
			if (stack.stackSize == 0)
			{
				setInventorySlotContents(var1, null);
			}
		}
		else
		{
			setInventorySlotContents(var1, null);
		}
		this.markDirty();
	}
	return stack;
}

@Override
public ItemStack getStackInSlotOnClosing(int var1)
{
	ItemStack stack = getStackInSlot(var1);
	if (stack != null)
	{
		setInventorySlotContents(var1, null);
	}
	return stack;
}

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

@Override
public String getInventoryName()
{
	return name;
}

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

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

@Override
public void markDirty()
{
	for (int i = 0; i < this.getSizeInventory(); ++i)
	{
		if (this.getStackInSlot(i) != null && this.getStackInSlot(i).stackSize == 0)
			this.setInventorySlotContents(i, null);
	}
}

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

@Override
public void openInventory(){}

@Override
public void closeInventory(){}

@Override
public boolean isItemValidForSlot(int var1, ItemStack var2)
{
	return true;
}

public void writeToNBT(NBTTagCompound compound)
{
	NBTTagList items = new NBTTagList();
	for (int i = 0; i < getSizeInventory(); ++i)
	{
		if (getStackInSlot(i) != null)
		{
			NBTTagCompound item = new NBTTagCompound();
			item.setByte("Slot", (byte) i);
			getStackInSlot(i).writeToNBT(item);
			items.appendTag(item);
		}
	}
	compound.setTag(tagName, items);
}

public void readFromNBT(NBTTagCompound compound)
{
	NBTTagList items = compound.getTagList(tagName,compound.getId());
	for (int i = 0; i < items.tagCount(); ++i)
	{
		NBTTagCompound item = (NBTTagCompound) items.getCompoundTagAt(i);
		byte slot = item.getByte("Slot");
		if (slot >= 0 && slot < getSizeInventory())
		{
			inventory[slot] = ItemStack.loadItemStackFromNBT(item);
		}
	}
}
}

Link to comment
Share on other sites

These are wrong. You do not want inventoryPlayer here. You want tileEntity as the first argument.

                addSlotToContainer(new Slot(inventoryPlayer, 0, 17, 27));
	addSlotToContainer(new Slot(inventoryPlayer, 1, 17, 45));
	addSlotToContainer(new Slot(inventoryPlayer, 2, 35, 36));

Also, please read what was said. You TileEntity only has 3 slots. You must make the array bigger for more than 3 or 9 or 100. Then use that number in your container code to add the slots.

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.