Solved For loop not running to fill players inventory?

Discussion in 'Spigot Plugin Development' started by YoDontBeThatGirl, Apr 22, 2017.

  1. This has really confused me and I'm not sure why it's not working?
    If someone could help me out it would be a dream. I have annotated the code (to help me get my head around why it's not working) so that may help you...
    Code (Java):

      @EventHandler
        public void itemPickup(PlayerPickupItemEvent e ) {
            ItemStack i = e.getItem().getItemStack();
            Player p = e.getPlayer();
            if (i.hasItemMeta()) {
                if (i.getItemMeta().hasLore()) {
                List<String> lore = i.getItemMeta().getLore();
                    for (String s : lore) {
                        if (s.startsWith("§b§lAmount : ")) {
                   
                            ItemStack ingot = new ItemStack(i.getType()); //For testing: Diamond
                            e.setCancelled(true);
                            int b = Integer.parseInt(s.substring(13)); //Gets the amount of diamond (from lore) by taking away "§b§lAmount : "
                            int amount = i.getAmount() * b;
                            int openSlots = getEmptySlots(p, p.getInventory())
                                    * i.getMaxStackSize(); //Gets players inventory slots (works fine)
                            if (openSlots >= amount) { // If they have enough inventory slots to hold the amount of the item
                                int sa = (int) (amount / i.getMaxStackSize()); //get the amount of full stacks it can give them
                                Bukkit.broadcastMessage("" + sa); //Returns 4 for 258 items (correct)
                                ingot.setAmount(i.getMaxStackSize()); //Sets the 'ingot' just an arbitrary item with the same item type as the stack.
                                Bukkit.broadcastMessage("" + ingot.getAmount()); //returns 64 if the max size is 64 (correct)
                                for (int I = 0; I > sa; I++) { //Does a loop to add the stacks to the players inventory (works below)...
                                    p.sendMessage("added");// this never runs?
                                    ItemStack g = p.getInventory().getItem(I); //Never runs
                                    if (g == null) {
                                        p.getInventory().setItem(I, ingot);
                                    }
                                }
                                int cAmount = amount - (sa * i.getMaxStackSize()); //Adds the remainder that wasn't nicely converted into stacks.
                                if (cAmount != 0) {
                                    ingot.setAmount(cAmount);
                                    p.getInventory().addItem(ingot);
                                }
                                e.getItem().remove();
                            } else {
                                ingot.setAmount(i.getMaxStackSize());
                                for (int I = 0; I < 36; I++) { // this works??
                                    ItemStack g = p.getInventory().getItem(I);
                                    if (g == null) {
                                        p.getInventory().setItem(I, ingot);
                                    }
                                }
                                ItemMeta cm = i.getItemMeta();
                                List<String> cmlore = new ArrayList<String>();
                                cmlore.add("§b§lAmount : " + (amount - openSlots));
                                cm.setLore(cmlore);
                                i.setItemMeta(cm);
                            }
                        }
                    }
                }
            }
        }
     
    Ignore the whacky variable names ;P Thanks in advance!
     
    #1 YoDontBeThatGirl, Apr 22, 2017
    Last edited: Apr 22, 2017
  2. "for (int I = 0; I > sa; I++) {"
    The "sa" variable is not a negative number, that's why it never runs. Change the ">" to "<".
     
  3. Thank you! Can't believe I couldn't see that ;)
    !Solved!