Jump to content

Saving Custom Player Data


Slit_bodmod

Recommended Posts

So I have a class called "CharecterData" that I want each player to essentially have an instance of as though it were a field in the player class.

It will contain a series of fields including ints, bools as well as some custom enums and potentially instances of classes. I was wondering what the ideal method of implementing this would be. It should be unique for each player for each world, be created on world creation (or on servers when they first log in), will survive when players die, be saved when they log out and reopened when they log back in and reopened when they log back in.

I am relatively new to forge (this is my first mod) and assume that this is a relatively common thing for mods to want to implement so if anyone can help that would be greatly appreciated.

thankyou,

Link to comment
Share on other sites

Ah, thank you. I'd heard of capabilities and read the docs page but couldn't quite understand what they were or were for. I got that they were some kind of interface for handling different features but wasn't quite sure how they were added to stuff as it were. Are they little packets of data that get attached to classes somehow or are they their own separate class that is simply used in tandem with separate ones.

 

So for example in the context of this would I create a capability that I somehow attach to a player class or is there a player capability that I need to somehow add data too, or both?

 

What is exposing a capability? Is this how you read data from it? As you can tell I'm a bit lost tbh, maybe it's just me but I feel like I don't quite understand.

 

btw, I also saw in the docs that it put IItemHandler on a tile entity instead of minecraft's inventory class, should I do the same?

Link to comment
Share on other sites

21 minutes ago, Slit_bodmod said:

Are they little packets of data that get attached to classes somehow or are they their own separate class that is simply used in tandem with separate ones.

They are basically temporary data holders that can be written to the specific object when saving the game. Their implementation seems separate, but that's mainly because they are a hook into the actual system that manages the NBT. So you would create a capability and attach it to the object class through AttachCapabilitiesEvent.

23 minutes ago, Slit_bodmod said:

What is exposing a capability? Is this how you read data from it?

Exposing a capability is making it noticeable to the object it's attached to. You're yelling out to someone saying "hey, I exist now" and grabbing that instance. Well, I should say almost that. The use of LazyOptionals basically say "hey, I might exist", and then you compare the cap being checked to your own to say that you exist.

25 minutes ago, Slit_bodmod said:

IItemHandler on a tile entity instead of minecraft's inventory class, should I do the same?

Yes, the IInventory class is deprecated for any use within Forge. It should be setup using capabilities such as IItemHandler. The only time where you should reference IInventory is maybe for recipes; however, those can be wrapped around.

Link to comment
Share on other sites

Oh, so i should probably create my CharacterData class as something that extends "Capability.IStorage<CharacterData>" and then use the write and read nbt methods to write and read the fields in that class onto said nbt tag. Using my own functions to turn the enums and classes that I want to implement into nbt data. Have a function that hooks onto the entity attachCapability event, and if it's a player or my npc mob, attach the data to the entity. Then, if I want to access that, I can call has capability to check an entity has it and get capability to access it and then read and write data into my capabilities methods and fields.

Is that correct/ did i miss anything?

 

also, my npc mob currently has an Inventory field, what class should it have instead as I can't find an ItemHandler class that I can instantiate.

 

Link to comment
Share on other sites

48 minutes ago, Slit_bodmod said:

Is that correct/ did i miss anything?

Not completely. Read through the documentation again. The IStorage class is just a helper for the capability itself. The actual serialization is handled by the provider you attach the capability to. Everything else I believe is roughly correct.

49 minutes ago, Slit_bodmod said:

also, my npc mob currently has an Inventory field, what class should it have instead as I can't find an ItemHandler class that I can instantiate.

ItemStackHandler iirc.

Link to comment
Share on other sites

1 hour ago, Slit_bodmod said:

do you know if there are any examples of this being used in vanilla/forge code (or an open source mod) that I could look too.

Vanilla and forge does not create any. As for an open source mod that does, there are plenty. However, I have not looked into them enough to give my approval of their practices and code. For that reason, I do not want to give you any links in case I promote such bad practices or outdated methods.

  • Like 1
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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.