Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-fix max health attribute #7678

Merged
merged 3 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +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;
Expand All @@ -16,10 +19,14 @@
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;

import java.util.Objects;

public class HealthRegenTimerTask extends TownyTimerTask {

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()));
Expand Down Expand Up @@ -78,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(getMaxHealthAttribute()).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;
Expand All @@ -96,12 +108,4 @@ private void tryIncreaseHealth(Player player, double currentHealth, double maxHe

player.setHealth(Math.min(maxHealth, event.getAmount() + currentHealth));
}

@SuppressWarnings("deprecation")
private Attribute getMaxHealthAttribute() {
if (MinecraftVersion.CURRENT_VERSION.isNewerThanOrEquals(MinecraftVersion.MINECRAFT_1_21_3))
return Attribute.MAX_HEALTH;
else
return Attribute.valueOf("GENERIC_MAX_HEALTH");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down