Tats Ou
  1. Adjustable Purchase and Sale Quantity

In the item interaction menu and transaction flow:

  • Add + and − buttons to increase or decrease the quantity.
  • The selected quantity must be clearly displayed.
  • Update the total purchase cost or sale earnings immediately whenever the quantity changes.
  • Never allow the quantity to go below 1.
  • For purchases, prevent selecting more items than the player's balance and inventory capacity allow.
  • For sales, prevent selecting more items than the player actually has available in their inventory.
  • The final confirmation screen must display the exact selected quantity and total price.
  • The transaction must use the confirmed quantity, not a different or outdated value.
  • Add a reasonable maximum quantity to prevent exploits and performance issues.
  • The quantity selector must work correctly with both normal items and different item stack sizes. Prove .jar file
Kodari AI
KodariOct 30 at 10:43 am 2
QuantityTradeGUI
A Bukkit plugin providing a GUI-based trading system with adjustable purchase and sale quantities. Features an interactive item menu with plus and minus buttons to modify transaction amounts, live display of selected quantity, and real-time updates of total purchase cost or sale earnings. Enforces minimum quantity of one, validates against player balance and inventory capacity for purchases, and checks available items for sales. Includes a maximum quantity cap to prevent exploits, a confirmation screen showing exact quantity and total price, and executes transactions using the confirmed quantity. Supports both normal items and varying stack sizes through a quantity selector system built on the inventory GUI framework.
  1. Create the plugin scaffold and configurable item catalog.

  2. Add buy/sell quantity selection with live totals, capacity/stock limits, and exact-quantity confirmation.

  3. Persist player balances and provide /shop plus /shop balance.

Created QuantityShop with configurable items, buy/sell quantity controls, live totals, inventory and balance limits, and a confirmation screen that uses the selected quantity. Player balances are saved between restarts.

To get the .jar, click Compile in the top bar, then Compile in the window. When it succeeds, click Download on the build.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
settings.gradle
Code
7 703
src/main/java/com/kodari/quantityshop/QuantityShopPlugin.java
package com.kodari.quantityshop;

