From 405ae278b79b7d49d2e1ea3fa73d8b3375094e3b Mon Sep 17 00:00:00 2001 From: Kiran Hart Date: Thu, 26 Jul 2018 12:27:57 -0400 Subject: [PATCH] Offline Player fixes with vault to ensure money removal and addition + Checks --- .../java/com/shadebyte/auctionhouse/Core.java | 21 +++++++++++++++---- .../auctionhouse/util/storage/MySQL.java | 8 ++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/shadebyte/auctionhouse/Core.java b/src/main/java/com/shadebyte/auctionhouse/Core.java index 4d50b85..986db3c 100644 --- a/src/main/java/com/shadebyte/auctionhouse/Core.java +++ b/src/main/java/com/shadebyte/auctionhouse/Core.java @@ -129,17 +129,26 @@ public final class Core extends JavaPlugin { return; } - Class.forName("com.mysql.jdbc.Driver"); - setConnection(DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.username, this.password)); + connect(); Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', "&aSuccessfully Connected to MySQL")); dbConnected = true; } } catch (SQLException e) { Debugger.report(e); Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', "&cCould not connect to MySQL")); + } + } + + public void connect() { + try { + Class.forName("com.mysql.jdbc.Driver"); + setConnection(DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.username, this.password)); + } catch (SQLException e) { + Debugger.report(e); } catch (ClassNotFoundException e) { Debugger.report(e); Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', "&cCould not connect to MySQL")); + } } @@ -209,6 +218,8 @@ public final class Core extends JavaPlugin { data.getConfig().set("expired." + auctionItem.getOwner() + "." + auctionItem.getKey() + ".display", AuctionAPI.getInstance().expiredAuctionItem(auctionItem)); } else { highestBidder.sendMessage(Core.getInstance().getSettings().getPrefix() + Core.getInstance().getLocale().getMessage(Lang.AUCTION_BUY.getNode()).replace("{itemname}", auctionItem.getDisplayName()).replace("{price}", AuctionAPI.getInstance().friendlyNumber(auctionItem.getCurrentPrice()))); + getEconomy().withdrawPlayer(highestBidder, auctionItem.getCurrentPrice()); + getEconomy().depositPlayer(Bukkit.getOfflinePlayer(UUID.fromString(auctionItem.getOwner())), auctionItem.getCurrentPrice()); if (AuctionAPI.getInstance().availableSlots(highestBidder.getInventory()) < 1) highestBidder.getWorld().dropItemNaturally(highestBidder.getLocation(), auctionItem.getItem()); else @@ -220,13 +231,15 @@ public final class Core extends JavaPlugin { data.saveConfig(); auctionItems.remove(auctionItem); } else { - if (getEconomy().getBalance(highestBidder) < auctionItem.getCurrentPrice()) { + if (getEconomy().getBalance(Bukkit.getOfflinePlayer(UUID.fromString(auctionItem.getHighestBidder()))) < auctionItem.getCurrentPrice()) { data.getConfig().set("expired." + auctionItem.getOwner() + "." + auctionItem.getKey() + ".item", auctionItem.getItem()); data.getConfig().set("expired." + auctionItem.getOwner() + "." + auctionItem.getKey() + ".display", AuctionAPI.getInstance().expiredAuctionItem(auctionItem)); } else { data.getConfig().set("expired." + auctionItem.getHighestBidder() + "." + auctionItem.getKey() + ".item", auctionItem.getItem()); data.getConfig().set("expired." + auctionItem.getHighestBidder() + "." + auctionItem.getKey() + ".display", AuctionAPI.getInstance().expiredAuctionItem(auctionItem)); - Transaction transaction = new Transaction(Transaction.TransactionType.AUCTION_WON, auctionItem, highestBidder.getUniqueId().toString(), System.currentTimeMillis()); + getEconomy().withdrawPlayer(Bukkit.getOfflinePlayer(UUID.fromString(auctionItem.getHighestBidder())), auctionItem.getCurrentPrice()); + getEconomy().depositPlayer(Bukkit.getOfflinePlayer(UUID.fromString(auctionItem.getOwner())), auctionItem.getCurrentPrice()); + Transaction transaction = new Transaction(Transaction.TransactionType.AUCTION_WON, auctionItem, auctionItem.getHighestBidder(), System.currentTimeMillis()); transaction.saveTransaction(); getServer().getPluginManager().callEvent(new TransactionCompleteEvent(transaction)); } diff --git a/src/main/java/com/shadebyte/auctionhouse/util/storage/MySQL.java b/src/main/java/com/shadebyte/auctionhouse/util/storage/MySQL.java index 552f465..238b6e2 100644 --- a/src/main/java/com/shadebyte/auctionhouse/util/storage/MySQL.java +++ b/src/main/java/com/shadebyte/auctionhouse/util/storage/MySQL.java @@ -3,6 +3,7 @@ package com.shadebyte.auctionhouse.util.storage; import com.shadebyte.auctionhouse.Core; import com.shadebyte.auctionhouse.api.AuctionAPI; import com.shadebyte.auctionhouse.auction.Transaction; +import com.shadebyte.auctionhouse.util.Debugger; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -19,6 +20,10 @@ public class MySQL { public void logTransaction(Transaction transaction) { Bukkit.getServer().getScheduler().runTaskAsynchronously(Core.getInstance(), () -> { try { + if (Core.getInstance().getConnection().isClosed() || !Core.getInstance().dbConnected || Core.getInstance().getConnection() == null) { + Core.getInstance().connect(); + Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', "&cDatabase connection is close, attempting re-connect")); + } PreparedStatement insert = Core.getInstance().getConnection().prepareStatement("INSERT INTO transactions (buyer, seller, auctiontype, startprice, buynowprice, increment, item, displayname, lore, enchantments, auctionid, timesold, finalprice) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"); insert.setString(1, transaction.getBuyer()); insert.setString(2, transaction.getAuctionItem().getOwner()); @@ -38,7 +43,8 @@ public class MySQL { insert.executeUpdate(); Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', "&aRecorded transaction id: &b" + transaction.getAuctionItem().getKey() + "&a to database.")); } catch (Exception e) { - e.printStackTrace(); + Debugger.report(e); + Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', "&cCould not save the transaction to the database, saved to transactions.yml")); } }); }