So I'm coding an OITC plugin for my server(One in the chamber) And I'd like to have a countdown when a player runs the command start. I'm not sure how to make a countdown. If you guys could help that'd be awesome!
That code Aperx gives u is wrong this is a code i made: import java.util.logging.Logger; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; public class Countdown extends JavaPlugin{ public final Logger logger = Logger.getLogger("Minecraft"); public static Countdown plugin; int startCount = 0; @Override public void onDisable() { PluginDescriptionFile pdfFile = this.getDescription(); this.logger.info(pdfFile.getName() + " has been Disabled."); } @Override public void onEnable() { PluginDescriptionFile pdfFile = this.getDescription(); this.logger.info(pdfFile.getName() + " has been Enabled."); } public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { Player player = (Player)sender; if(player.hasPermission("count.allow")); if (commandLabel.equalsIgnoreCase("countdown")) { if (args.length == 0) { player.sendMessage(ChatColor.DARK_RED + "Usage: /countdown <seconds>"); }else{ startCount = Integer.parseInt(args[0]); Bukkit.broadcastMessage(ChatColor.AQUA + " Started a countdown from "); while (startCount != 0) { Bukkit.broadcastMessage(ChatColor.GOLD + " " + startCount); startCount--; try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (startCount == 0) { Bukkit.broadcastMessage(ChatColor.AQUA + "Starting round 1!"); sender.sendMessage(ChatColor.GREEN + "Go!"); } } } return false; } } Like mine one? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
To get this right you must understand how the Minecraft Server and his event loop works. To make this a bit easier i made this picture: What you see here is what Bukkit/Minecraft does within ONE tick (Dont hit me if the order is wrong or i missed some steps inside the loop). For Minecraft not to lag you need > 17-18 of those Ticks (maximum Limit is 20 which is the best score you can get). If you block the Event Loop (which is run in the same Thread as the event you get [as long as its not an async event]) the Minecraft server will halt for the time you Block. For example you have a PlayerJoinEvent listener which gets called from the Network Handle code. Normaly the whole Tick has a execution time of: 1000/20 = 50ms per Tick. Now you go andget the Event and block on it for 1000ms. So you have a Tick timing for this Tick of 1050ms. You have a TPS under 1 and the Minecraft Server will drop you of or even crash (Cant keep up! Has the time changed?). So NEVER Thread.sleep() inside any events. You can use Threads or runTaskAsync and block in there because it doesnt affect any Minecraft Event Loop things (as long you dont try to syncronize these Loops [but i never saw one doing this ]) So for your example this would mean that you Block every second for one second if a command is entered which would lead to a disconnect instantly. You should use the Bukkit sheduler for these kind of works. Code:java import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.command.Command;import org.bukkit.command.CommandSender;import org.bukkit.entity.Player;import org.bukkit.plugin.java.JavaPlugin;import org.bukkit.scheduler.BukkitRunnable; public class Plugin extends JavaPlugin { private class CountdownRunnable extends BukkitRunnable { private Integer startCountdown = 0; public CountdownRunnable(Integer start) { this.startCountdown = start; } @Override public void run() { Bukkit.broadcastMessage(ChatColor.GOLD + " " + startCountdown); startCountdown--; if(startCountdown == 0) { Bukkit.broadcastMessage(ChatColor.GOLD + " Countdown finished"); Bukkit.getScheduler().cancelTask(this.getTaskId()); } } } public void onEnable() { //Tell Bukkit which CommandExecutor should be used for the "countdown" command getCommand("countdown").setExecutor(this); } public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { //Always check if the sender is a player, it also can be the console! if(!(sender instanceof Player)) { sender.sendMessage("This command can only be used by players"); return true; } //Cast the sender into a Player Object Player player = (Player)sender; //Check if the Player has the Permission to access t if(!player.hasPermission("count.allow")) { player.sendMessage("You dont have the permission to do this."); return true; } //Check if the argument is given if(args.length == 0) { player.sendMessage(ChatColor.DARK_RED + "Usage: /countdown <seconds>"); } else { //This could throw a NumberFormatException final Integer startCount = Integer.parseInt(args[0]); Bukkit.broadcastMessage(ChatColor.AQUA + " Started a countdown from "); Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new CountdownRunnable(startCount), 0, 20); } return true; }} Please care this is not tested but it should work. Also here is a example plugin.yml: Code: name: Countdown main: your.name.space.in.java.Plugin description: Simple Countdown version: 0.1.0 commands: countdown: description: Start the countdown usage: /countdown <seconds> permission: count.allow permission-message: You cant start a countdown permissions: count.allow: description: Allows to start a new Countdown If you have any further questions please thag me i dont watch threads. Thanks
My Thread.sleep(1000) Is how fast & how slow the countdown is It can make a bit of server connection bad but it works fine. I tested the plugin & it did begin and count from 10 and look now: View attachment 15972 Maybe my code wasnt so bad EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Learn about threading and general task handling before making a statement like that. Sleeping the main thread will stop the server for that same amount of time. Instead you should be working with ScheduledFuture<> and just using a task that runs every second counting down. You're still insinuating that it's perfectly fine to sleep the main thread... EDIT by Moderator: merged posts, please use the edit button instead of double posting.
I ment the -1 -2 -3 K help the guy dont whant to have a fight here .. So c ya Sorry if my pl is like it im gonna fix it EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Well thanks that you havent read my Posting about the Event Loop within minecraft. It says why its bad to sleep inside Minecraft and that my code is untested. I <3 this Community