Daeruin Posted February 24, 2017 Posted February 24, 2017 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. Quote
Choonster Posted February 24, 2017 Posted February 24, 2017 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. Quote 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.
Daeruin Posted February 25, 2017 Author Posted February 25, 2017 (edited) On 2/24/2017 at 9:10 AM, 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. Expand 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 February 25, 2017 by Daeruin Code formatting Quote
Recommended Posts
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.