World tick event?

Discussion in 'Plugin Development' started by Clanky, Feb 22, 2014.

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

    Clanky

    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!
     
  2. Offline

    RawCode

    sync task with 1 tick delay
     
    Clanky likes this.
  3. Offline

    Clanky

    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?
     
  4. Offline

    Stoux

    You could do something like this:

    Code:java
    1. private BukkitTask countdown;
    2.  
    3. public void vote() {
    4. //The code that you made when a player votes here
    5. //And when that's done:
    6.  
    7. if (countdown != null && countdown instanceof BukkitTask) { //Check if countdown exists
    8. countdown.cancel(); //Cancel the current countdown if there's still one
    9. }
    10.  
    11. //Create new countdown
    12. countdown = Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
    13.  
    14. @Override
    15. public void run() {
    16. //This will run if the countdown ends, and if no player has voted for 30 seconds
    17. endVote();
    18. }
    19. }, 30 * 20); //Run a new countdown, after [Seconds * Ticks]
    20. }
    21.  
    22. private void endVote() {
    23. Bukkit.broadcastMessage("The vote ended! The result is ...");
    24. //Any code
    25. }


    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.
     
    Clanky likes this.
  5. Offline

    Clanky

    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!
     
Thread Status:
Not open for further replies.

Share This Page