Spice of Life: Potato Edition

Spice of Life: Potato Edition

2M Downloads

Player benefits not applied on respawning

tedski999 opened this issue ยท 1 comments

commented

Description
The benefits applied to a player are not being set when the player respawns. Leaving and rejoining the server is a temporary fix to this. This is most likely due to the PlayerRespawnEvent event handler not calling updatePlayer(), as done in the onPlayerLogin method.
This is only noticeable if resetOnDeath config is true and some benefit is applied when a player has a diversity of 0.0. For example, I was trying to have max_health adjusted by -14 for a player who has a diversity of 0.0. A player who has just died will have a diversity of 0.0, but upon respawning, their max health has not been adjusted from the default.

To Reproduce

  • In the server config file, add a 0.0 to the thresholds list and some corresponding benefit to the benefitsUnparsed list so a player with 0.0 diversity should have that benefit applied.
  • Also change resetOnDeath to be true.
  • Ensure that minFoodsToActivate is 0.

With this configuration, a player who has died will have their diversity reset to 0.0. As such, once they respawn, the benefit associated with having a diversity of 0.0 should be applied. However...

  • Now in the game, confirm having a diversity of 0.0 applies the benefit you associated with it.
  • Optionally increase your diversity by eating something.
  • Kill yourself with /kill or whatever.
  • Once you respawn, your diversity will be 0.0 as expected, but the associated benefit will not have been applied.

Clearly, there is a problem where a respawning player is not updated by the mod. Interestingly, but not really relevant, the food book assures you that the 0.0 threshold has been passed and the benefit has been applied.

Possible Fix
After having a peek at the code, I think I've spotted an approach for a fix. I'm not familiar with the codebase or Forge but it seems that we just need to add an event handler for a player spawning in BenefitsHandler.java, which would look much like the onPlayerLogin method already present there. (Also see onPlayerRespawn method in CapabilityHandler.java).
Hope you can reproduce easily, it's only a small bug but would be nice to fix!

Mod version and Forge version
Mod: 1.16.X-1.10
Forge: 36.2.2

commented

Duplicate issues:

According to the Forge Docs. Player capabilities are not automatically copied when a player respawns after death or returns from The End. They suggest adding a handler to the player clone event:

https://docs.minecraftforge.net/en/1.16.x/datastorage/capabilities/#persisting-across-player-deaths

By default, the capability data does not persist on death. In order to change this, the data has to be manually copied when the player entity is cloned during the respawn process.

This can be done via PlayerEvent$Clone by reading the data from the original entity and assigning it to the new entity. In this event, the wasDead field can be used to distinguish between respawning after death and returning from the End. This is important because the data will already exist when returning from the End, so care has to be taken to not duplicate values in this case.