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

Add max health shareable. #423

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps a slightly more description string? maxHp or something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea possible, I was just following how the other stats named, such as Total experience being txp. But if you prefer maxHp instead, will change it.

/**
* Player health identifier.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -169,23 +170,71 @@ 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;
}
}).stringSerializer(new ProfileEntry(true, DataStrings.PLAYER_HEALTH))
.altName("health").altName("hp").altName("hitpoints").build();

/**
* Sharing Max Health.
*/
public static final Sharable<Double> MAX_HEALTH = new Sharable.Builder<Double>("max_hit_points", Double.class,
new SharableHandler<Double>() {
@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.
*/
Expand Down