Skip to content

Commit

Permalink
Account for the attribute prefix pre 1.21.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Warriorrrr committed Nov 30, 2024
1 parent 6056735 commit 74b4119
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()));
Expand Down Expand Up @@ -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;
Expand Down
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</modules>

<properties>
<java.version>21</java.version>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down

0 comments on commit 74b4119

Please sign in to comment.