Solved Cancelling InventoryClickEvent sometimes does not work!

Discussion in 'Spigot Plugin Development' started by Sulphate, May 26, 2017.

  1. Hello all,

    I'm making a plugin for someone, and it allows players to buy items from a GUI. All clicks in the inventory are cancelled, but for some reason if I pick up an item on my cursor and repeatedly click the GUI, it fails to cancel the event and the item gets stuck in the GUI.

    Any help appreciated.

    EDIT: On further investigation, it occurs when I click down and then move my cursor even slightly, it stays in the inventory. Is there another event I should be cancelling?

    EDIT 2: Problem solved! Simply cancel the InventoryDragEvent as well.
    #1 Sulphate, May 26, 2017
    Last edited: May 26, 2017
  2. If you could show us your code would be really good, seems that you haven't made the correct checks.
  3. I have made no incorrect checks. This is literally it:
    As stated in my edit, it occurs when the cursor moves while held down. Thanks in advance :)
  4. You should check if the name equals to s Shop than contains firstly, second this cannot do the whole work.
    Try this one, i am not sure if it works because i have long time to check something from an inventory but it might work i think.
    Code (Text):
        public void onInventoryClick(InventoryClickEvent event) {
            if (event.getClickedInventory() == null){
            if (event.getClickedInventory().getName() == null){
            if(!(event.getClickedInventory().getName()).equals(" 's Shop)){
            Player player = (Player) event.getWhoClicked();
            if(event.getCurrentItem() == null || event.getCurrentItem().getType()==Material.AIR||!event.getCurrentItem().hasItemMeta()){
  5. I appreciate your help, but I'm using contains as the title is [playername]'s Shop, not just "s Shop". Sorry if I was unclear about what it actually is, I just showed the first 2 lines, which should cancel the event no matter what happens if it is a click. There is a lot more code to it, but it's irrelevant. It cancels all the events unless I drag, so I'm just testing with cancelling InventoryDragEvent as well, i'll edit this comment with results.

    EDIT: So turns out that dragging does not count as clicking :) cancelling InventoryDragEvent for the inventory fixed the problem. Thanks for trying to help ^-^
    • Like Like x 1
  6. You should use String#endsWith if you know for sure that is how your titles are.
  7. I ran into the same problem. This only happens when you try to put an item into the inventory, not when taking out. What you could do is;

    - Store the user's items and if they do decide to put an item in the inventory; close the window, revert their items and open it again.
    - Do not allow players to interact with any part of the open inventory window, including their own.
    - Check if there's an item on the player's cursor when they try interact with a slot. If so, remove it from their cursor, add it back to their own inventory and cancel the event. The issue might still happen, but less likely since players will have to pickup their item again and move it back to where they want to place it. This way the chance of it happening on accident is smaller.

    I'm using the last solution, since it's not that heavy and people do not break any part of the plugin when placing an item in the other inventory, they just lose it.
    • Funny Funny x 1
  8. I solved it, just cancel the InventoryDragEvent as well :)