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.i <= 0) SimplyCountdown.this.cancel(); } } , 0L, 20L); } 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(player, true/**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 sender, Command cmd, String commandLabel, String[] 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(!(n >= 0)) { p.sendMessage("§6You can not use a negative number!"); } else { String m = ""; for(int i = 1; i < args.length; i++) { m = m + args[i] + " "; } SimplyCountdown c = new SimplyCountdown(plugin); c.startCountdown(p, true, Integer.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) (Move your mouse to reveal the content) Change Log (click to open) (open) Change Log (click to open) (close) v1.0 First release If you have any requests, questions, or comments, please let me know!
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 you're welcome. I do enjoy helping people with efficiency things that they either miss or don't know themselves
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.
JavaPlugin extends Plugin. In the future Bukkit may add support for some other language (hence the Plugin class) or some other support.