For context, I am trying to create an item that will allow the player to glide when held, and I want fall distance to reset to 0 if the player is gliding. I don't want to just cancel all fall damage because I will eventually create either a magic or stamina bar for the player which depletes as the player is gliding. If the bar reaches zero, the player should not be able to glide, and fall from that height.
To accomplish this, I added code to the item's onUpdate to check if the item is equipped and space is held (so the item can act like a parachute), and to modify fall distance appropriately.
I tried explicitly setting EntityPlayer's fallDistance property, but that didn't seem to do anything, so I instead saved the player's Y coordinate as lastGlidedYCoord, and created a handler for LivingFallEvent, which set the event.distance to be the difference between player.posY and lastGlidedYCoord. Still no luck.
Finally, I created a LivingHurtEvent handler and, for DamageSource.fall, explicitly computed fallDamage based on my fallDistance, and set event.ammount (actual spelling, I verified) to the value, and that worked.
However, even if the player glides all the way to the ground and takes no damage, the hurt sound and animation still play. To try to resolve this, I added a check in the LivingHurtEvent handler to see if the fallDamage was less than .5 hearts. If so, I canceled the event. No change. My current handler method looks like this:
// this boolean tracks if the player has glided since jumping
// so that don't recompute every time the player takes fall damage
boolean alterFall;
double lastGlidedYCoord;
@SubscribeEvent
public void onLivingHurtEvent(LivingHurtEvent event) {
if (alterFall && event.entity instanceof EntityPlayer && event.source == DamageSource.fall) {
EntityPlayer player = (EntityPlayer)event.entity;
double recompFallDamage = Math.floor( (lastGlidedYCoord- player.posY - 3) / 2 );
if (recompFallDamage < 1) {
event.setCanceled(true);
} else {
event.ammount = (float) recompFallDamage;
}
alterFall = false;
}
}
Any help getting my mod to work as desired would be much appreciated, thanks!