Solved event.setCancelled(true); not cancelling event

Discussion in 'Plugin Development' started by thechrisanator, Nov 29, 2018.

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

    thechrisanator

    sometimes, when Im clicking the inventory with a stack of sand or something, the stack of sand will get placed into the inventory, and I'm not sure why.
    Am I not canceling the event properly?

    Code:
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
           
            int cooldowntimer = 30; //(30 minutes)
           
            Player player = (Player) event.getWhoClicked();
            if (event.getClickedInventory() != null) {
                if (event.getClickedInventory().getTitle().equals(ChatColor.GREEN.toString() + ChatColor.BOLD.toString() + "The Grand Minechange")) {
                    if (player.getItemOnCursor().getType() == Material.AIR) {
                        System.out.println("tas");
                        ItemStack clickstack = event.getCurrentItem();
                        if (clickstack.getType() != Material.AIR) {
                            System.out.println(clickstack.getType());
                            if (clickstack.getItemMeta().hasDisplayName()) {
                                if (clickstack.getItemMeta().getDisplayName().equals(ChatColor.YELLOW.toString() + ChatColor.BOLD.toString() + "Create Request")) {
                                    long timeleft = System.currentTimeMillis() - main.getCooldown(player.getUniqueId());
                                    if (timeleft > cooldowntimer * 60000) {
                                        Inventory crinv = createrequest.createInventory();
                                        event.setCancelled(true);
                                        player.openInventory(crinv);
                                    } else {
                                        player.sendMessage(main.prefix + ChatColor.RED + "You must wait before making another request!\nTime left:" + Integer.toString((int)(timeleft / 1000)) + 's');
                                    }
                                }
                            }
                        }
                    }
                    event.setCancelled(true);
                }
            }
        }
       
    }
    turns out I wasnt accounting for InventoryDragEvent

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 29, 2018
Thread Status:
Not open for further replies.

Share This Page