Solved Cancel menu action when clicking on bottom inventory

Discussion in 'Plugin Development' started by AppleMen, Nov 27, 2015.

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

    AppleMen

    Hello,

    I made a custom inventory which works fine. However, when I click on the bottom inventory (Brick), it does the same as when I click on the added items in the top inventory (Book). How can I cancel this event?

    [​IMG]

    This is the code I used to add items to my inventory and perform actions:

    Code:
     @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
    
            Menu().getName())) return
    
            if(e.getInventory().getTitle().contains("Vote")) {
                e.setCancelled(true);
    
                if (e.getRawSlot() < e.getInventory().getSize() || e.isShiftClick()) {
                    e.setCancelled(true);
                }
    
                if (e.getCurrentItem() == null) {
                    return;
                }
    
                int sizeOfLinks = instance.getConfig().getConfigurationSection("Vote.Votelinks").getKeys(false).size();
                System.out.println(sizeOfLinks);
    
                for (String s : instance.getConfig().getConfigurationSection("Vote.Votelinks").getKeys(false)) {
                    String serviceName = instance.getConfig().getString("Vote.Votelinks." + s + ".servicename");
                    String serviceUrl = instance.getConfig().getString("Vote.Votelinks." + s + ".url");
    
                    Integer serviceId = new Integer(s);
    
                    if(e.getSlot() == (serviceId - 1)) { // Minus 1. Because slots starts at 0.
                        e.setCancelled(true);
                        p.sendMessage(Messages.Prefixes.VOTE + ChatColor.GREEN + serviceName + ": " + ChatColor.GRAY + serviceUrl);
                        p.closeInventory();
                    }
                }
            }
        }
     
  2. Offline

    Abstract97

    @AppleMen

    Code:java
    1.  
    2. @EventHandler
    3. public void onInvClick(InventoryClickEvent event) {
    4. if(event.getInventory().getTitle().equals("container.crafting")
    5. && event.getRawSlot() > 9) {
    6. event.setCancelled(true);
    7. }
    8. }
    9.  


    We check if the player is clicking in their own inventory, then if the raw slot is greater than 9, we do this because when you open a workbench that inventory is also the "container.crafting" inventory, and that has slots 1-9.
     
  3. Offline

    AppleMen

    Unfortunately the action after the slot check (Line #27 in my first post) still executes when clicking on the brick from the image.
     
  4. Offline

    Abstract97

    @AppleMen Add the code inside the event i showed you to the listener you made at the top, so before you do any of your checking, add the check i made, like so:

    Code:java
    1.  
    2. @EventHandler
    3. public void onInventoryClick(InventoryClickEvent e) {
    4. Player p = (Player) e.getWhoClicked();
    5.  
    6. if(event.getInventory().getTitle().equals("container.crafting")
    7. && event.getRawSlot() > 9) {
    8. e.setCancelled(true);
    9. }
    10.  
    11. Menu().getName())) return
    12.  
    13. if(e.getInventory().getTitle().contains("Vote")) {
    14. e.setCancelled(true);
    15.  
    16. if (e.getRawSlot() < e.getInventory().getSize() || e.isShiftClick()) {
    17. e.setCancelled(true);
    18. }
    19.  
    20. if (e.getCurrentItem() == null) {
    21. return;
    22. }
    23. int sizeOfLinks = instance.getConfig().getConfigurationSection("Vote.Votelinks").getKeys(false).size();
    24. System.out.println(sizeOfLinks);
    25.  
    26. for (String s : instance.getConfig().getConfigurationSection("Vote.Votelinks").getKeys(false)) {
    27. String serviceName = instance.getConfig().getString("Vote.Votelinks." + s + ".servicename");
    28. String serviceUrl = instance.getConfig().getString("Vote.Votelinks." + s + ".url");
    29.  
    30. Integer serviceId = new Integer(s);
    31.  
    32. if(e.getSlot() == (serviceId - 1)) { // Minus 1. Because slots starts at 0.
    33. e.setCancelled(true);
    34. p.sendMessage(Messages.Prefixes.VOTE + ChatColor.GREEN + serviceName + ": " + ChatColor.GRAY + serviceUrl);
    35. p.closeInventory();
    36. }
    37. }
    38. }
    39. }
    40.  


    EDIT: Actually in your case, you should be doing this:

    Code:java
    1.  
    2. if(event.getRawSlot() > 8) {
    3. e.setCancelled(true);
    4. }
    5.  


    So:

    Code:java
    1.  
    2.  
    3. @EventHandler
    4. public void onInventoryClick(InventoryClickEvent e) {
    5. Player p = (Player) e.getWhoClicked();
    6.  
    7. if(e.getRawSlot() > 8) {
    8. e.setCancelled(true);
    9. }
    10.  
    11. Menu().getName())) return
    12.  
    13. if(e.getInventory().getTitle().contains("Vote")) {
    14. e.setCancelled(true);
    15.  
    16. if (e.getRawSlot() < e.getInventory().getSize() || e.isShiftClick()) {
    17. e.setCancelled(true);
    18. }
    19.  
    20. if (e.getCurrentItem() == null) {
    21. return;
    22. }
    23. int sizeOfLinks = instance.getConfig().getConfigurationSection("Vote.Votelinks").getKeys(false).size();
    24. System.out.println(sizeOfLinks);
    25.  
    26. for (String s : instance.getConfig().getConfigurationSection("Vote.Votelinks").getKeys(false)) {
    27. String serviceName = instance.getConfig().getString("Vote.Votelinks." + s + ".servicename");
    28. String serviceUrl = instance.getConfig().getString("Vote.Votelinks." + s + ".url");
    29.  
    30. Integer serviceId = new Integer(s);
    31.  
    32. if(e.getSlot() == (serviceId - 1)) { // Minus 1. Because slots starts at 0.
    33. e.setCancelled(true);
    34. p.sendMessage(Messages.Prefixes.VOTE + ChatColor.GREEN + serviceName + ": " + ChatColor.GRAY + serviceUrl);
    35. p.closeInventory();
    36. }
    37. }
    38. }
    39. }
    40.  
     
    Last edited: Nov 27, 2015
  5. Offline

    AppleMen

    Still doesn't affect :/ It even happens when I click on the 1st or 2nd empty slot next to the brick.
     
  6. Offline

    AppleMen

    Anyone knows how to solve this??
     
  7. Offline

    CoolDude53

    @AppleMen

    Use the InventoryView to test where the event is happening, also, your logic doesn't make any sense. Here's how I would do it. This cancels any event that would modify the upper inventory, leaving the lower inventory free to be adjusted by the player. If the click happens in the upper inventory, it checks if the clicked item is a book and then and only then does it execute your vote code.

    Code:
    @EventHandler
        public void onInventoryClick(InventoryClickEvent e)
        {
             Player p = (Player) e.getWhoClicked();
             InventoryView view = e.getView();
             int slotNum = e.getRawSlot();
     
            if(e.getInventory().getTitle().contains("Vote"))
            {
                if (e.isShiftClick())
                    e.setCancelled(true);
    
                if (slotNum < view.getBottomInventory().getSize())
                {
                    e.setCancelled(true);
    
                    if (e.getCurrentItem().getType.equals(Material.BOOK))
                    {
                        int sizeOfLinks = instance.getConfig().getConfigurationSection("Vote.Votelinks").getKeys(false).size();
            
                        for (String s : instance.getConfig().getConfigurationSection("Vote.Votelinks").getKeys(false))
                        {
                            String serviceName = instance.getConfig().getString("Vote.Votelinks." + s + ".servicename");
                            String serviceUrl = instance.getConfig().getString("Vote.Votelinks." + s + ".url");
            
                            Integer serviceId = new Integer(s);
            
                            if(e.getSlot() == (serviceId - 1))
                            {
                                e.setCancelled(true);
                                p.sendMessage(Messages.Prefixes.VOTE + ChatColor.GREEN + serviceName + ": " + ChatColor.GRAY + serviceUrl);
                                p.closeInventory();
                            }
                        }
                    }
                }
            }
        }
     
  8. Offline

    AppleMen

    That did the job! Thank you very much :)
     
Thread Status:
Not open for further replies.

Share This Page