Solved Stopping/Avoiding loop?

Discussion in 'Plugin Development' started by KarimAKL, Jul 26, 2018.

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

    KarimAKL

    Hey, i've made this code and in the code i search through a player's inventory and in the end i send the player a message but currently the player gets a message for every stack of items detected(luckily not amount. :p), how would i stop the message from getting repeated and only send it once?
    Here is the code:
    Code:Java
    1. for (ItemStack content : player.getInventory().getContents()) {
    2. for (String blacklisted : plugin.getConfig().getStringList("blacklist")) {
    3. if (content == null || content.getType() != Material.matchMaterial(blacklisted.toUpperCase())) {
    4. continue;
    5. }
    6. if (content.getType() == Material.matchMaterial(blacklisted.toUpperCase())) {
    7. ItemStack items = content;
    8. player.getInventory().remove(items);
    9. player.sendMessage(Color("&cBlacklisted items were removed."));
    10. }
    11. }
    12. }

    I've tried moving around with the message and tried putting "break;" at the end and move the message to another place and stuff like that. :7
     
  2. Offline

    InstanceofDeath

    You should probably add a loop infront of your code (where the player still has all of his items) like this :

    for(ItemStack items : player.getInventory.getContents()) {
    if(plugin.getConfig().getStringList(“blacklist“).contains(items.getType().toString().toLowerCase) {
    // Message
    break;
    }
    }
     
  3. Offline

    KarimAKL

    @InstanceofDeath I've tried that and i got it working one time but then i wanted to try and change some stuff and now i can't get it working again and i can't remember where i placed what. :/
    It's currently sending 1 message(as it should) but only taking 1 blacklisted item from the content when it's supposed to take all of them. :7
    Any ideas? Btw here is my current code:
    Code:Java
    1. for (ItemStack content : player.getInventory().getContents()) {
    2. if (content == null || !plugin.getConfig().getStringList("blacklist").contains(content.getType().toString().toLowerCase())) {
    3. continue;
    4. }
    5. for (String blacklisted : plugin.getConfig().getStringList("blacklist")) {
    6. if (content.getType() == Material.matchMaterial(blacklisted.toUpperCase())) {
    7. ItemStack items = content;
    8. player.getInventory().remove(items);
    9. }
    10. }
    11. player.sendMessage(Color("&cBlacklisted items were removed."));
    12. break;
    13. }
     
  4. Offline

    InstanceofDeath

    Code:
    for(ItemStack items : player.getInventory.getContents()) {
        if(plugin.getConfig().getStringList(“blacklist“).contains(items.getType().toString().toLowerCase)) {
            player.sendMessage(Color("&cBlacklisted items were removed."));
            break;
        }
    }
    
    for(ItemStack items : player.getInventory.getContents()) {
        if(plugin.getConfig().getStringList(“blacklist“).contains(items.getType().toString().toLowerCase)) {
            player.getInventory().remove(items);
        }
     
    }
    
     
  5. Offline

    KarimAKL

    I see, didn't think about adding a whole new loop for inventory content. :7
    One last thing, would it be possible to make it so if no blacklisted items were found(after checking the whole inventory) then it would come with a message "no blacklisted items found"? I would like to do this without a BukkitRunnable if possible.
    Current code:
    Code:Java
    1. for (ItemStack items : player.getInventory().getContents()) {
    2. if (items == null || !plugin.getConfig().getStringList("blacklist").contains(items.getType().toString().toLowerCase())) continue;
    3. if (plugin.getConfig().getStringList("blacklist").contains(items.getType().toString().toLowerCase())) {
    4. player.sendMessage(Color("&cBlacklisted items were removed."));
    5. break;
    6. }
    7. }
    8. for (ItemStack items : player.getInventory().getContents()) {
    9. if (items == null) continue;
    10. if (plugin.getConfig().getStringList("blacklist").contains(items.getType().toString().toLowerCase())) {
    11. player.getInventory().remove(items);
    12. }
    13. }

    EDIT: Yes, i've tried some things but it didn't work .:/

    EDIT2: Solved. Final code:
    Code:Java
    1. boolean hasBlacklistedItem = false;
    2. PlayerInventory inv = player.getInventory();
    3. for (int i = 0; i < inv.getContents().length; i++) {
    4. ItemStack item = inv.getItem(i);
    5. if (item == null) continue;
    6. if (plugin.getConfig().getStringList("blacklisted").contains(item.getType().toString().toLowerCase())) {
    7. hasBlacklistedItem = true;
    8. inv.clear(i);
    9. }
    10. }
    11. if (hasBlacklistedItem == false) player.sendMessage("no blacklisted items found");
    12. else player.sendMessage("blacklisted items removed");
     
    Last edited by a moderator: Jul 26, 2018
    InstanceofDeath likes this.
Thread Status:
Not open for further replies.

Share This Page