Solved Scheduler help!

Discussion in 'Plugin Development' started by zakkinu2, Sep 22, 2013.

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

    zakkinu2

    Ok so i have this scheduler but it too long... and if i ever need to add more seconds or something it will be hard to add! Is there a btter way to compact this? This is an example of what my scheduler looks like:
    Code:
                            plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable(){
                                int timer = 30;
                                @Override
                                public void run() {
                                    if (timer > 0) {
                                        if (con.getStringList("bw.players").size() >= 2){
                                            if (timer == 30){Bukkit.broadcastMessage(timer+"");}
                                            if (timer == 29){}
                                            if (timer == 28){}
                                            if (timer == 27){}
                                            if (timer == 26){}
                                            if (timer == 25){}
                                            if (timer == 24){}
                                            if (timer == 23){}
                                            if (timer == 22){}
                                            if (timer == 21){}
                                            if (timer == 20){}
                                            if (timer == 19){}
                                            if (timer == 18){}
                                            if (timer == 17){}
                                            if (timer == 16){}
                                            if (timer == 15){Bukkit.broadcastMessage(timer+"");}
                                            if (timer == 14){}
                                            if (timer == 13){}
                                            if (timer == 12){}
                                            if (timer == 11){}
                                            if (timer == 10){}
                                            if (timer == 9){}
                                            if (timer == 8){}
                                            if (timer == 7){}
                                            if (timer == 6){}
                                            if (timer == 5){Bukkit.broadcastMessage(timer+"");}
                                            if (timer == 4){Bukkit.broadcastMessage(timer+"");}
                                            if (timer == 3){Bukkit.broadcastMessage(timer+"");}
                                            if (timer == 2){Bukkit.broadcastMessage(timer+"");}
                                            if (timer == 1){Bukkit.broadcastMessage(timer+"");}
                                            timer--;
                                        }
                                    }else{
                                        Bukkit.broadcastMessage("GO!");
                                        plugin.getServer().getScheduler().cancelAllTasks();
                                    }
                                }
                            }, 0L, 20L);
     
  2. Offline

    Compressions

    zakkinu2 Holy repetition! You can get rid of every if statement that doesn't involve broadcasting a message, and instead of the else statement, check if timer == 0.
     
  3. Offline

    zakkinu2

    Compressions i did this
    Code:
                            plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable(){
                                int timer = 30;
                                @Override
                                public void run() {
                                    if (timer > 0) {
                                        if (con.getStringList("bw.players").size() >= 1){
                                            if (timer == 30){Bukkit.broadcastMessage(timer+"");}
                                            if (timer == 15){Bukkit.broadcastMessage(timer+"");}
                                            if (timer == 5){Bukkit.broadcastMessage(timer+"");}
                                            if (timer == 4){Bukkit.broadcastMessage(timer+"");}
                                            if (timer == 3){Bukkit.broadcastMessage(timer+"");}
                                            if (timer == 2){Bukkit.broadcastMessage(timer+"");}
                                            if (timer == 1){Bukkit.broadcastMessage(timer+"");}
                                            if (timer < 1){
                                                Bukkit.broadcastMessage(timer+"");
                                                plugin.getServer().getScheduler().cancelAllTasks();
                                            }
                                            timer--;
                                        }
                                    }
                                }
                            }, 0L, 20L);
    
    It only broadcasts the number 30 and it stops
     
  4. Offline

    Compressions

    zakkinu2 From what it looks like, it shouldn't be stopping.
     
  5. Offline

    zakkinu2

    Compressions It always does this to me... is there any better way to do it? i know that there is somethign with switch or whatever but i dont understand it at all
     
  6. Offline

    Compressions

    zakkinu2 It won't really make a difference in if it works or not, but:
    Code:
    switch(timer) {
    case 30: Bukkit.broadcastMessage(timer + "");
    break;
    case 15: Bukkit.broadcastMessage(timer + "");
    break;
    default: break;
    }
     
  7. Offline

    zakkinu2

    Compressions ok now it works with that. What does the break; do
     
  8. Offline

    Compressions

    zakkinu2 It terminates the switch statement.
     
  9. Offline

    zakkinu2

  10. Offline

    Compressions

  11. Offline

    zakkinu2

    Compressions I just found a better way to compact it by a little. but it doesnt really matter:
    Code:
                                            switch (timer){
                                                case 30:
                                                case 15:
                                                case 5:
                                                case 4:
                                                case 3:
                                                case 2:
                                                case 1:
                                                Bukkit.broadcastMessage(timer+"");
                                                break;
                                                default : break;
                                            }
     
  12. Offline

    JPG2000

    zakkinu2 It wont work if you have any code in any od the case's, it will keep on going through.
     
  13. Offline

    zakkinu2

Thread Status:
Not open for further replies.

Share This Page