[Util] SimplyCountdown

Discussion in 'Resources' started by sgavster, Nov 24, 2013.

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

    sgavster

    I've noticed a lot of threads on "how to make a countdown timer" and such.
    This countdown timer is meant to be simple.
    This is meant to have it's own class, here it is:
    PHP:
    public class SimplyCountdown
    {
        private final 
    YOUR_MAIN_CLASS plugin;
        private 
    String prefix ChatColor.translateAlternateColorCodes('&'"&3[&6Prefix&3] &6");
        private 
    int countdownTimer;
     
        public 
    SimplyCountdown(YOUR_MAIN_CLASS i)
        {
            
    this.plugin i;
        }
     
        public 
    void startCountdown(final Player p, final boolean all, final int time, final String msg)
        {
            
    this.countdownTimer Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this.plugin, new Runnable()
            {
                
    int i time;
     
                public 
    void run()
                {
                    if(
    all)
                    {
                        
    Bukkit.broadcastMessage(SimplyCountdown.this.prefix ChatColor.translateAlternateColorCodes('&'msg.replace("#"Integer.toString(i))));
                    }
                    else
                    {
                        
    p.sendMessage(SimplyCountdown.this.prefix ChatColor.translateAlternateColorCodes('&'msg.replace("#"Integer.toString(i))));
                    }
                    
    this.i--;
                    if (
    this.<= 0SimplyCountdown.this.cancel();
                }
            }
            , 
    0L20L);
        }
     
        public 
    void cancel()
        {
            
    Bukkit.getScheduler().cancelTask(this.countdownTimer);
        }
    }
    Remember to replace "YOUR_MAIN_CLASS" with your main class.

    To use this, you would do:
    PHP:
    SimplyCountdown countdown = new SimplyCountdown(plugin/**or 'this' if in your main class*/);
    countdown.startCountdown(playertrue/**if you want it to broadcast the message, or just a certain player*/10/**Amount in seconds*/"&6Countdown starting in &2# &6seconds!" /**# replaces as the number in seconds left*/);
     
     
    An example for a countdown command could be like this:

    PHP:
    public boolean onCommand(CommandSender senderCommand cmdString commandLabelString[] args)
        {
            if(
    cmd.getName().equalsIgnoreCase("countdown"))
            {
                if (
    args.length == 0)
                {
                    if ((
    sender instanceof Player))
                    {
                        
    Player p = (Player)sender;
                        
    p.sendMessage("§4Not enough args!");
                        
    p.sendMessage("§6/countdown <time> <message>");
                        
    p.sendMessage("§6# replaces with the time, an example would be:");
                        
    p.sendMessage("§6/countdown 10 &5Starting in &6# &5seconds!");
                    }
                }
                else if(
    args.length == 1)
                {
                    
    Player p = (Player)sender;
                    
    p.sendMessage("§4Not enough args!");
                    
    p.sendMessage("§6/countdown <time> <message>");
                    
    p.sendMessage("§6# replaces with the time, an example would be:");
                    
    p.sendMessage("§6/countdown 10 &5Starting in &6# &5seconds!");
                }
                else
                {
                    
    Player p = (Player)sender;
                    try
                    {
                        
    int n Integer.parseInt(args[0]);
     
                        if(!(
    >= 0))
                        {
                            
    p.sendMessage("§6You can not use a  negative number!");
                        }
                        else
                        {
     
                            
    String m "";
                            for(
    int i 1args.lengthi++)
                            {
                                
    args[i] + " ";
                            }
                            
    SimplyCountdown c = new SimplyCountdown(plugin); c.startCountdown(ptrueInteger.parseInt(args[0]), m);
                        }
     
                    }
                    catch (
    NumberFormatException e)
                    {
                        
    p.sendMessage("§2" args[0] + " §6is not a number! :(");
                    }
                }
            }
            return 
    false;
        }
     
    //yes, I know a StringBuilder would be better; this was made in a few minutes, and is not a real command.
    //it's recommended to use a StringBuilder, this was just an example.
    Change Log (click to open) (open)

    v1.0

    First release


    If you have any requests, questions, or comments, please let me know!
     
    Joegameplay and BungeeTheCookie like this.
  2. Offline

    BungeeTheCookie

    Great job on this, I may use this in the future. Sorry if I stole your placeholder :D
     
  3. Offline

    sgavster

  4. Offline

    turt2live

    Due to abstraction you don't need YOUR_MAIN_CLASS, just replace it with "Plugin"
     
  5. Offline

    Not2EXceL

    And you also don't need the start countdown method. Its a useless wrapper for the start method
     
  6. Offline

    sgavster

    Not2EXceL I know I don't -need- I just put it there for neat-ness
     
  7. Offline

    Not2EXceL

    sgavster Then you should rename the start method. Unnecessary/useless wrappers create extra calls which is bad practice. Other than that this looks good for making countdowns simpler for people who don't know too much java
     
    sgavster likes this.
  8. Offline

    sgavster

    Not2EXceL Changed it.
    B.T.W thanks for telling me that..
     
  9. Offline

    Not2EXceL

    sgavster you're welcome. I do enjoy helping people with efficiency things that they either miss or don't know themselves
     
  10. Offline

    Ultimate_n00b

    I actually prefer JavaPlugin, but whatever.

    I would like to point out, you are using a Runnable. I suggest switching to a BukkitRunnable, as then you can cancel the cooldown from inside it.
     
  11. Offline

    turt2live

    Using JavaPlugin limits future compatibility :(
     
  12. Offline

    Ultimate_n00b

    It does?
     
  13. Offline

    turt2live

    JavaPlugin extends Plugin.

    In the future Bukkit may add support for some other language (hence the Plugin class) or some other support.
     
Thread Status:
Not open for further replies.

Share This Page