Jump to content

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


Recommended Posts

Posted

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

Posted

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.

Posted

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

Posted

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...

×   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.