diff --git a/src/main/java/com/kiranhart/auctionhouse/Core.java b/src/main/java/com/kiranhart/auctionhouse/Core.java index 8fed2fa..e81d302 100644 --- a/src/main/java/com/kiranhart/auctionhouse/Core.java +++ b/src/main/java/com/kiranhart/auctionhouse/Core.java @@ -4,12 +4,15 @@ import com.kiranhart.auctionhouse.api.statics.AuctionSettings; import com.kiranhart.auctionhouse.api.version.ServerVersion; import com.kiranhart.auctionhouse.auction.AuctionItem; import com.kiranhart.auctionhouse.cmds.CommandManager; +import com.kiranhart.auctionhouse.util.economy.Economy; +import com.kiranhart.auctionhouse.util.economy.VaultEconomy; import com.kiranhart.auctionhouse.util.locale.Locale; import com.kiranhart.auctionhouse.util.storage.ConfigWrapper; import org.apache.commons.lang.ArrayUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; import java.io.File; @@ -24,6 +27,7 @@ public final class Core extends JavaPlugin { private CommandManager commandManager; private Locale locale; + private Economy economy; private AuctionSettings auctionSettings; private CopyOnWriteArrayList auctionItems; @@ -35,6 +39,7 @@ public final class Core extends JavaPlugin { public void onEnable() { long start = System.currentTimeMillis(); + PluginManager pm = Bukkit.getPluginManager(); console.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6=========================================")); console.sendMessage(ChatColor.translateAlternateColorCodes('&', "&bLoading Auction House 1.10 - Multiversion")); @@ -62,6 +67,11 @@ public final class Core extends JavaPlugin { new Locale(this, "en_US"); this.locale = Locale.getLocale(getConfig().getString("lang")); + //Economy + if (pm.isPluginEnabled("Vault")) { + this.economy = new VaultEconomy(); + } + console.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8[&6AuctionHouse&8]&a Loading data files")); initDataFiles(); @@ -128,6 +138,10 @@ public final class Core extends JavaPlugin { return commandManager; } + public Economy getEconomy() { + return economy; + } + public ServerVersion getServerVersion() { return serverVersion; } diff --git a/src/main/java/com/kiranhart/auctionhouse/inventory/inventories/AuctionGUI.java b/src/main/java/com/kiranhart/auctionhouse/inventory/inventories/AuctionGUI.java index 881efd1..37ce748 100644 --- a/src/main/java/com/kiranhart/auctionhouse/inventory/inventories/AuctionGUI.java +++ b/src/main/java/com/kiranhart/auctionhouse/inventory/inventories/AuctionGUI.java @@ -9,12 +9,14 @@ package com.kiranhart.auctionhouse.inventory.inventories; import com.google.common.collect.Lists; import com.kiranhart.auctionhouse.Core; +import com.kiranhart.auctionhouse.api.statics.AuctionLang; import com.kiranhart.auctionhouse.api.statics.AuctionSettings; import com.kiranhart.auctionhouse.api.version.NBTEditor; import com.kiranhart.auctionhouse.api.version.XMaterial; import com.kiranhart.auctionhouse.auction.AuctionItem; import com.kiranhart.auctionhouse.inventory.AGUI; import com.kiranhart.auctionhouse.util.Debugger; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.InventoryClickEvent; @@ -86,13 +88,9 @@ public class AuctionGUI implements AGUI { AuctionItem possibleAuctionItem = null; /* - Perform the proper steps if the user left-clicks (not using bid system) + Perform the proper steps if the user left-clicks (using bid system) */ - if (e.getClick() == ClickType.LEFT) { - //Check if the bid system is set to false - if (!AuctionSettings.USE_BIDDING_SYSTEM) { - return; - } + if (e.getClick() == ClickType.LEFT && AuctionSettings.USE_BIDDING_SYSTEM) { //Get the key of the auction item String auctionItemKey = NBTEditor.getString(clicked, "AuctionItemKey"); @@ -100,10 +98,80 @@ public class AuctionGUI implements AGUI { if (auctionItem.getKey().equalsIgnoreCase(auctionItemKey)) possibleAuctionItem = auctionItem; } + //Check if player has enough money to bid + if (Core.getInstance().getEconomy().hasBalance(p, possibleAuctionItem.getCurrentPrice() + possibleAuctionItem.getBidIncrement())) { + //Check if the person who clicked is the owner + if (possibleAuctionItem.getOwner().equals(p.getUniqueId())) { + //can the owner bid on their own item? + if (AuctionSettings.OWNER_CAN_BID_ON_OWN) { + //Update the price + possibleAuctionItem.setCurrentPrice(possibleAuctionItem.getCurrentPrice() + possibleAuctionItem.getBidIncrement()); + //Alert the previous bidder someone has a higher bid than them + if (!possibleAuctionItem.getHighestBidder().equals(p.getUniqueId())) { + Core.getInstance().getLocale().getMessage(AuctionLang.OUT_BIDDED).processPlaceholder("player", p.getName()).sendPrefixedMessage(Bukkit.getOfflinePlayer(possibleAuctionItem.getHighestBidder()).getPlayer()); + } + //Set the highest bidder + possibleAuctionItem.setHighestBidder(p.getUniqueId()); + } else { + //Owner cannot bid on own item. + Core.getInstance().getLocale().getMessage(AuctionLang.CANT_BID_ON_OWN).sendPrefixedMessage(p); + } + } else { + //Clicked user is not the original owner + //Update the price + possibleAuctionItem.setCurrentPrice(possibleAuctionItem.getCurrentPrice() + possibleAuctionItem.getBidIncrement()); + //Alert the previous bidder someone has a higher bid than them + if (!possibleAuctionItem.getHighestBidder().equals(p.getUniqueId())) { + Core.getInstance().getLocale().getMessage(AuctionLang.OUT_BIDDED).processPlaceholder("player", p.getName()).sendPrefixedMessage(Bukkit.getOfflinePlayer(possibleAuctionItem.getHighestBidder()).getPlayer()); + } + //Set the highest bidder + possibleAuctionItem.setHighestBidder(p.getUniqueId()); + } + //Increase time on bid? + if (AuctionSettings.INCREASE_AUCTION_TIME_ON_BID) { + possibleAuctionItem.setTime(possibleAuctionItem.getTime() + AuctionSettings.TIME_TO_INCREASE_BY_BID); + } + } else { + //Not enough money to bid + } + + p.closeInventory(); + p.openInventory(new AuctionGUI(p).getInventory()); return; } + + /* + Perform the proper steps if the user right-clicks (using bid system) + */ + if (e.getClick() == ClickType.RIGHT && AuctionSettings.USE_BIDDING_SYSTEM) { + + //Get the key of the auction item + String auctionItemKey = NBTEditor.getString(clicked, "AuctionItemKey"); + for (AuctionItem auctionItem : Core.getInstance().getAuctionItems()) { + if (auctionItem.getKey().equalsIgnoreCase(auctionItemKey)) possibleAuctionItem = auctionItem; + } + + if (Core.getInstance().getEconomy().hasBalance(p, possibleAuctionItem.getBuyNowPrice())) { + if (AuctionSettings.OWNER_CAN_PURCHASE_OWN) { + p.closeInventory(); + p.openInventory(new ConfirmationGUI().getInventory()); + } else { + Core.getInstance().getLocale().getMessage(AuctionLang.CANT_BUY_OWN).sendPrefixedMessage(p); + } + } else { + //Not enough money to purchase + } + + p.closeInventory(); + p.openInventory(new AuctionGUI(p).getInventory()); + return; + } + + /* + Perform the proper steps if the user left clicks (Without the bid system) + */ } @Override diff --git a/src/main/java/com/kiranhart/auctionhouse/inventory/inventories/ConfirmationGUI.java b/src/main/java/com/kiranhart/auctionhouse/inventory/inventories/ConfirmationGUI.java index 91b83eb..cb43910 100644 --- a/src/main/java/com/kiranhart/auctionhouse/inventory/inventories/ConfirmationGUI.java +++ b/src/main/java/com/kiranhart/auctionhouse/inventory/inventories/ConfirmationGUI.java @@ -7,5 +7,26 @@ package com.kiranhart.auctionhouse.inventory.inventories; Code within this class is not to be redistributed without proper permission. */ -public class ConfirmationGUI { +import com.kiranhart.auctionhouse.inventory.AGUI; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +public class ConfirmationGUI implements AGUI { + + @Override + public void click(InventoryClickEvent e, ItemStack clicked, int slot) { + + } + + @Override + public void close(InventoryCloseEvent e) { + + } + + @Override + public Inventory getInventory() { + return null; + } } diff --git a/src/main/java/com/kiranhart/auctionhouse/util/economy/VaultEconomy.java b/src/main/java/com/kiranhart/auctionhouse/util/economy/VaultEconomy.java index 58e1d49..015e8f0 100644 --- a/src/main/java/com/kiranhart/auctionhouse/util/economy/VaultEconomy.java +++ b/src/main/java/com/kiranhart/auctionhouse/util/economy/VaultEconomy.java @@ -7,5 +7,29 @@ package com.kiranhart.auctionhouse.util.economy; Code within this class is not to be redistributed without proper permission. */ -public class VaultEconomy { +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; + +public class VaultEconomy implements Economy { + + private net.milkbowl.vault.economy.Economy vault; + + public VaultEconomy() { + this.vault = Bukkit.getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class).getProvider(); + } + + @Override + public boolean hasBalance(OfflinePlayer p, double cost) { + return vault.has(p, cost); + } + + @Override + public boolean withdrawBalance(OfflinePlayer p, double cost) { + return vault.withdrawPlayer(p, cost).transactionSuccess(); + } + + @Override + public boolean deposit(OfflinePlayer p, double amount) { + return vault.depositPlayer(p, amount).transactionSuccess(); + } }