Solved Code after for loop does not get executed?

Discussion in 'Spigot Plugin Development' started by pgr4567, Aug 14, 2018.

  1. I have set up the command sellall to sell all items in a player's inventory. Now, I have a for loop, looping trough all ItemStack´s in a players inventory and get the amount of the items. However, the code after the for loop does not get executed and I don't know why. Do you know why? Thank you for your help :)
    (I don't know how to format code in here, sorry)

    //Get the Counts
    int cobblestoneAmount = 0;
    int mossyCobblestoneAmount = 0;
    int stoneAmount = 0;

    for (ItemStack item : player.getInventory().getContents()) {
    if (item == null) {
    return true;
    }
    else {
    if (item.getType() == Material.COBBLESTONE) {
    cobblestoneAmount += item.getAmount();
    }
    else if (item.getType() == Material.MOSSY_COBBLESTONE) {
    mossyCobblestoneAmount += item.getAmount();
    }
    else if (item.getType() == Material.STONE) {
    stoneAmount += item.getAmount();
    }
    }
    }

    Bukkit.getServer().getConsoleSender().sendMessage("Hello?");
    //I don't get that console message..
     
  2. Just do
    Code (Java):

    //Get the Counts
    int cobblestoneAmount = 0;
    int mossyCobblestoneAmount = 0;
    int stoneAmount = 0;
    //loop through all the items
    for (ItemStack item: player.getInventory().getContents()) {
    //if ítem is not null, then check its type
     if (item != null) {
      Material type = item.getType();
      if (type.equals(Material.COBBLESTONE)) {
       cobblestoneAmount += item.getAmount();
      } else if (type.equals(Material.MOSSY_COBBLESTONE)) {
       mossyCobblestoneAmount += item.getAmount();
      } else if (type.equals(Material.STONE)) {
       stoneAmount += item.getAmount();
      }
     }
    }
    Bukkit.getServer().getConsoleSender().sendMessage("Hello?");


     
     
    #2 Whoktor, Aug 14, 2018
    Last edited: Aug 14, 2018
  3. Here what you're doing is if one ítem is null then STOP and return, if you return, the following code will not execute
    if you use return or return true/false the code that is forward Will not get executed
     
  4. In for loops you use continue instead of return
     
  5. Ohhh, thank you all! :)