Skip to content

Commit ab8f8b4

Browse files
committed
Fix max health causing doubling issues
1 parent 07d0135 commit ab8f8b4

1 file changed

Lines changed: 33 additions & 17 deletions

File tree

  • src/main/java/org/mvplugins/multiverse/inventories/share

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

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.dumptruckman.minecraft.util.Logging;
44
import com.google.common.collect.Sets;
5+
import org.bukkit.Material;
56
import org.bukkit.advancement.AdvancementProgress;
67
import org.jetbrains.annotations.ApiStatus;
78
import org.mvplugins.multiverse.core.economy.MVEconomist;
@@ -18,7 +19,6 @@
1819
import org.mvplugins.multiverse.inventories.util.PlayerStats;
1920
import org.bukkit.Bukkit;
2021
import org.bukkit.Location;
21-
import org.bukkit.Material;
2222
import org.bukkit.NamespacedKey;
2323
import org.bukkit.Registry;
2424
import org.bukkit.Statistic;
@@ -198,19 +198,18 @@ public boolean updatePlayer(Player player, ProfileData profile) {
198198
new SharableHandler<Double>() {
199199
@Override
200200
public void updateProfile(ProfileData profile, Player player) {
201-
profile.set(MAX_HEALTH, getMaxHealth(player));
201+
// todo: Look into storing max health modifiers as well
202+
profile.set(MAX_HEALTH, getBaseHealth(player));
202203
}
203204

204205
@Override
205206
public boolean updatePlayer(Player player, ProfileData profile) {
206207
Double value = profile.get(MAX_HEALTH);
207208
if (value == null) {
208-
Option.of(maxHealthAttr).map(player::getAttribute)
209-
.peek(attr -> attr.setBaseValue(attr.getDefaultValue()));
209+
setBaseHealth(player, getDefaultMaxHealth(player));
210210
return false;
211211
}
212-
Option.of(maxHealthAttr).map(player::getAttribute)
213-
.peek(attr -> attr.setBaseValue(value));
212+
setBaseHealth(player, value);
214213
return true;
215214
}
216215
}).stringSerializer(new ProfileEntry(true, DataStrings.PLAYER_MAX_HEALTH))
@@ -239,31 +238,48 @@ public boolean updatePlayer(Player player, ProfileData profile) {
239238
player.setHealth(PlayerStats.HEALTH);
240239
return false;
241240
}
242-
double maxHealth = getMaxHealth(player);
243-
// This share may handled before MAX_HEALTH.
244-
// Thus this is needed to ensure there is no loss in health stored
245-
if (value > maxHealth) {
246-
Option.of(maxHealthAttr).map(player::getAttribute)
247-
.peek(attr -> attr.setBaseValue(value));
241+
if (value < 0) {
242+
player.setHealth(0);
243+
return false;
248244
}
249-
player.setHealth(value);
245+
if (value > getMaxHealth(player)) {
246+
// Try set max health attribute
247+
setBaseHealth(player, Objects.requireNonNullElse(profile.get(MAX_HEALTH), getDefaultMaxHealth(player)));
248+
}
249+
double newMaxHealth = getMaxHealth(player);
250+
player.setHealth(value > newMaxHealth ? newMaxHealth : value);
251+
return true;
250252
} catch (IllegalArgumentException e) {
251253
Logging.warning("Invalid value '" + value + "': " + e.getMessage());
252-
player.setHealth(getMaxHealth(player));
253254
return false;
254255
}
255-
return true;
256256
}
257-
258257
}).stringSerializer(new ProfileEntry(true, DataStrings.PLAYER_HEALTH))
259258
.altName("health").altName("hp").altName("hitpoints").build();
260259

261-
private static double getMaxHealth(Player player) {
260+
private static double getDefaultMaxHealth(Player player) {
261+
return Option.of(maxHealthAttr).map(player::getAttribute)
262+
.map(AttributeInstance::getDefaultValue)
263+
.getOrElse(PlayerStats.MAX_HEALTH);
264+
}
265+
266+
private static double getBaseHealth(Player player) {
262267
return Option.of(maxHealthAttr).map(player::getAttribute)
263268
.map(AttributeInstance::getBaseValue)
264269
.getOrElse(PlayerStats.MAX_HEALTH);
265270
}
266271

272+
private static void setBaseHealth(Player player, double value) {
273+
Option.of(maxHealthAttr).map(player::getAttribute)
274+
.peek(attr -> attr.setBaseValue(value));
275+
}
276+
277+
private static double getMaxHealth(Player player) {
278+
return Option.of(maxHealthAttr).map(player::getAttribute)
279+
.map(AttributeInstance::getValue)
280+
.getOrElse(PlayerStats.MAX_HEALTH);
281+
}
282+
267283
/**
268284
* Sharing Remaining Air.
269285
*/

0 commit comments

Comments
 (0)