Finished the confirmation gui and added the auction tick task

This commit is contained in:
Kiran Hart 2019-08-09 16:51:09 -04:00
parent b96f2e15f6
commit 7fd314efc9
8 changed files with 298 additions and 10 deletions

View File

@ -11,6 +11,7 @@ import com.kiranhart.auctionhouse.util.economy.VaultEconomy;
import com.kiranhart.auctionhouse.util.locale.Locale;
import com.kiranhart.auctionhouse.util.storage.ConfigWrapper;
import com.kiranhart.auctionhouse.util.tasks.LoadAuctionsTask;
import com.kiranhart.auctionhouse.util.tasks.TickAuctionsTask;
import org.apache.commons.lang.ArrayUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -26,6 +27,7 @@ public final class Core extends JavaPlugin {
private static Core instance;
private ConsoleCommandSender console = Bukkit.getConsoleSender();
private PluginManager pm;
private ServerVersion serverVersion;
private CommandManager commandManager;
@ -42,7 +44,7 @@ public final class Core extends JavaPlugin {
public void onEnable() {
long start = System.currentTimeMillis();
PluginManager pm = Bukkit.getPluginManager();
pm = Bukkit.getPluginManager();
console.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6========================================="));
console.sendMessage(ChatColor.translateAlternateColorCodes('&', "&bLoading Auction House 1.10 - Multiversion"));
@ -98,6 +100,9 @@ public final class Core extends JavaPlugin {
console.sendMessage(ChatColor.translateAlternateColorCodes('&', " "));
console.sendMessage(ChatColor.translateAlternateColorCodes('&', "&aLoaded successfully in " + (System.currentTimeMillis() - start) + "ms"));
console.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6========================================="));
//Begin Auction Tick
TickAuctionsTask.startTask(this);
}
@Override
@ -143,6 +148,10 @@ public final class Core extends JavaPlugin {
return instance;
}
public PluginManager getPm() {
return pm;
}
public Locale getLocale() {
return locale;
}

View File

@ -8,12 +8,15 @@ package com.kiranhart.auctionhouse.api;
*/
import com.kiranhart.auctionhouse.Core;
import com.kiranhart.auctionhouse.api.version.NBTEditor;
import com.kiranhart.auctionhouse.api.version.ServerVersion;
import com.kiranhart.auctionhouse.api.version.XMaterial;
import com.kiranhart.auctionhouse.auction.AuctionItem;
import com.kiranhart.auctionhouse.util.Debugger;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import java.text.DecimalFormat;
@ -144,4 +147,28 @@ public class AuctionAPI {
}
return items;
}
/*
* @param stack is the auction item to be converted
* @return the new expired item.
*/
public ItemStack expiredAuctionItem(AuctionItem stack) {
ItemStack item = stack.getItem();
item = NBTEditor.set(item, stack.getKey(), "ExpiredAuctionItem");
return item;
}
/**
* @param inventory, the inventory that wil be slot checked
* @return if there is an available slot for the item
*/
public int availableSlots(PlayerInventory inventory) {
int count = 0;
for (ItemStack i : inventory) {
if (i == null) {
count++;
}
}
return count;
}
}

View File

@ -28,7 +28,8 @@ public class AuctionSettings {
public static boolean USE_SHORT_NUMBERS_ON_ITEMS = false;
public static int TIME_TO_INCREASE_BY_BID = 10;
public static int DECREASE_SECONDS_BY_TICK = 5;
public static int UPDATE_EVERY_TICK = 5;
@ -49,5 +50,7 @@ public class AuctionSettings {
USE_SHORT_NUMBERS_ON_ITEMS = Core.getInstance().getConfig().getBoolean("settings.use-short-numbers-on-items");
TIME_TO_INCREASE_BY_BID = Core.getInstance().getConfig().getInt("settings.time-to-increase-by-bid");
DECREASE_SECONDS_BY_TICK = Core.getInstance().getConfig().getInt("settings.decrease-seconds-by-tick");
UPDATE_EVERY_TICK = Core.getInstance().getConfig().getInt("settings.update-every-tick");
}
}

View File

@ -60,7 +60,7 @@ public class AuctionGUI implements AGUI {
//Different auction inventories
//Refresh Auction GUI
if (slot == 49) {
p.openInventory(new AuctionGUI(p).getInventory());
return;
}
@ -164,7 +164,7 @@ public class AuctionGUI implements AGUI {
if (Core.getInstance().getEconomy().hasBalance(p, possibleAuctionItem.getBuyNowPrice())) {
if (AuctionSettings.OWNER_CAN_PURCHASE_OWN) {
p.closeInventory();
p.openInventory(new ConfirmationGUI().getInventory());
p.openInventory(new ConfirmationGUI(possibleAuctionItem).getInventory());
} else {
Core.getInstance().getLocale().getMessage(AuctionLang.CANT_BUY_OWN).sendPrefixedMessage(p);
}
@ -196,7 +196,7 @@ public class AuctionGUI implements AGUI {
if (Core.getInstance().getEconomy().hasBalance(p, possibleAuctionItem.getBuyNowPrice())) {
if (AuctionSettings.OWNER_CAN_PURCHASE_OWN) {
p.closeInventory();
p.openInventory(new ConfirmationGUI().getInventory());
p.openInventory(new ConfirmationGUI(possibleAuctionItem).getInventory());
} else {
Core.getInstance().getLocale().getMessage(AuctionLang.CANT_BUY_OWN).sendPrefixedMessage(p);
}

View File

@ -7,17 +7,105 @@ package com.kiranhart.auctionhouse.inventory.inventories;
Code within this class is not to be redistributed without proper permission.
*/
import com.kiranhart.auctionhouse.Core;
import com.kiranhart.auctionhouse.api.AuctionAPI;
import com.kiranhart.auctionhouse.api.events.TransactionCompleteEvent;
import com.kiranhart.auctionhouse.api.statics.AuctionLang;
import com.kiranhart.auctionhouse.auction.AuctionItem;
import com.kiranhart.auctionhouse.auction.Transaction;
import com.kiranhart.auctionhouse.inventory.AGUI;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.util.stream.IntStream;
public class ConfirmationGUI implements AGUI {
private AuctionItem auctionItem;
public ConfirmationGUI(AuctionItem auctionItem) {
this.auctionItem = auctionItem;
}
private int[] yes = {0, 1, 2, 3};
private int[] no = {5, 6, 7, 8};
@Override
public void click(InventoryClickEvent e, ItemStack clicked, int slot) {
e.setCancelled(true);
Player p = (Player) e.getWhoClicked();
//Check if the slots clicked are within range (no)
if (IntStream.of(no).anyMatch(x -> x == slot)) {
p.closeInventory();
p.openInventory(new AuctionGUI(p).getInventory());
return;
}
//Check if player clicks yes
if (IntStream.of(yes).anyMatch(x -> x == slot)) {
//Check if the auction item is hasn't been purchased yet
if (Core.getInstance().getAuctionItems().contains(auctionItem)) {
//Check if they have enough money to purchase the item
if (Core.getInstance().getEconomy().hasBalance(p, auctionItem.getBuyNowPrice())) {
//Withdraw
Core.getInstance().getEconomy().withdrawBalance(p, auctionItem.getBuyNowPrice());
Core.getInstance().getEconomy().deposit(Bukkit.getOfflinePlayer(auctionItem.getOwner()), auctionItem.getBuyNowPrice());
//Check inventory space
if (AuctionAPI.getInstance().availableSlots(p.getInventory()) == 0) {
Core.getInstance().getData().getConfig().set("expired." + p.getUniqueId().toString() + "." + auctionItem.getKey() + ".item", auctionItem.getItem());
Core.getInstance().getData().getConfig().set("expired." + p.getUniqueId().toString() + "." + auctionItem.getKey() + ".display", AuctionAPI.getInstance().expiredAuctionItem(auctionItem));
} else {
p.getInventory().addItem(auctionItem.getItem());
}
Core.getInstance().getLocale().getMessage(AuctionLang.AUCTION_BUY).processPlaceholder("itemname", auctionItem.getDisplayName()).processPlaceholder("price", AuctionAPI.getInstance().getFriendlyNumber(auctionItem.getBuyNowPrice())).sendPrefixedMessage(p);
OfflinePlayer owner = Bukkit.getOfflinePlayer(auctionItem.getOwner());
if (owner.isOnline()) {
Core.getInstance().getLocale().getMessage(AuctionLang.AUCTION_SOLD).processPlaceholder("player", p.getName()).processPlaceholder("itemname", auctionItem.getDisplayName()).processPlaceholder("price", AuctionAPI.getInstance().getFriendlyNumber(auctionItem.getBuyNowPrice())).sendPrefixedMessage(owner.getPlayer());
}
//Perform the transaction
Transaction transaction = new Transaction(Transaction.TransactionType.BOUGHT, auctionItem, p.getUniqueId(), System.currentTimeMillis());
transaction.saveTransaction();
auctionItem.setTime(0);
Core.getInstance().getAuctionItems().remove(auctionItem);
p.closeInventory();
TransactionCompleteEvent completeEvent = new TransactionCompleteEvent(transaction);
Core.getInstance().getPm().callEvent(completeEvent);
p.openInventory(new AuctionGUI(p).getInventory());
} else {
p.closeInventory();
Core.getInstance().getLocale().getMessage(AuctionLang.NOT_ENOUGH_MONEY).sendPrefixedMessage(p);
}
} else {
p.closeInventory();
p.openInventory(new AuctionGUI(p).getInventory());
}
}
}
@Override
public Inventory getInventory() {
Inventory inventory = Bukkit.createInventory(this, 9, ChatColor.translateAlternateColorCodes('&', Core.getInstance().getConfig().getString("guis.confirm.title")));
for (int i = 0; i <= 3; i++) {
inventory.setItem(i, AuctionAPI.getInstance().createConfigurationItem("guis.confirm.items.confirm", 0, 0));
}
for (int i = 5; i <= 8; i++) {
inventory.setItem(i, AuctionAPI.getInstance().createConfigurationItem("guis.confirm.items.decline", 0, 0));
}
inventory.setItem(4, auctionItem.getItem());
return inventory;
}
@Override
@ -25,8 +113,4 @@ public class ConfirmationGUI implements AGUI {
}
@Override
public Inventory getInventory() {
return null;
}
}

View File

@ -0,0 +1,161 @@
package com.kiranhart.auctionhouse.util.tasks;
/*
The current file was created by Kiran Hart
Date: August 08 2019
Time: 6:19 PM
Code within this class is not to be redistributed without proper permission.
*/
import com.google.common.collect.Lists;
import com.kiranhart.auctionhouse.Core;
import com.kiranhart.auctionhouse.api.AuctionAPI;
import com.kiranhart.auctionhouse.api.events.AuctionEndEvent;
import com.kiranhart.auctionhouse.api.events.TransactionCompleteEvent;
import com.kiranhart.auctionhouse.api.statics.AuctionLang;
import com.kiranhart.auctionhouse.api.statics.AuctionSettings;
import com.kiranhart.auctionhouse.auction.AuctionItem;
import com.kiranhart.auctionhouse.auction.AuctionPlayer;
import com.kiranhart.auctionhouse.auction.Transaction;
import com.kiranhart.auctionhouse.util.Debugger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.List;
public class TickAuctionsTask extends BukkitRunnable {
private static TickAuctionsTask instance;
private static Core plugin;
private TickAuctionsTask(Core core) {
plugin = core;
}
public static TickAuctionsTask startTask(Core core) {
plugin = core;
if (instance == null) {
instance = new TickAuctionsTask(plugin);
instance.runTaskTimerAsynchronously(plugin, 0, 20 * 5);
}
return instance;
}
@Override
public void run() {
try {
//Check if auction items is not empty
if (Core.getInstance().getAuctionItems().size() != 0) {
//Tick the auction item
Core.getInstance().getAuctionItems().forEach(auctionItem -> auctionItem.updateTime(AuctionSettings.DECREASE_SECONDS_BY_TICK));
//Loop through each of the auction items
Core.getInstance().getAuctionItems().forEach(auctionItem -> {
//Check if the auction time is equal or 0.
if (auctionItem.getTime() <= 0) {
//Call the AuctionEndEvent
AuctionEndEvent auctionEndEvent = new AuctionEndEvent(auctionItem);
Core.getInstance().getPm().callEvent(auctionEndEvent);
//Check if the auction is not cancelled
if (!auctionEndEvent.isCancelled()) {
//Check if the highest bidder is equal to the owner, if so send to expiration box
if (auctionItem.getHighestBidder().equals(auctionItem.getOwner())) {
//Save to expiration bin
Core.getInstance().getData().getConfig().set("expired." + auctionItem.getOwner().toString() + "." + auctionItem.getKey() + ".item", auctionItem.getItem());
Core.getInstance().getData().getConfig().set("expired." + auctionItem.getOwner().toString() + "." + auctionItem.getKey() + ".display", AuctionAPI.getInstance().expiredAuctionItem(auctionItem));
Core.getInstance().getData().saveConfig();
Core.getInstance().getAuctionItems().remove(auctionItem);
} else {
//Highest bidder is not the owner, perform checks
OfflinePlayer highestBidder = Bukkit.getOfflinePlayer(auctionItem.getHighestBidder());
if (highestBidder.isOnline()) {
//Highest bidder is currently online
//Check the balance (has enough money)
if (Core.getInstance().getEconomy().hasBalance(highestBidder, auctionItem.getCurrentPrice())) {
//Withdraw the money
Core.getInstance().getEconomy().withdrawBalance(highestBidder, auctionItem.getCurrentPrice());
//Give money the owner
Core.getInstance().getEconomy().deposit(Bukkit.getOfflinePlayer(auctionItem.getOwner()), auctionItem.getCurrentPrice());
//TODO Send money removal / addition messages
//Check if player has available slots
if (AuctionAPI.getInstance().availableSlots(highestBidder.getPlayer().getInventory()) == 0) {
Core.getInstance().getData().getConfig().set("expired." + auctionItem.getHighestBidder().toString() + "." + auctionItem.getKey() + ".item", auctionItem.getItem());
Core.getInstance().getData().getConfig().set("expired." + auctionItem.getHighestBidder().toString() + "." + auctionItem.getKey() + ".display", AuctionAPI.getInstance().expiredAuctionItem(auctionItem));
} else {
highestBidder.getPlayer().getInventory().addItem(auctionItem.getItem());
}
//Perform the transaction
Transaction transaction = new Transaction(Transaction.TransactionType.AUCTION_WON, auctionItem, highestBidder.getPlayer().getUniqueId(), System.currentTimeMillis());
transaction.saveTransaction();
Core.getInstance().getPm().callEvent(new TransactionCompleteEvent(transaction));
} else {
//Doesn't have enough money
Core.getInstance().getLocale().getMessage(AuctionLang.NOT_ENOUGH_MONEY).sendPrefixedMessage(highestBidder.getPlayer());
Core.getInstance().getData().getConfig().set("expired." + auctionItem.getOwner().toString() + "." + auctionItem.getKey() + ".item", auctionItem.getItem());
Core.getInstance().getData().getConfig().set("expired." + auctionItem.getOwner().toString() + "." + auctionItem.getKey() + ".display", AuctionAPI.getInstance().expiredAuctionItem(auctionItem));
}
//Remove the auction item from list, and save data file
Core.getInstance().getData().saveConfig();
Core.getInstance().getAuctionItems().remove(auctionItem);
} else {
//Highest bidder is currently offline
if (Core.getInstance().getEconomy().hasBalance(highestBidder, auctionItem.getCurrentPrice())) {
//Has enough money
Core.getInstance().getData().getConfig().set("expired." + auctionItem.getHighestBidder().toString() + "." + auctionItem.getKey() + ".item", auctionItem.getItem());
Core.getInstance().getData().getConfig().set("expired." + auctionItem.getHighestBidder().toString() + "." + auctionItem.getKey() + ".display", AuctionAPI.getInstance().expiredAuctionItem(auctionItem));
Core.getInstance().getEconomy().withdrawBalance(highestBidder, auctionItem.getCurrentPrice());
Core.getInstance().getEconomy().deposit(Bukkit.getOfflinePlayer(auctionItem.getOwner()), auctionItem.getCurrentPrice());
//Perform the transaction
Transaction transaction = new Transaction(Transaction.TransactionType.AUCTION_WON, auctionItem, auctionItem.getHighestBidder(), System.currentTimeMillis());
transaction.saveTransaction();
Core.getInstance().getPm().callEvent(new TransactionCompleteEvent(transaction));
} else {
//Doesn't have enough money
Core.getInstance().getLocale().getMessage(AuctionLang.NOT_ENOUGH_MONEY).sendPrefixedMessage(highestBidder.getPlayer());
Core.getInstance().getData().getConfig().set("expired." + auctionItem.getOwner().toString() + "." + auctionItem.getKey() + ".item", auctionItem.getItem());
Core.getInstance().getData().getConfig().set("expired." + auctionItem.getOwner().toString() + "." + auctionItem.getKey() + ".display", AuctionAPI.getInstance().expiredAuctionItem(auctionItem));
}
//Remove the auction item from list, and save data file
Core.getInstance().getData().saveConfig();
Core.getInstance().getAuctionItems().remove(auctionItem);
}
}
}
}
});
//Refresh auction page is enabled
if (AuctionSettings.AUTO_REFRESH_AUCTION_PAGES) {
Bukkit.getOnlinePlayers().forEach(p -> {
if (p.getOpenInventory().getTitle().equalsIgnoreCase(ChatColor.translateAlternateColorCodes('&', Core.getInstance().getConfig().getString("guis.auctionhouse.title")))) {
p.getOpenInventory().getTopInventory().clear();
p.getOpenInventory().getTopInventory().setItem(45, AuctionAPI.getInstance().createConfigurationItem("gui.auction.items.yourauctions", new AuctionPlayer(p).getTotalActiveAuctions(), 0));
p.getOpenInventory().getTopInventory().setItem(46, AuctionAPI.getInstance().createConfigurationItem("gui.auction.items.collectionbin", 0, new AuctionPlayer(p).getTotalExpiredAuctions()));
p.getOpenInventory().getTopInventory().setItem(48, AuctionAPI.getInstance().createConfigurationItem("gui.auction.items.previouspage", 0, 0));
p.getOpenInventory().getTopInventory().setItem(49, AuctionAPI.getInstance().createConfigurationItem("gui.auction.items.refresh", 0, 0));
p.getOpenInventory().getTopInventory().setItem(50, AuctionAPI.getInstance().createConfigurationItem("gui.auction.items.nextpage", 0, 0));
p.getOpenInventory().getTopInventory().setItem(51, AuctionAPI.getInstance().createConfigurationItem("gui.auction.items.transactions", 0, 0));
p.getOpenInventory().getTopInventory().setItem(52, AuctionAPI.getInstance().createConfigurationItem("gui.auction.items.howtosell", 0, 0));
p.getOpenInventory().getTopInventory().setItem(53, AuctionAPI.getInstance().createConfigurationItem("gui.auction.items.guide", 0, 0));
List<List<AuctionItem>> chunks = Lists.partition(Core.getInstance().getAuctionItems(), 45);
chunks.get(0).forEach(item -> p.getOpenInventory().getTopInventory().setItem(p.getOpenInventory().getTopInventory().firstEmpty(), item.getAuctionStack(AuctionItem.AuctionItemType.MAIN)));
}
});
}
}
} catch (Exception e) {
Debugger.report(e);
}
}
}

View File

@ -16,6 +16,10 @@ settings:
use-short-numbers-on-items: false
increase-time-on-bid: true
time-to-increase-by-bid: 10
decrease-seconds-by-tick: 5
update-every-tick: 5
#MySql stuff for databases if you want
database:

View File

@ -27,7 +27,7 @@ auction.listed = "&eYou listed &6%itemname% &efor &a$%price%"
auction.listedwithbid = "&eYou listed &6%itemname% &ewith a starting bid of &a$%price%"
auction.max = "&cYou cannot auction more items, please upgrade to do so."
auction.buy = "&eYou purchased &6%itemname% &Efor &A$%price%"
auction.sold = "&6%player% &Ehas bought your listed item: &6%item% &efor &a$%price%"
auction.sold = "&6%player% &Ehas bought your listed item: &6%itemname% &efor &a$%price%"
#Command Messages
cmd.invalid = "&cThat isn't a valid subcommand!"