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!
The problem was just the way the GUI was being closed i got it working now!!