Fixed respawn issue when using YAML

This commit is contained in:
Jules 2023-06-25 23:15:05 +02:00
parent fdd1234d68
commit 5042f78fa3
3 changed files with 20 additions and 16 deletions

View File

@ -39,8 +39,14 @@ public class MMOCoreUtils {
: caseOnWords(item.getType().name().replace("_", " ")); : 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) { 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) { public static String caseOnWords(String s) {

View File

@ -78,15 +78,12 @@ public class MMOCoreDataSynchronizer extends SQLDataSynchronizer<PlayerData> {
getData().setUnlockedItems(unlockedItems); getData().setUnlockedItems(unlockedItems);
if (!isEmpty(result.getString("guild"))) { if (!isEmpty(result.getString("guild"))) {
final Guild guild = MMOCore.plugin.dataProvider.getGuildManager().getGuild(result.getString("guild")); final Guild guild = MMOCore.plugin.dataProvider.getGuildManager().getGuild(result.getString("guild"));
if (guild != null) if (guild != null) getData().setGuild(guild.hasMember(getData().getUniqueId()) ? guild : null);
getData().setGuild(guild.hasMember(getData().getUniqueId()) ? guild : null);
} }
if (!isEmpty(result.getString("attributes"))) if (!isEmpty(result.getString("attributes"))) getData().getAttributes().load(result.getString("attributes"));
getData().getAttributes().load(result.getString("attributes"));
if (!isEmpty(result.getString("professions"))) if (!isEmpty(result.getString("professions")))
getData().getCollectionSkills().load(result.getString("professions")); getData().getCollectionSkills().load(result.getString("professions"));
if (!isEmpty(result.getString("quests"))) if (!isEmpty(result.getString("quests"))) getData().getQuestData().load(result.getString("quests"));
getData().getQuestData().load(result.getString("quests"));
getData().getQuestData().updateBossBar(); getData().getQuestData().updateBossBar();
if (!isEmpty(result.getString("waypoints"))) if (!isEmpty(result.getString("waypoints")))
getData().getWaypoints().addAll(MMOCoreUtils.jsonArrayToList(result.getString("waypoints"))); getData().getWaypoints().addAll(MMOCoreUtils.jsonArrayToList(result.getString("waypoints")));
@ -101,8 +98,7 @@ public class MMOCoreDataSynchronizer extends SQLDataSynchronizer<PlayerData> {
JsonObject object = MythicLib.plugin.getGson().fromJson(result.getString("bound_skills"), JsonObject.class); JsonObject object = MythicLib.plugin.getGson().fromJson(result.getString("bound_skills"), JsonObject.class);
for (Map.Entry<String, JsonElement> entry : object.entrySet()) { for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
ClassSkill skill = getData().getProfess().getSkill(entry.getValue().getAsString()); ClassSkill skill = getData().getProfess().getSkill(entry.getValue().getAsString());
if (skill != null) if (skill != null) getData().bindSkill(Integer.parseInt(entry.getKey()), skill);
getData().bindSkill(Integer.parseInt(entry.getKey()), skill);
} }
} }
@ -123,15 +119,17 @@ public class MMOCoreDataSynchronizer extends SQLDataSynchronizer<PlayerData> {
* These should be loaded after to make sure that the * These should be loaded after to make sure that the
* MAX_MANA, MAX_STAMINA & MAX_STELLIUM stats are already loaded. * MAX_MANA, MAX_STAMINA & MAX_STELLIUM stats are already loaded.
*/ */
double health = result.getDouble("health");
getData().setMana(result.getDouble("mana")); getData().setMana(result.getDouble("mana"));
getData().setStamina(result.getDouble("stamina")); getData().setStamina(result.getDouble("stamina"));
getData().setStellium(result.getDouble("stellium")); getData().setStellium(result.getDouble("stellium"));
if (getData().isOnline()) { 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. /*
health = health == 0 && !getData().getPlayer().isDead() ? getData().getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue() : health; * If the player is not dead and the health is 0, this means that the data was
health = Math.max(Math.min(health, getData().getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()), 0); * 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); getData().getPlayer().setHealth(health);
} }

View File

@ -134,7 +134,7 @@ public class YAMLPlayerDataHandler extends YAMLSynchronizedDataHandler<PlayerDat
data.setStamina(config.contains("stamina") ? config.getDouble("stamina") : data.getStats().getStat("MAX_STAMINA")); data.setStamina(config.contains("stamina") ? config.getDouble("stamina") : data.getStats().getStat("MAX_STAMINA"));
data.setStellium(config.contains("stellium") ? config.getDouble("stellium") : data.getStats().getStat("MAX_STELLIUM")); data.setStellium(config.contains("stellium") ? config.getDouble("stellium") : data.getStats().getStat("MAX_STELLIUM"));
if (data.isOnline()) if (data.isOnline() && !data.getPlayer().isDead())
data.getPlayer().setHealth(MMOCoreUtils.fixResource(config.getDouble("health"), data.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue())); data.getPlayer().setHealth(MMOCoreUtils.fixResource(config.getDouble("health"), data.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()));
} }