Jump to content

[SOLVED] [1.14.4] How do you use capabilities to store variables such as thirst in the player entity?


Recommended Posts

Posted
  On 1/2/2020 at 10:34 PM, diesieben07 said:

What about the documentation is lacking? What have you tried?

Expand  

that's for 1.13, which is outdated.

for instance, 1.14 does not have the hasCapability method from ICapabilityProvider and makes no mention of LazyOptional.

 

what I did is tried looking at source code from 1.12 and 1.13 mods and see if I could adapt the code to 1.14 but it didn't end too well.

Posted

Post your code.

1.14 hasn’t changed much from 1.13.

Some tips:

  Reveal hidden contents

 

Posted
  On 1/3/2020 at 6:26 AM, DavidM said:

Post your code.

1.14 hasn’t changed much from 1.13.

Expand  

It's an attempt to adapt from 1.12 not 1.13

 

CustomClass:

  Reveal hidden contents

PlayerDispatcher:

  Reveal hidden contents

CounterStorage:

  Reveal hidden contents

PlayerPropertiesEvent:

  Reveal hidden contents

EventHandler: (this increases the value I want to store)

  Reveal hidden contents

 

Posted (edited)
  On 1/5/2020 at 12:52 PM, diesieben07 said:

This double lazy initialization is not necessary. The point of LazyOptional is that its lazy already (hence the name). No need to create the LazyOptional lazily.

Expand  

I copied from some mod, oh well. So what should the getCapability look like then? I still don't quite understand LazyOptional tbh

  On 1/5/2020 at 12:52 PM, diesieben07 said:

Are you for real...?

Expand  

My mistake, copied some old code, what I actually put was this, is this ok or is it bad as well?

this.getCapability(PLAYER_COUNTER, null).orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!"))

 

  On 1/5/2020 at 12:52 PM, diesieben07 said:

This makes zero sense. getEntityData has nothing to do with capabilities, this does nothing useful.

Expand  

Ye probably shouldn't adapt code from two different mods (it's from ToughAsNails, which I thought could be useful)

Edited by thebadscientist
Posted
  On 1/5/2020 at 8:54 PM, diesieben07 said:

It should just return a field from your class, which holds the LazyOptional. Create the LazyOptional in your constructor and put it in a final field.

The point of LazyOptional is to provide an optional value (it can be present or not) in a lazy way (i.e. only create the value once it's needed). That's why you give a Supplier to the LazyOptional instead of the actual value.

 

You can do that, but you could just as well just access the capability instance directly, stored in a field.

 

Don't take this mod's code for anything. I haven't seen anything good in it.

Expand  

Alright, I think I got everything sorted except that the value I want to store gets reset to 0 everytime I log into the world:

[23:41:41.634] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: reading NBT!
[23:41:41.634] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: NBT Data: 120
[23:41:41.634] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: Instance Data: 120
[23:41:41.636] [Server thread/INFO] [minecraft/PlayerList]: Dev[local:E:2d267046] logged in with entity id 372 at (193.58322637936269, 72.0, -162.9556295622775)
[23:41:41.642] [Server thread/INFO] [minecraft/MinecraftServer]: Dev joined the game
[23:41:41.719] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: PLAYER LOGIN EVENT
[23:41:41.719] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: reading NBT!
[23:41:41.719] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: NBT Data: 0
[23:41:41.719] [Server thread/INFO] [co.de.pr.se.PracticeMod/]: Instance Data: 0

Do you know what would cause this?

  • thebadscientist changed the title to [SOLVED] [1.14.4] How do you use capabilities to store variables such as thirst in the player entity?
Posted
  On 1/7/2020 at 12:04 AM, thebadscientist said:

nvm got it working.

Expand  

How did you get it working, for other people with the same problem in the future?

About Me

  Reveal hidden contents

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
  On 1/7/2020 at 2:31 AM, Cadiboo said:

How did you get it working, for other people with the same problem in the future?

Expand  

You mean regarding the value resetting to 0 or regarding the whole capability implementation?

 

Either way, check my GitHub repository to see how I implemented it. 

https://github.com/deneth-weerasinghe/PracticeMod

Note the capability's stored value is increased by 10 when right-clicking water with a custom item I made.

  • Thanks 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.