From e74a928541a63ba1bada57fc78606710c8c0d033 Mon Sep 17 00:00:00 2001 From: benwoo1110 <30431861+benwoo1110@users.noreply.github.com> Date: Sun, 28 Feb 2021 00:05:33 +0800 Subject: [PATCH] Add max health shareable. --- .../multiverseinventories/DataStrings.java | 4 ++ .../multiverseinventories/PlayerStats.java | 4 ++ .../share/Sharables.java | 53 ++++++++++++++++++- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/onarandombox/multiverseinventories/DataStrings.java b/src/main/java/com/onarandombox/multiverseinventories/DataStrings.java index a56c8cae..7a3fd88c 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/DataStrings.java +++ b/src/main/java/com/onarandombox/multiverseinventories/DataStrings.java @@ -73,6 +73,10 @@ public class DataStrings { * Player bed spawn location identifier. */ public static final String PLAYER_PROFILE_TYPE = "profileType"; + /** + * Player max health identifier. + */ + public static final String PLAYER_MAX_HEALTH = "mhp"; /** * Player health identifier. */ diff --git a/src/main/java/com/onarandombox/multiverseinventories/PlayerStats.java b/src/main/java/com/onarandombox/multiverseinventories/PlayerStats.java index 8c546a53..541ef5f2 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/PlayerStats.java +++ b/src/main/java/com/onarandombox/multiverseinventories/PlayerStats.java @@ -21,6 +21,10 @@ public class PlayerStats { * Default health value. */ public static final int HEALTH = 20; + /** + * Default max health value. + */ + public static final int MAX_HEALTH = 20; /** * Default experience value. */ diff --git a/src/main/java/com/onarandombox/multiverseinventories/share/Sharables.java b/src/main/java/com/onarandombox/multiverseinventories/share/Sharables.java index 8c0ad06a..8f870c07 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/share/Sharables.java +++ b/src/main/java/com/onarandombox/multiverseinventories/share/Sharables.java @@ -13,6 +13,7 @@ import org.bukkit.NamespacedKey; import org.bukkit.World; import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeInstance; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffect; @@ -169,16 +170,29 @@ public void updateProfile(PlayerProfile profile, Player player) { @Override public boolean updatePlayer(Player player, PlayerProfile profile) { + AttributeInstance maxHealthAttr = player.getAttribute(Attribute.GENERIC_MAX_HEALTH); + if (maxHealthAttr == null) { + Logging.warning("Unable to get max health attribute for %s.", player.getName()); + return false; + } + Double value = profile.get(HEALTH); if (value == null) { - player.setHealth(PlayerStats.HEALTH); + player.setHealth(maxHealthAttr.getValue()); return false; } + + // This share may handled before MAX_HEALTH. + // Thus this is needed to ensure there is no loss in health stored. + if (value > maxHealthAttr.getValue()) { + maxHealthAttr.setBaseValue(value); + } + try { player.setHealth(value); } catch (IllegalArgumentException e) { Logging.fine("Invalid value '" + value + "': " + e.getMessage()); - player.setHealth(player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()); + player.setHealth(maxHealthAttr.getValue()); return false; } return true; @@ -186,6 +200,41 @@ public boolean updatePlayer(Player player, PlayerProfile profile) { }).stringSerializer(new ProfileEntry(true, DataStrings.PLAYER_HEALTH)) .altName("health").altName("hp").altName("hitpoints").build(); + /** + * Sharing Max Health. + */ + public static final Sharable MAX_HEALTH = new Sharable.Builder("max_hit_points", Double.class, + new SharableHandler() { + @Override + public void updateProfile(PlayerProfile profile, Player player) { + AttributeInstance maxHealthAttr = player.getAttribute(Attribute.GENERIC_MAX_HEALTH); + if (maxHealthAttr == null) { + Logging.warning("Unable to get max health attribute for %s.", player.getName()); + return; + } + profile.set(MAX_HEALTH, maxHealthAttr.getValue()); + } + + @Override + public boolean updatePlayer(Player player, PlayerProfile profile) { + AttributeInstance maxHealthAttr = player.getAttribute(Attribute.GENERIC_MAX_HEALTH); + if (maxHealthAttr == null) { + Logging.warning("Unable to get max health attribute for %s.", player.getName()); + return false; + } + + Double value = profile.get(MAX_HEALTH); + if (value == null) { + maxHealthAttr.setBaseValue(PlayerStats.MAX_HEALTH); + return false; + } + + maxHealthAttr.setBaseValue(value); + return true; + } + }).stringSerializer(new ProfileEntry(true, DataStrings.PLAYER_MAX_HEALTH)) + .altName("maxhealth").altName("maxhp").altName("maxhitpoints").build(); + /** * Sharing Remaining Air. */