Skip to content

Commit acc420a

Browse files
committed
Improve null checking for maxhealth attribute
1 parent 2143369 commit acc420a

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/main/java/org/mvplugins/multiverse/inventories/share/Sharables.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.dumptruckman.minecraft.util.Logging;
44
import org.bukkit.NamespacedKey;
55
import org.bukkit.Registry;
6+
import org.bukkit.attribute.AttributeInstance;
67
import org.mvplugins.multiverse.core.teleportation.AsyncSafetyTeleporter;
8+
import org.mvplugins.multiverse.external.vavr.control.Option;
79
import org.mvplugins.multiverse.inventories.MultiverseInventories;
810
import org.mvplugins.multiverse.inventories.handleshare.SpawnChangeListener;
911
import org.mvplugins.multiverse.inventories.profile.group.WorldGroup;
@@ -65,7 +67,10 @@ public static void init(MultiverseInventories inventories) {
6567
if (Sharables.safetyTeleporter == null) {
6668
Sharables.safetyTeleporter = inventories.getServiceLocator().getService(AsyncSafetyTeleporter.class);
6769
}
68-
Sharables.maxHealthAttr = Registry.ATTRIBUTE.get(NamespacedKey.minecraft("max-health"));
70+
Sharables.maxHealthAttr = Registry.ATTRIBUTE.get(NamespacedKey.minecraft("max_health"));
71+
if (Sharables.maxHealthAttr == null) {
72+
Logging.warning("Could not find max_health attribute. Health related sharables may not work as expected.");
73+
}
6974
}
7075

7176
/**
@@ -178,8 +183,10 @@ public boolean updatePlayer(Player player, PlayerProfile profile) {
178183
public void updateProfile(PlayerProfile profile, Player player) {
179184
double health = player.getHealth();
180185
// Player is dead, so health should be regained to full.
181-
if (health <= 0 && maxHealthAttr != null) {
182-
health = player.getAttribute(maxHealthAttr).getValue();
186+
if (health <= 0) {
187+
health = Option.of(maxHealthAttr).map(player::getAttribute)
188+
.map(AttributeInstance::getValue)
189+
.getOrElse(PlayerStats.HEALTH);
183190
}
184191
profile.set(HEALTH, health);
185192
}
@@ -195,9 +202,10 @@ public boolean updatePlayer(Player player, PlayerProfile profile) {
195202
player.setHealth(value);
196203
} catch (IllegalArgumentException e) {
197204
Logging.fine("Invalid value '" + value + "': " + e.getMessage());
198-
if (maxHealthAttr != null) {
199-
player.setHealth(player.getAttribute(maxHealthAttr).getValue());
200-
}
205+
Option.of(maxHealthAttr).map(player::getAttribute)
206+
.map(AttributeInstance::getValue)
207+
.peek(player::setHealth)
208+
.onEmpty(() -> player.setHealth(PlayerStats.HEALTH));
201209
return false;
202210
}
203211
return true;

src/main/java/org/mvplugins/multiverse/inventories/util/PlayerStats.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class PlayerStats {
2020
/**
2121
* Default health value.
2222
*/
23-
public static final int HEALTH = 20;
23+
public static final double HEALTH = 20;
2424
/**
2525
* Default experience value.
2626
*/

0 commit comments

Comments
 (0)