Jump to content

[1.19.2] Cancelling Water Damage for water sensitive mobs


w1ll0311

Recommended Posts

Hello !
I'm trying to add a Water Resistance effect to the game. But, I must be missing something.

I'm using an event handler (LivingHurtEntity) to check if the damage source is DROWN and that the entity's air supply is higher than 0 (I just want to protect against water damage, but can still drown if they run out of oxygen). Then, checks if the entity has the water resistance effect. If the entity does have it, I cancel the event.

I've tested this by summoning an enderman, giving it the enchantment, then pushing/dropping him into water. I've checked, the event does get cancelled, but the enderman still takes damage on screen (turns red and teleports away).

I was just hoping somebody might have an idea of where I'm going wrong, if I should be using a different event or doing something else entirely. Thank you for your time and help, it is very much appreciated.

And here is my code. First is the event handler, then it's the water_resistance effect.

 

@Mod.EventBusSubscriber(modid = Extras.MOD_ID)
public class ModEvents {

	@SubscribeEvent
    public static void waterResistanceEffect(LivingHurtEvent event){
      
        Extras.LOGGER.debug("Check water dmg");
        Extras.LOGGER.debug("Entity: " + event.getEntity());
        Extras.LOGGER.debug("DamageSource: " + event.getSource());
        Extras.LOGGER.debug("AirSupply: " + event.getEntity().getAirSupply());
      
        if(event.getSource() == DamageSource.DROWN && event.getEntity().getAirSupply() > 0){
            Extras.LOGGER.debug("Entity is getting water damage");
          
            if(event.getEntity().hasEffect(ModMobEffects.WATER_RESISTANCE.get())){
                Extras.LOGGER.debug("Cancel event");
              
                event.setCanceled(true);
            }
        }
    }
}
public class ModEffect extends MobEffect {
    public ModEffect(MobEffectCategory category, int color) {
        super(category, color);
    }

    @Override
    public void applyEffectTick(LivingEntity p_19467_, int p_19468_) {
        super.applyEffectTick(p_19467_, p_19468_);
    }

    @Override
    public boolean isDurationEffectTick(int p_19455_, int p_19456_) {
        return true;
    }
}

 

Link to comment
Share on other sites

My understanding from reading the code for LivingEntity.hurt() is that cancelling the event does not stop the all the hurt processing?

It just makes the entity hurt for 0 damage.

 

For the teleportation. That is not related to the entity taking damage.

There is separate logic and event for this: EntityTeleportEvent.EnderEntity

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

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.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.