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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I opened up Minecraft today for the first time in a month and whenever I try and play forge I get an error 1 message. I restarted my computer, tried reinstalling both Minecraft and Forge and have updated all of my drivers. Nothing seems to work so I'm stumped. I have absolutely no mods installed ATM so I have no idea what could be causing the problem. I hope yall are able to help.   DebugLog:   [19May2024 20:33:51.600] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--username, dmoy18, --version, 1.20.1-forge-47.2.0, --gameDir, C:\Users\dmoyf\AppData\Roaming\.minecraft, --assetsDir, C:\Users\dmoyf\AppData\Roaming\.minecraft\assets, --assetIndex, 5, --uuid, c083972cd92d4dd2894beb25b82ebe82, --accessToken, ????????, --clientId, MDljMzIwMjYtOTJiNS00YWUxLTk1M2EtN2ExMGExZWM0MDAw, --xuid, 2535417310772497, --userType, msa, --versionType, release, --quickPlayPath, C:\Users\dmoyf\AppData\Roaming\.minecraft\quickPlay\java\1716168829421.json, --launchTarget, forgeclient, --fml.forgeVersion, 47.2.0, --fml.mcVersion, 1.20.1, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20230612.114412] [19May2024 20:33:51.604] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 10.0.9+10.0.9+main.dcd20f30 starting: java version 17.0.8 by Microsoft; OS Windows 11 arch amd64 version 10.0 [19May2024 20:33:51.634] [main/DEBUG] [cpw.mods.modlauncher.LaunchServiceHandler/MODLAUNCHER]: Found launch services [fmlclientdev,forgeclient,minecraft,forgegametestserverdev,fmlserveruserdev,fmlclient,fmldatauserdev,forgeserverdev,forgeserveruserdev,forgeclientdev,forgeclientuserdev,forgeserver,forgedatadev,fmlserver,fmlclientuserdev,fmlserverdev,forgedatauserdev,testharness,forgegametestserveruserdev] [19May2024 20:33:51.653] [main/DEBUG] [cpw.mods.modlauncher.NameMappingServiceHandler/MODLAUNCHER]: Found naming services : [srgtomcp] [19May2024 20:33:51.668] [main/DEBUG] [cpw.mods.modlauncher.LaunchPluginHandler/MODLAUNCHER]: Found launch plugins: [mixin,eventbus,slf4jfixer,object_holder_definalize,runtime_enum_extender,capability_token_subclass,accesstransformer,runtimedistcleaner] [19May2024 20:33:51.681] [main/DEBUG] [cpw.mods.modlauncher.TransformationServicesHandler/MODLAUNCHER]: Discovering transformation services [19May2024 20:33:51.688] [main/DEBUG] [net.minecraftforge.fml.loading.FMLPaths/CORE]: Path GAMEDIR is C:\Users\dmoyf\AppData\Roaming\.minecraft [19May2024 20:33:51.689] [main/DEBUG] [net.minecraftforge.fml.loading.FMLPaths/CORE]: Path MODSDIR is C:\Users\dmoyf\AppData\Roaming\.minecraft\mods [19May2024 20:33:51.689] [main/DEBUG] [net.minecraftforge.fml.loading.FMLPaths/CORE]: Path CONFIGDIR is C:\Users\dmoyf\AppData\Roaming\.minecraft\config [19May2024 20:33:51.689] [main/DEBUG] [net.minecraftforge.fml.loading.FMLPaths/CORE]: Path FMLCONFIG is C:\Users\dmoyf\AppData\Roaming\.minecraft\config\fml.toml
    • Yea, I figured I'd ask here since I saw someone else ask too. Also, the issue was Supplementaries. I'll see if I can make it work.
    • Backup your files and completely reinstall this modpack instance
×
×
  • Create New...

Important Information

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