YAML backup if sql fails

This commit is contained in:
Jules 2023-01-22 23:49:52 +01:00
parent 47f7517d48
commit f59af50463
2 changed files with 50 additions and 19 deletions

View File

@ -38,7 +38,7 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
public void saveData(PlayerData data, boolean logout) { public void saveData(PlayerData data, boolean logout) {
MythicLib.debug("MMOCoreSQL", "Saving data for: '" + data.getUniqueId() + "'..."); MythicLib.debug("MMOCoreSQL", "Saving data for: '" + data.getUniqueId() + "'...");
final PlayerDataTableUpdater updater = new PlayerDataTableUpdater(provider, data.getUniqueId()); final PlayerDataTableUpdater updater = new PlayerDataTableUpdater(provider, data);
updater.addData("class_points", data.getClassPoints()); updater.addData("class_points", data.getClassPoints());
updater.addData("skill_points", data.getSkillPoints()); updater.addData("skill_points", data.getSkillPoints());
updater.addData("skill_reallocation_points", data.getSkillReallocationPoints()); updater.addData("skill_reallocation_points", data.getSkillReallocationPoints());
@ -70,7 +70,8 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
updater.addData("class_info", createClassInfoData(data).toString()); updater.addData("class_info", createClassInfoData(data).toString());
if (logout) if (logout)
updater.addData("is_saved", 1); updater.addData("is_saved", 1);
updater.executeRequest();
updater.executeRequest(logout);
MythicLib.debug("MMOCoreSQL", "Saved data for: " + data.getUniqueId()); MythicLib.debug("MMOCoreSQL", "Saved data for: " + data.getUniqueId());
MythicLib.debug("MMOCoreSQL", String.format("{ class: %s, level: %d }", data.getProfess().getId(), data.getLevel())); MythicLib.debug("MMOCoreSQL", String.format("{ class: %s, level: %d }", data.getProfess().getId(), data.getLevel()));
@ -132,7 +133,7 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
public MySQLOfflinePlayerData(UUID uuid) { public MySQLOfflinePlayerData(UUID uuid) {
super(uuid); super(uuid);
/*
provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + uuid + "';", (result) -> { provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + uuid + "';", (result) -> {
try { try {
MythicLib.debug("MMOCoreSQL", "Loading OFFLINE data for '" + uuid + "'."); MythicLib.debug("MMOCoreSQL", "Loading OFFLINE data for '" + uuid + "'.");
@ -154,13 +155,14 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
}); }); */
} }
@Override @Override
public void removeFriend(UUID uuid) { public void removeFriend(UUID uuid) {
friends.remove(uuid); // TODO recode
new PlayerDataTableUpdater(provider, uuid).updateData("friends", friends.stream().map(UUID::toString).collect(Collectors.toList())); // friends.remove(uuid);
// new PlayerDataTableUpdater(provider, uuid).updateData("friends", friends.stream().map(UUID::toString).collect(Collectors.toList()));
} }
@Override @Override

View File

@ -2,32 +2,61 @@ package net.Indyuce.mmocore.manager.data.mysql;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.manager.data.yaml.YAMLPlayerDataManager;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.*; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
public class PlayerDataTableUpdater { public class PlayerDataTableUpdater {
private final UUID uuid; private final PlayerData playerData;
private final MySQLDataProvider provider; private final MySQLDataProvider provider;
private final Map<String, String> requestMap = new HashMap<>(); private final Map<String, String> requestMap = new HashMap<>();
public PlayerDataTableUpdater(MySQLDataProvider provider, UUID uuid) { public PlayerDataTableUpdater(MySQLDataProvider provider, PlayerData playerData) {
this.uuid = uuid; this.playerData = playerData;
this.provider = provider; this.provider = provider;
} }
public void updateData(String key, Object value) { public void executeRequest(boolean logout) {
addData(key, value);
executeRequest();
requestMap.clear();
}
public void executeRequest() {
final String request = "INSERT INTO mmocore_playerdata(uuid, " + formatCollection(requestMap.keySet(), false) final String request = "INSERT INTO mmocore_playerdata(uuid, " + formatCollection(requestMap.keySet(), false)
+ ") VALUES('" + uuid + "'," + formatCollection(requestMap.values(), true) + ")" + + ") VALUES('" + playerData.getUniqueId() + "'," + formatCollection(requestMap.values(), true) + ")" +
" ON DUPLICATE KEY UPDATE " + formatMap() + ";"; " ON DUPLICATE KEY UPDATE " + formatMap() + ";";
provider.executeUpdate(request);
try {
final Connection connection = provider.getConnection();
try {
final PreparedStatement statement = connection.prepareStatement(request);
try {
statement.executeUpdate();
} catch (SQLException exception) {
MMOCore.log(Level.WARNING, "Could not save player data of " + playerData.getUniqueId() + ", saving through YAML instead");
new YAMLPlayerDataManager(provider).saveData(playerData, logout);
exception.printStackTrace();
} finally {
statement.close();
}
} catch (SQLException exception) {
MMOCore.log(Level.WARNING, "Could not save player data of " + playerData.getUniqueId() + ", saving through YAML instead");
new YAMLPlayerDataManager(provider).saveData(playerData, logout);
exception.printStackTrace();
} finally {
connection.close();
}
} catch (SQLException exception) {
MMOCore.log(Level.WARNING, "Could not save player data of " + playerData.getUniqueId() + ", saving through YAML instead");
new YAMLPlayerDataManager(provider).saveData(playerData, logout);
exception.printStackTrace();
}
} }
public void addData(@NotNull String key, @Nullable Object value) { public void addData(@NotNull String key, @Nullable Object value) {