tweaked conditionals for blockshop with full inventory checks, low funds checks, and quantity/price checks and reloads

This commit is contained in:
kai ohara 2025-02-13 20:20:41 -05:00
parent 4cb1e717fb
commit e2e5e999f5
15 changed files with 50 additions and 16 deletions

Binary file not shown.

View File

@ -12,5 +12,6 @@ shop:
material: OAK_PLANKS material: OAK_PLANKS
price: 15 price: 15
display_name: "Oak Planks" display_name: "Oak Planks"
poor: "you're poor..."
pagination: pagination:
max_items_per_page: 45 # Max items to display per page max_items_per_page: 45 # Max items to display per page

View File

@ -164,27 +164,59 @@ public class BlockShop implements Listener {
ItemStack clickedItem = event.getCurrentItem(); ItemStack clickedItem = event.getCurrentItem();
if (clickedItem != null && clickedItem.getType() != Material.AIR) { if (clickedItem != null && clickedItem.getType() != Material.AIR) {
double price = getPrice(clickedItem); double price = getPrice(clickedItem);
if (economy.has(player, price)) { if (!economy.has(player, price)) {
economy.withdrawPlayer(player, price); player.sendMessage(ChatColor.RED + getConfigLine("poor", "You don't have enough money to buy this."));
// Create a new ItemStack with the default meta return;
ItemStack sold = new ItemStack(clickedItem.getType(), clickedItem.getAmount());
// Reset item meta (default name, no lore)
ItemMeta defaultMeta = sold.getItemMeta();
if (defaultMeta != null) {
defaultMeta.setDisplayName(null); // Reset to default name
defaultMeta.setLore(null); // Remove lore
sold.setItemMeta(defaultMeta);
}
player.getInventory().addItem(sold);
player.sendMessage(ChatColor.GREEN + "You bought " + sold.getType() + " for $" + price);
} else {
player.sendMessage(ChatColor.RED + "You don't have enough money to buy this.");
} }
// Create a new ItemStack with the default meta
ItemStack sold = new ItemStack(clickedItem.getType(), clickedItem.getAmount());
// Reset item meta (default name, no lore)
ItemMeta defaultMeta = sold.getItemMeta();
if (defaultMeta != null) {
defaultMeta.setDisplayName(null); // Reset to default name
defaultMeta.setLore(null); // Remove lore
sold.setItemMeta(defaultMeta);
}
// Check if player's inventory has space before proceeding
HashMap<Integer, ItemStack> leftover = player.getInventory().addItem(sold);
if (!leftover.isEmpty()) {
// If there are leftover items, inventory is full
player.sendMessage(ChatColor.RED + getConfigLine("inventory_full", "Your inventory is full! Purchase failed."));
return;
}
// If inventory had space, finalize the purchase
economy.withdrawPlayer(player, price);
player.sendMessage(ColorKey("&aYou bought &b" + clickedItem.getAmount() + " &7" +
String.valueOf(sold.getType()).toLowerCase().replace("_", " ") +
"&a for &6$" + price));
} }
} }
} }
public String ColorKey(String t) {
char searchChar = '&'; // Character to search for
char replacementChar = '§'; // Character to replace with
StringBuilder sb = new StringBuilder(t);
// Search for the character in the StringBuilder
int index = sb.indexOf(String.valueOf(searchChar));
// Replace the character if found
for (int j = 0; j < sb.length(); j++) {
if (sb.charAt(j) == searchChar) {
sb.setCharAt(j, replacementChar);
}
}
return sb.toString();
}
public String getConfigLine(String source, String dfault) {
plugin.reloadConfig();
return config != null ? config.getString(source, dfault) : dfault;
}
// Helper method to retrieve the price of an item (can be customized to use a config file for prices) // Helper method to retrieve the price of an item (can be customized to use a config file for prices)
private double getPrice(ItemStack item) { private double getPrice(ItemStack item) {
for (String category : config.getConfigurationSection("shop.categories").getKeys(false)) { for (String category : config.getConfigurationSection("shop.categories").getKeys(false)) {

View File

@ -12,5 +12,6 @@ shop:
material: OAK_PLANKS material: OAK_PLANKS
price: 15 price: 15
display_name: "Oak Planks" display_name: "Oak Planks"
poor: "you're poor..."
pagination: pagination:
max_items_per_page: 45 # Max items to display per page max_items_per_page: 45 # Max items to display per page