Timers

Discussion in 'Plugin Development' started by stefvanschie, May 26, 2015.

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

    stefvanschie

    Hey,

    I want to create a timer like this.

    Timer that loops every seconds
    {
    if (seconds == 0)
    {
    //stop timer
    }
    seconds--;
    }

    How can I do this the best way? I like to have it in the same class, but it's not a problem if thats not possible or something
     
  2. Offline

    mine-care

  3. Moved to Plugin Development,
     
  4. Offline

    stefvanschie

    @mine-care
    Thanks, but which one on that page should I use, cause I don't find it really clear, which one repeats and I can cancel inside the runnable (if heard several problems with that when I was looking for this).
     
  5. Offline

    guitargun

    @stefvanschie use an int outside the runnable with the times it should repeat (or seconds) then everytime the runnable goes the int will decrease by 1. then when it reaches 0 you can cancel the event from within.. what I did in such situations is a delayed task and I recall the method if it needs to be repeated and just don't recall when it ends. don't forget to decrease the value of the int each time with the task otherwise you have an infinite loop.
     
  6. Offline

    stefvanschie

    @guitargun That's a usefull trick! Didn't think of that, but I'll try tomorrow. I have good hope.

    @guitargun
    It doesn't remove the seconds and it starts at zero, while in my config it says timer: 300
    What am I doing wrong here?

    Code:
       
    int seconds = config.getInt("timer");
    public void timer()
        {
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            scheduler.scheduleSyncDelayedTask(this, new Runnable()
            {
                @Override
                public void run()
                {
                    Bukkit.broadcastMessage(seconds + "");
                    if (seconds == 60 || seconds == 30 || seconds == 15 || (seconds >= 1 && seconds <= 10))
                    {
                        for (Player pl : players)
                        {
                            pl.sendMessage(ChatColor.GOLD + "You have " + seconds + " seconds left!");
                        }
                    }
                    else if (seconds == 0)
                    {
                        for (Player pl : players)
                        {
                            pl.sendMessage(ChatColor.GOLD + "The time to build is over!");
                        }
                        String worldstr = arenas.getString("main-spawn.world");
                        World world = getServer().getWorld(worldstr);
                        int x = arenas.getInt("main-spawn.x");
                        int y = arenas.getInt("main-spawn.y");
                        int z = arenas.getInt("main-spawn.z");
                        Location location = new Location(world, x, y, z);
                        players.clear();
                        for (Player pl : players)
                        {
                            pl.teleport(location);
                            pl.sendMessage(ChatColor.GOLD + "Game done!");
                        }
                        players.clear();
                    }  
                    seconds--;
                    timer();
                }
            }, 20L);
     
    Last edited by a moderator: May 28, 2015
Thread Status:
Not open for further replies.

Share This Page