Code keeps on reapting its self with no loops

Discussion in 'Spigot Plugin Development' started by Yoreni, Jun 5, 2017.

  1. My code repeates it self but there is no for or while loop telling it to repeat
    Code (Text):

        [USER=17846]@EventHandler[/USER]
        public void onInvExit(InventoryCloseEvent event)
        {
            Player player = (Player) event.getPlayer();
            ArrayList<String> name = new ArrayList<String>();
            int a = 1;
            boolean run = true;
            while(run)
            {
                try
                {
                    name.add(ChatColor.translateAlternateColorCodes('&',getConfig().getString("Shop.Main." + a + ".GUIname")));
                    a++;
                }
                catch(NullPointerException NullPointerException)
                {
                    run = false;
                }
            }
            name.add("Shop");
            name.add("Amount");
            if(name.contains(event.getInventory().getTitle()))
            {
                if(deep.get(player.getUniqueId().toString()) == 3 && click.get(player.getUniqueId().toString()) == 0)
                {

                    Inventory shop = Bukkit.createInventory(null,9 * 6,ChatColor.translateAlternateColorCodes('&',getConfig().getString("Shop.Main." + cat.get(player.getUniqueId().toString()) + ".GUIname")));
                    run = true;
                    a = 1;
                    while(run)
                    {
                        try
                        {
                            String path = getConfig().getString("Shop.Main." + cat.get(player.getUniqueId().toString()) + ".Path");
                            @SuppressWarnings("deprecation")
                            ItemStack item = new ItemStack(getConfig().getInt("Shop." + path + "." + a + ".ID"));
                            item.setDurability((short) getConfig().getInt("Shop." + path + "." + a + ".Meta"));
                            ItemMeta meta = item.getItemMeta();
                            ArrayList<String> lore = new ArrayList<String>();
                            switch((int) getConfig().getDouble("Shop." + path + "." + a + ".Buy"))
                            {
                            case 0:
                                lore.add(ChatColor.GREEN + "Buy " + ChatColor.WHITE + "FREE");
                                break;
                            case -1:
                                break;
                            default:
                                lore.add(ChatColor.GREEN + "Buy " + ChatColor.WHITE + "$" + FormatterAPI.format(getConfig().getDouble("Shop." + path + "." + a + ".Buy")));
                                break;
                            }
                            switch((int) getConfig().getDouble("Shop." + path + "." + a + ".Sell"))
                            {
                            case 0:
                                lore.add(ChatColor.RED + "Sell " + ChatColor.WHITE + "Nothing");
                                break;
                            case -1:
                                break;
                            default:
                                lore.add(ChatColor.RED + "Sell " + ChatColor.WHITE + "$" + FormatterAPI.format(getConfig().getDouble("Shop." + path + "." + a + ".Sell")));
                                break;
                            }
                            meta.setLore(lore);
                            if(getConfig().getInt("Shop." + path + "." + a + ".ID") == 52) meta.setDisplayName(ChatColor.YELLOW + mobIdToName(getConfig().getInt("Shop." + path + "." + a + ".Meta")) + ChatColor.WHITE + " Spawner");
                            item.setItemMeta(meta);
                            shop.setItem(a - 1,item);
                            a += 1;
                        }
                        catch(NullPointerException NullPointerException)
                        {
                            run = false;
                        }
                    }
                    player.openInventory(shop);
                    player.sendMessage("Sample");
                }
            }
        }
     
    Iam fouceing on the
    Code (Text):

    player.openInventory(shop);
    player.sendMessage("Sample");
     
    part
     
    • Funny Funny x 2
  2. You have a while loop, and never set run to false. Why are you using a while loop anyways??
     
  3. i mean why is
    Code (Text):

    player.openInventory(shop);
    player.sendMessage("Sample");
     
    Repeateing
    thier is no while loop on it
     
  4. Yeah, you have while loop(s) that never terminate when a NPE is not generated.
     
    • Agree Agree x 1
  5. Ah I see. Because the while loop is repeatedly opening the inventory and the
    user is repeatedly trying to close the inventory, which is triggering the
    InventoryCloseEvent.

    In summary, the user cannot get away from the inventory screen.
     
    • Agree Agree x 1
  6. when i run the plugin it does not do that what happens is that the server stop for 5 sec then it does what you say 100 times then it stops
     
  7. Mas

    Mas

    You shouldn't be having to catch NullPointerExceptions. Perform null checks before performing methods on an object.
     
    • Agree Agree x 1