Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

Posted

My question: How to make container don't drop items after close and how to pass container slot limit (max. 9)?

Thanks for help!

  • Author

Still can't place item into inventory when more than 9 slots (you are thinking about slot stacksize, but i'm thinking about same slot (as I see there's a limiter to 9 slots in container)).

There's my classes (GUI class not neccesary):

 

xxx

  • Author

Still have a problem. When do't have 9 slots then i can't pickup the item from inventory:(

  • Author

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);
		}
	}
}
}

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.

  • Author

Ehh, changed it to 64 and still i can't click on the item in slot and move it to another:(

  • Author

When i want draw the item, it get out for a while from place and back to normal.

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...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.