InventoryClickEvent does not work

Discussion in 'Plugin Development' started by NimsTail, Jul 9, 2021.

Thread Status:
Not open for further replies.
  1. Offline

    NimsTail

    Hello!

    I am still trying to make a custom auction plugin and I have another problem.
    I tried many options, but it still does not work. When I click on the item, it does not open a new menu, I can grab it and put it in my inventory.

    GUI.class:
    Code:
    public class GUI {
    
        public static Inventory MainMenu = Bukkit.createInventory(null, 27, "Main menu");
    
        static {
    
            //Items
            ItemStack item = new ItemStack(Material.SUNFLOWER);
            ItemMeta im = item.getItemMeta();
            im.setLore(Arrays.asList("Purchase or sell", "stuff here!"));
            im.setDisplayName(ChatColor.YELLOW + "Shop");
    
            item.setItemMeta(im);
    
            //ItemSettings
            MainMenu.setItem(13, new ItemStack(item));
    
        }
    
    
    
        public static Inventory Category = Bukkit.createInventory(null, 27, "Choosing category");
            static {
                ItemStack item1 = new ItemStack(Material.APPLE);
                ItemMeta im1 = item1.getItemMeta();
                assert im1 != null;
                im1.setDisplayName(ChatColor.GREEN + "Products");
                item1.setItemMeta(im1);
    
                List<String> Products = new ArrayList<>();
                Products.add("All products are shown there!");
                im1.setLore(Products);
    
                Category.setItem(12, new ItemStack(item1));
    
    
    
                ItemStack item2 = new ItemStack(Material.STONE_HOE);
                ItemMeta im2 = item2.getItemMeta();
                assert im2 != null;
                im2.setDisplayName(ChatColor.LIGHT_PURPLE + "Services");
                item2.setItemMeta(im2);
    
                List<String> Services = new ArrayList<>();
                Services.add("All services are shown there!");
                im2.setLore(Services);
    
                Category.setItem(13, new ItemStack(item2));
    
    
    
                ItemStack item3 = new ItemStack(Material.BRICKS);
                ItemMeta im3 = item3.getItemMeta();
                assert im3 != null;
                im3.setDisplayName(ChatColor.RED + "Immovable property");
                item3.setItemMeta(im3);
    
                List<String> Property = new ArrayList<>();
                Property.add("Immovable property is shown there!");
                im3.setLore(Property);
    
                Category.setItem(14, new ItemStack(item3));
    
            }
    
        public static Inventory Example = Bukkit.createInventory(null, 27, "Random");
    
    
        }
    GUIInteraction.class:
    Code:
    public class GUIInteraction implements Listener {
    
    @EventHandlerpublic void onInventoryClick(InventoryClickEvent e) {
    Player player = (Player) e.getWhoClicked(); if (e.getView().getTitle().contains("Main menu")) {
    if (e.getCurrentItem() != null) {
    e.setCancelled(true); if (e.getCurrentItem().getType() == Material.SUNFLOWER) {
    player.openInventory(GUI.Category);}
    
    
    }
    }
    }
    }
    
    I open this (MainMenu) gui using command /shop:
    Code:
    public class Info implements CommandExecutor {
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            JavaPlugin.getPlugin(Main.class);
            if (sender instanceof Player) {
                Player player = (Player) sender;
    
                if (cmd.getName().equals("shop")) {
                    player.openInventory(GUI.MainMenu);
    
    
                } else {
                    System.out.println("You are not a player!");
                }
    
    
            }
            return false;
    
    
        }
    
    }
    Will be grateful for any help!
     
  2. Offline

    Strahan

    Well, yea, you're only acting on the Main Menu in the GUIInteraction.

    Spigot doesn't know how to do anything with your custom GUI unless you specifically tell it. You need to have a handler in your interaction class for the submenus as well.

    PS I'd also write that out a little differently. Well, what I'd really do is make it config driven and make a special GUI object for handling everything but to keep it simple and in the framework you have shown, I'd write it thus:
    Code:
    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) {
      if (e.getCurrentItem() == null) return;
    
      Player player = (Player) e.getWhoClicked();
    
      switch (ChatColor.stripColor(e.getView().getTitle().toLowerCase())) {
      case "main menu":
        e.setCancelled(true);
        menuMain(player, e.getCurrentItem());
        break;
      
      case "choosing category":
        e.setCancelled(true);
        menuCategory(player, e.getCurrentItem());
        break;
      }
    }
    
    private void menuMain(Player player, ItemStack selected) {
      switch (selected.getType()) {
      case SUNFLOWER:
        player.openInventory(GUI.Category);
        break;
      
      default:
        return;
      }
    }
    
    private void menuCategory(Player player, ItemStack selected) {
      switch (selected.getType()) {
      case APPLE:
        player.sendMessage("You want products");
        break;
      
      case STONE_HOE:
        player.sendMessage("You want services");
        break;
      
      case BRICKS:
        player.sendMessage("You want immovable property");
        break;
      
      default:
        return;
      }
    }
    IMO it's much cleaner to break each menu out to its own method. Event methods should not be doing any heavy lifting, though that's just my personal preference.
     
    Last edited: Jul 9, 2021
  3. Offline

    NimsTail

    Oh, I see! Thank you! Yeah, it is much better to use switches.
    But seems like
    (ChatColor.stripColor(e.getView().getTitle().toLowerCase()))
    does not work properly. Clicks are not cancelled. Is there alternative to getTitle()? Because it does not work for me at all with no reason.
    UPD: Just found out that does not work the whole method with InventoryClick
     
    Last edited: Jul 10, 2021
Thread Status:
Not open for further replies.

Share This Page