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) {
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("skill_points", data.getSkillPoints());
updater.addData("skill_reallocation_points", data.getSkillReallocationPoints());
@ -70,7 +70,8 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
updater.addData("class_info", createClassInfoData(data).toString());
if (logout)
updater.addData("is_saved", 1);
updater.executeRequest();
updater.executeRequest(logout);
MythicLib.debug("MMOCoreSQL", "Saved data for: " + data.getUniqueId());
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) {
super(uuid);
/*
provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + uuid + "';", (result) -> {
try {
MythicLib.debug("MMOCoreSQL", "Loading OFFLINE data for '" + uuid + "'.");
@ -154,13 +155,14 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
} catch (SQLException e) {
e.printStackTrace();
}
});
}); */
}
@Override
public void removeFriend(UUID uuid) {
friends.remove(uuid);
new PlayerDataTableUpdater(provider, uuid).updateData("friends", friends.stream().map(UUID::toString).collect(Collectors.toList()));
// TODO recode
// friends.remove(uuid);
// new PlayerDataTableUpdater(provider, uuid).updateData("friends", friends.stream().map(UUID::toString).collect(Collectors.toList()));
}
@Override

View File

@ -2,32 +2,61 @@ package net.Indyuce.mmocore.manager.data.mysql;
import com.google.gson.JsonArray;
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.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 {
private final UUID uuid;
private final PlayerData playerData;
private final MySQLDataProvider provider;
private final Map<String, String> requestMap = new HashMap<>();
public PlayerDataTableUpdater(MySQLDataProvider provider, UUID uuid) {
this.uuid = uuid;
public PlayerDataTableUpdater(MySQLDataProvider provider, PlayerData playerData) {
this.playerData = playerData;
this.provider = provider;
}
public void updateData(String key, Object value) {
addData(key, value);
executeRequest();
requestMap.clear();
}
public void executeRequest() {
public void executeRequest(boolean logout) {
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() + ";";
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) {