Check if Task is running

Discussion in 'Plugin Development' started by Oxore99, Aug 23, 2015.

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

    Oxore99

    Hello guys,
    I'm having troubles with a repeating task: how can I check if the task is running or not?

    Example:
    Code:
    public class Timer extends BukkitRunnable implements Listener {
    
    int t = 20;
    
    
    
    
      public void run()
      {
      
        if (this.t == 20) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t);
          // Task is running
    
    
        }
        if (this.t == 10) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t + " seconds remaining");
    
        }
        if (this.t == 5) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t " seconds remaining");
              }
        if (this.t == 4) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t " seconds remaining");
        }
        if (this.t == 3) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t " seconds remaining");
        }
        if (this.t == 2) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t " seconds remaining");
    
        }
        if (this.t == 1) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t " second remaining");
    
        }
        if (this.t == 0) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.AQUA + "FIIIGHTTT!" );
          cancel();
          // Task is finished
    
        }
        else
        {
          this.t -= 1;
        }
      }
    
    
    
    
        @EventHandler
      public void onPlayerJoin(PlayerJoinEvent e)
      {
       
          Player p = e.getPlayer() ;
        if (// check if task is running)
        {
            p.setGameMode(GameMode.ADVENTURE);
            Location loc = new Location(Bukkit.getWorld("world"), 0.0D, 77.0D, 0.0D);
            p.teleport(loc);
            p.setGameMode(GameMode.SPECTATOR);
        }
        else { // if  is not running
            p.setGameMode(GameMode.SPECTATOR);
        }
      }
    
    
    }
    
    I tried this, but didn't work:
    Code:
    public class Timer extends BukkitRunnable implements Listener {
    
    int t = 20;
    
    
        public boolean isRunning = false;
    
      public void run()
      {
      
        if (this.t == 20) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t);
          isRunning = true // Task is running
    
    
        }
        if (this.t == 10) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t + " seconds remaining");
    
        }
        if (this.t == 5) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t " seconds remaining");
              }
        if (this.t == 4) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t " seconds remaining");
        }
        if (this.t == 3) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t " seconds remaining");
        }
        if (this.t == 2) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t " seconds remaining");
    
        }
        if (this.t == 1) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t " second remaining");
    
        }
        if (this.t == 0) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.AQUA + "FIIIGHTTT!" );
          isRunning = false;  // Task is finished
          cancel();
    
    
        }
        else
        {
          this.t -= 1;
        }
      }
    
    
    
    
        @EventHandler
      public void onPlayerJoin(PlayerJoinEvent e)
      {
       
          Player p = e.getPlayer() ;
        if (isRunning = true // check if task is running)
        {
            p.setGameMode(GameMode.ADVENTURE);
            Location loc = new Location(Bukkit.getWorld("world"), 0.0D, 77.0D, 0.0D);
            p.teleport(loc);
            p.setGameMode(GameMode.SPECTATOR);
        }
        else { // if  is not running
            p.setGameMode(GameMode.SPECTATOR);
        }
      }
    
    
    }
    
    I think it's simple as well, but I spent hours on it.. :'(
     
  2. Offline

    Zombie_Striker

    @Oxore99
    First, lets fix your code
    Code:
    int t = 20;
    
    
        public boolean isRunning = true;// has to be true for testing
    
      public void run(){  
    if(isRunning){
        if (this.t == 20) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t);
          isRunning = true // Task is running
        }
        if (this.t == 10) 
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t + " seconds remaining");
        if (this.t == 5) 
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t " seconds remaining");
        if (this.t == 4) 
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t " seconds remaining");
        if (this.t == 3) 
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t " seconds remaining");
        if (this.t == 2) 
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t " seconds remaining");
        if (this.t == 1) 
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t " second remaining");
    
        if (this.t == 0) {
          Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.AQUA + "FIIIGHTTT!" );
          isRunning = false;  // Task is finished
          cancel();
        } else
          this.t -= 1;
      }else{
    System.out.println("Not running");
    }
    
    test this, see if this will work.
     
  3. Offline

    SkyleTyler1337

    @Zombie_Striker You dont need to set it running if it already is.

    Code:
    
    int t = 20;
    
        boolean isRunning = false;
    
        public void run() {
            if (this.t == 20) {
                Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t);
                /** set running to true */
                isRunning = true;
            }
            if (this.t == 10)
                Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t + " seconds remaining");
            if (this.t == 5)
                Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t + " seconds remaining");
            if (this.t == 4)
                Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t + " seconds remaining");
            if (this.t == 3)
                Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t + " seconds remaining");
            if (this.t == 2)
                Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t + " seconds remaining");
            if (this.t == 1)
                Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.GREEN + this.t + " second remaining");
            if (this.t == 0) {
                Bukkit.broadcastMessage(ChatColor.AQUA + "[Timer] " + ChatColor.AQUA + "FIIIGHTTT!");
                isRunning = false; // Task is finished
                cancel();
            } else {
                this.t -= 1;
            }
        }
    
    
        /** will return true if its running */
        public boolean isRunning(){
            return this.isRunning;
        }
    }
    
    this may not be 100% correct



    for a reference this how my timers work.

    Code:
    @Override
        public void run() {
            /** check if the seconds not equal to zero */
            if (this.sec != 0) {
                this.sec--;
                this.match.setState(MatchState.START);
                if (this.sec % 5 == 0 && this.sec > 5 || this.sec <= 5 && this.sec != 0) {
                    Bukkit.getServer().broadcastMessage(status(this.sec));
                }
            } else {
                this.match.start();
                Bukkit.getScheduler().cancelTask(this.timer);
                setFinished(true);
            }
        }
    
        private String status(int sec) {
            String seconds = null;
            if (sec != 1) {
                seconds = GREEN + " seconds ";
            } else {
                seconds = GREEN + " second ";
            }
    
            return GREEN + "Match starting in " + DARK_RED + "" + this.sec
                    + seconds + "";
        }
    Last Edit: just check if field of "t" is not equal to 0.

    just like this below:

    Code:
     
    /** if its not 0 it will return true otherwise false */
    public boolean isRunning(){
        return this.t != 0;
    }
    
     
    Last edited: Aug 23, 2015
  4. Offline

    Oxore99

    Thanks for answers..but still deosn't work :
    Code:
      public boolean isRunning(){
          return this.t != 0;
      }
     
      @EventHandler
      public void onPlayerJoin(PlayerJoinEvent e)
      {
          
          Player p = e.getPlayer() ;
        if (isRunning = true)
        {
            p.setGameMode(GameMode.ADVENTURE);
            Location loc = new Location(Bukkit.getWorld("world"), 0.0D, 77.0D, 0.0D);
            p.teleport(loc);
        }
        else if(isRunning = false)
        {
            p.setGameMode(GameMode.SPECTATOR);
            Location loc = new Location(Bukkit.getWorld("world"), 0.0D, 77.0D, 0.0D);
            p.teleport(loc);
        }
      }
    It can't works, because , when the task is cancelled (so isRunning = false) , t =20

    Code:
    int t = 20;
    I would create "states", like: 1-Lobby (Player join in Adventure GM), 2-Game (Player who join during this state is spectator).. :/
     
  5. Offline

    FabeGabeMC

    @Oxore99 I personally don't suggest making your Thread/Runnable inside of your Listener (or vice-versa). That might be the problem.
     
  6. Offline

    Oxore99

    Still not working:
    Code:
      @EventHandler
      public void onJoin(PlayerJoinEvent e) {
          Player p = e.getPlayer() ;
          if(Bukkit.getScheduler().isCurrentlyRunning(t)) {
              p.setGameMode(GameMode.ADVENTURE);
          }
          else {
              p.setGameMode(GameMode.SPECTATOR);
          }
      }
    
    
    Always spectator
     
  7. @Oxore99
    Because 't' is a counter, not the task's id. Use BukkitRunnable#getTaskId() to get it's task id
     
  8. Offline

    Oxore99

    Code:
     @EventHandler
      public void onJoin(PlayerJoinEvent e) {
          int run = this.getTaskId() ;
          Player p = e.getPlayer() ;
          if(Bukkit.getScheduler().isCurrentlyRunning(run)) {
              p.setGameMode(GameMode.ADVENTURE);
          }
          else {
              p.setGameMode(GameMode.SPECTATOR);
          }
      }
    

    Code:
    [12:53:38 ERROR]: Could not pass event PlayerJoinEvent to OxHg v0.1b
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.PlayerList.onPlayerJoin(PlayerList.java:298) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.PlayerList.a(PlayerList.java:157) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.LoginListener.b(LoginListener.java:144) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.LoginListener.c(LoginListener.java:54) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.NetworkManager.a(NetworkManager.java:231) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.ServerConnection.c(ServerConnection.java:148) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:813) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_51]
    Caused by: java.lang.IllegalStateException: Not scheduled yet
        at org.bukkit.scheduler.BukkitRunnable.getTaskId(BukkitRunnable.java:134) ~[spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
        at fr.oxore.oxhg.StartTimer.onJoin(StartTimer.java:64) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_51]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_51]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_51]
        at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_51]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8.jar:git-Spigot-fdc1440-53fac9f]
        ... 14 more
    I'm fed up with this runnable..
     
  9. put it in a try-catch block, if it fails then it isn't running? :p
     
  10. Offline

    hexaan

    @Oxore99

    Code:
    if (isRunning = true) //You are setting the isRunning variable to true
    if(isRunning == true)//This is checking if the value of isRunning is true
    if(isRunning)//Or shorter
    
    With your code it will always go into your first if statement
     
  11. Offline

    Oxore99

    Thanks, it helped , but, at the beggining :
    Code:
    int t = 20;
    So
    Code:
    public boolean isRunning {
    return t!=;
    }
    will always return false because of int t=20 ..

    Same thing with
    Code:
    public boolean {
    return false;
    } 
    , because when it checks , it checks the static value..
     
  12. @Oxore99
    Ugh, it probably throws that because the instance you schedule is probably different from the one you register your events at. And next time tahg me please
     
Thread Status:
Not open for further replies.

Share This Page