Jump to content

The command addItemStackToInventory adds an invisible item.


Funlerz

Recommended Posts

I have created a block with a TileEntity and when the player right clicks on it with a empty bucket it adds a bucket of milk to their inventory. I have it all working except for some reason when the milk bucket is added to the player's inventory it is invisible until the player interacts with one of the invisible milk buckets.

 

public void giveMilk(EntityPlayer playerIn) {
	if (milkBucketCount > 0) {
		if (ItemStack.areItemsEqual(playerIn.inventory.getCurrentItem(), new ItemStack(Items.bucket))) {
			this.fillBucket(playerIn);
		}
	}
}

private void fillBucket(EntityPlayer playerIn) {
	ItemStack emptyBuckets = playerIn.inventory.getCurrentItem();
	int currentSlot = playerIn.inventory.currentItem;

	if (playerIn.capabilities.isCreativeMode) {
		return;
	}
	else if (emptyBuckets.stackSize <= 1)
        {
		playerIn.inventory.setInventorySlotContents(currentSlot, new ItemStack(Items.milk_bucket));
		milkBucketCount--;
        }
        else
        {
            if (!playerIn.inventory.addItemStackToInventory(new ItemStack(Items.milk_bucket)))
            {
                playerIn.dropPlayerItemWithRandomChoice(new ItemStack(Items.milk_bucket, 1, 0), false);
            }
            
            playerIn.inventory.setInventorySlotContents(currentSlot, new ItemStack(Items.bucket, --emptyBuckets.stackSize));
		milkBucketCount--;
        }
}

 

Those two functions are in my TileEntity. The giveMilk() function is called when the block is right clicked on by a player. If the player is only holding one bucket the empty bucket gets replaced with a full milk bucket that is visible, so that works fine while for some reason if the player is holding a stack of empty buckets they get the item but as I said it is invisible.

 

I did some googling and the only results I found were that it was because of a server/client discrepancy, but I have tried checking to see if (!this.worldObj.isRemote) in various places but it had no effect, and if I tried checking for if (this.worldObj.isRemote) then it just never executed whatever was inside that if statement.

 

I am really stuck on this, I have everything working how I want except that the items are invisible! Any help?

Link to comment
Share on other sites

You need to call

player.inventoryContainer.detectAndSendChanges();

after you add an item to the player's inventory to send the changes to the client.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

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.