Scheduler

Discussion in 'Spigot Plugin Development' started by drhopeness, Apr 15, 2017.

  1. Hi, i'm writing a minigame plugin. I use a scheduler for pretime(Game will start in 60 seconds). It's counting down but it doesn't stop. It contiunes like "Game will start in -1 minute"

    Code (Text):
    package net.teamenderss;

    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.teamenderss.Util.ChatUtil;



    public class Main extends JavaPlugin{

        public static FileConfiguration config,data;
        public static Main pl;
       
       
       
        public static int preGamePID,GamePID;
        public static int presure,oyunsure;
       
        public void onEnable(){
            pl = this;
           
            Logger log = Bukkit.getLogger();
            log.info(ChatColor.YELLOW+"--------------------------------");
            log.info(ChatColor.YELLOW+ "DALGA Plugini Aktif");
            log.info(ChatColor.YELLOW+"--------------------------------");
       
            registerEvents();
            Ayarlar.getInstance().setup(this);
            configler();
            presure = getConfig().getInt("Ayarlar.oyunsuresi") + 60;
            GeriSayimBasla();
           
        }

        private void registerEvents() {
            PluginManager pm = Bukkit.getServer().getPluginManager();
           
        }
       
        public void configler(){
            config = getConfig();
            saveDefaultConfig();
            data = Ayarlar.getInstance().getData();
        }
       
       
        public static void GeriSayimBasla(){
            preGamePID = Bukkit.getScheduler().scheduleSyncRepeatingTask(pl, new Runnable(){
               
               
           
                    public void run() {
                        if(presure % 60 == 0){
                            ChatUtil.broadcast("Oyun " + presure/60 + " dakika sonra başlayacaktır.");
                            //Translate = ChatUtil.broadcast("Game will start in "+ presure/60 + "minutes" )
                        }
                       
                        if(presure == 45 || presure == 30 || presure == 15 || (presure <= 10) && (presure >= 0)){
                            ChatUtil.broadcast("Oyun " + presure + " saniye içinde başlayacaktır.");
                            //Translate = ChatUtil.broadcast("Game will start in "+ presure + "seconds." )
                        }
                       
                        if(presure == 0){
                            basla();
                        }
                        presure--;
                    }
                }
               
            , 0, 20);
        }
       
       
        public static void basla(){
            ChatUtil.broadcast("Oyun Başladı.");
        }
       
        public static void durdur(){
           
        }
       
       
    }
     
    How can i stop the scheduler when it counts 0?
     
  2. Type cancel(); to stop a schedular
     
  3. You are literally asking Bukkit to run a repeating task. I am sure there are alternatives to this without having to cancel(); it.
    Beside that matter, a friendly piece of advice; Always use English terms in your code. Output such as messages in the chat might be your own language, but for the sakes of readability and (possibly) cooperation with others, you should always use English in your code.
    scheduleSyncDelayedTask();
     
  4. what are these alternatives to stop task?
     
  5. scheduleSyncDelayedTask instead of scheduleSyncRepeatingTask.
     
  6. I'm already using scheduleSyncRepeatingTask
     
  7. Try this:

    Code (Text):

    Bukkit.getScheduler().runTaskTimer(plugin, new BukkitRunnable() {
       
        int cancel = 30;
       
        @Override
        public void run() {
            if(cancel <= 0) {
                this.cancel();
            }
           
            cancel--;
        }
    }, 0L, 20L);
     
     
  8. There are multiple ways to do this, just try to call the cancel() method on your task like the others suggested. My idea might be not suitable for your project. There are a couple of timer related classes out there to choose from too, if this isn't the right fit for ya!
    Good luck
     
  9. I didn't understand clearly, how can i stop count down when time is at 0?
    NOTE = When i try call cancel() method, there isn't any method called cancel.
     
  10. I think you misunderstood what those do
     
  11. No I know what they do, I just thought that maybe if he couldn't cancel(); the scheduled task, he could perhaps make another task to remove that task. But yeah that's a little bit unnecessary right now, cancel(); indeed is the best option to go with together with some conditional logic.
    Good luck!
     
    • Like Like x 1