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,33 +318,55 @@ public abstract class Database {
* @param price Price (buyprice or sellprice, depends on {@code type}) * @param price Price (buyprice or sellprice, depends on {@code type})
* @param type Whether the player bought or sold something * @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) {
PreparedStatement ps = null;
try { Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
ps = connection.prepareStatement("INSERT INTO shop_log (timestamp,executor,product,vendor,world,x,y,z,price,type) VALUES(?,?,?,?,?,?,?,?,?,?)"); @Override
public void run() {
ps.setString(1, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime())); PreparedStatement ps = null;
ps.setString(2, executor.getUniqueId().toString() + " (" + executor.getName() + ")"); boolean debugLogEnabled = plugin.getShopChestConfig().enable_debug_log;
ps.setString(3, product.getAmount() + " x " + LanguageUtils.getItemName(product));
ps.setString(4, vendor.getUniqueId().toString() + " (" + vendor.getName() + ")" + (shopType == ShopType.ADMIN ? " (ADMIN)" : ""));
ps.setString(5, location.getWorld().getName());
ps.setInt(6, location.getBlockX());
ps.setInt(7, location.getBlockY());
ps.setInt(8, location.getBlockZ());
ps.setDouble(9, price);
ps.setString(10, type.toString());
plugin.debug("Logged economy transaction to database"); try {
ps = connection.prepareStatement("INSERT INTO shop_log (timestamp,executor,product,vendor,world,x,y,z,price,type) VALUES(?,?,?,?,?,?,?,?,?,?)");
ps.executeUpdate(); ps.setString(1, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
} catch (SQLException ex) { ps.setString(2, executor.getUniqueId().toString() + " (" + executor.getName() + ")");
plugin.getLogger().severe("Failed to access database"); ps.setString(3, product.getAmount() + " x " + LanguageUtils.getItemName(product));
plugin.debug("Failed to log economy transaction to database"); ps.setString(4, vendor.getUniqueId().toString() + " (" + vendor.getName() + ")" + (shopType == ShopType.ADMIN ? " (ADMIN)" : ""));
plugin.debug(ex); ps.setString(5, location.getWorld().getName());
} finally { ps.setInt(6, location.getBlockX());
close(ps, null); ps.setInt(7, location.getBlockY());
} ps.setInt(8, location.getBlockZ());
ps.setDouble(9, price);
ps.setString(10, type.toString());
ps.executeUpdate();
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);
}
}
});
} }
/** /**