How can I get the number of slots in a blocks inventory?

Started by Fir3_H3art on Wed, 10/21/2020 - 21:03

Topic category: Help with modding (Java Edition)

Last seen on 22:52, 28. Oct 2020
Joined Oct 2020
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
How can I get the number of slots in a blocks inventory?
Wed, 10/21/2020 - 21:03

I am trying to make a block that functions similarly to a hopper. I just want it to put an item from its inventory to an adjacent block's inventory.

I don't know how to detect if a block has an inventory, nor what the number of slots it might have? Is there a way to do this? Or perhaps is there a better way of iterating through all the slots in an inventory. I'm not opposed to writing custom code if necessary.

As a bonus question, I was wondering how you would detect dropped items above a block. Not what I'm trying to do here; just wondering.

Last seen on 22:52, 28. Oct 2020
Joined Oct 2020
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
I figured out how to get the…
Thu, 10/22/2020 - 18:58

I figured out how to get the number of slots in a block's inventory. I did so using two custom code snippets. I am using the the version of mcreator for minecraft 1.12.2 btw, so I am no sure if this would work for everyone. I'll briefly explain the code below.

TileEntity te = world.getTileEntity(new BlockPos(x, y, z-1));
if (te instanceof IInventory) { slots = (double) ((IInventory) te).getSizeInventory();};

Get the TileEntity of the block you're trying to check. I named it "te". All you would need to modify is the x y and z values; I change the z by -1 to get the block north of the starting position. Then I check if "te" interfaces with the "IInventory" interface with "instanceof". This is the place that chests, hoppers, droppers, etc get their inventory methods and implementation. You cast "te" as "IInventory" then use the ".getSizeInventory()" method. Lastly I make sure to cast that as a double and assign it to my local "slots" variable. That I had previously declared. Something to note is that slots are numbered in index format, meaning it starts counting from 0; but the "getSizeInventory()" method returns the number of slots rather than the highest index.

Last seen on 23:33, 14. Nov 2020
Joined Sep 2020
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Is there any way to do this…
Sat, 10/24/2020 - 16:21

Is there any way to do this with procedure blocks? If not, please explain to me how I can make this item transfer to the inventory of a block that has the coordinates saved in 3 NBT tags in an item, x tag, y tag, z tag, and when holding the shift with the item in hand, take the dropped items and place in that specific block. I did most of it with procedure blocks but I can't tell when the block's inventory is full or how many slots the block has because the procedure blocks are very limited in this, so I can't collect items like in the vanilla hopper, there is a stone in the chest , so if another stone is placed, it will join with the one that already existed there, this is not possible with procedure blocks so I only insert items if the slot is empty. The counting of slots works very badly in procedure blocks because it is not possible to know how many slots there are in a block, so you try to send items to a slot that doesn't exist and the game crashes. Can you help me? If it is with code there is no promble, but I don't understand anything about java :(