Jump to content

[1.12.2] Unable to Store my Items in the Item Handler Capability [SOLVED]


Recommended Posts

Posted (edited)

If anyone can maybe take a look at this and see what I am royally fucking up I'd appreciate it, because I have been at it for hours. I just want to open my GUI lol. Here is the relevant pieces of code: 
https://pastebin.com/dLrL2JDu (The Item) 

 


Something in there has to be screwed up somewhere. I just can't see it maybe because I have been working too long on it. According to the crash it looks like it is crashing when it attempts to open up the container as opposed to the GUI. I see that it is throwing a null but I don't quite understand why? The item exists, you need to actually right-click it, the slots are there in the container? Maybe I am injecting the capability into the item wrong? Maybe I forgot to register it somewhere? (although it is a forge pre-made cap which I think automatically registers with the annotation?) The report seems to point to adding slots? Which also doesn't seem to make much sense.

 

So any help f would be greatly appreciated. I know there may be "inefficient" code in this but I am just trying to get this work at the moment and I can work with efficency later since this mod isn't released yet.

 

EDIT: And yes I know this could be the completly wrong way to do this and thats fine. The http://mcforge.readthedocs.io/en/latest/datastorage/capabilities/ is where I was bascially copying from. If it is completly wrong to do it this way I understand but I still would like to know the correct way to do this. I created a few capabilities already and I know I could do the same for this, but if I can use a built in capability (which I have not tried yet hence why I am trying this) then why not.

 

EDIT 2: Yeah apparently I did the whole capability thing wrong so I reworked the whole thing (I left the original pastebin link, but put a strikethrough) and here is the new version of the Item. I am stuck on how to just make sure it saves the entire inventory. Help is appreciated:

https://pastebin.com/uaSan6C0

 

EDIT 3: I fixed the crashing (I made the stupidest mistake in my Container code), now I just need to figure out how to get the items to save correctly to the ItemStack. Please help with that if possible. I spoilered out the other links to the other pieces of code since they are no longer relevant for this issue.

Edited by HalestormXV
Posted
35 minutes ago, jhdoran said:

Creating a new ItemStackHandler in getCapability does not seem like a good idea.  Anytime someone wants to use the inventory, they get a brand new one.

Okay, so what would you suggest then. Like I said, this is a first time for me working with the pre-made Capabilities so I am shooting in the dark and just hoping for the best until I get and understand it.

Posted
15 hours ago, diesieben07 said:
  • You cannot store the ItemStackHandler in your item class like that. Your Item instance is singleton-like, there is only one instance of it. This needs to go in your ICapabilityProvider that's returned from initCapabilities.
  • Why do you have both an ItemStackHandler and a List<ItemStack>? ItemStackHandler takes care of the entire inventory management, it's all you need.
  • You need to return the existing ItemStackHandler from getCapability.

- That was dumb, should have recognized it and have since changed it.

- I don't quite follow? Do you mean that ItemStackHelper isn't to be used at all? Everything is done with the ItemStackHandler ? So, I'd have to cycle through the 4 slots etc. and add the items like that? I originally put the List<ItemStack> because I thought that is what is required by ItemStackHelper. 

- Alright

 

https://hastebin.com/bikilixoca.java

 

So that is the updated code. Better? Note I left the List<ItemStack> in there because I know that is still the outstanding issue now and it is simply a place holder at this point.

Posted

ItemStackHandler (not Helper, that is a different class) implements IItemHandler -- this is the Capability.  If you walk up to a cow and ask for an inventory capability, it will hand you an ItemStackHandler.  So this single ItemStackHandler is given to the cow early on (when it is constructed the capability will attach) in the form of a CapabilityProvider.

Posted (edited)

Alright, so like above I created the ItemStackHandler in the constructor. So ItemStackHelper (which is what I have seen used in TileEntities) doesn't need to be used at all here to save the items. So then how do you save everything here: 

 

@Override
public void deserializeNBT(NBTTagCompound nbt)
{
    //Do your saving here
    if (nbt.hasKey("RunesStored"))
    {
        CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.getStorage().readNBT(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, handler, null, nbt.getTag("RunesStored"));
    }
}

 

Do you need to put everything into a List/Field? Or into something like that. Or am I a complete idiot and just having that is all I need? I'm beginning to think I am just a total idiot becasue I am testing with this code now:https://hastebin.com/ajakojucic.java and it seems to be working just fine. And is that what you mean @diesieben07 when you were saying I literally only needed the handler?

Edited by HalestormXV
Posted
4 hours ago, HalestormXV said:

And is that what you mean @diesieben07 when you were saying I literally only needed the handler?

Remove the List<ItemStack>

Make it gone.

You do not need it.

Take it out.

The ItemStackHandler contains its own List

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)
4 hours ago, diesieben07 said:

I am failing to see any capability code there.

I placed the wrong link. https://pastebin.com/sE4jZ6TH

 

7 hours ago, Draco18s said:

Remove the List<ItemStack>

Make it gone.

You do not need it.

Take it out.

The ItemStackHandler contains its own List

- Did

- Done

- Got It

- Done

- Alright now I understand.

 

The code linked above in this post works just fine it seems. And to get this particular capability off this item I'd do:

stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);

right?

Edited by HalestormXV

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.