Skip to content

Commit

Permalink
Fix for recent change in Spigot API changing the name of the max health
Browse files Browse the repository at this point in the history
attribute.
  • Loading branch information
LlmDl committed Oct 28, 2024
1 parent 93f85d7 commit 52c530a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.object.TownBlockType;
import com.palmergames.bukkit.towny.utils.CombatUtil;
import com.palmergames.bukkit.towny.utils.MinecraftVersion;
import com.palmergames.bukkit.util.BukkitTools;

public class HealthRegenTimerTask extends TownyTimerTask {
Expand Down Expand Up @@ -76,7 +77,7 @@ 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(Attribute.GENERIC_MAX_HEALTH).getValue();
final double maxHP = player.getAttribute(getAttribute()).getValue();

// Shrink gained to fit below the maxHP.
final double gained = futureHP > maxHP ? 1.0 - (futureHP - maxHP) : 1.0;
Expand All @@ -87,6 +88,14 @@ private void evaluateHealth(Player player) {
plugin.getScheduler().run(player, () -> tryIncreaseHealth(player, currentHP, maxHP, gained));
}

@SuppressWarnings("deprecation")
private Attribute getAttribute() {
if (MinecraftVersion.CURRENT_VERSION.isNewerThan(MinecraftVersion.MINECRAFT_1_21_3))
return Attribute.MAX_HEALTH;
else
return Attribute.valueOf("GENERIC_MAX_HEALTH");
}

private void tryIncreaseHealth(Player player, double currentHealth, double maxHealth, double gained) {
EntityRegainHealthEvent event = new EntityRegainHealthEvent(player, gained, RegainReason.REGEN);
if (BukkitTools.isEventCancelled(event))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ private MinecraftVersion() {}
public static final Version MINECRAFT_1_20_2 = Version.fromString("1.20.2");
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_3 = Version.fromString("1.21.3");

public static final Version CURRENT_VERSION = Version.fromString(Bukkit.getBukkitVersion());
public static final Version OLDEST_VERSION_SUPPORTED = MINECRAFT_1_19_1;
Expand Down

0 comments on commit 52c530a

Please sign in to comment.