Jump to content

Recommended Posts

Posted

So, I'm trying to create an item that can hold an inventory with a GUI. My problem is I can't really find a good guide on how to set it up. I know I need to use the capabilities implementation forge has, but that's about it. The forge documentation alone doesn't really give me much direction. Does anyone have a guide or a code example of creating a simple item with a GUI with an inventory?

 

Posted

You need to override initCapabilities in your item and attach your capabilities to the ItemStack.

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted

Use forges ItemHandlerCapability. Look at other mods that do this, I’ll make an example when I get home

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted

@Daeruin I have. There isn't anything on creating items with inventories/data. I know its comparable to blocks, but I was hoping for *something* to show me the basic way to get something like that to work,

Posted

Ah, I missed that you wanted it to be an item.

 

I did this last year sometime but don't remember it well enough to explain in detail. But maybe my source code will help you as an example. The link I just gave is for a custom ItemBlock I made. You could probably do something very similar in your custom item class. As Cadiboo mentioned, you override initCapabilities in your Item class and return an instance of ICapabilityProvider. I made a custom implementation of this but Forge may have something built in. In the ICapabilityProvider implementation, you would use hasCapability and getCapability to check for and return an instance of IItemHandler (of which Forge does have a basic built-in version). Not sure I'm explaining it correctly, but maybe that will help you get started.

Posted

@Daeruin kinda? Like I get that, but is there a "tile entity" equivalent for items? or is that what the class that implements a capability would be. If my understanding is correct, all a tile entity is, is just a wrapped capabilities type thing that is used to attach a capability to blocks and handle them being placeable and such. so I create my own interface  that extends ICapability for my needs (lets say a backpack for example) then create an object that uses that custom interface to "do things" like handles writing and reading nbt to the item its attached to (the backpack in this case) then register it with forge and "attach" it to my item class, right? is that the basic idea, or am I missing something? It's really weird, you'd think that someone would have a tutorial on how to do this by now. Not that I just want to copy someone else's work, but I learn better when I have someone (or an article) walk me through it first. Yeah It's similar to making a tile entity, but it's the different parts that confuse me; the new stuff is where I struggle because I'm not great at synthesizing solutions to problems I've never encountered. A lot of Minecraft modding is like that. It's not the easiest thing to do when you get to a point where the "basics" end. That's all most ever get to, unfortunately.

Posted

I'm with you. I have really struggled to make that jump from using tutorials to do basic stuff to combing through code from Minecraft, Forge, and other mods to try and understand their examples and come up with something that's kinda similar but unique. Every inch I've gained has been tough.

 

You implement ICapabilityProvider to "serve up" your inventory (an ItemStackHandler instance) when it's being asked for. You can probably copy my BasketInventoryProvider almost exactly, except replacing the ItemStackHandlerBasket with Forge's built-in ItemStackHandler. As you can see in my notes on that class, I ended up having to implement ICapabilitySerializable in order to get my inventory to persist in various situations. ICapabilitySerializable is an extension of ICapabilityProvider that, in addition to providing the inventory when asked for, also writes and reads the inventory itself into your ItemStack's NBT. At that point you just return an instance of your ICapabilityProvider (or ICapabilitySerializable) from your Item's initCapabilities method, and you're off to the races. It doesn't have to be registered with Forge in any special way beyond simply returning it from initCapabilities (I think initCapabilities is a method that was injected into the Item class by Forge for the very purpose of attaching capabilities to items—initCapabilities is called from the ItemStack constructor when your item is being instantiated in an inventory).

 

If you want to have a GUI on your item, there's more to it. For that you need the GUI itself plus a GuiProvider. There are examples of those in my mod, as well. I used my item's onItemRightClick method to call up the GUI.

  • Like 1

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.