Jump to content

[Solved] [1.15.2] Trouble making a custom inventory attached to the player


Recommended Posts

Posted

I'm making a custom inventory (similar to curios or cosmetic armor) but I want to avoid using IInventory since its deprecated. What steps do i take to make one attached to a player that would open on a hit of a keybind?

Posted (edited)

I tried doing that, but im a bit confused on whether or not I need to make a readNBT and writeNBT for it. Do you need NBT data for an inventory not attached to an item? My code is here (Check the MCap fork) if you want to take a look. Right now it produces a nullpointerexception because of the readNBT function, and I can't figure out how to fix it.

Edited by GarlicBread
Posted

Alright I fixed that issue, now I've basically made a trash can since the items I put in the slot disappear. Is there anything else I need to add to my code, or am I doing something wrong with the capabilities?

Posted

Hmm.. I added code in my ICapabilityProvider, and I think I'm not very sure what data I'm supposed to store. I took some code from ItemFlowerBag in MBE and I'm assuming this would work, but I still have the same issue. I have a feeling it's from this:

image.png.7b747f96239d8972ea1b457fef9e7ec0.png 

but im not exactly sure how to fix this. I've looked at MBE examples and I think I'm following them correctly, but the capability is never equal to the capability that I've made. How do I get about to fixing it?

    @Override
    public INBT serializeNBT() {
        return CapabilityRestraintsInventory.INVENTORY.writeNBT(getCachedInventory(), null);
    }

    @Override
    public void deserializeNBT(INBT nbt) {
        CapabilityRestraintsInventory.INVENTORY.readNBT(getCachedInventory(), null, nbt);
    }

 

Posted (edited)
15 minutes ago, GarlicBread said:

Hmm.. I added code in my ICapabilityProvider, and I think I'm not very sure what data I'm supposed to store. I took some code from ItemFlowerBag in MBE and I'm assuming this would work, but I still have the same issue. I have a feeling it's from this:

image.png.7b747f96239d8972ea1b457fef9e7ec0.png 

but im not exactly sure how to fix this. I've looked at MBE examples and I think I'm following them correctly, but the capability is never equal to the capability that I've made. How do I get about to fixing it?


    @Override
    public INBT serializeNBT() {
        return CapabilityRestraintsInventory.INVENTORY.writeNBT(getCachedInventory(), null);
    }

    @Override
    public void deserializeNBT(INBT nbt) {
        CapabilityRestraintsInventory.INVENTORY.readNBT(getCachedInventory(), null, nbt);
    }

 

Okay, a few things. Provider#getCapability should return this,

capability == CAPABILITY ? lazyOptional.cast() : LazyOptional.empty()

You should use LazyOptional#cast instead of a real cast. Btw, the reason it says that's always false is because it's currently null, it only gets a value at runtime through magic annotation bs.

As for your serialize and deserialize NBT methods, you shouldn't write to the capability, you should write to its storage, something like this, 

@Override
public INBT serializeNBT() {
   return CAPABILITY.getStorage().writeNBT(CAPABILITY, lazyOptional.orElseThrow(() -> new IllegalArgumentException("LazyOptional is empty!")), null);
}
@Override
public void deserializeNBT(INBT nbt) {
   CAPABILITY.getStorage().readNBT(CAPABILITY, lazyOptional.orElseThrow(() -> new IllegalArgumentException("LazyOptional is empty!")), null, nbt);

}

 

Edit: ...I have no idea what happened to the second example's formatting...

Edited by Novârch

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

Posted

Alright, I followed what you said and changed my code to the following:
 

    @Override
    public INBT serializeNBT() {
        return CapabilityRestraintsInventory.INVENTORY.getStorage().writeNBT(CapabilityRestraintsInventory.INVENTORY, lazyInitialisionSupplier.orElseThrow(() -> new IllegalArgumentException("LazyOptional is empty!")), null);
    }

    @Override
    public void deserializeNBT(INBT nbt) {
        CapabilityRestraintsInventory.INVENTORY.getStorage().readNBT(CapabilityRestraintsInventory.INVENTORY, lazyInitialisionSupplier.orElseThrow(() -> new IllegalArgumentException("LazyOptional is empty!")), null, nbt);
    }

 

It still seems to act like a trash can, and it says it will produce an NPE on running .getStorage() on my capability, but I dont seem to get an NPE when going into the game. Does that have anything to do with the cap variable never being equal to my capability?

Posted
18 minutes ago, GarlicBread said:

cap variable never being equal to my capability

As I said, the cap variable isn't equal to your capability because your capability is null in your IDE, it gets a value at runtime through magic annotation bs (@CapabilityInject).

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

Posted
1 minute ago, Novârch said:

As I said, the cap variable isn't equal to your capability because your capability is null in your IDE, it gets a value at runtime through magic annotation bs (@CapabilityInject).

ah,alright then. I got a bit confused cuz in MBE the capabilities dont seem to be null. Thanks for letting me know!

Posted (edited)

Alright, I fixed that problem, now the issue im having is I can't add more than one slot to my container without the game crashing. It says that the array only has 0 and 1 when it crashes. Is there any way to change this?

 

Edit: Solved this, marking thread as solved

Edited by GarlicBread
  • GarlicBread changed the title to [Solved] [1.15.2] Trouble making a custom inventory attached to the player

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.