Adding in timers to mah pluginz

Discussion in 'Plugin Development' started by JOPHESTUS, Aug 24, 2012.

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

    JOPHESTUS

    Hey,

    Some users have requested that on my plugin (JOPHWarn) after a certain amount of time (24 hours) of being warned, the warning will be erased.

    evilmidget38
     
  2. Offline

    skore87

    I don't believe a scheduler is appropriate given the amount of time you're asking for. The server could easily go offline and thus causing a warning to not be removed. I would suggest recording the time and date the player is warned either with yaml, sqlite or mysql and comparing their time with the current time when you need to.
     
    Ne0nx3r0 likes this.
  3. Offline

    Drew1080

    Yeah I was thinking about that after when I posted it.
     
  4. Offline

    Theodossis

    I use Thread.sleep(milliseconds);
     
    Ne0nx3r0 likes this.
  5. Offline

    DrAgonmoray

    Use the sheduler and check every so often (maybe 5 minute) if the 24 hours has passed
     
  6. Offline

    skore87

    I honestly hope you don't do that in the main thread. The schedulers are there for a reason, use them instead of sleeping a thread unless you, for some unique reason, have to use it.
     
    DrAgonmoray likes this.
  7. Offline

    sternmin8or

    I chuckled
     
  8. Offline

    Barinade

    System.currentTimeMillis();

    Aaaaand that's all I have to say D:
     
  9. Offline

    smilne74

    This is mostly pseudo code, but should get you started.

    When you place someone on a warning, add a "timer" to your storage with their name (I'll let you figure that out based on your data storage system)

    Use the following to create your timer value:

    Code:
    // This math is just to help visualize the timer conversion.
    // Typically you would do (24 * 60 * 60 * 1000)
    int hours = 24;
    int minutes = hours * 60;
    int seconds = minutes * 60;
    int milliseconds = seconds * 1000;
     
    long warningTimer = (System.currentTimeMillis() + milliseconds);
    
    Then set this value somewhere with their name.


    Create a listener for when they log in. In the listener do an initial check to see if they're still on timer, and if they are, setup a scheduler run this check again every so often. The scheduler would remove the timer while they are logged in should they be on for an extended period of time. If the server is restarted, the timer still remains in storage and will be checked again upon logging back in.


    To check to see if they're still on warning or not:

    Code:
    // do whatever you need to do to get out the stored timer
    long warningTimer = ????
     
    // check the timer against the current server time
    if (warningTimer > System.currentTimeMillis()) {
     
    // get the amount of time remaining on the warning
    long remaining = (warningTimer - System.currentTimeMillis());
     
    // possibly tell them they're still on warning and have to wait "remaining" time.
     
    } else {
     
    // They're no longer on warning, remove them from the warning list
    }
    

    To convert the remaining time from milliseconds into a human readable form:

    Code:
    SimpleDateFormat hourFormat = new SimpleDateFormat("HH");
    SimpleDateFormat minFormat = new SimpleDateFormat("mm");
    SimpleDateFormat secFormat = new SimpleDateFormat("ss");
     
    hourFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    minFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    secFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
     
    String hours = hourFormat.format(remaining);
    String minutes = minFormat.format(remaining);
    String seconds = secFormat.format(remaining);
     
    String timeRemaining = hours + ":" + minutes + ":" + seconds;
    
    Hope that helps
     
Thread Status:
Not open for further replies.

Share This Page