update buy menu

This commit is contained in:
Ryder Belserion 2024-10-25 10:52:09 -04:00
parent e5a1b8c786
commit 28f34bb486
No known key found for this signature in database
3 changed files with 50 additions and 60 deletions

View File

@ -1,6 +1,5 @@
package com.badbones69.crazyauctions.api.guis.types.other; package com.badbones69.crazyauctions.api.guis.types.other;
import com.badbones69.crazyauctions.Methods;
import com.badbones69.crazyauctions.api.builders.ItemBuilder; import com.badbones69.crazyauctions.api.builders.ItemBuilder;
import com.badbones69.crazyauctions.api.enums.Messages; import com.badbones69.crazyauctions.api.enums.Messages;
import com.badbones69.crazyauctions.api.enums.ShopType; import com.badbones69.crazyauctions.api.enums.ShopType;
@ -120,8 +119,6 @@ public class BidMenu extends Holder {
final Player player = bidMenu.player; final Player player = bidMenu.player;
final ItemStack item = bidMenu.auction.getActiveItem(ShopType.BID).getItemStack();
final AuctionItem auction = bidMenu.auction; final AuctionItem auction = bidMenu.auction;
switch (type) { switch (type) {

View File

@ -11,6 +11,7 @@ import com.badbones69.crazyauctions.api.guis.Holder;
import com.badbones69.crazyauctions.api.guis.HolderManager; import com.badbones69.crazyauctions.api.guis.HolderManager;
import com.badbones69.crazyauctions.api.GuiManager; import com.badbones69.crazyauctions.api.GuiManager;
import com.badbones69.crazyauctions.currency.VaultSupport; import com.badbones69.crazyauctions.currency.VaultSupport;
import com.badbones69.crazyauctions.tasks.objects.AuctionItem;
import io.papermc.paper.persistence.PersistentDataContainerView; import io.papermc.paper.persistence.PersistentDataContainerView;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@ -23,18 +24,22 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
public class BuyingMenu extends Holder { public class BuyingMenu extends Holder {
private FileConfiguration config; private FileConfiguration config;
private FileConfiguration data; private FileConfiguration data;
private AuctionItem auction;
private List<String> options; private List<String> options;
private String id; private String id;
public BuyingMenu(final Player player, final String id, final String title) { public BuyingMenu(final AuctionItem auction, final Player player, final String id, final String title) {
super(player, title, 9); super(player, title, 9);
this.auction = auction;
this.config = Files.config.getConfiguration(); this.config = Files.config.getConfiguration();
this.data = Files.data.getConfiguration(); this.data = Files.data.getConfiguration();
@ -47,11 +52,15 @@ public class BuyingMenu extends Holder {
@Override @Override
public final Holder build() { public final Holder build() {
if (!this.data.contains("Items." + this.id)) { final UUID uuid = this.player.getUniqueId();
GuiManager.openShop(this.player, ShopType.SELL, HolderManager.getShopCategory(this.player), 1);
if (!this.data.contains("active_auctions." + uuid + "." + this.id)) {
GuiManager.openShop(this.player, ShopType.BID, HolderManager.getShopCategory(this.player), 1);
this.player.sendMessage(Messages.ITEM_DOESNT_EXIST.getMessage(this.player)); this.player.sendMessage(Messages.ITEM_DOESNT_EXIST.getMessage(this.player));
this.userManager.removeAuctionItem(this.auction); // remove auction item, as it's not in the active_auctions
return this; return this;
} }
@ -99,37 +108,7 @@ public class BuyingMenu extends Holder {
} }
} }
ItemBuilder itemBuilder = ItemBuilder.convertItemStack(this.data.getString("Items." + this.id + ".Item")); this.inventory.setItem(4, this.auction.getActiveItem(ShopType.SELL).build());
if (itemBuilder == null) {
this.plugin.getLogger().warning("The item with store id " + this.data.getString("Items." + this.id + ".StoreID", "buying_menu") + " obtained from your data.yml could not be converted!");
return this;
}
List<String> lore = new ArrayList<>(itemBuilder.getUpdatedLore());
lore.add(" ");
String price = Methods.getPrice(this.id, false);
String time = Methods.convertToTime(this.data.getLong("Items." + this.id + ".Time-Till-Expire"));
String id = this.data.getString("Items." + this.id + ".Name", "None");
for (String l : this.config.getStringList("Settings.GUISettings.SellingItemLore")) {
lore.add(l.replace("%Price%", price).replace("%price%", price)
.replace("%Seller%", id)
.replace("%seller%", id)
.replace("%Time%", time)
.replace("%time%", time));
}
itemBuilder.setLore(lore);
itemBuilder.addInteger(this.data.getInt("Items." + this.id + ".StoreID"), Keys.auction_store_id.getNamespacedKey());
itemBuilder.addString(this.id, Keys.auction_number.getNamespacedKey());
this.inventory.setItem(4, itemBuilder.build());
this.player.openInventory(this.inventory); this.player.openInventory(this.inventory);
@ -138,13 +117,13 @@ public class BuyingMenu extends Holder {
@Override @Override
public void run(InventoryClickEvent event) { public void run(InventoryClickEvent event) {
if (!(event.getInventory().getHolder(false) instanceof BuyingMenu menu)) return; if (!(event.getInventory().getHolder(false) instanceof BuyingMenu buyingMenu)) return;
event.setCancelled(true); event.setCancelled(true);
final int slot = event.getSlot(); final int slot = event.getSlot();
final Inventory inventory = menu.getInventory(); final Inventory inventory = buyingMenu.getInventory();
if (slot > inventory.getSize()) return; if (slot > inventory.getSize()) return;
@ -162,25 +141,30 @@ public class BuyingMenu extends Holder {
if (type.isEmpty()) return; if (type.isEmpty()) return;
final FileConfiguration data = Files.data.getConfiguration(); final FileConfiguration data = buyingMenu.data;
final Player player = (Player) event.getWhoClicked(); final Player player = buyingMenu.player;
final UUID uuid = player.getUniqueId();
final AuctionItem auction = buyingMenu.auction;
switch (type) { switch (type) {
case "Confirm" -> { case "Confirm" -> {
String id = menu.id; final String id = buyingMenu.id;
if (!data.contains("Items." + id)) { if (!this.data.contains("active_auctions." + uuid + "." + id)) {
menu.click(player); buyingMenu.click(player);
GuiManager.openShop(player, HolderManager.getShopType(player), HolderManager.getShopCategory(player), 1); GuiManager.openShop(player, HolderManager.getShopType(player), HolderManager.getShopCategory(player), 1);
player.sendMessage(Messages.ITEM_DOESNT_EXIST.getMessage(player)); player.sendMessage(Messages.ITEM_DOESNT_EXIST.getMessage(player));
this.userManager.removeAuctionItem(this.auction); // remove auction item, as it's not in the active_auctions
return; return;
} }
if (Methods.isInvFull(player)) { if (Methods.isInvFull(player)) {
menu.click(player); buyingMenu.click(player);
player.closeInventory(); player.closeInventory();
player.sendMessage(Messages.INVENTORY_FULL.getMessage(player)); player.sendMessage(Messages.INVENTORY_FULL.getMessage(player));
@ -188,16 +172,18 @@ public class BuyingMenu extends Holder {
return; return;
} }
long cost = data.getLong("Items." + id + ".Price"); final long cost = auction.getPrice();
String seller = data.getString("Items." + id + ".Seller"); final String seller = auction.getName();
final VaultSupport support = this.plugin.getSupport(); final VaultSupport support = this.plugin.getSupport();
if (support.getMoney(player) < cost) { if (support.getMoney(player) < cost) {
menu.click(player); buyingMenu.click(player);
player.closeInventory(); player.closeInventory();
Map<String, String> placeholders = new HashMap<>(); Map<String, String> placeholders = new HashMap<>();
placeholders.put("%Money_Needed%", (cost - support.getMoney(player)) + ""); placeholders.put("%Money_Needed%", (cost - support.getMoney(player)) + "");
placeholders.put("%money_needed%", (cost - support.getMoney(player)) + ""); placeholders.put("%money_needed%", (cost - support.getMoney(player)) + "");
@ -206,15 +192,15 @@ public class BuyingMenu extends Holder {
return; return;
} }
ItemStack i = Methods.fromBase64(data.getString("Items." + id + ".Item")); final ItemStack item = auction.asItemStack();
this.server.getPluginManager().callEvent(new AuctionBuyEvent(player, i, cost)); this.server.getPluginManager().callEvent(new AuctionBuyEvent(player, item, cost));
support.removeMoney(player, cost); support.removeMoney(player, cost);
support.addMoney(Methods.getOfflinePlayer(seller), cost); support.addMoney(Methods.getOfflinePlayer(seller), cost);
Map<String, String> placeholders = new HashMap<>(); Map<String, String> placeholders = new HashMap<>();
String price = Methods.getPrice(id, false); String price = String.valueOf(auction.getPrice());
placeholders.put("%Price%", price); placeholders.put("%Price%", price);
placeholders.put("%price%", price); placeholders.put("%price%", price);
@ -231,22 +217,23 @@ public class BuyingMenu extends Holder {
FileConfiguration config = Files.config.getConfiguration(); FileConfiguration config = Files.config.getConfiguration();
String sound = config.getString("Settings.Sold-Item-Sound", ""); String sound = config.getString("Settings.Sold-Item-Sound", "UI_BUTTON_CLICK");
if (sound.isEmpty()) return; if (sound.isEmpty()) return;
try { player.playSound(player.getLocation(), Sound.valueOf(sound), 1f, 1f);
player.playSound(player.getLocation(), Sound.valueOf(sound), 1f, 1f);
} catch (Exception ignored) {}
} }
} }
player.getInventory().addItem(i); player.getInventory().addItem(item);
data.set("active_auctions." + player.getUniqueId() + "." + id, null);
this.userManager.removeAuctionItem(this.auction);
data.set("Items." + id, null);
Files.data.save(); Files.data.save();
menu.click(player); buyingMenu.click(player);
GuiManager.openShop(player, HolderManager.getShopType(player), HolderManager.getShopCategory(player), 1); GuiManager.openShop(player, HolderManager.getShopType(player), HolderManager.getShopCategory(player), 1);
} }
@ -254,7 +241,7 @@ public class BuyingMenu extends Holder {
case "Cancel" -> { case "Cancel" -> {
GuiManager.openShop(player, HolderManager.getShopType(player), HolderManager.getShopCategory(player), 1); GuiManager.openShop(player, HolderManager.getShopType(player), HolderManager.getShopCategory(player), 1);
menu.click(player); buyingMenu.click(player);
} }
} }
} }

View File

@ -116,6 +116,10 @@ public class AuctionItem {
return this.itemStack; return this.itemStack;
} }
public final ItemBuilder asItemBuilder() {
return new ItemBuilder(this.itemStack);
}
public final String asBase64() { public final String asBase64() {
return Methods.toBase64(asItemStack()); return Methods.toBase64(asItemStack());
} }
@ -163,7 +167,9 @@ public class AuctionItem {
} }
} }
itemBuilder.setLore(lore).addString(getStoreID(), Keys.auction_store_id.getNamespacedKey()); itemBuilder.setLore(lore)
.addString(this.id, Keys.auction_number.getNamespacedKey())
.addString(getStoreID(), Keys.auction_store_id.getNamespacedKey());
return itemBuilder; return itemBuilder;
} }