suppergerrie2 Posted May 2, 2018 Share Posted May 2, 2018 (edited) 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 May 2, 2018 by suppergerrie2 Quote Link to comment Share on other sites More sharing options...
Draco18s Posted May 2, 2018 Share Posted May 2, 2018 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 Quote 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 More sharing options...
suppergerrie2 Posted May 2, 2018 Author Share Posted May 2, 2018 Ah seems like getItemHandler is doing exactly that! Thanks! Quote Link to comment Share on other sites More sharing options...
Draco18s Posted May 2, 2018 Share Posted May 2, 2018 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. Quote 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 More sharing options...
suppergerrie2 Posted May 3, 2018 Author Share Posted May 3, 2018 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); } } } Quote Link to comment Share on other sites More sharing options...
suppergerrie2 Posted May 3, 2018 Author Share Posted May 3, 2018 Ah that makes sense thanks! Yeah EnumFacing.UP was just for testing, will change that later. Quote Link to comment Share on other sites More sharing options...
Draco18s Posted May 3, 2018 Share Posted May 3, 2018 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? Quote 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 More sharing options...
Draco18s Posted May 4, 2018 Share Posted May 4, 2018 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 Quote 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 More sharing options...
Cadiboo Posted May 5, 2018 Share Posted May 5, 2018 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 Quote About Me Spoiler My Discord - Cadiboo#8887 My Website - Cadiboo.github.io My Mods - Cadiboo.github.io/projects My Tutorials - Cadiboo.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 More sharing options...
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.