Make logEconomy asynchronous (#21)

This commit is contained in:
MineTheCube 2016-08-18 16:59:57 +02:00 committed by Eric
parent ff7248aba4
commit e94463ccbe

View File

@ -318,8 +318,14 @@ public abstract class Database {
* @param price Price (buyprice or sellprice, depends on {@code type})
* @param type Whether the player bought or sold something
*/
public void logEconomy(Player executor, ItemStack product, OfflinePlayer vendor, ShopType shopType, Location location, double price, ShopBuySellEvent.Type type) {
public void logEconomy(final Player executor, final ItemStack product, final OfflinePlayer vendor, final ShopType shopType, final Location location, final double price, final ShopBuySellEvent.Type type) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
PreparedStatement ps = null;
boolean debugLogEnabled = plugin.getShopChestConfig().enable_debug_log;
try {
ps = connection.prepareStatement("INSERT INTO shop_log (timestamp,executor,product,vendor,world,x,y,z,price,type) VALUES(?,?,?,?,?,?,?,?,?,?)");
@ -335,17 +341,33 @@ public abstract class Database {
ps.setDouble(9, price);
ps.setString(10, type.toString());
plugin.debug("Logged economy transaction to database");
ps.executeUpdate();
} catch (SQLException ex) {
if (debugLogEnabled) {
Bukkit.getScheduler().runTask(plugin, new Runnable() {
@Override
public void run() {
plugin.debug("Logged economy transaction to database");
}
});
}
} catch (final SQLException ex) {
plugin.getLogger().severe("Failed to access database");
if (debugLogEnabled) {
Bukkit.getScheduler().runTask(plugin, new Runnable() {
@Override
public void run() {
plugin.debug("Failed to log economy transaction to database");
plugin.debug(ex);
}
});
}
} finally {
close(ps, null);
}
}
});
}
/**
* Closes a {@link PreparedStatement} and a {@link ResultSet}