I'm new to plugin coding (but not really new to Java), and I'm having trouble with creating a plugin. I want to make a vote, where players can vote, if the world time or weather should change. Now, I'm finished so far, everything works well. But it only works, when ALL players on the server have voted (if ArrayList equals Bukkit.getOnlinePlayers.count or something).. Now, for sure some players can be inactive and the vote runs into the endless. So I wanted to make something like a timer. Whenever a player has voted, the timer will be reset. And if no player votes for 30 seconds or so, the voting ends and the world time/weather will be changed (or not). But, how would I do such a timer/countdown? Thanks for any help!
Yea, this was my idea, but I need to put the sync task in an event, right? But in which event should I put it?
You could do something like this: Code:java private BukkitTask countdown; public void vote() { //The code that you made when a player votes here //And when that's done: if (countdown != null && countdown instanceof BukkitTask) { //Check if countdown exists countdown.cancel(); //Cancel the current countdown if there's still one } //Create new countdown countdown = Bukkit.getScheduler().runTaskLater(plugin, new Runnable() { @Override public void run() { //This will run if the countdown ends, and if no player has voted for 30 seconds endVote(); } }, 30 * 20); //Run a new countdown, after [Seconds * Ticks] } private void endVote() { Bukkit.broadcastMessage("The vote ended! The result is ..."); //Any code } However, there are 10 different ways to do this. You could also set a timer from the start of the vote, so that people have like 1 minute to vote.
Oh my god, thanks man! I've got extreme headache now and you just rescued my a*s. It works! Just fixing some bugs now and then I'm finally finished. Thanks to everyone!