SavingPlayerData. Object to store all the data of an offline player. Restructuration for /mmocore admin transferdata

This commit is contained in:
Ka0rX 2022-07-27 17:28:33 +02:00
parent 5589609750
commit 6af4c2ee46
2 changed files with 29 additions and 18 deletions

View File

@ -1,5 +1,6 @@
package net.Indyuce.mmocore.manager.data.mysql; package net.Indyuce.mmocore.manager.data.mysql;
import com.google.gson.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
@ -14,6 +15,7 @@ import net.Indyuce.mmocore.api.util.MMOCoreUtils;
import net.Indyuce.mmocore.guild.provided.Guild; import net.Indyuce.mmocore.guild.provided.Guild;
import net.Indyuce.mmocore.manager.data.PlayerDataManager; import net.Indyuce.mmocore.manager.data.PlayerDataManager;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -238,12 +240,14 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
result.getString("class_info")); result.getString("class_info"));
savingPlayerDataList.add(data); savingPlayerDataList.add(data);
Bukkit.broadcastMessage(new Gson().toJson(data));
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
}); });
} }

View File

@ -19,6 +19,7 @@ import net.Indyuce.mmocore.guild.provided.Guild;
import net.Indyuce.mmocore.manager.data.DataProvider; import net.Indyuce.mmocore.manager.data.DataProvider;
import net.Indyuce.mmocore.manager.data.PlayerDataManager; import net.Indyuce.mmocore.manager.data.PlayerDataManager;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -166,10 +167,11 @@ public class YAMLPlayerDataManager extends PlayerDataManager {
config.set("level", data.level()); config.set("level", data.level());
config.set("experience", data.experience()); config.set("experience", data.experience());
config.set("class", data.classId()); config.set("class", data.classId());
config.set("waypoints", data.waypoints()); config.set("waypoints", data.waypoints().stream().toList());
config.set("friends", data.friends()); config.set("friends", data.friends());
config.set("last-login", data.lastLogin()); config.set("last-login", data.lastLogin());
config.set("guild", data.guildId()); if (data.guildId() != null)
config.set("guild", data.guildId());
config.set("skill", null); config.set("skill", null);
data.skills().forEach((key1, value) -> config.set("skill." + key1, value)); data.skills().forEach((key1, value) -> config.set("skill." + key1, value));
@ -185,7 +187,7 @@ public class YAMLPlayerDataManager extends PlayerDataManager {
for (Map.Entry<String, JsonElement> entry : jo.entrySet()) { for (Map.Entry<String, JsonElement> entry : jo.entrySet()) {
try { try {
String id = entry.getKey().toLowerCase().replace("_", "-").replace(" ", "-"); String id = entry.getKey().toLowerCase().replace("_", "-").replace(" ", "-");
config.set("attributes." + id, entry.getValue().getAsInt()); config.set("attribute." + id, entry.getValue().getAsInt());
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -248,6 +250,8 @@ public class YAMLPlayerDataManager extends PlayerDataManager {
MMOCore.log(Level.WARNING, "Could not load class info '" + entry.getKey() + "': " + exception.getMessage()); MMOCore.log(Level.WARNING, "Could not load class info '" + entry.getKey() + "': " + exception.getMessage());
} }
} }
Bukkit.broadcastMessage("SAVED");
file.save(); file.save();
} }
@ -262,11 +266,13 @@ public class YAMLPlayerDataManager extends PlayerDataManager {
FileConfiguration config = new ConfigFile(uuid).getConfig(); FileConfiguration config = new ConfigFile(uuid).getConfig();
Map<String, Integer> skills = new HashMap<>(); Map<String, Integer> skills = new HashMap<>();
config.getConfigurationSection("skill").getKeys(false).forEach(id -> skills.put(id, config.getInt("skill." + id))); if (config.contains("skill"))
config.getConfigurationSection("skill").getKeys(false).forEach(id -> skills.put(id, config.getInt("skill." + id)));
Map<String, Integer> itemClaims = new HashMap<>(); Map<String, Integer> itemClaims = new HashMap<>();
for (String key : config.getConfigurationSection("times-claimed").getKeys(true)) if (config.contains("times-claimed"))
itemClaims.put(key, config.getInt("times-claimed." + key)); for (String key : config.getConfigurationSection("times-claimed").getKeys(true))
itemClaims.put(key, config.getInt("times-claimed." + key));
//Creates the attributes json //Creates the attributes json
@ -274,10 +280,11 @@ public class YAMLPlayerDataManager extends PlayerDataManager {
ConfigurationSection section = config.getConfigurationSection("attributes"); ConfigurationSection section = config.getConfigurationSection("attributes");
JsonObject attributesJson = new JsonObject(); JsonObject attributesJson = new JsonObject();
for (String key : section.getKeys(false)) { if (section != null)
String id = key.toLowerCase().replace("-", "_").replace(" ", "_"); for (String key : section.getKeys(false)) {
attributesJson.addProperty(id, section.getInt(key)); String id = key.toLowerCase().replace("-", "_").replace(" ", "_");
} attributesJson.addProperty(id, section.getInt(key));
}
//Creates the profession json //Creates the profession json
@ -285,16 +292,16 @@ public class YAMLPlayerDataManager extends PlayerDataManager {
section = config.getConfigurationSection("profession"); section = config.getConfigurationSection("profession");
JsonObject collectionSkillsJson = new JsonObject(); JsonObject collectionSkillsJson = new JsonObject();
; if (section != null)
for (String key : config.getKeys(false)) { for (String key : section.getKeys(false)) {
if (MMOCore.plugin.professionManager.has(key)) { if (MMOCore.plugin.professionManager.has(key)) {
JsonObject object = new JsonObject(); JsonObject object = new JsonObject();
object.addProperty("exp", section.getDouble(key + ".exp")); object.addProperty("exp", section.getDouble(key + ".exp"));
object.addProperty("level", section.getInt(key + ".level")); object.addProperty("level", section.getInt(key + ".level"));
collectionSkillsJson.add(key, object); collectionSkillsJson.add(key, object);
}
} }
}
//Creates the questJson //Creates the questJson