Expired Items GUI, active and expired commands added

This commit is contained in:
Kiran Hart 2021-03-15 16:35:38 -04:00
parent a57e82a07b
commit 0c42f17534
10 changed files with 192 additions and 123 deletions

View File

@ -111,7 +111,7 @@ public class AuctionAPI {
* @param data Data to deserialize.
* @return Deserialized ItemStack.
*/
public static ItemStack deserializeItem(byte[] data) {
public ItemStack deserializeItem(byte[] data) {
ItemStack item = null;
try (BukkitObjectInputStream stream = new BukkitObjectInputStream(new ByteArrayInputStream(data))) {
item = (ItemStack) stream.readObject();
@ -128,7 +128,7 @@ public class AuctionAPI {
* @param item Item to serialize.
* @return Serialized data.
*/
public static byte[] serializeItem(ItemStack item) {
public byte[] serializeItem(ItemStack item) {
try (ByteArrayOutputStream stream = new ByteArrayOutputStream(); BukkitObjectOutputStream bukkitStream = new BukkitObjectOutputStream(stream)) {
bukkitStream.writeObject(item);
return stream.toByteArray();

View File

@ -45,7 +45,7 @@ public class AuctionItem implements Serializable {
public AuctionItem(UUID owner, UUID highestBidder, ItemStack originalItem, AuctionItemCategory category, UUID key, double basePrice, double bidStartPrice, double bidIncPrice, double currentPrice, int remainingTime, boolean expired) {
this.owner = owner;
this.highestBidder = highestBidder;
this.rawItem = AuctionAPI.serializeItem(originalItem);
this.rawItem = AuctionAPI.getInstance().serializeItem(originalItem);
this.category = category;
this.key = key;
this.basePrice = basePrice;
@ -62,7 +62,7 @@ public class AuctionItem implements Serializable {
}
public ItemStack getDisplayStack(AuctionStackType type) {
ItemStack itemStack = AuctionAPI.deserializeItem(this.rawItem).clone();
ItemStack itemStack = AuctionAPI.getInstance().deserializeItem(this.rawItem).clone();
itemStack.setAmount(Math.max(itemStack.getAmount(), 1));
ItemMeta meta = itemStack.getItemMeta();
List<String> lore = (meta.hasLore()) ? meta.getLore() : new ArrayList<>();

View File

@ -0,0 +1,50 @@
package ca.tweetzy.auctionhouse.commands;
import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.auctionhouse.guis.GUIActiveAuctions;
import ca.tweetzy.auctionhouse.guis.GUIAuctionHouse;
import ca.tweetzy.core.commands.AbstractCommand;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
/**
* The current file has been created by Kiran Hart
* Date Created: March 15 2021
* Time Created: 4:32 p.m.
* Usage of any code found within this class is prohibited unless given explicit permission otherwise
*/
public class CommandActive extends AbstractCommand {
public CommandActive() {
super(CommandType.PLAYER_ONLY, "active");
}
@Override
protected ReturnType runCommand(CommandSender sender, String... args) {
Player player = (Player) sender;
AuctionHouse.getInstance().getGuiManager().showGUI(player, new GUIActiveAuctions(AuctionHouse.getInstance().getAuctionPlayerManager().getPlayer(player.getUniqueId())));
return ReturnType.SUCCESS;
}
@Override
public String getPermissionNode() {
return "auctionhouse.cmd.active";
}
@Override
public String getSyntax() {
return "active";
}
@Override
public String getDescription() {
return "View all your auction listings";
}
@Override
protected List<String> onTab(CommandSender sender, String... args) {
return null;
}
}

View File

@ -0,0 +1,50 @@
package ca.tweetzy.auctionhouse.commands;
import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.auctionhouse.guis.GUIActiveAuctions;
import ca.tweetzy.auctionhouse.guis.GUIExpiredItems;
import ca.tweetzy.core.commands.AbstractCommand;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
/**
* The current file has been created by Kiran Hart
* Date Created: March 15 2021
* Time Created: 4:32 p.m.
* Usage of any code found within this class is prohibited unless given explicit permission otherwise
*/
public class CommandExpired extends AbstractCommand {
public CommandExpired() {
super(CommandType.PLAYER_ONLY, "expired");
}
@Override
protected ReturnType runCommand(CommandSender sender, String... args) {
Player player = (Player) sender;
AuctionHouse.getInstance().getGuiManager().showGUI(player, new GUIExpiredItems(AuctionHouse.getInstance().getAuctionPlayerManager().getPlayer(player.getUniqueId())));
return ReturnType.SUCCESS;
}
@Override
public String getPermissionNode() {
return "auctionhouse.cmd.expired";
}
@Override
public String getSyntax() {
return "expired";
}
@Override
public String getDescription() {
return "View all your expired/cancelled listings";
}
@Override
protected List<String> onTab(CommandSender sender, String... args) {
return null;
}
}

View File

@ -7,8 +7,10 @@ import ca.tweetzy.auctionhouse.helpers.MaterialCategorizer;
import ca.tweetzy.auctionhouse.helpers.PlayerHelper;
import ca.tweetzy.auctionhouse.settings.Settings;
import ca.tweetzy.core.commands.AbstractCommand;
import ca.tweetzy.core.compatibility.CompatibleHand;
import ca.tweetzy.core.compatibility.XMaterial;
import ca.tweetzy.core.utils.NumberUtils;
import ca.tweetzy.core.utils.PlayerUtils;
import org.apache.commons.lang.WordUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -106,6 +108,7 @@ public class CommandSell extends AbstractCommand {
.processPlaceholder("base_price", basePrice)
.sendPrefixedMessage(player);
PlayerUtils.takeActiveItem(player, CompatibleHand.MAIN_HAND, itemToSell.getAmount());
} else {
// they want to use the bidding system, so make it a bid item
if (!NumberUtils.isDouble(args[0])) {
@ -180,6 +183,8 @@ public class CommandSell extends AbstractCommand {
.processPlaceholder("start_price", bidStartPrice)
.processPlaceholder("increment_price", bidIncPrice)
.sendPrefixedMessage(player);
PlayerUtils.takeActiveItem(player, CompatibleHand.MAIN_HAND, itemToSell.getAmount());
}
return ReturnType.SUCCESS;

View File

@ -1,111 +0,0 @@
package ca.tweetzy.auctionhouse.guis;
import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.auctionhouse.helpers.ConfigurationItemHelper;
import ca.tweetzy.auctionhouse.managers.SoundManager;
import ca.tweetzy.auctionhouse.settings.Settings;
import ca.tweetzy.core.compatibility.XMaterial;
import ca.tweetzy.core.inventory.TInventory;
import ca.tweetzy.core.utils.PlayerUtils;
import ca.tweetzy.core.utils.nms.NBTEditor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.util.List;
/**
* The current file has been created by Kiran Hart
* Date Created: February 18 2021
* Time Created: 10:07 p.m.
* Usage of any code found within this class is prohibited unless given explicit permission otherwise
*/
public class ExpiredItemsGUI extends TInventory {
private final Player player;
private List<List<ItemStack>> items;
public ExpiredItemsGUI(Player player) {
this.player = player;
// this.items = Lists.partition(AuctionHouse.getInstance().getAuctionPlayerManager().locateAndSelectPlayer(player).getExpiredItems(player, true), 45);
setTitle(Settings.GUI_EXPIRED_AUCTIONS_TITLE.getString());
setPage(1);
setRows(6);
setDynamic(false);
}
@Override
public void onClick(InventoryClickEvent e, int slot) {
e.setCancelled(true);
Player player = (Player) e.getWhoClicked();
switch (slot) {
case 45:
// back button (send them to the main auction house page)
// player.openInventory(new AuctionHouseGUI(AuctionHouse.getInstance().getAuctionPlayerManager().locateAndSelectPlayer(player)).getInventory());
break;
case 46:
// claim all of the expired auctions
// PlayerUtils.giveItem(player, AuctionHouse.getInstance().getAuctionPlayerManager().locateAndSelectPlayer(player).getExpiredItems(player, false));
AuctionHouse.getInstance().getData().set("expired." + this.player.getUniqueId().toString(), null);
AuctionHouse.getInstance().getData().save();
// reopen the gui
player.openInventory(new ExpiredItemsGUI(this.player).getInventory());
break;
case 48:
if (getPage() > 1) {
player.openInventory(setPage(getPage() - 1).getInventory());
SoundManager.getInstance().playSound(player, Settings.SOUNDS_NAVIGATE_GUI_PAGES.getString(), 1f, 1f);
}
break;
case 49:
player.openInventory(new ExpiredItemsGUI(this.player).getInventory());
break;
case 50:
if (getPage() < this.items.size()) {
player.openInventory(setPage(getPage() + 1).getInventory());
SoundManager.getInstance().playSound(player, Settings.SOUNDS_NAVIGATE_GUI_PAGES.getString(), 1f, 1f);
}
break;
default:
if (e.getCurrentItem() != null && e.getCurrentItem().getType() != XMaterial.AIR.parseMaterial()) {
if (!NBTEditor.contains(e.getCurrentItem(), "AuctionItemKey")) {
return;
}
String key = NBTEditor.getString(e.getCurrentItem(), "AuctionItemKey");
if (!AuctionHouse.getInstance().getData().contains("expired." + player.getUniqueId().toString() + "." + key)) {
return;
}
PlayerUtils.giveItem(player, AuctionHouse.getInstance().getData().getItemStack("expired." + player.getUniqueId().toString() + "." + key + ".item"));
AuctionHouse.getInstance().getData().set("expired." + player.getUniqueId().toString() + "." + key, null);
AuctionHouse.getInstance().getData().save();
}
break;
}
}
@Override
public Inventory getInventory() {
Inventory inventory = Bukkit.createInventory(this, getSize(), getTitle());
// set the buttons
inventory.setItem(45, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_BACK_BTN_ITEM.getString(), Settings.GUI_BACK_BTN_NAME.getString(), Settings.GUI_BACK_BTN_LORE.getStringList(), null));
inventory.setItem(46, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_EXPIRED_AUCTIONS_ITEM.getString(), Settings.GUI_EXPIRED_AUCTIONS_NAME.getString(), Settings.GUI_EXPIRED_AUCTIONS_LORE.getStringList(), null));
inventory.setItem(48, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_BACK_BTN_ITEM.getString(), Settings.GUI_BACK_BTN_NAME.getString(), Settings.GUI_BACK_BTN_LORE.getStringList(), null));
inventory.setItem(49, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_REFRESH_BTN_ITEM.getString(), Settings.GUI_REFRESH_BTN_NAME.getString(), Settings.GUI_REFRESH_BTN_LORE.getStringList(), null));
inventory.setItem(50, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_NEXT_BTN_ITEM.getString(), Settings.GUI_NEXT_BTN_NAME.getString(), Settings.GUI_NEXT_BTN_LORE.getStringList(), null));
// populate the gui with the active items
if (items.size() != 0) {
items.get(getPage() - 1).forEach(item -> inventory.setItem(inventory.firstEmpty(), item));
}
return inventory;
}
}

View File

@ -56,7 +56,7 @@ public class GUIAuctionHouse extends Gui {
setButton(5, 1, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_AUCTION_HOUSE_ITEMS_COLLECTION_BIN_ITEM.getString(), Settings.GUI_AUCTION_HOUSE_ITEMS_COLLECTION_BIN_NAME.getString(), Settings.GUI_AUCTION_HOUSE_ITEMS_COLLECTION_BIN_LORE.getStringList(), new HashMap<String, Object>(){{
put("%expired_player_auctions%", auctionPlayer.getItems(true).size());
}}), e -> e.manager.closeAll());
}}), e -> e.manager.showGUI(e.player, new GUIExpiredItems(this.auctionPlayer)));
setButton(5, 6, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_AUCTION_HOUSE_ITEMS_TRANSACTIONS_ITEM.getString(), Settings.GUI_AUCTION_HOUSE_ITEMS_TRANSACTIONS_NAME.getString(), Settings.GUI_AUCTION_HOUSE_ITEMS_TRANSACTIONS_LORE.getStringList(), null), null);
setButton(5, 7, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_AUCTION_HOUSE_ITEMS_HOW_TO_SELL_ITEM.getString(), Settings.GUI_AUCTION_HOUSE_ITEMS_HOW_TO_SELL_NAME.getString(), Settings.GUI_AUCTION_HOUSE_ITEMS_HOW_TO_SELL_LORE.getStringList(), null), null);

View File

@ -0,0 +1,70 @@
package ca.tweetzy.auctionhouse.guis;
import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.auctionhouse.api.AuctionAPI;
import ca.tweetzy.auctionhouse.auction.AuctionItem;
import ca.tweetzy.auctionhouse.auction.AuctionPlayer;
import ca.tweetzy.auctionhouse.auction.AuctionStackType;
import ca.tweetzy.auctionhouse.helpers.ConfigurationItemHelper;
import ca.tweetzy.auctionhouse.settings.Settings;
import ca.tweetzy.core.gui.Gui;
import ca.tweetzy.core.utils.PlayerUtils;
import ca.tweetzy.core.utils.TextUtils;
import ca.tweetzy.core.utils.items.TItemBuilder;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* The current file has been created by Kiran Hart
* Date Created: March 15 2021
* Time Created: 3:19 p.m.
* Usage of any code found within this class is prohibited unless given explicit permission otherwise
*/
public class GUIExpiredItems extends Gui {
final AuctionPlayer auctionPlayer;
public GUIExpiredItems(AuctionPlayer auctionPlayer) {
this.auctionPlayer = auctionPlayer;
setTitle(TextUtils.formatText(Settings.GUI_EXPIRED_AUCTIONS_TITLE.getString()));
setRows(6);
setAcceptsItems(false);
draw();
}
private void draw() {
reset();
// Pagination
pages = (int) Math.max(1, Math.ceil(this.auctionPlayer.getItems(true).size() / (double) 45));
setPrevPage(5, 3, new TItemBuilder(Objects.requireNonNull(Settings.GUI_BACK_BTN_ITEM.getMaterial().parseMaterial())).setName(Settings.GUI_BACK_BTN_NAME.getString()).setLore(Settings.GUI_BACK_BTN_LORE.getStringList()).toItemStack());
setButton(5, 4, new TItemBuilder(Objects.requireNonNull(Settings.GUI_REFRESH_BTN_ITEM.getMaterial().parseMaterial())).setName(Settings.GUI_REFRESH_BTN_NAME.getString()).setLore(Settings.GUI_REFRESH_BTN_LORE.getStringList()).toItemStack(), e -> draw());
setNextPage(5, 5, new TItemBuilder(Objects.requireNonNull(Settings.GUI_NEXT_BTN_ITEM.getMaterial().parseMaterial())).setName(Settings.GUI_NEXT_BTN_NAME.getString()).setLore(Settings.GUI_NEXT_BTN_LORE.getStringList()).toItemStack());
setOnPage(e -> draw());
// Other Buttons
setButton(5, 0, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_CLOSE_BTN_ITEM.getString(), Settings.GUI_CLOSE_BTN_NAME.getString(), Settings.GUI_CLOSE_BTN_LORE.getStringList(), null), e -> e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer)));
setButton(5, 1, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_EXPIRED_AUCTIONS_ITEM.getString(), Settings.GUI_EXPIRED_AUCTIONS_NAME.getString(), Settings.GUI_EXPIRED_AUCTIONS_LORE.getStringList(), null), e -> {
this.auctionPlayer.getItems(true).forEach(item -> {
AuctionHouse.getInstance().getAuctionItemManager().adjustItemsInFile(item, false);
AuctionHouse.getInstance().getAuctionItemManager().removeItem(item.getKey());
PlayerUtils.giveItem(e.player, AuctionAPI.getInstance().deserializeItem(item.getRawItem()));
});
draw();
});
List<AuctionItem> data = this.auctionPlayer.getItems(true).stream().sorted(Comparator.comparingInt(AuctionItem::getRemainingTime).reversed()).skip((page - 1) * 45L).limit(45).collect(Collectors.toList());
int slot = 0;
for (AuctionItem item : data) {
setButton(slot++, AuctionAPI.getInstance().deserializeItem(item.getRawItem()), e -> {
AuctionHouse.getInstance().getAuctionItemManager().adjustItemsInFile(item, false);
AuctionHouse.getInstance().getAuctionItemManager().removeItem(item.getKey());
PlayerUtils.giveItem(e.player, AuctionAPI.getInstance().deserializeItem(item.getRawItem()));
draw();
});
}
}
}

