Posted May 20, 201411 yr 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); } } } http://i43.tinypic.com/95v5n9.png[/img]
May 20, 201411 yr 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.
May 20, 201411 yr Author Sorry, I meant to say that this works. I was wondering if there is some nifty forge way that does all of this work via IInventory or something. http://i43.tinypic.com/95v5n9.png[/img]
May 20, 201411 yr 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.
May 20, 201411 yr Author Wow. I should have thought of that. Been staring at this code too long. Need to take a break lol. Thanks. http://i43.tinypic.com/95v5n9.png[/img]
May 21, 201411 yr 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
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.