Item Amount Multiplier Bug in GUI

I have this GUI here where it has an item in the middle that has been given from another class before, and that item amount can be increased or decreased with buttons. When the player has the amount they want, they can click the diamonds and get the item if they have enough doubloons. But, I’m having the problem where when the player opens the GUI and lets say wants one of that item, and then clicks confirm (aka the diamond), it then gives them 1 item and the GUI closes. However, if they open the GUI again and lets say they again want one item, so they click confirm again and now in their inventory they have a total of 3 of that item. This keeps increasing until the server has been restarted, and then it starts all over again. Basically, it means it gets the last purchase plus the new one, and that’s what the player gets, and it keeps going up.

Here is my code:

package pro.jabo.jabo.gui;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import pro.jabo.jabo.utils.PlayerConfigUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class PurchaseItemGUI implements Listener {

    private final Inventory inventory;
    private int amount = 1;
    private final PlayerConfigUtils playerConfigUtils;
    private final UUID shopOwnerUUID;
    private final double pricePerItem;
    private final String shopName;
    private boolean isConfirming = false; 

    public PurchaseItemGUI(JavaPlugin plugin, ItemStack item, Player player, UUID shopOwnerUUID, double pricePerItem, String shopName) {
        this.playerConfigUtils = new PlayerConfigUtils(plugin);
        this.shopOwnerUUID = shopOwnerUUID;
        this.pricePerItem = pricePerItem;
        this.shopName = shopName;

        this.inventory = Bukkit.createInventory(null, 27, ChatColor.GOLD + "Purchase " + item.getType());

        Bukkit.getPluginManager().registerEvents(this, plugin);

        ItemStack itemToPurchase = new ItemStack(item);
        itemToPurchase.setAmount(amount);
        ItemMeta meta = itemToPurchase.getItemMeta();
        meta.setDisplayName(ChatColor.GREEN + item.getType().name());
        List<String> lore = new ArrayList<>();
        lore.add(ChatColor.WHITE + "Amount: " + amount);
        lore.add(ChatColor.WHITE + "Total Price: " + pricePerItem * amount);
        meta.setLore(lore);
        itemToPurchase.setItemMeta(meta);

        inventory.setItem(13, itemToPurchase);


        setControlButtons();
    }

    private void setControlButtons() {
        inventory.setItem(10, createButton(Material.REDSTONE, ChatColor.RED + "-10"));
        inventory.setItem(11, createButton(Material.REDSTONE, ChatColor.RED + "-1"));
        inventory.setItem(15, createButton(Material.EMERALD, ChatColor.GREEN + "+1"));
        inventory.setItem(16, createButton(Material.EMERALD, ChatColor.GREEN + "+10"));
        inventory.setItem(22, createButton(Material.DIAMOND, ChatColor.BLUE + "CONFIRM"));
    }

    private ItemStack createButton(Material material, String name) {
        ItemStack item = new ItemStack(material);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(name);
        item.setItemMeta(meta);
        return item;
    }

    public Inventory getInventory() {
        return inventory;
    }

    public String getShopName() {
        return shopName;
    }

    public UUID getShopOwnerUUID() {
        return shopOwnerUUID;
    }

    @EventHandler
    public void onButtonClick(InventoryClickEvent event) {
        if (!event.getView().getTitle().startsWith(ChatColor.GOLD + "Purchase ")) {
            return;
        }

        event.setCancelled(true);

        Player player = (Player) event.getWhoClicked();
        ItemStack clickedItem = event.getCurrentItem();

        if (clickedItem == null || clickedItem.getType() == Material.AIR) {
            return;
        }

        if (clickedItem.getType() == Material.REDSTONE && clickedItem.getItemMeta().getDisplayName().equals(ChatColor.RED + "-10")) {
            amount = Math.max(1, amount - 10);
        } else if (clickedItem.getType() == Material.REDSTONE && clickedItem.getItemMeta().getDisplayName().equals(ChatColor.RED + "-1")) {
            amount = Math.max(1, amount - 1);
        } else if (clickedItem.getType() == Material.EMERALD && clickedItem.getItemMeta().getDisplayName().equals(ChatColor.GREEN + "+1")) {
            amount++;
        } else if (clickedItem.getType() == Material.EMERALD && clickedItem.getItemMeta().getDisplayName().equals(ChatColor.GREEN + "+10")) {
            amount += 10;
        } else if (clickedItem.getType() == Material.DIAMOND && clickedItem.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "CONFIRM")) {
            if (!isConfirming) {
                isConfirming = true;
                confirmPurchase(player);
                isConfirming = false;
            }
        }

        updateGUI();
    }

    private void updateGUI() {
        ItemStack item = inventory.getItem(13);
        if (item != null) {
            ItemMeta meta = item.getItemMeta();
            if (meta != null) {
                List<String> lore = new ArrayList<>();
                lore.add(ChatColor.WHITE + "Amount: " + amount);
                lore.add(ChatColor.WHITE + "Total Price: " + pricePerItem * amount);
                meta.setLore(lore);
                item.setItemMeta(meta);
                inventory.setItem(13, item);
            }
        }
    }

    private void confirmPurchase(Player player) {
        double totalCost = pricePerItem * amount;
        FileConfiguration playerConfig = playerConfigUtils.getPlayerConfig(player.getUniqueId());
        int playerDoubloons = playerConfig.getInt("doubloons");

        if (playerDoubloons < totalCost) {
            player.sendMessage(ChatColor.RED + "You do not have enough doubloons.");
            player.closeInventory();
            return;
        }

        playerConfig.set("doubloons", playerDoubloons - totalCost);
        FileConfiguration shopOwnerConfig = playerConfigUtils.getPlayerConfig(shopOwnerUUID);
        int shopOwnerDoubloons = shopOwnerConfig.getInt("doubloons");
        shopOwnerConfig.set("doubloons", shopOwnerDoubloons + totalCost);

        ItemStack purchasedItem = new ItemStack(inventory.getItem(13));
        purchasedItem.setAmount(amount);
        player.getInventory().addItem(purchasedItem);


        player.sendMessage(ChatColor.GREEN + "Purchase successful!");

        playerConfigUtils.savePlayerConfig(player.getUniqueId(), playerConfig);
        playerConfigUtils.savePlayerConfig(shopOwnerUUID, shopOwnerConfig);
        player.updateInventory();

        amount = 1;
        updateGUI();

        player.closeInventory();
    }
}

I’ve tried many different ways of resetting the amount, but no matter what, it still gives the last purchase plus the new purchase to the player every time!

2

The problem was just the way the GUI was being closed i got it working now!!

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật