1.15.2 Need help with breaking out of nested loops

Discussion in 'Spigot Plugin Development' started by DapperNurd, Jan 30, 2020.

  1. This is my current code:
    Code (Java):
    for(int i = 0; i < inventory.getSize(); i++) {
                        if(individualBooks.get(inventory.getItemInMainHand().getItemMeta().getLore()).contents[i] != null && inventory.getItem(i) == null) {
                            inventory.setItem(i, individualBooks.get(inventory.getItemInMainHand().getItemMeta().getLore()).contents[i]); //maybe add it to a new itemstack[] and then set it to the new one at the end
                        }
                        else if (individualBooks.get(inventory.getItemInMainHand().getItemMeta().getLore()).contents[i] != null && inventory.getItem(i) != null) {
                            for(int k = i; k < inventory.getSize(); k++) {
                                if(inventory.getItem(k) == null) {
                                    inventory.setItem(k, individualBooks.get(inventory.getItemInMainHand().getItemMeta().getLore()).contents[i]);
                                    break;
                                }
                            }
                            itemsToDrop.add(individualBooks.get(inventory.getItemInMainHand().getItemMeta().getLore()).contents[i]);
                        }
                    }
    Sorry if it's a mess.

    Anyways, I'm basically just combining two inventories, but if one of them is full, I want to add the items to an itemsToDrop list. Right now, it is adding every item that is detected in the else if.
    What I want to have happen is, in the else if: the for loop runs; if there is an empty space, it sets the item and stops the entire else if statement. If the for loop goes through the entire thing without having the nested if statement run, only then will the itemsToDrop.add(); get called.

    Sorry if this is kind of an obvious Java skill, I'm still fairly new to this.
     
  2. https://www.developer.com/java/data/understanding-the-java-labeled-statement.html
     
  3. Inventory#addItem(ItemStack...) already has this functionality, pseudocode below:

    Inventory one
    Inventory two
    Collection<ItemStack> drop = two.addItem(one.getContents())
    for item in drop: drop the item
     
  4. That isn't really where I'm having the issue. The issue is trying to break the else-if loop.
     
  5. You can't break out of an if-else statement. You can use a boolean to determine if your nested loop runs at least once. i.e.
    Code (Java):
    boolean didRun = false;
    for loop {
        didRun = true;
        // blahblahblah
    }
    if (didRun) {
        // add item drops
    }
    However, this nested loop is not necessary to determine the first empty slot in an Inventory. Inventory has a #firstEmpty method that returns the index of the first empty slot, or -1 if the inventory is full.
     
  6. TeamBergerhealer

    Supporter

    The break label is often dubbed the goto of Java, in a negative sense. If you find yourself nested too deep in the code, its often a better idea to split your code up into multiple smaller functions. Then your (early) return is your break.
     
    • Agree Agree x 1
  7. Strahan

    Benefactor

    ...

    yea.. but you only have that issue because you wrote code that attempts to do (in a far less efficient manner) what he was talking about.

    If you leverage his advice, you don't need any of this looping.
     
    • Agree Agree x 1