1
0
mirror of https://e.coding.net/circlecloud/QuickShop.git synced 2024-11-22 01:58:54 +00:00

异步保存商店到数据库

由于handleChat已切回主线程,这里需要异步调用
This commit is contained in:
17jiong 2018-01-29 01:06:12 +08:00
parent 1a624a8384
commit 4f3ee83b47

View File

@ -97,27 +97,38 @@ public class ShopManager {
public void createShop(final Shop shop) { public void createShop(final Shop shop) {
final Location loc = shop.getLocation(); final Location loc = shop.getLocation();
final ItemStack item = shop.getItem(); final ItemStack item = shop.getItem();
final String serializeItem = Util.serialize(item);
final String worldName = loc.getWorld().getName();
final int x = loc.getBlockX();
final int y = loc.getBlockY();
final int z = loc.getBlockZ();
// Async database execute
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
public void run() {
try { try {
// Write it to the database // Write it to the database
final String q = "INSERT INTO shops (owner, price, itemConfig, x, y, z, world, unlimited, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; final String q = "INSERT INTO shops (owner, price, itemConfig, x, y, z, world, unlimited, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
plugin.getDB().execute(q, plugin.getDB().execute(q,
shop.getOwner(), shop.getOwner(),
shop.getPrice(), shop.getPrice(),
Util.serialize(item), serializeItem,
loc.getBlockX(), x,
loc.getBlockY(), y,
loc.getBlockZ(), y,
loc.getWorld().getName(), worldName,
(shop.isUnlimited() ? 1 : 0), (shop.isUnlimited() ? 1 : 0),
shop.getShopType().toID()); shop.getShopType().toID());
// Add it to the world
addShop(loc.getWorld().getName(), shop);
} catch (final Exception e) { } catch (final Exception e) {
plugin.getLogger().warning("无法保存商店到数据库! 下次重启商店将会消失!"); plugin.getLogger().warning("无法保存商店到数据库! 下次重启商店将会消失!");
plugin.getLogger().warning("错误信息: " + e.getMessage()); plugin.getLogger().warning("错误信息: " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
} }
} }
});
// Add it to the world
addShop(worldName, shop);
}
public String format(final double d) { public String format(final double d) {
return plugin.getEcon().format(d); return plugin.getEcon().format(d);