Events firing more than once

Discussion in 'Plugin Development' started by glasseater, May 8, 2016.

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

    glasseater

    Hey does anyone know why my events are firing more than once?

    Code:
    @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (!e.getInventory().getName().equalsIgnoreCase(((Inventory) GUI).getName())) return;
            if (e.getCurrentItem().getItemMeta() == null) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Farmer")){
                e.setCancelled(true);
                e.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
                if(e.getWhoClicked().hasPermission("rank.farmer"))
                    e.getWhoClicked().sendMessage("You already have this rank!");
               
                   
                    return;
            }
           
                 if (SQL.getValue(e.getWhoClicked().getName(), "coins") >= 20000) {
                        SQL.buyRank(e.getWhoClicked().getName());
                       
                        McMyths.chat().sendMessage(e.getWhoClicked(), "You bought the §aFarmer §7rank!");
                        Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "removecoins " + e.getWhoClicked().getName() + " 20000"
    );                  } else {
       
                        e.getWhoClicked().sendMessage("§7You need 20000 coins to buy this rank!");
                       
                      }
                   
               
                e.getWhoClicked().closeInventory();
            }
    
        @EventHandler
        public void onInventoryClick2(InventoryClickEvent e) {
    
            if (!e.getInventory().getName().equalsIgnoreCase(((Inventory) GUI).getName()))
                return;
            if (e.getCurrentItem().getItemMeta() == null)
                return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Hunter")) {
                e.setCancelled(true);
                e.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
                if (e.getWhoClicked().hasPermission("rank.Hunter"))
                    e.getWhoClicked().sendMessage("You already have this rank!");
    
                return;
            }
    
            if (SQL.getValue(e.getWhoClicked().getName(), "coins") >= 20000) {
                SQL.buyRank(e.getWhoClicked().getName());
    
                McMyths.chat().sendMessage(e.getWhoClicked(), "You bought the §aHunter §7rank!");
                Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(),
                        "removecoins " + e.getWhoClicked().getName() + " 20000");
            } else {
    
                e.getWhoClicked().sendMessage("§7You need 20000 coins to buy this rank!");
    
            }
    
            e.getWhoClicked().closeInventory();
        }
       
       
        @EventHandler
        public void onInventoryClick3(InventoryClickEvent e) {
            if (!e.getInventory().getName().equalsIgnoreCase(((Inventory) GUI).getName())) return;
            if (e.getCurrentItem().getItemMeta() == null) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Knight")){
                e.setCancelled(true);
                e.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
                if(e.getWhoClicked().hasPermission("rank.knight"))
                    e.getWhoClicked().sendMessage("You already have this rank!");
               
                   
                    return;
            }
           
                 if (SQL.getValue(e.getWhoClicked().getName(), "coins") >= 20000) {
                        SQL.buyRank(e.getWhoClicked().getName());
                       
                        McMyths.chat().sendMessage(e.getWhoClicked(), "You bought the §aKnight §7rank!");
                        Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "removecoins " + e.getWhoClicked().getName() + " 20000"
    );                  } else {
       
                        e.getWhoClicked().sendMessage("§7You need 20000 coins to buy this rank!");
                       
                      }
                   
               
                e.getWhoClicked().closeInventory();
            }
    
       
        @EventHandler
        public void onInventoryClick4(InventoryClickEvent e) {
            if (!e.getInventory().getName().equalsIgnoreCase(((Inventory) GUI).getName())) return;
            if (e.getCurrentItem().getItemMeta() == null) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Wizard")){
                e.setCancelled(true);
                e.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
                if(e.getWhoClicked().hasPermission("rank.wizard"))
                    e.getWhoClicked().sendMessage("You already have this rank!");
               
                    return;
            }
                 if (SQL.getValue(e.getWhoClicked().getName(), "coins") >= 20000) {
                        SQL.buyRank(e.getWhoClicked().getName());
                       
                        McMyths.chat().sendMessage(e.getWhoClicked(), "You bought the §aWizard §7rank!");
                        Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "removecoins " + e.getWhoClicked().getName() + " 20000"
    );                  } else {
       
                        e.getWhoClicked().sendMessage("§7You need 20000 coins to buy this rank!");
                       
                      }
                   
                e.getWhoClicked().closeInventory();
            }
    
        @EventHandler
        public void onInventoryClick5(InventoryClickEvent e) {
            if (!e.getInventory().getName().equalsIgnoreCase(((Inventory) GUI).getName())) return;
            if (e.getCurrentItem().getItemMeta() == null) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Overlord")){
                e.setCancelled(true);
                e.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
               
                if(e.getWhoClicked().hasPermission("rank.overlord"))
                    e.getWhoClicked().sendMessage("You already have this rank!");
               
                   
                    return;
            }
           
                 if (SQL.getValue(e.getWhoClicked().getName(), "coins") >= 20000) {
                        SQL.buyRank(e.getWhoClicked().getName());
                       
                        McMyths.chat().sendMessage(e.getWhoClicked(), "You bought the §aOverlord §7rank!");
                        Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "removecoins " + e.getWhoClicked().getName() + " 20000"
    );                  } else {
       
                        e.getWhoClicked().sendMessage("§7You need 20000 coins to buy this rank!");
                       
                      }
                   
               
                e.getWhoClicked().closeInventory();
            }
    }
    It doesn't do it if there are only two events, I've tried debugging but with no luck, if anyone could help that would be greatly appreciated! Thanks!
     
  2. Offline

    Zombie_Striker

    @glasseater
    Yes. It is because you have five methods in the same class checking for the same event. You should only check for an event once per plugin.
     
  3. Offline

    I Al Istannen

    @Zombie_Striker
    Could you elaborate why? I try to keep it modular and therefore I might be listening to one event in more than one class.

    In his case you are definitely right, it is unneeded.
     
    Last edited: May 8, 2016
  4. Offline

    glasseater

    @Zombie_Striker

    So you're saying I should have all of those events, in one inventory click event?
     
  5. Offline

    Zombie_Striker

    @glasseater @I Al Istannen
    The main problem here is that most of the code in each method is repetitive. You are always checking for the same inventory, and always making sure the itemmeta is not null. Moving all the current events into one single event will reduce filesize and prevents double checking values. It's only useful to have multiple events if they A) are in different classes, B) Do completely different things, or C) have to be able to cancel and "un-cancel" events.
     
    I Al Istannen likes this.
  6. Offline

    ChipDev

    Yeah. Just use different if else statements, otherwise this will get rather confusing / buggy.
     
  7. Perfect Naming Convention...
     
Thread Status:
Not open for further replies.

Share This Page