Jump to content

[SOLVED] Adding Items to Chests


godbaba

Recommended Posts

Hmm... I think you have to make a new TileEntityChest or something... I know theinstitutions has a tutorial for such things. And don't worry about the fact that it is outdated and he uses modloader, but it is world gen, which is cross compatible. I would explain it for you (the code for this that is) but I don't have the time nor can I remember what the code is...

 

So just find his tutorial for it xD

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Link to comment
Share on other sites

Hey, I threw together something that might help, the function returns the spaces that are free in the chest and the ones that have the same item or block whose stacksize is less then the max stack size for that item or block. with this it should be pretty easy to add an item to the correct slot in the chest.

 

public List<Integer> checkSpace(int x, int y, int z, ItemStack stack)
 {
	 List<Integer> freeSpaces = new ArrayList();

	 try
	 {
	 TileEntity tempTile = worldObj.getBlockTileEntity(x, y, z);

	 if(tempTile!=null&&tempTile instanceof IInventory)
	 {
		 int i =0;
			int num = 0;
			boolean didFail = false;
			while(!didFail)
			{
				try
				{
					((IInventory) tempTile).getStackInSlot(i);
					i++;
					num++;

				}
				catch(Exception e)
				{
					didFail = true;
				}
			}
			for(int ix = 0; ix<num; ix++)
			{
				if(((IInventory) tempTile).getStackInSlot(ix)==null)
				{
					freeSpaces.add(ix);
				}

				else if (((IInventory) tempTile).getStackInSlot(ix).itemID == stack.itemID)
				{
					if(stack.stackSize+((IInventory) tempTile).getStackInSlot(ix).stackSize<stack.getItem().getItemStackLimit())
					{
						freeSpaces.add(ix);
					}
				}
			}
	 }


	 }
	 catch(Exception e)
	 {
		 e.printStackTrace();
	 }


	 return freeSpaces;
 }

Link to comment
Share on other sites

Hmm... I think you have to make a new TileEntityChest or something... I know theinstitutions has a tutorial for such things. And don't worry about the fact that it is outdated and he uses modloader, but it is world gen, which is cross compatible. I would explain it for you (the code for this that is) but I don't have the time nor can I remember what the code is...

 

So just find his tutorial for it xD

 

Thank you! I checked the tutorial it really helped thats mate :)

 

Hey, I threw together something that might help, the function returns the spaces that are free in the chest and the ones that have the same item or block whose stacksize is less then the max stack size for that item or block. with this it should be pretty easy to add an item to the correct slot in the chest.

 

public List<Integer> checkSpace(int x, int y, int z, ItemStack stack)
 {
	 List<Integer> freeSpaces = new ArrayList();

	 try
	 {
	 TileEntity tempTile = worldObj.getBlockTileEntity(x, y, z);

	 if(tempTile!=null&&tempTile instanceof IInventory)
	 {
		 int i =0;
			int num = 0;
			boolean didFail = false;
			while(!didFail)
			{
				try
				{
					((IInventory) tempTile).getStackInSlot(i);
					i++;
					num++;

				}
				catch(Exception e)
				{
					didFail = true;
				}
			}
			for(int ix = 0; ix<num; ix++)
			{
				if(((IInventory) tempTile).getStackInSlot(ix)==null)
				{
					freeSpaces.add(ix);
				}

				else if (((IInventory) tempTile).getStackInSlot(ix).itemID == stack.itemID)
				{
					if(stack.stackSize+((IInventory) tempTile).getStackInSlot(ix).stackSize<stack.getItem().getItemStackLimit())
					{
						freeSpaces.add(ix);
					}
				}
			}
	 }


	 }
	 catch(Exception e)
	 {
		 e.printStackTrace();
	 }


	 return freeSpaces;
 }

 

Thank you too mate ! I solved my problem :)

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


×
×
  • Create New...

Important Information

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