Solved For loops

Discussion in 'Plugin Development' started by greeves12, Oct 2, 2017.

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

    greeves12

    Yesterday I switched from a standard for loop to an enhanced for loop, this is to generate the reports for the UI, however, for page 1 it works good but when it's on page 2 it doesn't. So for example I have 58 reports and page 1 holds 53, on page 2 it will continue from 54 to 58 but then it will restart from 6 again. How do I fix that?

    Code:
     public static void createInv() {
            for (String k : DataFile.getData().getConfigurationSection("Reports.").getKeys(false)) {
    
                String player = DataFile.getData().getString("Reports." + Integer.parseInt(k) + ".Player");
                String title = DataFile.getData().getString("Reports." + Integer.parseInt(k) + ".Title");
                ItemStack Stack = new ItemStack(Material.BOOK, 1);
                ItemMeta meta = Stack.getItemMeta();
                ArrayList<String> lore = new ArrayList<String>();
                meta.setDisplayName("§aReport Number: §d" + Integer.parseInt(k));
                lore.add(("By: " + player));
                lore.add(("Title: " + title));
                meta.setLore(lore);
                Stack.setItemMeta(meta);
                Main.reportInv.setItem(Integer.parseInt(k), Stack);
                if(Integer.parseInt(k) == 53){
                    break;
                }
            }
            ItemStack anvil = new ItemStack(Material.ANVIL, 1);
            ItemMeta Meta = anvil.getItemMeta();
            Meta.setDisplayName("§bNext Page");
            anvil.setItemMeta(Meta);
            Main.reportInv.setItem(53, anvil);
        }
    
        public static void createInv2() {
            for (String i  : DataFile.getData().getConfigurationSection("Reports.").getKeys(false)) {
    
                        String player = DataFile.getData().getString("Reports." + i + ".Player");
                        String title = DataFile.getData().getString("Reports." + i + ".Title");
                        ItemStack Stack = new ItemStack(Material.BOOK, 1);
                        ItemMeta meta = Stack.getItemMeta();
                        ArrayList<String> lore = new ArrayList<String>();
                        meta.setDisplayName("§aReport Number: §d" + i);
                        lore.add(("By: " + player));
                        lore.add(("Title: " + title));
                        meta.setLore(lore);
                        Stack.setItemMeta(meta);
                        Main.reportInv2.setItem(Integer.valueOf(i) % 54, Stack);
                        if(Integer.parseInt(i) == 99){
                            break;
                        }
                }
        }
     
  2. Offline

    Zombie_Striker

    @greeves12
    The best way to store indexes would be to create a new ArrayList. In that list, store all the data needed. In your case, you will need to create your own class to store the player, title, and who it is applied for. In the onEnable, loop through all the reports and add them to the arraylist. When you want to get reports for each page, create a for int loop. It will start at 0, and loop up to the amount you need (in your case, it would be 53). Then, use ArrayList#get((page*53)+int) to get the reports for that page.
     
  3. Offline

    greeves12

    Yeah I'm sorry I'm an idiot, I just solved it by doing and that fixed it.

    Code:
     for (String i  : DataFile.getData().getConfigurationSection("Reports.").getKeys(false)) {
                int j = Integer.parseInt(i);
    
                        if(j > 53) {
                            execute code here
                            }
                        }
    
                }
     
Thread Status:
Not open for further replies.

Share This Page