Timing Commands

Discussion in 'Plugin Development' started by jay275475, Sep 25, 2013.

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

    jay275475

    Hey Ik this is poosible to not let a player do a command until a certain time. I'm making a Kit Plugin I want them to get kits every 5 H0urs
     
  2. Offline

    Chinwe

    Use System.currentTimeMillis() to get the time (in milliseconds since midnight 1/1/1970) when they use the command, and save this time + 5 hours in milliseconds (18000000) to a .yml file. Then, when they try to use the command, check if System.currentTimeMillis() >= this time: if it is, it has been at least 5 hours between uses, and you can allow it, otherwise they still have to wait :)
     
    tommycake50 likes this.
  3. Offline

    jay275475

    Chinwe Can you put that into an example code I still don't understand
     
  4. Offline

    chasechocolate

    jay275475 savedTime + TimeUnit.HOURS.toMillis(5) >= System.currentTimeMillis(). Were "savedTime" is the saved time.
     
  5. Offline

    jay275475

    uhh I Still dont get this :(
     
  6. Offline

    Janmm14

    jay275475
    I would say:
    savedTime + TimeUnit.HOURS.toMillis(5) <= System.currentTimeMillis()
     
  7. Offline

    tommycake50

    Use a database to save the time rather than yaml/plaintext.
    It's probably the safest way and will cause the least hassle I think.
     
  8. Offline

    Janmm14

    When the player used the command, do
    getConfig().set("cooldown." + player.getName(), System.currentTimeMillis());
    but before executing the command do:
    if(getConfig().getLong("cooldown." + player.getName) + TimeUnit.HOURS.toMillis(5) <= System.currentTimeMillis()) {
    //do the command
    } else {
    //command was executed in the last 5 hours
    }

    tommycake50
    I think a database should only be used if it is really needed, because setting the database up and requiring that the plugin users have a database for saving cooldowns is not tbe best idea.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  9. Offline

    chasechocolate

  10. Offline

    tommycake50

    Yeah i guess it isn't as easy and in this case not necessary.
     
Thread Status:
Not open for further replies.

Share This Page