Jump to content

[1.7.10] Canceling LivingHurtEvent for player doesn't prevent hurt animation


Recommended Posts

Posted

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.  :D

 

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!

Posted

Not sure if it helps but there is also the LivingFallEvent which can be canceled to set fallDistance to 0.  Not sure if that also prevents the hurt animation, but I think it will because of the following.

 

The hurt animation is related to the attackEntityFrom() method.  If you look in EntityLivingBase there is method called fall() and that is where it triggers the LivingFallEvent (and passes fall distance), plays the hurt sound and also invokes the attackEntityFrom() with a DamageSource.FALL.  If you cancel the event, it won't play the sound or invoke the damage.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

The problem is that to check if space is held, you must be on the client side, but to affect the fall distance, you must be on the server side, so your checks are incompatible.

 

Two solutions:

 

a) Set your item in use via right click and check if the player is using the item before setting fall distance

 

b) Check each tick if space bar is still pressed and send a packet when it is first pressed, to set the item in use or something, and then another when it is released; this will give the server the information it needs in your item's onUpdate method.

Posted

Not sure if it helps but there is also the LivingFallEvent which can be canceled to set fallDistance to 0.

 

The problem is that to check if space is held, you must be on the client side, but to affect the fall distance, you must be on the server side, so your checks are incompatible.

 

Turns out, both these things were necessary to fix the problem. I created a handler for LivingFallEvent, gave it access to some data from the client-side gliding checks I was performing in the onUpdate, and made sure it executed on the server side. No more hurt animation or sound!  :)

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.