Auto-Broadcaster crashing server

Discussion in 'Plugin Development' started by Adrian Pasternak, Sep 14, 2016.

Thread Status:
Not open for further replies.
  1. Hello,
    I'm making an auto broadcaster plugin, but it's putting to much strain on the server so it crashes. I haven't really started working with threads yet so I appreciate the help!
    Code:
    public class Broadcast extends JavaPlugin{
    
        YamlConfiguration config;
         public void onEnable() {
             config = (YamlConfiguration) getConfig(); 
    
             if(!new File(getDataFolder(), "config.yml").exists()){
                 saveDefaultConfig();
             }
         
         
          BukkitScheduler scheduler = getServer().getScheduler();
          scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
              @Override
              public void run() {
                     
                 
                  int counter = 0; 
                  if(counter == 0){
                      Bukkit.broadcastMessage("§7-------------------------------------------");
                      Bukkit.broadcastMessage("§9# §7Remember, don't log off in the mines! ");
                      Bukkit.broadcastMessage("§7#              §7- §9Mine§7Zone              ");
                      Bukkit.broadcastMessage("§7-------------------------------------------");
                      counter++;
                      try {
                        Thread.sleep(45000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                     
                  }
                  if(counter == 1){
                      Bukkit.broadcastMessage("§7----------------------------------");
                      Bukkit.broadcastMessage("§9#  §7Check out our website!      ");
                      Bukkit.broadcastMessage("§7#          http://minezone.net/  ");
                      Bukkit.broadcastMessage("§9#              §7- §9Mine§7Zone  ");
                      Bukkit.broadcastMessage("§7-----------------------------------");
                      counter++;
                      try {
                        Thread.sleep(45000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                     
                      }
                 
                  if(counter == 2){
                      Bukkit.broadcastMessage("§7----------------------------------");
                      Bukkit.broadcastMessage("§9#  §7Don't forget to vote!      ");
                      Bukkit.broadcastMessage("§7#          /vote  ");
                      Bukkit.broadcastMessage("§9#              §7- §9Mine§7Zone  ");
                      Bukkit.broadcastMessage("§7-----------------------------------");
                      counter++;
                      try {
                        Thread.sleep(45000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                     
                     
                  }
                  if(counter == 3){
                      Bukkit.broadcastMessage("§7----------------------------------");
                      Bukkit.broadcastMessage("§9# §7Found a bug? Report it to staff or on the forums!");
                      Bukkit.broadcastMessage("§7#    http://minezone.net/   ");
                      Bukkit.broadcastMessage("§9#              §7- §9Mine§7Zone  ");
                      Bukkit.broadcastMessage("§7-----------------------------------");
                      counter = 0; 
                      try {
                        Thread.sleep(45000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                                 
                  }
                 
          }
          }, 0L, 1000L);
    }
         
         
    }
    
           
    
    
    
     
  2. Offline

    Jakeeeee

    Don't sleep the server's thread, use a runnable and add to the variable counter.
     
  3. Offline

    Zombie_Striker

    @Adrian Pasternak
    Don't use sleep on the main thread. That purposefully stops the thread. Instead, change the repeat delay to the delay you want.
     
  4. Offline

    ArsenArsen

    Sleeping for 45 seconds??? No wonders it crashes. It expects a tick once every 50 millis, and delaying that for other 45000 millis means all events go out of sync.
     
    MordorKing78 likes this.
Thread Status:
Not open for further replies.

Share This Page