Jump to content

How to check if block can hold items


suppergerrie2

Recommended Posts

Hello,

 

For my mod I need to know if a block can hold items (like a chest or hopper). I'm currently checking if it is an instance of BlockContainer but that doesn't seem to work on modded chests. 

Is there a way to check if a block has an inventory without hardcoding all of the names or something strange like that?

 

edit: I am now using VanillaInventoryCodeHooks.getItemHandler() and checking if the result is null. It seems to work but are there any negative effects I didn't think about?

Edited by suppergerrie2
Link to comment
Share on other sites

All container blocks require Tile Entities to store those items. All of them. Every single one.

So:

1) Get the tile entity

2) Get the tile entity's item handler capability (CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)

3) Done

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

28 minutes ago, suppergerrie2 said:

Ah seems like getItemHandler is doing exactly that! Thanks!

VanillaInventoryCodeHooks.getItemHandler() is the compatibility patch from vanilla style inventory to the Forge Capability system.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

So I should do something like this?

IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();

if (block.hasTileEntity(state)) {
	TileEntity tileentity = world.getTileEntity(pos);
	if (tileentity != null) {
		if (tileentity.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP))	{
			itemHandler = tileentity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.UP);
		}
	}
}

 

Link to comment
Share on other sites

7 hours ago, diesieben07 said:

If you are going to call getCapability anyways, do not call hasCapability beforehand. getCapability will return null if the capability is not available.

So, I'm curious, why have hasCapability at all then?

i.e. what's the usecase where you wouldn't be calling getCapability?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I'd assumed it was to query whether or not a cap existed, and if so, get the cap.

Hence my lazy-programmer implementation of has() being return get() != null

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

21 hours ago, Draco18s said:

I'd assumed it was to query whether or not a cap existed, and if so, get the cap.

Hence my lazy-programmer implementation of has() being return get() != null

 

On 04/05/2018 at 6:10 PM, diesieben07 said:

Good question. I am sure there is some.

Maybe for some small performance benefit when only checking if the capability exists and not actually using the capability? Cases of this would be rare, but not nonexistent. Rendering functions like TESRs and HUDs come to mind

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

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.

×
×
  • Create New...

Important Information

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