Offline Player fixes with vault to ensure money removal and addition + Checks

This commit is contained in:
Kiran Hart 2018-07-26 12:27:57 -04:00
parent 53cfee31eb
commit 405ae278b7
2 changed files with 24 additions and 5 deletions

View File

@ -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));
}

View File

@ -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"));
}
});
}