|
2 | 2 |
|
3 | 3 | import com.dumptruckman.minecraft.util.Logging; |
4 | 4 | import com.google.common.collect.Sets; |
| 5 | +import org.bukkit.Material; |
5 | 6 | import org.bukkit.advancement.AdvancementProgress; |
6 | 7 | import org.jetbrains.annotations.ApiStatus; |
7 | 8 | import org.mvplugins.multiverse.core.economy.MVEconomist; |
|
18 | 19 | import org.mvplugins.multiverse.inventories.util.PlayerStats; |
19 | 20 | import org.bukkit.Bukkit; |
20 | 21 | import org.bukkit.Location; |
21 | | -import org.bukkit.Material; |
22 | 22 | import org.bukkit.NamespacedKey; |
23 | 23 | import org.bukkit.Registry; |
24 | 24 | import org.bukkit.Statistic; |
@@ -198,19 +198,18 @@ public boolean updatePlayer(Player player, ProfileData profile) { |
198 | 198 | new SharableHandler<Double>() { |
199 | 199 | @Override |
200 | 200 | 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)); |
202 | 203 | } |
203 | 204 |
|
204 | 205 | @Override |
205 | 206 | public boolean updatePlayer(Player player, ProfileData profile) { |
206 | 207 | Double value = profile.get(MAX_HEALTH); |
207 | 208 | if (value == null) { |
208 | | - Option.of(maxHealthAttr).map(player::getAttribute) |
209 | | - .peek(attr -> attr.setBaseValue(attr.getDefaultValue())); |
| 209 | + setBaseHealth(player, getDefaultMaxHealth(player)); |
210 | 210 | return false; |
211 | 211 | } |
212 | | - Option.of(maxHealthAttr).map(player::getAttribute) |
213 | | - .peek(attr -> attr.setBaseValue(value)); |
| 212 | + setBaseHealth(player, value); |
214 | 213 | return true; |
215 | 214 | } |
216 | 215 | }).stringSerializer(new ProfileEntry(true, DataStrings.PLAYER_MAX_HEALTH)) |
@@ -239,31 +238,48 @@ public boolean updatePlayer(Player player, ProfileData profile) { |
239 | 238 | player.setHealth(PlayerStats.HEALTH); |
240 | 239 | return false; |
241 | 240 | } |
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; |
248 | 244 | } |
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; |
250 | 252 | } catch (IllegalArgumentException e) { |
251 | 253 | Logging.warning("Invalid value '" + value + "': " + e.getMessage()); |
252 | | - player.setHealth(getMaxHealth(player)); |
253 | 254 | return false; |
254 | 255 | } |
255 | | - return true; |
256 | 256 | } |
257 | | - |
258 | 257 | }).stringSerializer(new ProfileEntry(true, DataStrings.PLAYER_HEALTH)) |
259 | 258 | .altName("health").altName("hp").altName("hitpoints").build(); |
260 | 259 |
|
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) { |
262 | 267 | return Option.of(maxHealthAttr).map(player::getAttribute) |
263 | 268 | .map(AttributeInstance::getBaseValue) |
264 | 269 | .getOrElse(PlayerStats.MAX_HEALTH); |
265 | 270 | } |
266 | 271 |
|
| 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 | + |
267 | 283 | /** |
268 | 284 | * Sharing Remaining Air. |
269 | 285 | */ |
|
0 commit comments