Jump to content

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


Recommended Posts

Posted

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.

Posted

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.

Posted (edited)
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

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hey, sorry I haven't said anything in a while. I was banned on here for sending you straight up crash logs and they banned me for spam... but I learned that these forums are for forge only and I was working with neoforge... but thank you for all the help.
    • la verdad nose que hago
    • Ok i check and this is real axioM, BUT i have seen strange thing when mods less then 100 is working great but when its more then 100, i got crash   i realy need this mod can i some how fix it? or only delete axiom
    • It is an issue with the mod Axiom
    • I am in 1.20.1, and I have about 50 mods, I'd say. But for some reason, the search bar broke at some point. It's a real pain to search for a certain object.. Mods that I have(I had to write ALL OF THESE OUT arhg): Epic Knights. AI-Improvements, Alexscaves, alexsmobs, AmbientSounds, Aquamirae, Architectury, Betterfpsdist, BiomesOPlenty, Bookshelf, Born In Chaos, Bountiful Critters, CarbonConfig, Citadel, Cloth config, Clumps, Corgilib, Creative Core, Creeperoverhaul, crittersandcompanions, CullLessLeaves, Cupboard, Duckling, Embeddium, Enchantment Descriptions, Enderman Overhaul, Entity Model Features, Entity Texture Features, Entity Culling, Exotic Birds, Extra Sounds, Falling Leaves, Farmers Delight, Fast Leaf Decay, Fastload, Ferritecore, FpsReducer2, Framework Forge, Gamma Creatures Mod, Geckolib, Geophilic, Ghosts, Goblin Traders, Gpumemleakfix, Guardvillagers, Ichunutil, ImmediatelyFast Forge, Immersive Paintings, Incendium, Konkrete Forge, Let Sleeping Dogs Lie, Make Bubbles Pop, MCW Bridges, MCW doors, MCW Fences, MCW lights, MCW paths, MCW trapdoors, Medieval Paintings, More Creative Tabs Combo, Nether's Overhaul, Nyfss Spiders Forge, Obscure Api, oculus MC, Oh The Biomes We've Gone, Oh The Trees You'll Grow, Physics Mod, Prehistoric Park, Quark Delight, Quark, Rainbow Reef, Refurbished Furniture Forge, Regrowth, Repurposed Structures, Resourcefulconfig, Ribbits, Rubidium, Saturn mc, Skin Layers 3d Forge, Smoothchunk, Starlight, Structory, TerraBlender, Terralith, Visuality, Water Erosion, Xaeros Minimap, Xaeros World Map, YungsApi, YungsBetter Desert Temples, Yungs Better Dungeons, Yungs Jungle Templates, Yungs Better Mineshafts, Yungs Better Nether Fortresses, Yungs Better Strongholds, Yungs Better Witch Huts, Yungs Bridges, Yungs Extras, Zeta.   Why isn't my search bar working!!!!😭
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.