View File

@ -47,7 +47,7 @@ public class AuctionItemManager {
if (category == null) {
return Collections.unmodifiableList(this.auctionItems);
}
return Collections.unmodifiableList(auctionItems.stream().filter(auctionItem -> MaterialCategorizer.getMaterialCategory(AuctionAPI.deserializeItem(auctionItem.getRawItem())) == category).collect(Collectors.toList()));
return Collections.unmodifiableList(auctionItems.stream().filter(auctionItem -> MaterialCategorizer.getMaterialCategory(AuctionAPI.getInstance().deserializeItem(auctionItem.getRawItem())) == category).collect(Collectors.toList()));
}
public void loadItems() {
@ -75,7 +75,7 @@ public class AuctionItemManager {
List<AuctionItem> foundItems = AuctionHouse.getInstance().getData().getStringList("auction items").stream().map(AuctionAPI.getInstance()::convertBase64ToObject).map(object -> (AuctionItem) object).collect(Collectors.toList());
foundItems.addAll(items);
AuctionHouse.getInstance().getData().set("auction items", foundItems);
AuctionHouse.getInstance().getData().set("auction items", foundItems.stream().map(AuctionAPI.getInstance()::convertToBase64).collect(Collectors.toList()));
AuctionHouse.getInstance().getData().save();
}
@ -87,7 +87,7 @@ public class AuctionItemManager {
}
List<AuctionItem> items = AuctionHouse.getInstance().getData().getStringList("auction items").stream().map(AuctionAPI.getInstance()::convertBase64ToObject).map(object -> (AuctionItem) object).collect(Collectors.toList());
if (items.stream().anyMatch(i -> i.getKey().equals(item.getKey())) && !add) {
if (items.stream().anyMatch(i -> i.getKey().equals(item.getKey())) || !add) {
items.removeIf(i -> i.getKey().equals(item.getKey()));
} else {
items.add(item);

View File

@ -57,6 +57,7 @@ public class TickAuctionsTask extends BukkitRunnable {
// check if the auction item owner is the same as the highest bidder
if (item.getOwner().equals(item.getHighestBidder())) {
// was not sold
item.setExpired(true);
AuctionHouse.getInstance().getAuctionItemManager().adjustItemsInFile(item, true);
} else {
// the item was sold ?? then do the checks
@ -64,29 +65,31 @@ public class TickAuctionsTask extends BukkitRunnable {
if (offlinePlayer.isOnline()) {
if (AuctionHouse.getInstance().getEconomy().has(offlinePlayer, item.getCurrentPrice())) {
// since they're online, try to add the item to their inventory
PlayerUtils.giveItem(offlinePlayer.getPlayer(), AuctionAPI.deserializeItem(item.getRawItem()));
PlayerUtils.giveItem(offlinePlayer.getPlayer(), AuctionAPI.getInstance().deserializeItem(item.getRawItem()));
// withdraw money and give to the owner
AuctionHouse.getInstance().getEconomy().withdrawPlayer(offlinePlayer, item.getCurrentPrice());
AuctionHouse.getInstance().getEconomy().depositPlayer(Bukkit.getOfflinePlayer(item.getOwner()), item.getCurrentPrice());
// send a message to each of them
AuctionHouse.getInstance().getLocale().getMessage("auction.bidwon")
.processPlaceholder("item", WordUtils.capitalizeFully(AuctionAPI.deserializeItem(item.getRawItem()).getType().name().replace("_", " ")))
.processPlaceholder("amount", AuctionAPI.deserializeItem(item.getRawItem()).getAmount())
.processPlaceholder("item", WordUtils.capitalizeFully(AuctionAPI.getInstance().deserializeItem(item.getRawItem()).getType().name().replace("_", " ")))
.processPlaceholder("amount", AuctionAPI.getInstance().deserializeItem(item.getRawItem()).getAmount())
.processPlaceholder("price", String.format("%,.2f", item.getCurrentPrice()))
.sendPrefixedMessage(offlinePlayer.getPlayer());
AuctionHouse.getInstance().getLocale().getMessage("pricing.moneyremove").processPlaceholder("price", String.format("%,.2f", item.getCurrentPrice())).sendPrefixedMessage(offlinePlayer.getPlayer());
// if the original owner is online, let them know they sold an item
if (Bukkit.getOfflinePlayer(item.getOwner()).isOnline()) {
AuctionHouse.getInstance().getLocale().getMessage("auction.itemsold")
.processPlaceholder("item", WordUtils.capitalizeFully(AuctionAPI.deserializeItem(item.getRawItem()).getType().name().replace("_", " ")))
.processPlaceholder("item", WordUtils.capitalizeFully(AuctionAPI.getInstance().deserializeItem(item.getRawItem()).getType().name().replace("_", " ")))
.processPlaceholder("price", String.format("%,.2f", item.getCurrentPrice()))
.sendPrefixedMessage(Bukkit.getOfflinePlayer(item.getOwner()).getPlayer());
AuctionHouse.getInstance().getLocale().getMessage("pricing.moneyadd").processPlaceholder("price", String.format("%,.2f", item.getCurrentPrice())).sendPrefixedMessage(Bukkit.getOfflinePlayer(item.getOwner()).getPlayer());
}
AuctionHouse.getInstance().getAuctionItemManager().removeItem(item.getKey());
AuctionHouse.getInstance().getAuctionItemManager().adjustItemsInFile(item, false);
} else {
// they don't have enough money to buy it, so send it back to the original owner
item.setExpired(true);
AuctionHouse.getInstance().getAuctionItemManager().adjustItemsInFile(item, true);
}
} else {
@ -96,9 +99,11 @@ public class TickAuctionsTask extends BukkitRunnable {
AuctionHouse.getInstance().getEconomy().withdrawPlayer(offlinePlayer, item.getCurrentPrice());
AuctionHouse.getInstance().getEconomy().depositPlayer(Bukkit.getOfflinePlayer(item.getOwner()), item.getCurrentPrice());
item.setOwner(offlinePlayer.getUniqueId());
item.setExpired(true);
AuctionHouse.getInstance().getAuctionItemManager().adjustItemsInFile(item, true);
} else {
// they don't have enough money to buy it, so send it back to the original owner
item.setExpired(true);
AuctionHouse.getInstance().getAuctionItemManager().adjustItemsInFile(item, true);
}
}