Countdown

Discussion in 'Plugin Development' started by CompuIves, Apr 12, 2012.

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

    CompuIves

    Hi! Here I am again with a new question xD.

    I'm finishing my HungerGames plugin, I got almost everything I need, there is though one point that's really irritating me.
    When I want the game to start (using /hg start) I want to set a countdown, counting from 60 seconds, with this while the countdown is ticking:

    60 seconds: Bukkit.getServer.broadcast("60 seconds left");
    30 seconds: Bukkit.getServer.broadcast("30 seconds left");
    10 seconds: Warp every player (got the method for that)
    Bukkit.getServer.broadcast("10 seconds left");

    and then a countdown from 10 to 0, I've looked at many plugin sources/tutorials but I still don't understand how I need to make this...
    Could someone clear it up for me? :S
    Thanks in advance!
     
  2. Offline

    Father Of Time

    The following should work, try using something like this:

    Code:
        if(( X % 10 ) == 0 && X > 10 )
        {
            //if above 10 and divisable by 10 message here
        }
     
        if( X < 10 )
        {
            //if above below 10 message here (regardless of divisibility
        }
    
    The first condition only triggers when the variable "X" is evenly divisible by 10 and greater then ten, the second condition messages every tick, regardless of the divisibility.

    Your timing was perfect, I only learned this yesterday when trying to figure out a way to stop spamming a persons console when notifying them of block breaks; my system now only notifies them on every 10th block break (they are working off a prison sentence).

    Anywho, I hope this helps... Good luck!
     
  3. Offline

    CompuIves

    Okay, so I got to place this in a timer?
     
  4. Offline

    Father Of Time

    Well no, you make a timer that ticks every second, and then when ever it ticks, inside it's runnable, you subtract 1 from a "countdown" integer variable, then you call a function that contains the above code to evaluate the countdown variable, if it's either dividable by 10 or below 10 it will message, otherwise it will just continue and wait for the next game tick.

    The timer is a continual loop that just keeps triggering events, but the actions should be taken by a "CountDown" function and a "Notification" function. However, this is only my perspective on the project, and it could likely be handled in other fashions.
     
  5. Offline

    CompuIves

    Aw, I've read that too late XD.
    I've made a Countdown class:
    Code:
    public class Countdown
      implements Runnable
    {
       
      HungerGames plugin;
      private int ticks;
     
      public Countdown(HungerGames instance, int ticks)
      {
        plugin = instance;
        this.ticks = ticks;
      }
     
      public void run() {
          int counter = 0;
          if(( this.ticks % 10 ) == 0 && ticks > 10 )
            {
                this.ticks--;
              counter = ticks + 1;
                Bukkit.getServer().broadcastMessage(ticks + " seconds left!");
            }
          else if (ticks == 10) {
              this.ticks--;
              counter = ticks + 1;
              plugin.getCommander().Warp();
          }
          else if( ticks < 10 )
            {
                this.ticks--;
                counter = ticks + 1;
                Bukkit.getServer().broadcastMessage(ticks + " ");
            }
          else if(ticks == 0)
                plugin.Frozen.clear();
      }
    }
    and way to call it:
    Code:
    if (args[0].equalsIgnoreCase("Start")) {
       Bukkit.getServer().broadcastMessage(ChatColor.RED + "Get ready for The Hunger Games!!");
    Bukkit.getServer().broadcastMessage(ChatColor.RED + "You will be teleported in 1 minute, please be ready.");
    Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Countdown(plugin, 60), 20L);
    return true; //call successful
    But it calls it only one, does someone have a solution for this?

    Gah, I've searched on many places, but I still haven't found a solution :S.
    The Hunger Games will start soon, so please, can somebody help me with this problem?

    Edit: Ow lol, I think I got the solution xD.

    Ok, it still doesn't work, but I've changed ALOT of code, just to test things out, now I have a command:
    Code:
    public class HungerGamesCommander implements CommandExecutor {
        private HungerGames plugin;
        public int ticks = 60;
     
        public HungerGamesCommander(HungerGames instance) {
            plugin = instance;
        }
     
    ...
     
                    if (args[0].equalsIgnoreCase("Start")) {
                        plugin.getServer().broadcastMessage(ChatColor.RED + "Get ready for The Hunger Games!!");
                        plugin.getServer().broadcastMessage(ChatColor.RED + "You will be teleported in 1 minute, please be ready.");
                        plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() {
                              public void run() {
                                  if (ticks != -1) {
                                    if(ticks != 0) {
                                          if(( ticks % 10 ) == 0 && ticks > 10 )
                                    {
                                      Bukkit.getServer().broadcastMessage(ticks + " seconds left!");
                                      ticks--;
                                    }
                                      if (ticks == 10) {
                                      plugin.getCommander().Warp();
                                      ticks--;
                                  }
                                      if( ticks < 10 )
                                    {
                                        Bukkit.getServer().broadcastMessage(ticks + "");
                                        ticks--;
                                    }
                                      }
                                      else  {
                                        Bukkit.getServer().broadcastMessage("Let the Hunger Games begin!");
                                        plugin.Frozen.clear();
                                  }
                                    }
                              }
                        }, 0L, 20L);
                    }
    When I start the code, I only get "60 seconds left!". And after that nothing...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  6. Offline

    dsmyth1915

    You may want a different class for the timer, and call it in the commandexecutor on /hg <whateverhere>. I'm not great at runables, but I'm pretty sure the class needs to extend Runnable. I'll look more into it and get back to you.
     
  7. Offline

    CompuIves

    Thank you very much!
     
Thread Status:
Not open for further replies.

Share This Page