[Solved] InventoryClickEvent only firing on second click

Discussion in 'Spigot Plugin Development' started by BlackBeltPanda, May 1, 2015.

  1. Spigot version: git-Spigot-2ec6f06-7722428 (MC: 1.8.3) (Implementing API version 1.8.3-R0.1-SNAPSHOT)

    Having a strange issue with "InventoryClickEvent" where it only runs on the second click. I.E. - When I click an object in an inventory, it binds to my cursor and I can move it outside the inventory or elsewhere in the inventory. When I click again to move the item, the "InventoryClickEvent" fires.

    Any idea what's going on and why it's not firing for the first click?

    Here's a code example:

    Code (Text):

        public void onInventoryClick(InventoryClickEvent event){
            if (!event.isCancelled()){
                if (event.getInventory().getName().equalsIgnoreCase(ChatColor.BLUE + "Custom Inventory")){
                    event.setCancelled(true);
                    Player player = (Player) event.getWhoClicked();
                    player.closeInventory();
                }
            }
        }
     
    In the above example, the inventory only closes when I "drop" the item on the second click. I also tried adding a delay of 1 tick to the "player.closeInventory()", but it didn't help. Strangely, it DOES close on the first click for only two items in the inventory, as well as empty slots, but not for the rest.
     
    #1 BlackBeltPanda, May 1, 2015
    Last edited: May 1, 2015
  2. Try this code

    Code (Text):
    public void onInventoryClick(InventoryClickEvent event){
            if (event.isCancelled()) return;
            final Player player = (Player) event.getWhoClicked();
            if (event.getInventory().getName().equalsIgnoreCase("§rInventory")){
                event.setCancelled(true);
                player.closeInventory();
            }
        }
     
  3. The inventory name doesn't change between the two clicks, though.
     
  4. This should work, I have tried it.
    Code (Text):
    public void onInventoryClick(InventoryClickEvent event){
            if (!event.isCancelled()) {
            Player player = (Player) event.getWhoClicked();
            if (event.getInventory().getName().equalsIgnoreCase("§rInventory")){
                event.setCancelled(true);
                player.closeInventory();
                 }
            }
        }
     
  5. It worked for two items in the inventory (and for slots with no items) but it still doesn't fire the event until the second click for the other items in the inventory. Very strange. o_O

    Edit: I've updated my original post with the latest code.

    Also, if it helps, the items I'm trying to click on are various spawn eggs.
     
    #5 BlackBeltPanda, May 1, 2015
    Last edited: May 1, 2015
  6. No errors after the second click?
     
  7. No errors. I tried this:

    Code (Text):
    public void onInventoryClick(InventoryClickEvent event){
        Bukkit.broadcastMessage("Clicked");
    }
    The message only broadcasted with 2 of the items. It never ran when I clicked on any of the other items in the inventory until I clicked them a second time. It doesn't seem to fire at all. =/
     
  8. I'm not sure, I never tried it before but... no wait, that makes no sense... Maybe still try to remove that if-sentence.. :?
    • What else does something with the InventoryClickEvent?
    • Maybe search your project for the number 2
     
  9. gigosaurus

    Supporter

    What type of inventory are you clicking on?
     
  10. Tried removing the cancel check; didn't help.
    Nothing in my plugin that limits the inventory to 2 of anything.

    Custom inventory created like so:

    Code (Text):
    public void customMenu(final Player player){
        final Inventory inv = Bukkit.createInventory(null, 54, ChatColor.BLUE + "Custom Inventory");
        ItemStack pig = new ItemStack(Material.MONSTER_EGG, 1, EntityType.PIG.getTypeId());
        ItemMeta pigMeta = pig.getItemMeta();
        ItemStack chicken = new ItemStack(Material.MONSTER_EGG, 1, EntityType.CHICKEN.getTypeId());
        ItemMeta chickenMeta = chicken.getItemMeta();
        ItemStack cow = new ItemStack(Material.MONSTER_EGG, 1, EntityType.COW.getTypeId());
        ItemMeta cowMeta = cow.getItemMeta();
        pigMeta.setDisplayName(ChatColor.AQUA + "Pig");
        pigMeta.setLore(Arrays.asList("¥300"));
        pig.setItemMeta(pigMeta);
        chickenMeta.setDisplayName(ChatColor.AQUA + "Chicken");
        chickenMeta.setLore(Arrays.asList("¥400"));
        chicken.setItemMeta(chickenMeta);
        cowMeta.setDisplayName(ChatColor.AQUA + "Cow");
        cowMeta.setLore(Arrays.asList("¥500"));
        cow.setItemMeta(cowMeta);
        inv.setItem(0,pig);
        inv.setItem(1,chicken);
        inv.setItem(2,cow);
        plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
            public void run() {
                player.openInventory(inv);
            }
        }, 1L);
    }
    With that inventory above, the pig and cow will fire the "InventoryClickEvent", but the sheep won't.
     
  11. gigosaurus

    Supporter

    Using that code I got the desired results, which means that there must be something else stopping it.
    Do you have any other plugins on the server? Do you have any other EventHandlers for the InventoryClickEvent?
     
  12. Problem solved:
    Turns out in "server.properties", the value of "network-compression-threshold" had reset on one of my servers. I originally had them both set to "0", but one reset to "256". When both are different, strange things tend to happen like enchanted bows not working, InventoryClickEvents not firing, chat getting cut off, etc.