From f59af504634eeae7f640da93dfa3173e4eb2fc38 Mon Sep 17 00:00:00 2001 From: Jules Date: Sun, 22 Jan 2023 23:49:52 +0100 Subject: [PATCH] YAML backup if sql fails --- .../data/mysql/MySQLPlayerDataManager.java | 14 +++-- .../data/mysql/PlayerDataTableUpdater.java | 55 ++++++++++++++----- 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/data/mysql/MySQLPlayerDataManager.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/data/mysql/MySQLPlayerDataManager.java index 99434d6b..5f81e45c 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/data/mysql/MySQLPlayerDataManager.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/data/mysql/MySQLPlayerDataManager.java @@ -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 diff --git a/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/data/mysql/PlayerDataTableUpdater.java b/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/data/mysql/PlayerDataTableUpdater.java index 63d96617..5e11bc8f 100644 --- a/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/data/mysql/PlayerDataTableUpdater.java +++ b/MMOCore-API/src/main/java/net/Indyuce/mmocore/manager/data/mysql/PlayerDataTableUpdater.java @@ -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 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) {