add buying menu

This commit is contained in:
Ryder Belserion 2024-10-11 15:06:37 -04:00
parent 7bd79b19ed
commit cf29c17f67
No known key found for this signature in database
4 changed files with 274 additions and 187 deletions

View File

@ -8,6 +8,7 @@ import com.badbones69.crazyauctions.api.guis.types.AuctionsMenu;
import com.badbones69.crazyauctions.api.guis.types.CategoriesMenu; import com.badbones69.crazyauctions.api.guis.types.CategoriesMenu;
import com.badbones69.crazyauctions.api.guis.types.CurrentMenu; import com.badbones69.crazyauctions.api.guis.types.CurrentMenu;
import com.badbones69.crazyauctions.api.guis.types.ExpiredMenu; import com.badbones69.crazyauctions.api.guis.types.ExpiredMenu;
import com.badbones69.crazyauctions.api.guis.types.transactions.BuyingMenu;
import com.badbones69.crazyauctions.api.support.MetricsWrapper; import com.badbones69.crazyauctions.api.support.MetricsWrapper;
import com.badbones69.crazyauctions.commands.AuctionCommand; import com.badbones69.crazyauctions.commands.AuctionCommand;
import com.badbones69.crazyauctions.commands.AuctionTab; import com.badbones69.crazyauctions.commands.AuctionTab;
@ -126,6 +127,7 @@ public class CrazyAuctions extends Vital {
manager.registerEvents(new CategoriesMenu(), this); // register categories menu manager.registerEvents(new CategoriesMenu(), this); // register categories menu
manager.registerEvents(new CurrentMenu(), this); // register current listings menu manager.registerEvents(new CurrentMenu(), this); // register current listings menu
manager.registerEvents(new ExpiredMenu(), this); // register expired menu manager.registerEvents(new ExpiredMenu(), this); // register expired menu
manager.registerEvents(new BuyingMenu(), this); // register buying menu
manager.registerEvents(new GuiListener(), this); manager.registerEvents(new GuiListener(), this);
manager.registerEvents(new MiscListener(), this); manager.registerEvents(new MiscListener(), this);

View File

@ -54,6 +54,10 @@ public abstract class Holder implements InventoryHolder, Listener {
this(player, null, title, size, page); this(player, null, title, size, page);
} }
public Holder(Player player, String title, int size) {
this(player, null, title, size, 1);
}
public Holder() {} public Holder() {}
public abstract Holder build(); public abstract Holder build();

View File

