Jump to content

Recommended Posts

Posted

How would I go about restoring the players hp equal to the damage the player dealt to an entity? I could simply restore a fixed amount, but I wish to restore an amount that depends on the damage dealt. Has anyone done something likes this before?

Posted

Well, for some time now everything is strongly based on Attributes and AttributeModifiers. Same goes for swords which return Modifiers thet need to be applied when you attack.

 

Since on the moment of attack you don't really know how much dmg you actually cause (target armour and stuff) you need to to it post-damaging.

 

You could:

* Use LivingHurtEvent - event.source. Check if source is entitiy and if he holds some kinds fo sword. Heal attacker for event.amount.

 

There are other ways I guess.

1.7.10 is no longer supported by forge, you are on your own.

Posted

LivingHurtEvent is not completely reliable  because other mods can subscribe to it and modify the amount - you can try to make sure your handler is the last one called by setting its priority to LOWEST, but that is no guarantee - other mods may have done the same.

 

Even if you are last, you still must account for armor and potion effects, both of which are handled after the event is finished (see Entity#damageEntity).

 

One solution would be to flag the entity as struck by the sword, also saving a reference to the sword-wielder and the damaged entity's current health; then, in the following tick compare the damaged entity's current health to its health at the time it was struck. The con to this approach is that if something else damaged the entity in the same tick for more damage, the sword-wielder would recover more health than he should, or possibly the entity even regenerated some health and the sword-wielder recovers less or nothing at all.

 

Another solution is to write a method replicating the logic from #damageEntity (and the methods that it calls) - this will give you a way to pretty much guarantee that you get the correct amount so long as there isn't a handler called after yours that does something to the damage amount.

 

In both cases you are taking a (small) chance.

 

Yet another solution would be to change your mechanic - rather than basing the amount healed on the damage inflicted, have it as an AttributeModifier or Enchantment like effect where upon inflicting any damage at all, the attacker heals a set amount per attribute point, e.g. each point gives 1 heart of healing, so vampire attribute / enchantment at level 3 gives (3 x 2.0) or 3 hearts (6 hp) of healing. Just a thought.

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.