Jump to content

[1.7.2] The best way to add items to an inventory


navybofus

Recommended Posts

I've got a few automation blocks that add items to nearby chests. Until I started picking up EntityItems, everything worked fine. Now that I'm getting the ItemStack from the EntityItem and adding them, I'm having troubles. Here's the code that I use to add the items to chests.

 

/***
* Takes an int[] with the x, y, z and slot of a nearby inventory
* with either a matching item or empty slot. Also takes the stack to add.
*/
private void placeItemsInChest(int[] loc, ItemStack stack){
	TileEntityChest te = (TileEntityChest) worldObj.getTileEntity(loc[0], loc[1], loc[2]);
	if(te.getStackInSlot(loc[3]) != null){
		ItemStack chestStack = te.getStackInSlot(loc[3]);
		if(chestStack.getItem() == stack.getItem()){
			if(chestStack.getItemDamage() == stack.getItemDamage()){
				Item passedItem = stack.getItem();
				int size = 0;
				size = chestStack.stackSize;
				size += stack.stackSize;
				if(size <= chestStack.getMaxStackSize()){
					te.setInventorySlotContents(loc[3], new ItemStack(passedItem, size));
				} else {
					int sizeRemain = size - chestStack.getMaxStackSize();
					te.setInventorySlotContents(loc[3], new ItemStack(passedItem, size-sizeRemain));
					ItemStack remainingStack = new ItemStack(passedItem, sizeRemain);
					if(findChestWithRoom(remainingStack) != null){
					//Run this method again with the remaining stack.
						int[] tmpLoc = findChestWithRoom(remainingStack).clone();
						placeItemsInChest(tmpLoc, remainingStack);
					} else {
						spawnItems(remainingStack);
						//Spawns EntityItems into the world if no valid inventory found
					}
				}
			} else {
				te.setInventorySlotContents(loc[3], stack);
			}
		} else {
			te.setInventorySlotContents(loc[3], stack);
		}
	}
}

Link to comment
Share on other sites

But it does work for itemstacks you create? I thought an itemstack was an itemstack, but if not... you could instantiate a new itemstack using data retrieved from the entityitem's itemstack.

 

But I feel like I misunderstood the question. Does it work if you pass in any itemstack? If not throw in some println debugging. I usually start with confirming that each if statement really did return true at some point. Then I root out any nulls.

Link to comment
Share on other sites

Now I understand. I highly doubt it. I believe LexManos likes to say that if there's a perfectly good hard way to do something then do it the hard way. If you want to compare your code to something someone more official wrote you can check the addItemStackToInventory method inside InventoryPlayer.class.

Link to comment
Share on other sites

I believe LexManos likes to say that if there's a perfectly good hard way to do something then do it the hard way.
I'm not a fan of utility functions that save the modder ~10 lines of code by sacrificing understanding of what they are doing. However, there are some vanilla utility functions that exist for these types of things, so either use those or make your own, it's not hard.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

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.