@ -0,0 +1,265 @@
package com.badbones69.crazyauctions.api.guis.types.transactions;
import com.badbones69.crazyauctions.Methods;
import com.badbones69.crazyauctions.api.builders.ItemBuilder;
import com.badbones69.crazyauctions.api.enums.Messages;
import com.badbones69.crazyauctions.api.enums.ShopType;
import com.badbones69.crazyauctions.api.enums.misc.Files;
import com.badbones69.crazyauctions.api.enums.misc.Keys;
import com.badbones69.crazyauctions.api.events.AuctionBuyEvent;
import com.badbones69.crazyauctions.api.guis.Holder;
import com.badbones69.crazyauctions.api.guis.HolderManager;
import com.badbones69.crazyauctions.api.guis.types.CategoriesMenu;
import com.badbones69.crazyauctions.controllers.GuiListener;
import com.badbones69.crazyauctions.currency.VaultSupport;
import io.papermc.paper.persistence.PersistentDataContainerView;
import org.bukkit.OfflinePlayer;
import org.bukkit.Sound;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BuyingMenu extends Holder {
private FileConfiguration config;
private FileConfiguration data;
private List<String> options;
private String id;
public BuyingMenu(final Player player, final String id, final String title, final int size) {
super(player, title, size);
this.config = Files.config.getConfiguration();
this.data = Files.data.getConfiguration();
this.options = new ArrayList<>();
this.id = id;
}
public BuyingMenu() {}
@Override
public final Holder build() {
Methods.updateAuction();
if (!this.data.contains("Items." + this.id)) {
GuiListener.openShop(this.player, ShopType.SELL, HolderManager.getShopCategory(this.player), 1);
this.player.sendMessage(Messages.ITEM_DOESNT_EXIST.getMessage(this.player));
return this;
}
this.options.addAll(List.of(
"Confirm",
"Cancel"
));
for (final String key : this.options) {
if (!this.config.contains("Settings.GUISettings.OtherSettings." + key)) {
continue;
}
if (!this.config.getBoolean("Settings.GUISettings.OtherSettings." + key + ".Toggle", true)) {
continue;
}
final String id = this.config.getString("Settings.GUISettings.OtherSettings." + key + ".Item");
final String name = this.config.getString("Settings.GUISettings.OtherSettings." + key + ".Name");
final ItemBuilder itemBuilder = new ItemBuilder().setMaterial(id).setName(name).setAmount(1);
if (this.config.contains("Settings.GUISettings.OtherSettings." + key + ".Lore")) {
itemBuilder.setLore(this.config.getStringList("Settings.GUISettings.OtherSettings." + key + ".Lore")).addString(key);
}
switch (key) {
case "Confirm" -> {
final ItemStack itemStack = itemBuilder.addString("Confirm").build();
this.inventory.setItem(0, itemStack);
this.inventory.setItem(1, itemStack);
this.inventory.setItem(2, itemStack);
this.inventory.setItem(3, itemStack);
}
case "Cancel" -> {
final ItemStack itemStack = itemBuilder.addString("Cancel").build();
this.inventory.setItem(5, itemStack);
this.inventory.setItem(6, itemStack);
this.inventory.setItem(7, itemStack);
this.inventory.setItem(8, itemStack);
}
}
}
ItemBuilder itemBuilder = ItemBuilder.convertItemStack(data.getString("Items." + this.id + ".Item"));
List<String> lore = new ArrayList<>(itemBuilder.getUpdatedLore());
lore.add(" ");
String price = Methods.getPrice(this.id, false);
String time = Methods.convertToTime(data.getLong("Items." + this.id + ".Time-Till-Expire"));
OfflinePlayer target = null;
String id = data.getString("Items." + this.id + ".Seller");
if (id != null) {
target = Methods.getOfflinePlayer(id);
}
for (String l : config.getStringList("Settings.GUISettings.SellingItemLore")) {
lore.add(l.replace("%Price%", price).replace("%price%", price)
.replace("%Seller%", target != null ? target.getName() : "N/A")
.replace("%seller%", target != null ? target.getName() : "N/A")
.replace("%Time%", time)
.replace("%time%", time));
}
itemBuilder.setLore(lore);
this.inventory.setItem(4, itemBuilder.build());
HolderManager.addId(this.player, this.id);
this.player.openInventory(this.inventory);
return this;
}
@Override
public void run(InventoryClickEvent event) {
if (!(event.getInventory().getHolder(false) instanceof BuyingMenu menu)) return;
event.setCancelled(true);
final int slot = event.getSlot();
final Inventory inventory = menu.getInventory();
if (slot > inventory.getSize()) return;
if (event.getCurrentItem() == null) return;
final ItemStack itemStack = event.getCurrentItem();
if (itemStack == null) return;
final PersistentDataContainerView container = itemStack.getPersistentDataContainer();
if (!container.has(Keys.auction_button.getNamespacedKey())) return;
final String type = container.getOrDefault(Keys.auction_button.getNamespacedKey(), PersistentDataType.STRING, "");
if (type.isEmpty()) return;
final FileConfiguration data = Files.data.getConfiguration();
final Player player = (Player) event.getWhoClicked();
switch (type) {
case "Confirm" -> {
String ID = HolderManager.getId(player);
long cost = data.getLong("Items." + ID + ".Price");
String seller = data.getString("Items." + ID + ".Seller");
if (!data.contains("Items." + ID)) {
click();
//openShop(player, HolderManager.getShopType(player), HolderManager.getShopCategory(player), 1);
player.sendMessage(Messages.ITEM_DOESNT_EXIST.getMessage(player));
return;
}
if (Methods.isInvFull(player)) {
click();
player.closeInventory();
player.sendMessage(Messages.INVENTORY_FULL.getMessage(player));
return;
}
final VaultSupport support = this.plugin.getSupport();
if (support.getMoney(player) < cost) {
click();
player.closeInventory();
Map<String, String> placeholders = new HashMap<>();
placeholders.put("%Money_Needed%", (cost - support.getMoney(player)) + "");
placeholders.put("%money_needed%", (cost - support.getMoney(player)) + "");
player.sendMessage(Messages.NEED_MORE_MONEY.getMessage(player, placeholders));
return;
}
ItemStack i = Methods.fromBase64(data.getString("Items." + ID + ".Item"));
this.server.getPluginManager().callEvent(new AuctionBuyEvent(player, i, cost));
support.removeMoney(player, cost);
support.addMoney(Methods.getOfflinePlayer(seller), cost);
Map<String, String> placeholders = new HashMap<>();
String price = Methods.getPrice(ID, false);
placeholders.put("%Price%", price);
placeholders.put("%price%", price);
placeholders.put("%Player%", player.getName());
placeholders.put("%player%", player.getName());
player.sendMessage(Messages.BOUGHT_ITEM.getMessage(player, placeholders));
if (seller != null && Methods.isOnline(seller) && Methods.getPlayer(seller) != null) {
Player sell = Methods.getPlayer(seller);
if (sell != null) {
sell.sendMessage(Messages.PLAYER_BOUGHT_ITEM.getMessage(player, placeholders));
FileConfiguration config = Files.config.getConfiguration();
String sound = config.getString("Settings.Sold-Item-Sound", "");
if (sound.isEmpty()) return;
try {
player.playSound(player.getLocation(), Sound.valueOf(sound), 1f, 1f);
} catch (Exception ignored) {}
}
}
player.getInventory().addItem(i);
data.set("Items." + ID, null);
Files.data.save();
click();
GuiListener.openShop(player, HolderManager.getShopType(player), HolderManager.getShopCategory(player), 1);
}
case "Cancel" -> {
GuiListener.openShop(player, HolderManager.getShopType(player), HolderManager.getShopCategory(player), 1);
click();
}
}
}
}

