Jump to content

[1.10]How to save items in Player?


WildHeart

Recommended Posts

Little Vague, Would you be able to supply the story for this?

 

aka

 

"I would like to use XYZ to store Items within a Capability for Players, so then I will do something with said items"

 

Is there a reason why the default Player Inventory is not sufficient?

 

Link to comment
Share on other sites

Little Vague, Would you be able to supply the story for this?

 

aka

 

"I would like to use XYZ to store Items within a Capability for Players, so then I will do something with said items"

 

Is there a reason why the default Player Inventory is not sufficient?

what are the issues? I specifically asked how to save the player object. If you're wondering, I have a tab with endless containers that are in inventory of survival and I need to keep the items in the player, but I don't know how to do it in 1.10. I hope you feel clearer

Link to comment
Share on other sites

Well, if that's the case it's fairly simple, Create the capability, have it store a list or array of ItemStack, make sure it reads and writes the NBT correctly (for saving)

 

then use packets to update the client (server will have all the info, so need to send that to the client so they know what items they have. Important for your GUI to render) However someone might know a better way for syncing it as you might be able to use the container system? (someone verfiy, I haven't dealt with containers in a long time)

 

there are several examples around on capabilities now, you can even look at my source code in my sig to see how I have my capabilities setup. They don't store ItemStacks, but it can be easily done.

Link to comment
Share on other sites

Well, if that's the case it's fairly simple, Create the capability, have it store a list or array of ItemStack, make sure it reads and writes the NBT correctly (for saving)

 

then use packets to update the client (server will have all the info, so need to send that to the client so they know what items they have. Important for your GUI to render) However someone might know a better way for syncing it as you might be able to use the container system? (someone verfiy, I haven't dealt with containers in a long time)

 

there are several examples around on capabilities now, you can even look at my source code in my sig to see how I have my capabilities setup. They don't store ItemStacks, but it can be easily done.

itemextra I understand this folder contains all I need to save the items in the player?

Link to comment
Share on other sites

actually playerextra would, I've named each one for what the capability attaches too. itemextra for items, mobextra for mobs (or entities that are non-player and hostile), and playerextra (for players) there is "attribute" which is a generic capability

 

So inside "htd.rot.capability.playerextra"

 

you will see 3 files

CapPlayerExtra (read over it, and change what you need to)

CapPlayerExtraData (this is where you will store your data, so methods and variables can be removed except writeData() and readData() that is for NBT saving and writing.)

^^^ This above file is where you will have your array or list of ItemStack (items that you want)

CapPlayerExtraProvider (read over it, and change what you need to)

 

after you have those all setup in your main mod class call the "register" method from your capability (I doubt you will leave it named as CapPlayerExtra) in the preInit event. (I have a CapabilityManager but all that does is just group the registers so I can keep my main class clean)

 

Once you have them registered "htd.rot.events#EventAddCapabilities" will show you have to attach them to entities. Mainly you will want to look at the event "public void onAddCapabilitiesEntity(AttachCapabilitiesEvent<Entity> e)"

Now don't try to access anything from the object in this event just add the capability and move on (because capability attachment happens before the object is even fully constructed so most fields if not all of them will be null)

 

Now that you have that to look at, you will need packets. I do my packets in a strange way, so referencing them would most likely be a bad idea and probably just be confusing.

 

But you will want to include a method or value in your Capability so when you update it server-side (adding or removing or altering an Item) you will want to tell it to fire a packet off to update the player client-side so that once they are in sync and any GUIs used to show that data show correctly.

 

looking at https://mcforge.readthedocs.io/en/latest/datastorage/capabilities/ just to make sure there wasn't some sort of container business that would make syncing items easier, there appears to be not.

 

Bit lengthy but hope that helps you a bit.

Link to comment
Share on other sites

actually playerextra would, I've named each one for what the capability attaches too. itemextra for items, mobextra for mobs (or entities that are non-player and hostile), and playerextra (for players) there is "attribute" which is a generic capability

 

So inside "htd.rot.capability.playerextra"

 

you will see 3 files

CapPlayerExtra (read over it, and change what you need to)

CapPlayerExtraData (this is where you will store your data, so methods and variables can be removed except writeData() and readData() that is for NBT saving and writing.)

^^^ This above file is where you will have your array or list of ItemStack (items that you want)

CapPlayerExtraProvider (read over it, and change what you need to)

 

after you have those all setup in your main mod class call the "register" method from your capability (I doubt you will leave it named as CapPlayerExtra) in the preInit event. (I have a CapabilityManager but all that does is just group the registers so I can keep my main class clean)

 

Once you have them registered "htd.rot.events#EventAddCapabilities" will show you have to attach them to entities. Mainly you will want to look at the event "public void onAddCapabilitiesEntity(AttachCapabilitiesEvent<Entity> e)"

Now don't try to access anything from the object in this event just add the capability and move on (because capability attachment happens before the object is even fully constructed so most fields if not all of them will be null)

 

Now that you have that to look at, you will need packets. I do my packets in a strange way, so referencing them would most likely be a bad idea and probably just be confusing.

 

But you will want to include a method or value in your Capability so when you update it server-side (adding or removing or altering an Item) you will want to tell it to fire a packet off to update the player client-side so that once they are in sync and any GUIs used to show that data show correctly.

 

looking at https://mcforge.readthedocs.io/en/latest/datastorage/capabilities/ just to make sure there wasn't some sort of container business that would make syncing items easier, there appears to be not.

 

Bit lengthy but hope that helps you a bit.

Well, useful. I decompiled mod advinv(forgive me, the author) and there was watching how it's done. Now I already know how to solve my problem, but there are a couple BUT. If you press in the container stored on the subject then he abruptly tosses the hot bar, and not taken to the cursor. Also in the inventory there are duplicates, very strange. Ie if I click on the last slot of the first row, then I take the pants from slots of armor, and if I click on the bottom line will take things from the hot bar, in the tabs I clean the slots and render for a specific GUI, what is the reason. And last question, can I use the same class (ExtendedPlayer implements ICapabilitySerializable, IInventory) for other containers? Thank you for responding to the call of help!

Link to comment
Share on other sites

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.