From 38625312a167c1c419166a3327f771f84cb74fca Mon Sep 17 00:00:00 2001
From: David Choo <davchoo@users.noreply.github.com>
Date: Sat, 21 May 2022 11:54:32 -0400
Subject: [PATCH] Prevent max health from being set below 0 (#2980)

* Prevent max health from being set below 0

* Add more detail to comment
---
 .../java/org/geysermc/geyser/entity/type/LivingEntity.java    | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/geysermc/geyser/entity/type/LivingEntity.java b/core/src/main/java/org/geysermc/geyser/entity/type/LivingEntity.java
index 0cce0f8df..87b709309 100644
--- a/core/src/main/java/org/geysermc/geyser/entity/type/LivingEntity.java
+++ b/core/src/main/java/org/geysermc/geyser/entity/type/LivingEntity.java
@@ -300,7 +300,9 @@ public class LivingEntity extends Entity {
         if (javaAttribute.getType() instanceof AttributeType.Builtin type) {
             switch (type) {
                 case GENERIC_MAX_HEALTH -> {
-                    this.maxHealth = (float) AttributeUtils.calculateValue(javaAttribute);
+                    // Since 1.18.0, setting the max health to 0 or below causes the entity to die on Bedrock but not on Java
+                    // See https://github.com/GeyserMC/Geyser/issues/2971
+                    this.maxHealth = Math.max((float) AttributeUtils.calculateValue(javaAttribute), 1f);
                     newAttributes.add(createHealthAttribute());
                 }
                 case GENERIC_ATTACK_DAMAGE -> newAttributes.add(calculateAttribute(javaAttribute, GeyserAttributeType.ATTACK_DAMAGE));