Jump to content

Why are my capabilities not accessible on my old player?


Recommended Posts

I made a few custom capabilities for my mod and I'm trying to use the player clone event to make the capabilities persistent between deaths. If I look at the debugger I can see that the oldPlayer variable has the capabilities and the correct values attached to it. But when I try to use getCapability, it just returns a LazyOptional with null as you would expect when it is not found. Also the ifPresent(...) method does not execute the code inside. I have used the getCapability multiple times already in other parts and never ran into any problem. For example, I've supplied a debug 3 variable from my LivingEntity death event (this part is only active on players) and this one works just fine and returns the capability as you would expect. Does anyone see anything I did wrong or a reason as to why it doesn't work and how to fix this issue?
Edit: Solved: I needed to reviveCaps() before I could get them.




@SubscribeEvent public void onPlayerClone(PlayerEvent.Clone event) {
Player oldPlayer = event.getOriginal();
Player newPlayer = event.getEntity();
var debug = oldPlayer.getCapability(UpgradeDataProvider.UPGRADE_DATA); // This returns a LazyOptional with supplier null
var debug2 = newPlayer.getCapability(UpgradeDataProvider.UPGRADE_DATA); // This returns a LazyOptional with a valid supplier and works fine
oldPlayer.getCapability(PlayerDataProvider.PLAYER_DATA).ifPresent(oldStore -> { newPlayer.getCapability(PlayerDataProvider.PLAYER_DATA).ifPresent(newStore -> { newStore.copyFrom(oldStore); }); }); // this does absolutely nothing oldPlayer.getCapability(UpgradeDataProvider.UPGRADE_DATA).ifPresent(oldStore -> { newPlayer.getCapability(UpgradeDataProvider.UPGRADE_DATA).ifPresent(newStore -> { newStore.copyFrom(oldStore); }); }); // this doesn't either oldPlayer.getCapability(CurrencyDataProvider.CURRENCY_DATA).ifPresent(oldStore -> { newPlayer.getCapability(CurrencyDataProvider.CURRENCY_DATA).ifPresent(newStore -> { newStore.copyFrom(oldStore); }); }); } // neither does this

In the LivingDeath event:
var debug3 = player.getCapability(UpgradeDataProvider.UPGRADE_DATA); // This works just fine and returns the capability as expected.

Edited by DerpDeBouwerXD
Solved
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.