Skip to content

Commit

Permalink
fix: investor data lost on disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
RoinujNosde committed Nov 21, 2022
1 parent a4416ab commit 1c1d533
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ private void insert(@NotNull Investor investor) {
}

public void unloadInvestor(@NotNull final OfflinePlayer player) {
Investor investor = getInvestor(player);
if (investor == null) { //not loaded
return;
}
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> save(investor));
ONLINE_INVESTORS.removeIf(i -> i.getUniqueId().equals(player.getUniqueId()));
}

Expand Down Expand Up @@ -168,6 +173,22 @@ public void saveAll() {
}
}

public void save(Investor investor) {
OfflinePlayer player = investor.getPlayer();
debug(String.format("Saving %s's data", player.getName()));

try (Connection connection = new ConnectionFactory(plugin).getConnection()) {
try (PreparedStatement ps = connection.prepareStatement("UPDATE investors SET balances=? WHERE uuid=?;")) {
ps.setString(1, gson.toJson(investor.getBalances(), BALANCES_TYPE));
ps.setString(2, player.getUniqueId().toString());
ps.executeUpdate();
}
} catch (SQLException ex) {
CryptoMarket.warn(String.format("Error saving investor (%s %s) data!", player.getName(), player.getUniqueId()));
ex.printStackTrace();
}
}

public interface DatabaseConfigurationCallback {

/**
Expand Down

0 comments on commit 1c1d533

Please sign in to comment.