Messages getting sent multiple times on click

Discussion in 'Plugin Development' started by glasseater, Jun 12, 2016.

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

    glasseater

    Hey everyone,

    So I am making a rank shop in game, with in game money and it takes the money and everything of that nature but when I click the item the message gets sent multiple times.

    Here is a gif:
    https://gyazo.com/aab529de73e4db2ff906c1d3701f3d42

    Here is the code:

    Code:
          private static ItemStack farmer;
            private static ItemStack hunter;
            private static ItemStack knight;
            private static ItemStack wizard;
            private static ItemStack overlord;
    
            public static Inventory GUI = Bukkit.createInventory(null, 9, "Rank GUI");
    
            {
    
                farmer = item1("§e§lFarmer");
                hunter = item2("§c§lHunter");
                knight = item3("§6§lKnight");
                wizard = item4("§d§lWizard");
                overlord = item5("§4§lOverlord");
    
                ((Inventory) GUI).setItem(0, farmer);
                ((Inventory) GUI).setItem(2, hunter);
                ((Inventory) GUI).setItem(4, knight);
                ((Inventory) GUI).setItem(6, wizard);
                ((Inventory) GUI).setItem(8, overlord);
            }
    
            private static ItemStack item1(String name) {
                ItemStack i = new ItemStack(new ItemStack(Material.EMERALD_BLOCK, 1));
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList("§7Click to purchase the §aFarmer §7rank!"));
                i.setItemMeta(im);
                return i;
            }
    
            private static ItemStack item2(String name) {
                ItemStack i = new ItemStack(new ItemStack(Material.EMERALD_BLOCK, 1));
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList("§7Click to purchase the §aHunter §7rank!"));
                i.setItemMeta(im);
                return i;
            }
    
            private static ItemStack item3(String name) {
                ItemStack i = new ItemStack(new ItemStack(Material.EMERALD_BLOCK, 1));
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList("§7Click to purchase the §aKnight §7rank!"));
                i.setItemMeta(im);
                return i;
            }
    
            private static ItemStack item4(String name) {
                ItemStack i = new ItemStack(new ItemStack(Material.EMERALD_BLOCK, 1));
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList("§7Click to purchase the §aWizard §7rank!"));
                i.setItemMeta(im);
                return i;
            }
    
            private static ItemStack item5(String name) {
                ItemStack i = new ItemStack(new ItemStack(Material.EMERALD_BLOCK, 1));
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList("§7Click to purchase the §aOverlord §7rank!"));
                i.setItemMeta(im);
                return i;
            }
    
        @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.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
                    if(e.getWhoClicked().hasPermission("rank.farmer"));
                       
               
                }  else {
                       
                        e.getWhoClicked().sendMessage("You already have this rank!");
                  
                      
                        return;
                }
              
                     if (SQL.getValue(e.getWhoClicked().getName(), "money") >= 10000) {
                            SQL.buyKit(e.getWhoClicked().getName());
                          
                            McMyths.chat().sendMessage(e.getWhoClicked(), "You bought the §aFarmer §7rank!");
                            Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "removemoney " + e.getWhoClicked().getName() + " 10000"
        );                  } else {
          
                            e.getWhoClicked().sendMessage("§7You need 10000 money 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.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
                    if (e.getWhoClicked().hasPermission("rank.Hunter"));
                   
                }  else {
                   
                        e.getWhoClicked().sendMessage("You already have this rank!");
                    return;
                }
                if (SQL.getValue(e.getWhoClicked().getName(), "money") >= 20000) {
                    SQL.buyKit(e.getWhoClicked().getName());
                    McMyths.chat().sendMessage(e.getWhoClicked(), "You bought the §aHunter §7rank!");
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(),
                            "removemoney " + e.getWhoClicked().getName() + " 20000");
                } else {
                    e.getWhoClicked().sendMessage("§7You need 20000 money 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.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
                    if(e.getWhoClicked().hasPermission("rank.knight"));
                   
                }  else {
                   
                        e.getWhoClicked().sendMessage("You already have this rank!");
                  
                      
                        return;
                }
              
                     if (SQL.getValue(e.getWhoClicked().getName(), "money") >= 25000) {
                            SQL.buyKit(e.getWhoClicked().getName());
                          
                            McMyths.chat().sendMessage(e.getWhoClicked(), "You bought the §aKnight §7rank!");
                            Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "removemoney " + e.getWhoClicked().getName() + " 25000"
        );                  } else {
          
                            e.getWhoClicked().sendMessage("§7You need 25000 money 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.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
                    if(e.getWhoClicked().hasPermission("rank.wizard"));
                   
                }  else {
                   
                        e.getWhoClicked().sendMessage("You already have this rank!");
                  
                        return;
                }
                     if (SQL.getValue(e.getWhoClicked().getName(), "money") >= 32000) {
                            SQL.buyKit(e.getWhoClicked().getName());
                          
                            McMyths.chat().sendMessage(e.getWhoClicked(), "You bought the §aWizard §7rank!");
                            Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "removemoney " + e.getWhoClicked().getName() + " 32000"
        );                  } else {
          
                            e.getWhoClicked().sendMessage("§7You need 32000 money 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.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
                  
                    if(e.getWhoClicked().hasPermission("rank.overlord"));
                   
                }  else {
                   
                        e.getWhoClicked().sendMessage("You already have this rank!");
                  
                      
                        return;
                }
              
                     if (SQL.getValue(e.getWhoClicked().getName(), "money") >= 40000) {
                            SQL.buyKit(e.getWhoClicked().getName());
                          
                            McMyths.chat().sendMessage(e.getWhoClicked(), "You bought the §aOverlord §7rank!");
                            Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "removemoney " + e.getWhoClicked().getName() + " 40000"
        );                  } else {
          
                            e.getWhoClicked().sendMessage("§7You need 40000 money to buy this rank!");
                          
                          }
                      
                  
                    e.getWhoClicked().closeInventory();
                    
                }
        }
    Would anyone know what the problem is? Thank you all
     
  2. Offline

    FisheyLP

    Code:
     if(e.getWhoClicked().hasPermission("rank.farmer"));
    Java before Bukkit
     
    mine-care likes this.
  3. Offline

    glasseater

    @FisheyLP Oh would I have to change it to

    Code:
    if(e.getWhoClicked().hasPermission("rank.farmer")) {
     
  4. Offline

    Koningpeter

    You have to use 1 InventoryClickEvent
     
  5. Offline

    glasseater

    @Koningpeter @FisheyLP
    I fixed the messages getting duped, but now if I wanted to add the permission when they click to check if they have the rank, where can I add
    Code:
    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user " + e.getWhoClicked().getName() + " add" + " " + "rank.farmer");
    I tried adding it like this

    Code:
    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "removemoney " + e.getWhoClicked().getName() + " 10000"
        );                     Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user " + e.getWhoClicked().getName() + " add" + " " + "rank.farmer");
    But that did not work. I want to add the permission so if they have the permission it will say you already have this rank. So it doesn't take their money
     
  6. Offline

    Zombie_Striker

    @glasseater
    You need to test if the player does not has that rank before you give them the rank. I If the player has the rank, don't dispatch the commands. If they don't, dispatch the commands.
     
  7. Offline

    glasseater

    @Zombie_Striker
    I do check? I check if they already have the permission but I but its not sending the "You already have this rank" Message even if they have the permission
     
  8. Offline

    Zombie_Striker

    @glasseater
    Please post your updated class, as you were not checking the permission in the code posted above.
     
  9. Offline

    glasseater

    Code:
    public class Menu implements Listener {
    
          private static ItemStack farmer;
            private static ItemStack hunter;
            private static ItemStack knight;
            private static ItemStack wizard;
            private static ItemStack overlord;
    
            public static Inventory GUI = Bukkit.createInventory(null, 9, "Rank GUI");
    
            {
    
                farmer = item1("§e§lFarmer");
                hunter = item2("§c§lHunter");
                knight = item3("§6§lKnight");
                wizard = item4("§d§lWizard");
                overlord = item5("§4§lOverlord");
    
                ((Inventory) GUI).setItem(0, farmer);
                ((Inventory) GUI).setItem(2, hunter);
                ((Inventory) GUI).setItem(4, knight);
                ((Inventory) GUI).setItem(6, wizard);
                ((Inventory) GUI).setItem(8, overlord);
            }
    
            private static ItemStack item1(String name) {
                ItemStack i = new ItemStack(new ItemStack(Material.EMERALD_BLOCK, 1));
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList("§7Click to purchase the §aFarmer §7rank!"));
                i.setItemMeta(im);
                return i;
            }
    
            private static ItemStack item2(String name) {
                ItemStack i = new ItemStack(new ItemStack(Material.EMERALD_BLOCK, 1));
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList("§7Click to purchase the §aHunter §7rank!"));
                i.setItemMeta(im);
                return i;
            }
    
            private static ItemStack item3(String name) {
                ItemStack i = new ItemStack(new ItemStack(Material.EMERALD_BLOCK, 1));
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList("§7Click to purchase the §aKnight §7rank!"));
                i.setItemMeta(im);
                return i;
            }
    
            private static ItemStack item4(String name) {
                ItemStack i = new ItemStack(new ItemStack(Material.EMERALD_BLOCK, 1));
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList("§7Click to purchase the §aWizard §7rank!"));
                i.setItemMeta(im);
                return i;
            }
    
            private static ItemStack item5(String name) {
                ItemStack i = new ItemStack(new ItemStack(Material.EMERALD_BLOCK, 1));
                ItemMeta im = i.getItemMeta();
                im.setDisplayName(name);
                im.setLore(Arrays.asList("§7Click to purchase the §aOverlord §7rank!"));
                i.setItemMeta(im);
                return i;
            }
    
        @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.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
                    if(e.getWhoClicked().hasPermission("rank.farmer")) {
                       
               
                }  else {
                       
                        e.getWhoClicked().sendMessage("You already have this rank!");
                  
                      
                        return;
                }
              
                     if (SQL.getValue(e.getWhoClicked().getName(), "money") >= 10000) {
                            SQL.buyKit(e.getWhoClicked().getName());
                          
                            McMyths.chat().sendMessage(e.getWhoClicked(), "You bought the §aFarmer §7rank!");
                            Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "removemoney " + e.getWhoClicked().getName() + " 10000"
        );                     Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user " + e.getWhoClicked().getName() + " add" + " " + "rank.farmer");
                            } else {
          
                            e.getWhoClicked().sendMessage("§7You need 10000 money 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.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
                    if (e.getWhoClicked().hasPermission("rank.hunter")) {
                   
                }  else {
                   
                        e.getWhoClicked().sendMessage("You already have this rank!");
                    return;
                }
                if (SQL.getValue(e.getWhoClicked().getName(), "money") >= 20000) {
                    SQL.buyKit(e.getWhoClicked().getName());
                    McMyths.chat().sendMessage(e.getWhoClicked(), "You bought the §aHunter §7rank!");
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(),
                            "removemoney " + e.getWhoClicked().getName() + " 20000");
                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user " + e.getWhoClicked().getName() + " add" + " " + "rank.hunter");
                } else {
                    e.getWhoClicked().sendMessage("§7You need 20000 money 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.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
                    if(e.getWhoClicked().hasPermission("rank.knight")) {
                   
                }  else {
                   
                        e.getWhoClicked().sendMessage("You already have this rank!");
                  
                      
                        return;
                }
              
                     if (SQL.getValue(e.getWhoClicked().getName(), "money") >= 25000) {
                            SQL.buyKit(e.getWhoClicked().getName());
                          
                            McMyths.chat().sendMessage(e.getWhoClicked(), "You bought the §aKnight §7rank!");
                            Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "removemoney " + e.getWhoClicked().getName() + " 25000"
        );                  Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user " + e.getWhoClicked().getName() + " add" + " " + "rank.knight");
                            } else {
          
                            e.getWhoClicked().sendMessage("§7You need 25000 money 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.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
                    if(e.getWhoClicked().hasPermission("rank.wizard")) {
                   
                }  else {
                   
                        e.getWhoClicked().sendMessage("You already have this rank!");
                  
                        return;
                }
                     if (SQL.getValue(e.getWhoClicked().getName(), "money") >= 32000) {
                            SQL.buyKit(e.getWhoClicked().getName());
                          
                            McMyths.chat().sendMessage(e.getWhoClicked(), "You bought the §aWizard §7rank!");
                            Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "removemoney " + e.getWhoClicked().getName() + " 32000"
        );                    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user " + e.getWhoClicked().getName() + " add" + " " + "rank.wizard");
                            } else {
          
                            e.getWhoClicked().sendMessage("§7You need 32000 money 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.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
                  
                    if(e.getWhoClicked().hasPermission("rank.overlord")) {
                   
                }  else {
                   
                        e.getWhoClicked().sendMessage("You already have this rank!");
                  
                      
                        return;
                }
              
                     if (SQL.getValue(e.getWhoClicked().getName(), "money") >= 40000) {
                            SQL.buyKit(e.getWhoClicked().getName());
                          
                            McMyths.chat().sendMessage(e.getWhoClicked(), "You bought the §aOverlord §7rank!");
                            Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "removemoney " + e.getWhoClicked().getName() + " 40000"
        );                     Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user " + e.getWhoClicked().getName() + " add" + " " + "rank.overlord");
                            } else {
          
                            e.getWhoClicked().sendMessage("§7You need 40000 money to buy this rank!");
                          
                          }
                      
                  
                    e.getWhoClicked().closeInventory();
                    
                }
        }
    }
    @Zombie_Striker
     
  10. Offline

    Zombie_Striker

    @glasseater
    First, you should only have one of each event for the whole plugin. Merge all the events into one method. Second, you have a ton of copied and pasted code that could be reduced. Delete all duplicate code, and move all non-duplicated code into their correct spots.

    I am only telling you to do this because you code really hard to read, and it can't even tell what differs between each event.

    (After a while of looking at your code), it seems as though you forgot to invert your if statements. What you're actually doing is testing if a player does have a permission, and then re-adding the permission. If the player however does not have the permission, you are telling them that they already have the rank when they don't. This is why proper encapsulation is important. Remove that "else" statement from each permission check and it will work,
     
  11. Offline

    glasseater

    @Zombie_Striker
    Thanks for the ideas. I will go through my code! Your suggestion worked! Thanks so much! Now I just need to fix my economy system lol
     
Thread Status:
Not open for further replies.

Share This Page