From 5042f78fa3e6cd19816ae7cd2bd5e5deffb2d8c2 Mon Sep 17 00:00:00 2001 From: Jules Date: Sun, 25 Jun 2023 23:15:05 +0200 Subject: [PATCH] Fixed respawn issue when using YAML --- .../mmocore/api/util/MMOCoreUtils.java | 8 +++++- .../data/sql/MMOCoreDataSynchronizer.java | 26 +++++++++---------- .../data/yaml/YAMLPlayerDataHandler.java | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/util/MMOCoreUtils.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/util/MMOCoreUtils.java index a084557c..752ddea1 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/util/MMOCoreUtils.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/api/util/MMOCoreUtils.java @@ -39,8 +39,14 @@ public class MMOCoreUtils { : caseOnWords(item.getType().name().replace("_", " ")); } + /** + * @param current Current value of resource + * @param maxStat Maximum value of resource + * @return Clamped resource value. If the provided current value is 0, + * this function will return the maximum resource value. + */ public static double fixResource(double current, double maxStat) { - return current == 0 ? maxStat : Math.min(current, maxStat); + return current == 0 ? maxStat : Math.max(0, Math.min(current, maxStat)); } public static String caseOnWords(String s) { diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/data/sql/MMOCoreDataSynchronizer.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/data/sql/MMOCoreDataSynchronizer.java index 264666a1..30736a42 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/data/sql/MMOCoreDataSynchronizer.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/data/sql/MMOCoreDataSynchronizer.java @@ -78,15 +78,12 @@ public class MMOCoreDataSynchronizer extends SQLDataSynchronizer { getData().setUnlockedItems(unlockedItems); if (!isEmpty(result.getString("guild"))) { final Guild guild = MMOCore.plugin.dataProvider.getGuildManager().getGuild(result.getString("guild")); - if (guild != null) - getData().setGuild(guild.hasMember(getData().getUniqueId()) ? guild : null); + if (guild != null) getData().setGuild(guild.hasMember(getData().getUniqueId()) ? guild : null); } - if (!isEmpty(result.getString("attributes"))) - getData().getAttributes().load(result.getString("attributes")); + if (!isEmpty(result.getString("attributes"))) getData().getAttributes().load(result.getString("attributes")); if (!isEmpty(result.getString("professions"))) getData().getCollectionSkills().load(result.getString("professions")); - if (!isEmpty(result.getString("quests"))) - getData().getQuestData().load(result.getString("quests")); + if (!isEmpty(result.getString("quests"))) getData().getQuestData().load(result.getString("quests")); getData().getQuestData().updateBossBar(); if (!isEmpty(result.getString("waypoints"))) getData().getWaypoints().addAll(MMOCoreUtils.jsonArrayToList(result.getString("waypoints"))); @@ -101,8 +98,7 @@ public class MMOCoreDataSynchronizer extends SQLDataSynchronizer { JsonObject object = MythicLib.plugin.getGson().fromJson(result.getString("bound_skills"), JsonObject.class); for (Map.Entry entry : object.entrySet()) { ClassSkill skill = getData().getProfess().getSkill(entry.getValue().getAsString()); - if (skill != null) - getData().bindSkill(Integer.parseInt(entry.getKey()), skill); + if (skill != null) getData().bindSkill(Integer.parseInt(entry.getKey()), skill); } } @@ -123,15 +119,17 @@ public class MMOCoreDataSynchronizer extends SQLDataSynchronizer { * These should be loaded after to make sure that the * MAX_MANA, MAX_STAMINA & MAX_STELLIUM stats are already loaded. */ - double health = result.getDouble("health"); getData().setMana(result.getDouble("mana")); getData().setStamina(result.getDouble("stamina")); getData().setStellium(result.getDouble("stellium")); - if (getData().isOnline()) { - //If the player is not dead and the health is 0, this means that the data was - //missing from the data base and it gives full health to the player. - health = health == 0 && !getData().getPlayer().isDead() ? getData().getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue() : health; - health = Math.max(Math.min(health, getData().getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()), 0); + if (getData().isOnline() && !getData().getPlayer().isDead()) { + + /* + * If the player is not dead and the health is 0, this means that the data was + * missing from the data base and it gives full health to the player. If the + * player is dead however, it must not account for that subtle edge case. + */ + final double health = MMOCoreUtils.fixResource(result.getDouble("health"), getData().getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()); getData().getPlayer().setHealth(health); } diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/data/yaml/YAMLPlayerDataHandler.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/data/yaml/YAMLPlayerDataHandler.java index 0d11a606..8cd0d6cd 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/data/yaml/YAMLPlayerDataHandler.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/data/yaml/YAMLPlayerDataHandler.java @@ -134,7 +134,7 @@ public class YAMLPlayerDataHandler extends YAMLSynchronizedDataHandler