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.

Featured Replies

Posted

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

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.

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.

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.

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.