diff --git a/src/main/java/net/epconsortium/cryptomarket/database/dao/InvestorDao.java b/src/main/java/net/epconsortium/cryptomarket/database/dao/InvestorDao.java index 49f0416..30f99f6 100644 --- a/src/main/java/net/epconsortium/cryptomarket/database/dao/InvestorDao.java +++ b/src/main/java/net/epconsortium/cryptomarket/database/dao/InvestorDao.java @@ -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())); } @@ -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 { /**