Deny double clicks in Inventory

Discussion in 'Spigot Plugin Development' started by TomDev, Apr 30, 2017.

  1. Hi, I would like create an inventory but I have a problem

    When I double-click on an object already present in the inventory, I retrieve all the objects identical to the one I just clicked

    Before
    [​IMG]

    After
    [​IMG]

    Code

    Code (Text):
    if(inv.getName().equals("§cTest")){
                e.setCancelled(true);

                if(iteminv.isSimilar(getBookManager().getBasicBook().getBasic())){
                    getBookManager().getBasicBook().give((Player) e.getWhoClicked());
                } else
                if(iteminv.isSimilar(getBookManager().getRareBook().getBasic())){
                    getBookManager().getRareBook().give((Player) e.getWhoClicked());
                } else
                if(iteminv.isSimilar(getBookManager().getLegendaryBook().getBasic())){
                    getBookManager().getLegendaryBook().give((Player) e.getWhoClicked());
                } else
                if(iteminv.isSimilar(getBookManager().getDivinBook().getBasic())){
                    getBookManager().getDivinBook().give((Player) e.getWhoClicked());
                }
            }
    Inventory Initi:
    Code (Text):
            InventoryBuilder inventoryBuilder = new InventoryBuilder();
            inventoryBuilder.setName("§cTest");
            inventoryBuilder.setSize(9);
            inventoryBuilder.getInventoryContainers().full(new ItemStackBuilder().setMaterial(Material.STAINED_GLASS_PANE).setData((byte) 8).build());
            inventoryBuilder.getInventoryContainers().setItem(1, getBookManager().getBasicBook().getBasic());
            inventoryBuilder.getInventoryContainers().setItem(3, getBookManager().getRareBook().getBasic());
            inventoryBuilder.getInventoryContainers().setItem(5, getBookManager().getLegendaryBook().getBasic());
            inventoryBuilder.getInventoryContainers().setItem(7, getBookManager().getDivinBook().getBasic());
            this.generate = inventoryBuilder.build();
     
  2. sothatsit

    Patron

    If you cancel all InventoryClickEvents for your inventory you should have no issues.
     
    • Agree Agree x 1
  3. Code (Text):
    if(inv.getName().equals("§Test")){
                e.setCancelled(true);

                if(iteminv.isSimilar(getBookManager().getBasicBook().getBasic())){
                    getBookManager().getBasicBook().give((Player) e.getWhoClicked());
                } else
                if(iteminv.isSimilar(getBookManager().getRareBook().getBasic())){
                    getBookManager().getRareBook().give((Player) e.getWhoClicked());
                } else
                if(iteminv.isSimilar(getBookManager().getLegendaryBook().getBasic())){
                    getBookManager().getLegendaryBook().give((Player) e.getWhoClicked());
                } else
                if(iteminv.isSimilar(getBookManager().getDivinBook().getBasic())){
                    getBookManager().getDivinBook().give((Player) e.getWhoClicked());
                }
            }
    I already cancelled all InventoryClickEvent for Test inventory
    Code (Text):
    if(inv.getName().equals("§Test")){
                e.setCancelled(true);
    And I juste have another condition:

    Code (Text):
            if(e.getClickedInventory() == null) return;
     
  4. sothatsit

    Patron

    It may be an issue in the server not updating the inventory of the player after you have cancelled the event. Run this after you cancel the event:
    Code (Text):
    final HumanEntity player = event.getWhoClicked();
    Bukkit.getScheduler().runTask(pluginInstance, () -> { player.updateInventory(); });
    This will force an update of the player's inventory after the event has been cancelled.
     
  5. Out of curiousity, why did you put that in a scheduler?
     
  6. sothatsit

    Patron

    So that it runs after the event has been handled by the server. Sometimes issues can occur if it is run in the listener as the server has not reverted the changes the player made to the inventory and therefore updateInventory will send them the same broken inventory.
     
    • Informative Informative x 1
  7. Doestn't work
     
  8. Post whole code please.
     
  9. sothatsit

    Patron

    That is so incredibly vague.
     
  10. Exactly, that's why I asked him to show all code, because a simple 'doesn't work' gives us no way to help...
     
    • Agree Agree x 1
  11. I have the same result code :

    Code (Text):
    if(inv.getName().equals("§cTest")){
                e.setCancelled(true);
                Bukkit.getScheduler().runTask(getTest(), () -> { ((Player) e.getWhoClicked()).updateInventory(); });

                if(iteminv.isSimilar(getBookManager().getBasicBook().getBasic())){
                    getBookManager().getBasicBook().give((Player) e.getWhoClicked());
                } else
                if(iteminv.isSimilar(getBookManager().getRareBook().getBasic())){
                    getBookManager().getRareBook().give((Player) e.getWhoClicked());
                } else
                if(iteminv.isSimilar(getBookManager().getLegendaryBook().getBasic())){
                    getBookManager().getLegendaryBook().give((Player) e.getWhoClicked());
                } else
                if(iteminv.isSimilar(getBookManager().getDivinBook().getBasic())){
                    getBookManager().getDivinBook().give((Player) e.getWhoClicked());
                }
            }
     
  12. I havent tested it, but it should be possibly to detect with " if(event.getClick() == event.getClick().DOUBLE_CLICK){}"
     
  13. sothatsit

    Patron

    Is there an error, does it not compile or does it fail silently?
     
  14. No compilation error and no consol error
     
  15. sothatsit

    Patron

    Broadcast a message to ensure that section of code is being triggered.

    Something like
    Code (Text):
    Bukkit.broadcastMessage("It's working");
    If that doesn't work, post your whole listener, not just the part of it you think is relevant. Also, make sure your events are registered, that is a common mistake.
     
  16. When I double click in my inventory, the event does not trigger because when I click on my inventory I do not click on the inventory "Test"
     
  17. sothatsit

    Patron

    You used InventoryClickEvent#getClickedInventory() instead of InventoryClickEvent#getInventory(). If you had shown us your whole listener in the first place this could have been solved long ago :\.
     
    • Agree Agree x 1
  18. Thanks !