Permanently lose hearts when you take damage

87 Views Asked by At

I want that if the player takes damage and the player permanently loses that hearts.

import org.bukkit.entity.Player;
import org.bukkit.entity.Entity;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.EventHandler;


@EventHandler
    public void EntityDamage(EntityDamageEvent event) {
        Entity e = event.getEntity();
        if (e instanceof Player) {
            Player player = (Player) e;
            double newhealth = player.getHealth();
            player.setMaxHealth(newhealth);
        }
    }

i use this code but this only reduces hearts after the second damage event

1

There are 1 best solutions below

2
Lanfix On

player.getHealth() gets you the health of the player before its modification. By looking into the event's documentation i found this solution :

You can calculate the new health with event.getFinalDamage() :

double newhealth = player.getHealth() - event.getFinalDamage();
player.setMaxHealth(newhealth);

Hope it will help you !

PS: If you just want the player not to regenerate you can change the 'NaturalRegeneration' gamerule (but it will still allow the regeneration effect)