Solved Didn't work but you know.

Discussion in 'Plugin Development' started by KmanCrazy, May 21, 2014.

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

    KmanCrazy

    How would I stop the for statement before it iterates through all of the code and sends double sometimes triple messages.

    Code:java
    1. @EventHandler
    2. public void onChat2(AsyncPlayerChatEvent e) {
    3. for (String s : list) {//How would I stop this so it doesnt spam the message that they have sent?
    4. if (e.getMessage().contains(s.toLowerCase()) || e.getMessage().contains(s.toUpperCase())) {
    5. String badword = s;
    6. e.getPlayer().sendMessage("The word " + badword + " is not allowed on this server!");
    7. e.setCancelled(true);
    8.  
    9. } else {
    10.  
    11.  
    12. for (Player p : Bukkit.getOnlinePlayers()) {
    13.  
    14. p.sendMessage(ChatColor.GRAY + e.getPlayer().getName() + " ➽ " + e.getMessage());
    15.  
    16. }
    17. e.setCancelled(true);
    18. }
    19.  
    20. }
    21. }
    22.  
     
  2. Offline

    MrSparkzz

    KmanCrazy
    Code:java
    1. for (int i = 0; i < list.size(); i++) {
    2. String s = list.get(i);
    3. }
     
  3. Offline

    KmanCrazy

    MrSparkzz
    I don't think that is correct because I can't end it without it going into the else statement. How would I end it at the b4 the else?
     
  4. Offline

    MrSparkzz

    KmanCrazy
    Actually.. you know what, you can just do break; to exit any loop. I don't know why I didn't think of that..
     
  5. Offline

    SnipsRevival

    KmanCrazy break and continue are two very useful keywords to know when dealing with loops. The keyword break will immediately terminate the loop and continue on through the code. The keyword continue will immediately proceed to the next iteration of the loop. It sounds like you want to use break in your code.

    Your other option, if you do not want to use break, is to try and be clever with your termination condition statement when you are declaring the for loop. Although you are using a for each loop, so this isn't really an option in this case.
     
  6. Offline

    1Rogue

    ....why not just use "return".
     
  7. Offline

    Rocoty

    1Rogue In this particular case that would work, yes. Although I'd still encourage using break since you'll never know when you need to add extra code to that method. Plus it makes your intent more clear
     
  8. Offline

    1Rogue

    break statements are often discouraged by standards as it shifts the flow of the method unexpectedly (similar to goto / continue). My own rule of thumb is that if you have to use a break or continue, it should likely be moved to a separate method.
     
  9. Offline

    Rocoty

    1Rogue See, I can see where you're coming from. But you're saying break will shift the flow of the method whereas return will not. I would say both of them do shift the flow of the method, but to unexpectedly. A loop has a clear beginning and end, in the form of brackets. Break simply ends the loop, much like returning a method.
     
  10. Offline

    teej107

    1Rogue KmanCrazy I don't like break either. I would change your for-each loop to a for loop, then add in a boolean before your loop. When your if condition if met, change the boolean. Also add in the boolean to your if condition.
     
  11. Offline

    Rocoty

    teej107 So, continue looping even if condition is met? Seems rather unnecessary
     
    rsod and 1Rogue like this.
  12. Offline

    teej107

    Rocoty Your if condition would return false because of the boolean so it wouldn't execute the code inside. But now that I think about it, a better way would to set your integer in your for loop so it will fail the for loop's test when the condition is met. No boolean needed.
    Code:java
    1. for(int i = 0; i < 123; i++)
    2. {
    3. if(conditionIsMet)
    4. {
    5. i = 123;
    6. }
    7. }
     
  13. Offline

    Rocoty

    teej107 And how does that make the code more clear than using break?
     
  14. Offline

    theguynextdoor

    I'd use a break if I wanted to add additional code after the for loop has been run, or a return if that was all I really wanted that method to do. The difference in this case would be insignificant and would be no downside to using either.

    Personally, here, I would actually have a return in the if statement where the bad word has been found. I would then remove the else statement and put the for loop which is in the else statement outside the first for loop. This means that if a message has been found the contain a bad word then the code inside the if statement is ran, and I would return the method there after cancelling the event. Otherwise if there was no bad words, then the code inside the if statement would never be ran and the loop would finish and would then go on to run the second loop of sending players the message (Although use of Bukkit.broadcastMessage(String message) would be more suited than your second for loop).
     
  15. Offline

    teej107

    Rocoty I just said that I don't like break. It's not a bad statement to use. I just find it kind of lazy to use and you could easily miss important code while using it. By all means, use it if you want. I just prefer not to use them.
     
  16. Offline

    Garris0n

    Eh, I often use continue to loops cleaner instead of cluttering it with if statements, similar to using return to exit the method early instead of nesting tons of if statements.

    The break statement, on the other hand, just generally appears ambiguous. I don't like it.
     
Thread Status:
Not open for further replies.

Share This Page