Jump to content

[1.10.2] Move item from hand to inventory slot from block


Daeruin

Recommended Posts

I am trying to create a campfire block, and I had this idea that if a player is holding a log in their hand, it would be cool if they could just right click on the campfire block and add that log to the fuel slot in the campfire without opening the GUI (I like the idea of players interacting directly with things, without a GUI as intermediary, when possible). I can get the log into the fuel slot, but I'm not sure I'm doing it in a good way. I had to make the tile entity's ItemStackHandler public in order to access it from the block's onBlockActivated code, and I'm not sure that's a good idea. It does give me access to methods like setStackInSlot to put the log into the fuel slot. However, doing that leaves behind an itemstack in the player's hot bar with a size of 0. I'm not sure how to avoid that. Any advice you have would be appreciated.

Link to comment
Share on other sites

I'd suggest having a method on your TileEntity called something like insertHeldItem that takes an EntityPlayer and an EnumHand and attempts to insert the player's held item into the TileEntity's inventory.

 

Use IItemHandler#insertItem to insert into a specific slot or ItemHandlerHelper.insertItemStacked to try and insert into the first slot possible, filling up existing stacks first. These both return the remaining ItemStack that couldn't be inserted (null in 1.10.2 or ItemStack.EMPTY in 1.11.2 if the full stack was accepted), so use EntityLivingBase#setHeldItem to replace the player's held item with this.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

19 hours ago, Choonster said:

I'd suggest having a method on your TileEntity called something like insertHeldItem that takes an EntityPlayer and an EnumHand and attempts to insert the player's held item into the TileEntity's inventory.

 

Use IItemHandler#insertItem to insert into a specific slot or ItemHandlerHelper.insertItemStacked to try and insert into the first slot possible, filling up existing stacks first. These both return the remaining ItemStack that couldn't be inserted (null in 1.10.2 or ItemStack.EMPTY in 1.11.2 if the full stack was accepted), so use EntityLivingBase#setHeldItem to replace the player's held item with this.

 

Oh, of course! Simple and works perfectly. Here's the method I ended up creating:

 

// Attempts to insert the player's held item into the appropriate slot in the campfire
// Returns true if something was inserted, otherwise false
public boolean insertHeldItem(EntityPlayer player, EnumHand hand)
{
	ItemStack heldItem = player.getHeldItem(hand);
	int destinationSlot = 0;
	
	if (isItemFuel(heldItem))
		destinationSlot = SLOT_FUEL;
	else if (FurnaceRecipes.instance().getSmeltingResult(heldItem) != null)
		destinationSlot = SLOT_INPUT;
	else return false;
	
	ItemStack remainder = inventory.insertItem(destinationSlot, heldItem, false);
	player.setHeldItem(hand, remainder);
	if (remainder == heldItem) return false;
	else return true;
}

 

SLOT_FUEL and SLOT_INPUT are int constants I use to identify the campfire's slots, and inventory is my tile entity's IItemHandler instance. I created the method to work with both fuel and input (anything that can be smelted). I call it from the block's onBlockActivated method anytime the campfire is already burning and the player's held item isn't null.

 

However, for some reason the first item I place into the inventory that can be smelted doesn't start cooking automatically; I have to open the GUI and remove and replace them in the input slot. It works fine from them on. I'll have to dig in and figure out why.

Edited by Daeruin
Code formatting
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.