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.

How to set + get the slot in players inventory that the current ItemStack is in?

Featured Replies

  • Author

ok this is what I have now and it works but sometimes is makes 2 items instead of 1 and it crashes after a couple jumps.

public void onUpdate(ItemStack itemstack, World worldIn, Entity entity, int itemSlot, boolean isSelected) {
	EntityPlayer player = (EntityPlayer) entity;
	if (player.inventory.hasItem(this)) {
		List stacks = player.inventoryContainer.inventoryItemStacks;
		List slots = player.inventoryContainer.inventorySlots;

		int randintamount = slots.size();

		int index = rand.nextInt(slots.size());
		Slot slot = (Slot) slots.get(index);
		ItemStack stack = (ItemStack) stacks.get(index);
		if (player.worldObj.rand.nextInt(100) == 1) {
			if (stack == null) {

				player.inventory.setInventorySlotContents(index, itemstack);

				player.inventory.setInventorySlotContents(itemSlot, null);

			} else if (stack != null) {
				if (stack != itemstack) {
					player.inventory.setInventorySlotContents(index, itemstack);
					player.inventory.setInventorySlotContents(itemSlot, stack);

				}
			}
		}
	}

}

BioWarfare Mod: http://goo.gl/BYWQty

So. On update gets called everytime you have the item in your inventory. and still you are checking if you have items in your inventory.

ย 

If you got a crash it helps A LOT if you show the crash.

Also im not sure why you are checking the random stack you get for null, since its not really making any difference. keep your code clean and it makes errors much easier to find

ur item has nothing to do in the armor slot. dont put it there..? modify ur random so its not giving u a random number that is a an armor slot

  • Author

Fixed armour slot and crashing but it still multipies and I am not sure why becasue I make one item and delete the other thefore there should only be one item?

private Random rand = new Random();
public void onUpdate(ItemStack itemstack, World worldIn, Entity entity, int itemSlot, boolean isSelected) {
	EntityPlayer player = (EntityPlayer) entity;
	if (player.inventory.hasItem(this)) {
		List stacks = player.inventoryContainer.inventoryItemStacks;
		List slots = player.inventoryContainer.inventorySlots;


		int index = rand.nextInt(35);

		Slot slot = (Slot) slots.get(index);
		ItemStack stack = (ItemStack) stacks.get(index);




			if (stack == null && index != itemSlot ) {

				player.inventory.setInventorySlotContents(index, itemstack);

				player.inventory.setInventorySlotContents(itemSlot, null);

			} else if (stack != null && index != itemSlot) {
				if (stack != itemstack) {
					player.inventory.setInventorySlotContents(index, itemstack);
					player.inventory.setInventorySlotContents(itemSlot, stack);


			}
		}
	}

}

BioWarfare Mod: http://goo.gl/BYWQty

Without even reading rest of code: You can (should, only exception is synchronization or administrative tools) only edit inventory from server: if(!world.isRemote){edit}

1.7.10 is no longer supported by forge, you are on your own.

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.