View File

@ -14,6 +14,7 @@ import com.badbones69.crazyauctions.api.guis.types.AuctionsMenu;
import com.badbones69.crazyauctions.api.guis.types.CategoriesMenu; import com.badbones69.crazyauctions.api.guis.types.CategoriesMenu;
import com.badbones69.crazyauctions.api.guis.types.CurrentMenu; import com.badbones69.crazyauctions.api.guis.types.CurrentMenu;
import com.badbones69.crazyauctions.api.guis.types.ExpiredMenu; import com.badbones69.crazyauctions.api.guis.types.ExpiredMenu;
import com.badbones69.crazyauctions.api.guis.types.transactions.BuyingMenu;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.Sound; import org.bukkit.Sound;
@ -63,92 +64,9 @@ public class GuiListener implements Listener {
} }
public static void openBuying(Player player, String ID) { public static void openBuying(Player player, String ID) {
Methods.updateAuction(); final FileConfiguration config = Files.config.getConfiguration();
FileConfiguration config = Files.config.getConfiguration(); new BuyingMenu(player, ID, config.getString("Settings.Buying-Item", "N/A"), 9).build();
FileConfiguration data = Files.data.getConfiguration();
if (!data.contains("Items." + ID)) {
openShop(player, ShopType.SELL, HolderManager.getShopCategory(player), 1);
player.sendMessage(Messages.ITEM_DOESNT_EXIST.getMessage(player));
return;
}
Inventory inv = plugin.getServer().createInventory(null, 9, Methods.color(config.getString("Settings.Buying-Item")));
List<String> options = new ArrayList<>();
options.add("Confirm");
options.add("Cancel");
for (String o : options) {
String id = config.getString("Settings.GUISettings.OtherSettings." + o + ".Item");
String name = config.getString("Settings.GUISettings.OtherSettings." + o + ".Name");
ItemStack item;
ItemBuilder itemBuilder = new ItemBuilder().setMaterial(id).setName(name).setAmount(1);
List<String> lore = new ArrayList<>(itemBuilder.getUpdatedLore());
lore.add(" ");
if (config.contains("Settings.GUISettings.OtherSettings." + o + ".Lore")) {
lore.addAll(config.getStringList("Settings.GUISettings.OtherSettings." + o + ".Lore"));
itemBuilder.setLore(lore);
}
item = itemBuilder.build();
if (o.equals("Confirm")) {
inv.setItem(0, item);
inv.setItem(1, item);
inv.setItem(2, item);
inv.setItem(3, item);
}
if (o.equals("Cancel")) {
inv.setItem(5, item);
inv.setItem(6, item);
inv.setItem(7, item);
inv.setItem(8, item);
}
}
ItemBuilder itemBuilder = ItemBuilder.convertItemStack(data.getString("Items." + ID + ".Item"));
List<String> lore = new ArrayList<>(itemBuilder.getUpdatedLore());
lore.add(" ");
String price = Methods.getPrice(ID, false);
String time = Methods.convertToTime(data.getLong("Items." + ID + ".Time-Till-Expire"));
OfflinePlayer target = null;
String id = data.getString("Items." + ID + ".Seller");
if (id != null) {
target = Methods.getOfflinePlayer(id);
}
for (String l : config.getStringList("Settings.GUISettings.SellingItemLore")) {
lore.add(l.replace("%Price%", price).replace("%price%", price)
.replace("%Seller%", target != null ? target.getName() : "N/A")
.replace("%seller%", target != null ? target.getName() : "N/A")
.replace("%Time%", time)
.replace("%time%", time));
}
itemBuilder.setLore(lore);
inv.setItem(4, itemBuilder.build());
HolderManager.addId(player, ID);
player.openInventory(inv);
} }
public static void openBidding(Player player, String ID) { public static void openBidding(Player player, String ID) {
@ -379,18 +297,6 @@ public class GuiListener implements Listener {
return itemBuilder.build(); return itemBuilder.build();
} }
private void playSoldSound(@NotNull Player player) {
FileConfiguration config = Files.config.getConfiguration();
String sound = config.getString("Settings.Sold-Item-Sound", "");
if (sound.isEmpty()) return;
try {
player.playSound(player.getLocation(), Sound.valueOf(sound), 1, 1);
} catch (Exception ignored) {}
}
@EventHandler @EventHandler
public void onInvClick(InventoryClickEvent e) { public void onInvClick(InventoryClickEvent e) {
FileConfiguration config = Files.config.getConfiguration(); FileConfiguration config = Files.config.getConfiguration();
@ -505,96 +411,6 @@ public class GuiListener implements Listener {
} }
} }
} }
if (strippedTitle.contains(Methods.strip(config.getString("Settings.Buying-Item")))) {
e.setCancelled(true);
int slot = e.getRawSlot();
if (slot > inv.getSize()) return;
if (strippedDisplayName.equalsIgnoreCase(Methods.strip(config.getString("Settings.GUISettings.OtherSettings.Confirm.Name")))) {
String ID = HolderManager.getId(player);
long cost = data.getLong("Items." + ID + ".Price");
String seller = data.getString("Items." + ID + ".Seller");
if (!data.contains("Items." + ID)) {
playClick(player);
openShop(player, HolderManager.getShopType(player), HolderManager.getShopCategory(player), 1);
player.sendMessage(Messages.ITEM_DOESNT_EXIST.getMessage(player));
return;
}
if (Methods.isInvFull(player)) {
playClick(player);
player.closeInventory();
player.sendMessage(Messages.INVENTORY_FULL.getMessage(player));
return;
}
if (plugin.getSupport().getMoney(player) < cost) {
playClick(player);
player.closeInventory();
Map<String, String> placeholders = new HashMap<>();
placeholders.put("%Money_Needed%", (cost - plugin.getSupport().getMoney(player)) + "");
placeholders.put("%money_needed%", (cost - plugin.getSupport().getMoney(player)) + "");
player.sendMessage(Messages.NEED_MORE_MONEY.getMessage(player, placeholders));
return;
}
ItemStack i = Methods.fromBase64(data.getString("Items." + ID + ".Item"));
plugin.getServer().getPluginManager().callEvent(new AuctionBuyEvent(player, i, cost));
plugin.getSupport().removeMoney(player, cost);
plugin.getSupport().addMoney(Methods.getOfflinePlayer(seller), cost);
Map<String, String> placeholders = new HashMap<>();
String price = Methods.getPrice(ID, false);
placeholders.put("%Price%", price);
placeholders.put("%price%", price);
placeholders.put("%Player%", player.getName());
placeholders.put("%player%", player.getName());
player.sendMessage(Messages.BOUGHT_ITEM.getMessage(player, placeholders));
if (seller != null && Methods.isOnline(seller) && Methods.getPlayer(seller) != null) {
Player sell = Methods.getPlayer(seller);
if (sell != null) {
sell.sendMessage(Messages.PLAYER_BOUGHT_ITEM.getMessage(player, placeholders));
playSoldSound(sell);
}
}
player.getInventory().addItem(i);
data.set("Items." + ID, null);
Files.data.save();
playClick(player);
openShop(player, HolderManager.getShopType(player), HolderManager.getShopCategory(player), 1);
return;
}
if (strippedDisplayName.equalsIgnoreCase(Methods.strip(config.getString("Settings.GUISettings.OtherSettings.Cancel.Name")))) {
openShop(player, HolderManager.getShopType(player), HolderManager.getShopCategory(player), 1);
playClick(player);
return;
}
}
} }
private static void playClick(Player player) { private static void playClick(Player player) {