From 74b41198bc1f1917f86f6478cdb5484a3db8a40c Mon Sep 17 00:00:00 2001 From: Warrior <50800980+Warriorrrr@users.noreply.github.com> Date: Sat, 30 Nov 2024 12:34:52 +0100 Subject: [PATCH] Account for the attribute prefix pre 1.21.2 --- .../bukkit/towny/tasks/HealthRegenTimerTask.java | 12 ++++++++++-- .../bukkit/towny/utils/MinecraftVersion.java | 1 + pom.xml | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/tasks/HealthRegenTimerTask.java b/Towny/src/main/java/com/palmergames/bukkit/towny/tasks/HealthRegenTimerTask.java index fb2b15ee97..7c1892b379 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/tasks/HealthRegenTimerTask.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/tasks/HealthRegenTimerTask.java @@ -5,10 +5,12 @@ import com.palmergames.bukkit.towny.object.Town; import com.palmergames.bukkit.towny.object.TownBlock; +import com.palmergames.bukkit.towny.utils.MinecraftVersion; import org.bukkit.NamespacedKey; import org.bukkit.Registry; import org.bukkit.Server; import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeInstance; import org.bukkit.entity.Player; import org.bukkit.event.entity.EntityRegainHealthEvent; import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason; @@ -23,7 +25,8 @@ public class HealthRegenTimerTask extends TownyTimerTask { - private static final Attribute MAX_HEALTH = Objects.requireNonNull(Registry.ATTRIBUTE.get(NamespacedKey.minecraft("max_health")), "max health attribute"); + private static final boolean ATTRIBUTE_PREFIX = MinecraftVersion.CURRENT_VERSION.isOlderThan(MinecraftVersion.MINECRAFT_1_21_2); // Attributes had a prefix before 1.21.2 + private static final Attribute MAX_HEALTH = Objects.requireNonNull(Registry.ATTRIBUTE.get(NamespacedKey.minecraft((ATTRIBUTE_PREFIX ? "generic." : "") + "max_health")), "max health attribute"); static { TownySettings.addReloadListener(NamespacedKey.fromString("towny:health-regen-task"), () -> TownyTimerHandler.toggleHealthRegen(TownySettings.hasHealthRegen())); @@ -82,7 +85,12 @@ private void evaluateHealth(Player player) { // Heal 1 HP while in town. final double currentHP = player.getHealth(); final double futureHP = currentHP + 1; - final double maxHP = player.getAttribute(MAX_HEALTH).getValue(); + + final AttributeInstance maxHealth = player.getAttribute(MAX_HEALTH); + if (maxHealth == null) + return; + + final double maxHP = maxHealth.getValue(); // Shrink gained to fit below the maxHP. final double gained = futureHP > maxHP ? 1.0 - (futureHP - maxHP) : 1.0; diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/utils/MinecraftVersion.java b/Towny/src/main/java/com/palmergames/bukkit/towny/utils/MinecraftVersion.java index c9f6857b8c..dd735d1143 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/utils/MinecraftVersion.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/utils/MinecraftVersion.java @@ -16,6 +16,7 @@ private MinecraftVersion() {} public static final Version MINECRAFT_1_20_3 = Version.fromString("1.20.3"); public static final Version MINECRAFT_1_20_5 = Version.fromString("1.20.5"); public static final Version MINECRAFT_1_21 = Version.fromString("1.21"); + public static final Version MINECRAFT_1_21_2 = Version.fromString("1.21.2"); public static final Version MINECRAFT_1_21_3 = Version.fromString("1.21.3"); public static final Version CURRENT_VERSION = Version.fromString(Bukkit.getBukkitVersion()); diff --git a/pom.xml b/pom.xml index 9a786fa19d..0a26d90ef6 100644 --- a/pom.xml +++ b/pom.xml @@ -34,7 +34,7 @@ - 21 + 17 UTF-8