Weird Error with InventoryClick

Discussion in 'Spigot Plugin Development' started by Snowmite, May 3, 2017.

  1. I am trying to make it so that if you click an item in your inventory, it won't rebuy the item, but it keeps on doing it. I did the checks but it is still buying the item, and you're only supposed to buy it from the menu, not from your inventory.

    Code (Text):
        @EventHandler(ignoreCancelled=true)
        public void onClick(InventoryClickEvent event) {
            if (!(event.getWhoClicked() instanceof Player)) return;
            Player p = (Player) event.getWhoClicked();
            if (event.getInventory() == null || !users.containsKey(p.getUniqueId())) return;
            ScrollerInventory inv = users.get(p.getUniqueId());
            if (event.getCurrentItem() == null || event.getCurrentItem().getItemMeta() == null ||
                    event.getCurrentItem().getItemMeta().getDisplayName() == null ||
                    Objects.equals(event.getInventory().getName(), p.getInventory().getName()) ||
                    event.getInventory().equals(p.getInventory()) ||
                    event.getInventory().getName().equals(Core.getInstance().getConfig().getString("options.food-central.menu-name").replace("&", "ยง"))) {
            }
            if (event.getCurrentItem().getItemMeta().getDisplayName().equals(nextPageName)) {
                event.setCancelled(true);
                if (inv.currpage >= inv.pages.size() - 1) {
                    return;
                }
                inv.currpage += 1;
                p.openInventory(inv.pages.get(inv.currpage));
            }
            if (event.getCurrentItem().getItemMeta().getDisplayName().equals(previousPageName)) {
                event.setCancelled(true);
                if (inv.currpage > 0) {
                    inv.currpage -= 1;
                    p.openInventory(inv.pages.get(inv.currpage));
                }
            }
            for (Food food : FoodManager.getFoods()) {
                String name = ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName());
                if (name.contains(food.getName())) {
                    event.setCancelled(true);
                    Bukkit.getPluginManager().callEvent(new FoodBuyEvent(p, food, food.getCost()));
                }
            }
        }
     
  2. Could you clarify? Are you using a shop within the player's inventory, or are you opening a totally seperate inventory for your shop?

    You need to insert a check for which inventory was clicked before you execute your code. For example, if your shop inventory was called "Shop", we could do this:
    Code (Text):
    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) {
            if (e.getClickedInventory().getName().toLowercase().equals("shop") {
                    // your code
            }
            return;
    }

    Edit: sorry for any typos, I'm on my phone. :p