hey guys, i want to make my item give the player 2 hearts of absorption when theyre holding it (and when they arent take those hearts away). thing is, when i switch to it and off of it rapidly it glitches and gives either too much or none. can anyone help me here? below is my code
@Override
public void inventoryTick(ItemStack p_41404_, Level p_41405_, Entity p_41406_, int p_41407_, boolean p_41408_) {
super.inventoryTick(p_41404_, p_41405_, p_41406_, p_41407_, p_41408_);
if (p_41406_ instanceof Player player){
if (player.getMainHandItem() == p_41404_){
if (!flag) {
player.setAbsorptionAmount(player.getAbsorptionAmount() + 4);
flag = true;
}
} else {
if (flag) {
player.setAbsorptionAmount(player.getAbsorptionAmount() - 4);
flag = false;
}
}
}
}