Voting time?

Discussion in 'Plugin Development' started by lionking23, Aug 4, 2011.

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

    lionking23

    I have a plugin where you choose what color u want op's username to be, and I want to make ops vote for it.
    I looked up how to make some timers but some people are saying that certain ways to do it aren't safe and I'm a little confused... does anyone know a simple way of how to add like a 40 second or a minute long timer for people to vote... meaning that ops can only vote for a period of 40 secs to 1 min? Any help is greatly appreciated.. thanks.
     
  2. Offline

    bassfader

    Setup a bukkit scheduled task that stops the vote
     
  3. Offline

    IcySeal

    I would use scheduleasyncdelayedtask for the background
    now if u wanted to be really fancy make another object that will count down the vote.(scheduleasyncrepeatingtask)
     
  4. Offline

    lionking23

    Do you know how to make one, and where to put it in my plugin (main class or listener)? I'm not familiar with it.
     
  5. Offline

    Shamebot

    You don't even need a delayed task, I suppose you can start the voting with a command.
    In that command simply set a variable to the current time and check when somebody votes how big the difference between the current time and this variable is.
     
  6. Offline

    lionking23

    So I would have to use the .wait()?
     
  7. Offline

    bassfader

    Don't dare you! :mad:
    That makes the whole thread stop, which could / will mostlikely cause lag.
    (No offense intended, just saying you know :) )

    Just use a bukkit scheduled task like everyone mentioned here before if you want to have the vote timeout and everyone beeing notified, like a message beeing sent "vote expired" or something similar.
    Otherwise do it like Shamebot said, just save the time when the vote was started and check the current time when somebody wants to paricipate in a vote. If the difference between the time when the person wants to paricipate in a vote and the time when the vote was started is too big just send a message to him that he's too late or something similar.

    (For informations on the scheduled task search this forums, there are plenty of threads about it, and read here: http://jd.bukkit.org/doxygen/df/dfa/interfaceorg_1_1bukkit_1_1scheduler_1_1BukkitScheduler.html )
     
  8. Offline

    Shamebot

    No,
    if you have a /startvoting command set a variable to the current time
    Code:
    long curMilli = System.currentTimeMillis();
    and if you have a /vote color command you simply check if the difference
    Code:
    if(System.currentTimeMillis() - curMilli < 60*1000)
    {
        //do stuff
    }
    else
    {
        sender.sendMessage("Too late");
    }
     
  9. Offline

    cholo71796

    Definitely do what @Shamebot said. If you have specific occurrences in which you must know the time, simply compare against a saved time. It's by far the most accurate and simple.
     
  10. Offline

    nisovin

    Using a saved timestamp is a good idea, but in this case it may not be enough. If you want something to actually happen when the time is up (such as changing name colors), you'd want to use a delayed task.
     
  11. Offline

    lionking23

    Thanks guys! I'll ask if I need more help :D

    I decided that when a player tries to change the color, a vote will be called, so when it is called, will curMilli ++; work? Then i put the if statement if curMilli is les than 60*1000, add to a yes voteList or no voteList.

    also, if curMilli ++; is used, how would i stop it from continuing to count?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 18, 2016
  12. Offline

    Shamebot

    Method 1
    Code:java
    1. boolean votingEnabled = false;
    2. public boolean onCommand(...)
    3. {
    4. if( /*test if command is starting command here*/ )
    5. {
    6. votingEnabled = true;
    7. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(/* instance to plugin class here, "this" in main class*/, new Runnable()
    8. {
    9. votingEnabled = false;
    10. //do stuff, set color...
    11. }, 20*60);
    12. }
    13. else if ( /* is voting command*/)
    14. {
    15. if(votingEnabled)
    16. {
    17. //do stuff, cast vote...
    18. }
    19. else
    20. {
    21. sender.sendMessage("Sry too late");
    22. }
    23. }
    24. }

    Method 2
    Code:java
    1. long startTime = -1;
    2. public boolean onCommand(...)
    3. {
    4. if( /*test if command is starting command here*/ )
    5. {
    6. startTime = System.currentMillis();
    7. }
    8. else if ( /* is voting command*/)
    9. {
    10. if(startTime != -1 && System.currentMillis()-startTime < 1000*60)
    11. {
    12. //do stuff, cast vote...
    13. }
    14. else
    15. {
    16. sender.sendMessage("Sry too late");
    17.  
    18. }
    19. }
    20. else if ( /* is command to change something to the voting result */ )
    21. {
    22. //do stuff, set color...
    23. }
    24. }
     
Thread Status:
Not open for further replies.

Share This Page