Solved InventoryClickEvent don't work

Discussion in 'Spigot Plugin Development' started by parpar8090, Feb 14, 2020.

  1. This is my code:
    Code (Java):
    @EventHandler
          public void onInventoryClick(InventoryClickEvent e) {
            Player p = (Player)e.getWhoClicked();
            ItemStack clickedItem = e.getCurrentItem();
            p.sendMessage(e.getInventory() + " =? " + getInventory());
            if (e.getInventory().equals(getInventory()) && p != null) {
              if (inv.getHolder() != this)
                return;
              e.setCancelled(true);
              if (clickedItem == null || clickedItem.getType() == Material.AIR)
                return;
             
             
              for (String perk : pl.pc.getSection().getKeys(false)) {
                if (e.getRawSlot() == pl.pc.getGuiSlot(perk) &&
                  pl.pc.isVisible(perk)) {
                    if(pl.udc.has(p, perk) == false) {
                        if (canPurchase(p, pl.pc.getPerkPrice(perk))) {
                            pl.udc.setPerk(p, perk, true);
                        }
                    }
                    else if(pl.udc.has(p, perk) == true){
                        p.sendMessage("§cThis perk has already Purchased!");
                    }
                 }
              }
              if (e.getRawSlot() == pl.gc.getCloseBtnSlot())
                p.closeInventory();
            }
          }
    This code don't output ANY errors at all. The thing is that the
    Code (Java):
    p.sendMessage(e.getInventory() + " =? " + getInventory());
    don't output the same thing.

    My main file contains:
    Code (Java):
    public void onEnable() {
            setupConfigs();

            getCommand("command1").setExecutor(new GUIOpenCmd(this));
            getCommand("command2").setExecutor(new StatsCmd(this));
            getCommand("command3").setExecutor(new StatsCmd(this));
            getServer().getPluginManager().registerEvents(this, this);
            getServer().getPluginManager().registerEvents(new AddCoins(this), this);
            getServer().getPluginManager().registerEvents(new GUIMenu(this), this);
        }
     
  2. What exactly are you trying to achieve with the output message?

    Also I believe this is redundant
    Code (Java):
    if (e.getInventory().equals(getInventory())
     
  3. Output a debug msg. It shows they are not the same thing
     
  4. yeah but I don't want to cancel the event for ALL the inventories in the server. That's why I need to specify and match the inventories, then it was working by cancelling only this GUI and not ALL the GUIs.
     
  5. Why don't you just match the title of the inventory? For example:

    Code (Java):
    public void clickGoToMinecartSize(InventoryClickEvent event) {
             String inv = event.getView().getTitle();
             Player p = (Player)event.getWhoClicked();
             ItemStack clicked = event.getCurrentItem();
            if (clicked != null && inv.equals("EntityInventories") && clicked.getType() == Material.MINECART && clicked.getItemMeta().getDisplayName().equals("§3Minecart Size") && clicked != null) {
                p.closeInventory();
                event.setCancelled(true);
                SecCommands.newMinecartSizes(p);
            }
        }
    I use this on one of my plugins.
     
  6. Sorry, forgot to add:
    Code (Java):
    private static Inventory inv;
    It's now fixed
     
  7. I don't think it's a good idea, what if someone has the same idea as me? and the title is the same?
     
  8. Same Idea and you happen to have their plugin and your own loaded onto the same server operating at the same time? I suppose that could be an issue...
     
  9. Code (Text):
    private Inventory crucibleInv;
    Followed by
    Code (Text):
    @EventHandler
        public void clickInventarCruciblu(InventoryClickEvent e) {
            if(e.getClickedInventory()==null) {
                return;
            }
            if(e.getClickedInventory().equals(crucibleInv)) {
                if(e.getCurrentItem()==null) {
                    return;
                }...
    Want to do it easy way? have them both in same class
    Wanna be fancy make a class holding all the inventory and get instance of it...

    The inventory will have custom stuff,doesnt matter if another one has same title,it will check its contents if they do and so on.. we all know how equals go