Jump to content

Recommended Posts

Posted

I am trying to use Capabilities to add another slot to the Player's inventory. To do so, I am using an AttachCapabilitiesEvent<Entity> event. After checking that event.getObject() is an instance of EntityPlayer, I am attempting to retrieve EntityPlayer.inventory, and pass it to the constructor for my capability, which is using it to construct a Slot. The problem is, a NPE exception is thrown while constructing the slot, because EntityPlayer.inventory is null. Why?? Am I missing something major here, or is this some sort of bug...

If I ever say something stupid, or simply incorrect, please excuse me. I don't know anything about 1.8 modding, and I don't know much about entities either, But I try to help when I can.

Posted

By the time the AttachCapabilitiesEvent is fired the player's inventory has not yet constructed(as didn't many other things like the UUID, game profile and many others). You must construct your slot later.

Anyway, why are you constructing a Slot in your capability? Slots are constructed in the Containers, not in the capability.

Posted
5 minutes ago, SuperKael said:

Why?? Am I missing something major here, or is this some sort of bug...

I'm assuming that the inventory hasn't been created yet while you are trying to use it, but you see I cant say for certain because I dont know what you have done. Also you do not need the inventory in your capability at all for this.

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

I was afraid of something like that. Capabilities are so confusing... Frankly, I came to this process by patching together a bunch of different tutorials .-. What should I be doing? I have read a ton about capabilities, but I still don't understand them completely.

If I ever say something stupid, or simply incorrect, please excuse me. I don't know anything about 1.8 modding, and I don't know much about entities either, But I try to help when I can.

Posted

I guess what I'm asking is, if not what I'm doing, how am I supposed to add a new slot to an existing container/gui (that player's inventory), using a capability? I have read that it's possible, but I don't understand how.

If I ever say something stupid, or simply incorrect, please excuse me. I don't know anything about 1.8 modding, and I don't know much about entities either, But I try to help when I can.

Posted
10 minutes ago, SuperKael said:

I have read that it's possible, but I don't understand how.

  • Have a capability that contains an ItemStackHandler handling the contents of your custom slot
  • Have a Gui/Container pair open on a press of a button/keybinding/however you want it to be
  • In the Container of that pair access your capability and create a SlotItemHandler that references your ItemStackHandler.

That's pretty much it. See baubles for an example of custom slots.

  • Thanks 1
Posted

Hm, that works, in fact that was my original plan, but I didn't want to have to put the slot in a different container, like baubles does. I thought I read that it was possible to add the slot to the already-existing inventory container, but I suppose that's not worth the trouble. Whatever. Thanks very much for your time and help! I guess you saved me from a lot more headache.

If I ever say something stupid, or simply incorrect, please excuse me. I don't know anything about 1.8 modding, and I don't know much about entities either, But I try to help when I can.

Posted
11 minutes ago, SuperKael said:

it was possible to add the slot to the already-existing inventory container,

I guess it is possible by handling the PlayerContainerEvent.Open and then adding your custom slot to that container's slot list(Container#addSlotToContainer although you wouldn't be able to access that method directly since it is protected so you would need to either use reflection or replicate the functionality of that method). However all sorts of hell can and will break loose as soon as Container#transferStackInSlot is called. I couldn't even begin to imagine what would happen if the container attempted to handle the slot that it didn't know existed(read: wasn't programmed to handle). Probably the duping glitches would be the least of your worries. The containers are just not built to have slots added to them post-construction.

Another alternative would be replacing the container with a different one alltogether... and creating a hard incompatibility with everything else that does the same.

Having a separate Gui/Container pair is the most sane solution.

Posted
2 hours ago, V0idWa1k3r said:

I guess it is possible by handling the PlayerContainerEvent.Open

Sadly not true. It doesn't fire for the players own inventory gui, at least in survival mode. You will need to modify EntityPlayer#inventoryContainer directly.

2 hours ago, V0idWa1k3r said:

replicate the functionality of that method

Do this obviously, reflection is pricey.

2 hours ago, V0idWa1k3r said:

However all sorts of hell can and will break loose as soon as Container#transferStackInSlot is called.

Not true, at least for the ContainerPlayer. It just doesn't allow you to shift click into the slot, but you can do so out of it.

2 hours ago, V0idWa1k3r said:

Having a separate Gui/Container pair is the most sane solution.

However this is true do to other mods messing with this Container. There should be some form of forge implementation around this as a proper method of doing this.

 

@SuperKael If you do wish to do this Use OnEntityJoinWorldEvent to modify EntityPlayer#inventoryContainer and GuiScreenEvent.BackgroundDrawnEvent to render more onto the Gui if it can happen before the original gui is rendered, and if it needs to be after use GuiScreenEvent.DrawScreenEvent.Pre/Post

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.

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.