Class based Ressources that are also saved/set when quitting & connecting to the server.

This commit is contained in:
Ka0rX 2023-03-20 16:55:13 +01:00
parent 4d89fa8fd9
commit 6b91315b57
5 changed files with 19 additions and 8 deletions

View File

@ -291,6 +291,8 @@ public class MMOCore extends JavaPlugin {
for (PlayerData data : PlayerData.getAll())
if (data.isFullyLoaded()) {
data.close();
//Saves player health before saveData as the player will be considered offline into it if it is async.
data.setHealth(data.getPlayer().getHealth());
dataProvider.getDataManager().saveData(data, true);
}

View File

@ -82,6 +82,10 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
private int level, classPoints, skillPoints, attributePoints, attributeReallocationPoints, skillTreeReallocationPoints, skillReallocationPoints;
private double experience;
private double mana, stamina, stellium;
/**
* Health is stored in playerData because when saving the playerData we can't access the player health anymore as the payer is Offline.
*/
private double health;
private Guild guild;
private SkillCastingHandler skillCasting;
private final PlayerQuests questData;
@ -946,7 +950,7 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
@Override
public double getHealth() {
return getPlayer().getHealth();
return health;
}
@Override
@ -972,6 +976,10 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
mana = Math.max(0, Math.min(amount, getStats().getStat("MAX_MANA")));
}
public void setHealth(double amount) {
this.health = amount;
}
public void setStamina(double amount) {
stamina = Math.max(0, Math.min(amount, getStats().getStat("MAX_STAMINA")));
}

View File

@ -16,7 +16,9 @@ import net.Indyuce.mmocore.skill.ClassSkill;
import net.Indyuce.mmocore.skilltree.SkillTreeNode;
import net.Indyuce.mmocore.skilltree.tree.SkillTree;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.attribute.Attribute;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.Nullable;
import java.sql.ResultSet;
@ -41,12 +43,6 @@ public class MMOCoreDataSynchronizer extends DataSynchronizer {
//Reset stats linked to triggers
data.resetTriggerStats();
// Initialize custom resources
data.setMana(result.getFloat("mana"));
data.setStellium(result.getFloat("stellium"));
data.setStamina(result.getFloat("stamina"));
data.setClassPoints(result.getInt("class_points"));
data.setSkillPoints(result.getInt("skill_points"));
data.setSkillReallocationPoints(result.getInt("skill_reallocation_points"));
@ -127,6 +123,7 @@ public class MMOCoreDataSynchronizer extends DataSynchronizer {
data.setStamina(result.getDouble("stamina"));
data.setStellium(result.getDouble("stamina"));
double health = result.getDouble("health");
health = health == 0 ? 20 : health;
health = Math.min(health, data.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue());
data.getPlayer().setHealth(health);

View File

@ -44,7 +44,7 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
updater.addData("attribute_realloc_points", data.getAttributeReallocationPoints());
updater.addJSONArray("waypoints", data.getWaypoints());
updater.addData("skill_tree_reallocation_points", data.getSkillTreeReallocationPoints());
updater.addData("health",data.getPlayer().getHealth());
updater.addData("health",data.getHealth());
updater.addData("mana", data.getMana());
updater.addData("stellium", data.getStellium());
updater.addData("stamina", data.getStamina());

View File

@ -73,6 +73,10 @@ public class PlayerListener implements Listener {
@EventHandler
public void saveDataOnQuit(PlayerQuitEvent event) {
PlayerData playerData = PlayerData.get(event.getPlayer());
/**
* We save player health as it won't be accessible anymore when saving the player data (player will be offline).
*/
playerData.setHealth(event.getPlayer().getHealth());
MMOCore.plugin.dataProvider.getDataManager().unregisterSafe(playerData);
}