Jump to content

Recommended Posts

Posted

I made a little mod with three kinds of wicker baskets that have different sizes of inventory. The baskets are blocks with tile entities, but when you shift-right-click on the block, the basket will "drop" as an EntityItem and maintain its inventory. You can then pick it up and carry it around, then place it somewhere else and it keeps its inventory intact.

 

I tried to make it so the player can open the basket's inventory when right-clicking with the basket in their hand, but it's not working. Specifically, when picking up the EntityItem, I get a ticking player exception caused by an ArrayIndexOutOfBoundsException. I added a slew of println statements everywhere, and it appears that when I spawn the EntityItem, the NBT data is lost. Everything seems to be in order until I call Block#spawnAsEntity from Block#onBlockActivated. The ItemStack I'm passing into spawnAsEntity is fine.

 

Do I need to do something special here? It was working fine before I started on this particular feature (opening the basket inventory by right clicking with the basket in hand). As part of this, I made a custom ItemBlock so I could trigger the GUI when right-clicking. I'm wondering if I need to override Item#createEntity? But it seems weird to me that vanilla ItemBlock doesn't do anything with this method. To be honest I'm not even sure I'm approaching this right in the first place, so any pointers are greatly appreciated.

 

Code is here: https://bitbucket.org/Daeruin/basketcase/src/master/src/main/java/com/daeruin/basketcase/

Posted

Yeah, I totally overlooked that. I think I've fixed that, but the error is still occurring. Do I need to create a static ItemBlock that I can use when spawning the EntityItem?

Posted
29 minutes ago, Daeruin said:

Yeah, I totally overlooked that. I think I've fixed that, but the error is still occurring. Do I need to create a static ItemBlock that I can use when spawning the EntityItem?

Using Item.getItemFromBlock will return the ItemBlock for your block so long as it has been registered.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

Well, I'm trying to do this from Block#onBlockActivated when the player right clicks on the block with an empty hand. I don't have access to the item unless I create one or refer to an existing one.

Posted
17 minutes ago, Daeruin said:

Well, I'm trying to do this from Block#onBlockActivated when the player right clicks on the block with an empty hand. I don't have access to the item unless I create one or refer to an existing one.

There is only one instance of your item class. Its the one you registered with the game.

Reference that.

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.

Posted (edited)

I've been trying to do this with the extension of ItemBlock I created for my Block, since that's what will be in the player's inventory and therefore what they will use to open the basket inventory when they're holding it. In the past I have never created static instances of my ItemBlocks. I just instantiate them in my registration method and register them there. Hence my question above:

 

7 hours ago, Daeruin said:

Do I need to create a static ItemBlock that I can use when spawning the EntityItem?

 

Edited by Daeruin
Posted

You can also use Item.getFromBlock()

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.

Posted

That's what Animefan said above, but I misunderstood what he meant. That worked! Thank you!

 

For the record, this is what worked:

 

ItemStack stack = new ItemStack(Item.getItemFromBlock(this), 1, 0, inventoryTag);
spawnAsEntity(world, pos, stack);

 

So it's not crashing anymore—but of course nothing else works! The inventory isn't even being saved anymore. I'll see if I can figure it out and let you know if I need more help.

Posted
25 minutes ago, Daeruin said:

So it's not crashing anymore—but of course nothing else works! The inventory isn't even being saved anymore. I'll see if I can figure it out and let you know if I need more help.

Are you even adding your storage data to the ItemStack?

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted
2 hours ago, Animefan8888 said:

Are you even adding your storage data to the ItemStack?

Inventory is now saving again. I can see the inventory attached to the ItemStack right before calling Block#spawnAsEntity, but the item handler capability seems to be disappearing when spawning the EntityItem

 

You can see the Block code here.

Posted

I added a breakpoint and it appears that the capability remains attached to the EntityItem clear until it gets added to the world's entity list. So that all seems fine. Yet when the player picks the EntityItem up, the capability is gone.

 

It would be nice to get some validation that I'm even headed down the right path here. I have a container that's working great, and I have it saving its inventory when broken, picked up, and then re-placed into the world. All fine and dandy. What I'm trying to do now is make it so when the player is holding the block in their inventory, they can right click with it to view its inventory.

 

My thought was that I need to attach an ItemHandler capability to the ItemBlock, so when the player right clicks with it, I can supply the block's inventory to my GuiHandler. However, I already have the inventory saved in the item's NBT data--that's how it knows what to put back in the block's inventory when the player places it. Do I even need a separate capability? Am I going about it the right way? How would you do it?

 

My code is here: https://bitbucket.org/Daeruin/basketcase/src/master/src/main/java/com/daeruin/basketcase/

Posted
23 hours ago, Daeruin said:

I already have the inventory saved in the item's NBT data--that's how it knows what to put back in the block's inventory when the player places it. Do I even need a separate capability?

 

The answer is no. I was able to instantiate a new inventory using the NBT data I was already saving on the dropped block, and send that to my GuiHandler. I now have the block's inventory displaying when the player right-clicks with the ItemBlock while holding it.

The new problem is that changes to the ItemBlock inventory aren't saving. I'm guessing I need to send a packet or something, but I'm not sure. Would it be a client-to-server packet to let the server know what has changed in the ItemBlock's inventory?

Posted
1 hour ago, Daeruin said:

I'm guessing I need to send a packet or something, but I'm not sure. Would it be a client-to-server packet to let the server know what has changed in the ItemBlock's inventory?

A Gui and Container should do this automatically, but instead of instantiating the inventory from the nbt for the container. You should instantiate an IItemHandler capability based on the nbt.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted
5 minutes ago, Animefan8888 said:

A Gui and Container should do this automatically, but instead of instantiating the inventory from the nbt for the container. You should instantiate an IItemHandler capability based on the nbt.

That's what I was trying to do, but the capability kept disappearing when picking up the item. Nobody replied to that problem, so I tried something else. Any clue why my capability wouldn't stick? See my messages above.

Posted
5 minutes ago, Daeruin said:

That's what I was trying to do, but the capability kept disappearing when picking up the item. Nobody replied to that problem, so I tried something else. Any clue why my capability wouldn't stick? See my messages above.

From the code I can see you never load the ItemStacks into the IItemHandler.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted
54 minutes ago, Daeruin said:

How/where would I do that? How is what you're telling me different from this?

Do this instead when you initialize your ICapabilityProvider and its IItemHandler. 

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted
7 hours ago, Animefan8888 said:

Do this instead when you initialize your ICapabilityProvider and its IItemHandler. 

I feel like you're teasing me. Where and when do I initialize the ICapabilityProvider?

 

I thought I was doing that here by passing the NBT data to the ItemStack constructor when spawning the block's EntityItem and relying on the ItemBlock's initCapabilities here. I commented out all that code because it wasn't working. The capability somehow disappeared from the ItemStack by the time it reached the player's inventory.

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



×
×
  • Create New...

Important Information

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