import com.cryptomorin.xseries.XMaterial;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public final class QuantityShopPlugin extends JavaPlugin implements CommandExecutor {
    private final Map<UUID, BigDecimal> balances = new HashMap<>();
    private final List<ShopItem> shopItems = new ArrayList<>();
    private File balanceFile;
    private ShopMenu shopMenu;

    @Override
    public void onEnable() {
        saveDefaultConfig();
        loadShopItems();
        loadBalances();
        shopMenu = new ShopMenu(this);
        getServer().getPluginManager().registerEvents(shopMenu, this);
        getCommand("shop").setExecutor(this);
    }

    @Override
    public void onDisable() {
        saveBalances();
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player player)) {
            sender.sendMessage("This command can only be used in-game.");
            return true;
        }
        if (args.length == 1 && args[0].equalsIgnoreCase("balance")) {
            player.sendMessage(ChatColor.GREEN + "Your balance: " + formatMoney(getBalance(player)));
            return true;
        }
        if (args.length != 0) {
            player.sendMessage(ChatColor.YELLOW + "Usage: /shop [balance]");
            return true;
        }
        shopMenu.openCatalog(player);
        return true;
    }

    private void loadShopItems() {
        ConfigurationSection items = getConfig().getConfigurationSection("items");
        if (items == null) {
            getLogger().warning("No shop items are configured.");
            return;
        }
        for (String key : items.getKeys(false)) {
            ConfigurationSection section = items.getConfigurationSection(key);
            if (section == null) {
                continue;
            }
            String material = section.getString("material", "");
            ItemStack parsedItem = XMaterial.matchXMaterial(material)
                    .map(XMaterial::parseItem)
                    .orElse(null);
            if (parsedItem == null) {
                getLogger().warning("Skipping shop item '" + key + "': invalid material '" + material + "'.");
                continue;
            }
            String name = section.getString("name", key);
            BigDecimal buyPrice = BigDecimal.valueOf(section.getDouble("buy-price", 0.0))
                    .setScale(2, RoundingMode.HALF_UP);
            BigDecimal sellPrice = BigDecimal.valueOf(section.getDouble("sell-price", 0.0))
                    .setScale(2, RoundingMode.HALF_UP);
            shopItems.add(new ShopItem(key, material, name, buyPrice, sellPrice));
        }
    }

    private void loadBalances() {
        if (!getDataFolder().exists() && !getDataFolder().mkdirs()) {
            getLogger().severe("Could not create the plugin data folder.");
        }
        balanceFile = new File(getDataFolder(), "balances.yml");
        YamlConfiguration data = YamlConfiguration.loadConfiguration(balanceFile);
        for (String key : data.getKeys(false)) {
            try {
                UUID playerId = UUID.fromString(key);
                BigDecimal balance = new BigDecimal(data.getString(key, "0.00"));
                balances.put(playerId, balance.max(BigDecimal.ZERO).setScale(2, RoundingMode.HALF_UP));
            } catch (IllegalArgumentException exception) {
                getLogger().warning("Ignoring invalid balance entry '" + key + "'.");
            }
        }
    }

    private void saveBalances() {
        if (balanceFile == null) {
            return;
        }
        YamlConfiguration data = new YamlConfiguration();
        balances.forEach((playerId, balance) -> data.set(playerId.toString(), balance.toPlainString()));
        try {
            data.save(balanceFile);
        } catch (IOException exception) {
            getLogger().severe("Could not save player balances: " + exception.getMessage());
        }
    }

    public List<ShopItem> getShopItems() {
        return shopItems;
    }

    public ShopItem getShopItem(String key) {
        for (ShopItem item : shopItems) {
            if (item.key().equals(key)) {
                return item;
            }
        }
        return null;
    }

    public ItemStack createBaseItem(ShopItem item) {
        return XMaterial.matchXMaterial(item.material()).map(XMaterial::parseItem).orElse(null);
    }

    public BigDecimal getBalance(Player player) {
        return balances.computeIfAbsent(player.getUniqueId(), ignored -> startingBalance());
    }

    public void setBalance(Player player, BigDecimal balance) {
        balances.put(player.getUniqueId(), balance.max(BigDecimal.ZERO).setScale(2, RoundingMode.HALF_UP));
    }

    public int getMaxQuantity() {
        return Math.max(1, Math.min(2304, getConfig().getInt("max-quantity", 2304)));
    }

    public int getBuyCapacity(Player player, ShopItem item) {
        ItemStack baseItem = createBaseItem(item);
        if (baseItem == null) {
            return 0;
        }
        int capacity = 0;
        int maxStackSize = baseItem.getMaxStackSize();
        for (ItemStack stack : player.getInventory().getStorageContents()) {
            if (stack == null) {
                capacity += maxStackSize;
            } else if (stack.isSimilar(baseItem)) {
                capacity += Math.max(0, maxStackSize - stack.getAmount());
            }
        }
        return Math.min(capacity, getMaxQuantity());
    }

    public int getSellCapacity(Player player, ShopItem item) {
        ItemStack baseItem = createBaseItem(item);
        if (baseItem == null) {
            return 0;
        }
        int count = 0;
        for (ItemStack stack : player.getInventory().getStorageContents()) {
            if (stack != null && stack.isSimilar(baseItem)) {
                count += stack.getAmount();
            }
        }
        return Math.min(count, getMaxQuantity());
    }

    public boolean giveItems(Player player, ShopItem item, int amount) {
        ItemStack baseItem = createBaseItem(item);
        if (baseItem == null || getBuyCapacity(player, item) < amount) {
            return false;
        }
        ItemStack[] contents = player.getInventory().getStorageContents();
        int remaining = amount;
        int maxStackSize = baseItem.getMaxStackSize();
        for (int slot = 0; slot < contents.length && remaining > 0; slot++) {
            ItemStack stack = contents[slot];
            if (stack != null && stack.isSimilar(baseItem)) {
                int added = Math.min(remaining, maxStackSize - stack.getAmount());
                stack.setAmount(stack.getAmount() + added);
                remaining -= added;
            }
        }
        for (int slot = 0; slot < contents.length && remaining > 0; slot++) {
            if (contents[slot] == null) {
                int added = Math.min(remaining, maxStackSize);
                ItemStack stack = baseItem.clone();