Solved Large Cooldowns

Discussion in 'Plugin Development' started by MordorKing78, May 31, 2015.

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

    MordorKing78

    Hey, So I've made some KitPVP code and I want to add cooldowns between the kits. I want a cooldown that waits like 2 weeks. But how would I do that? The Timer should go when the server is down.. I have no qlue how to do it..

    Like after the command has been executed he cant use the kit. And in 2 weeks he execute it again.. :)
    I hope someone can help me.. Thank you ;)
     
  2. @MordorKing78 System.currentTimeMillis(), store in config the time you want them to use it again, job done.
     
  3. Offline

    MordorKing78

    @AdamQpzm but what if the server is offline? And could you give me an example because I have never done this kinda cooldowns

    Thanks
     
  4. Offline

    vhbob

    @MordorKing78 it is impossible for a plugin to do anything when the server isnt running. my only thought is mabye storing data in mySQL...
     
  5. Offline

    MordorKing78

    @vhbob How does essentials kits do it then? Cant I do something with the time and date eg it saves the time and date the kit has been spawned and then if there are eg 7 days over it detects that and let you use the command again
     
  6. Offline

    nverdier

    @MordorKing78 If you store the time in a config you can just find the difference between then and the time something happens, and if it is higher than a certain time, allow it to happen.
     
    Konato_K, vhbob and WishMachine like this.
  7. Offline

    WishMachine

    @MordorKing78 this video was the only solid tutorial i could find for config files, if you know java you should be able to figure everything out from this
     
  8. Offline

    nverdier

  9. Offline

    WishMachine

    I would honestly disagree, when I first tried learning this I went to that wiki page at least 5 separate times and tried to understand it but failed every time, but once I found a solid tutorial video I picked it up immediately.
    The bukkit wiki, like all wiki pages, was created to give you information, not to teach you how to understand something. They also don't give you a sample code or break anything down, which helps a lot.
     
  10. Offline

    nverdier

    This does give you sample code, and breaks it down a ton. What do you think all of the code snippets are for? And the pieces of text around them?
     
  11. Offline

    MordorKing78

    @nverdier I was thinking of something like that. But I don't know how..
     
  12. Offline

    teej107

    @MordorKing78 Here is a basic cooldown example
    Code:
    //Putting player in cooldown
    myMap.put(thePlayer, System.currentTimeMillis());
    
    
    //Time to check the cooldown.
    //Getting the value in map;
    Long time = myMap.get(thePlayer);
    //Checking to see if value retrieved is not null
    if(time != null )
    {
        //Example checking to see if 6 seconds have passed
        if(System.currentTimeMillis() - time >= 6000)
        {
            //Do something
        }
    }
    This is a basic example. You don't need to use a map. You could use a config instead (Map and config are pretty much the same anyway).
    Now the other part is when to check for the cooldown. Since 2 weeks is very far apart, you could probably pass it off by just checking the cooldown when the player joins.
     
  13. Since this is for kits, the best choice would be to check when they try to use the kit ;)
     
    teej107 likes this.
  14. Offline

    WishMachine

    @nverdier
    "When HashMaps are used as a value, they are treated slightly differently. The Map must parameterized with a String type for the key, and the value must be parameterized as a boxed primitive, String, List, Map, or a ConfigurationSerializable."

    I glazed over all of this shit lol, of course it has good information, but when someone in a video breaks everything down as they go and explains what each thing does in layman terms, learning it as someone who doesn't know it at all becomes way simpler. It has breaks down raw code but it doesn't teach you how to use it. I'm just saying that reading a wall of information with complicated terminology, even as someone who understands the terminology, it is near impossible to learn from for a lot of people because it's not engaging if you feel me
     
  15. Offline

    teej107

    Oh of course xD
     
    AdamQpzm likes this.
  16. Offline

    MordorKing78

    @teej107 So I bassicly store the System.currentTimeMillis() in config. And then if the time has passed and the player executes the command again it will be set to System.currentTimeMillis() again and then the player would need to wait another e.g. 2 weeks?
     
  17. Offline

    teej107

    Yes. It also depends on how you store the time. My example stored the time when the cooldown started vs. when the cooldown should end.
     
  18. Offline

    MordorKing78

    @teej107 So I would kinda store it like
    Code:
    <Kit>
      Cooldowns:
        Cooldown Started: -Stuff-
        Cooldown End: -Stuff- System.currentTimeMillis() + <ticks?>
    And then check if the System.currentTimeMillis() is past Cooldown_End if it is let the player execute it again?

    Well, The Cooldown_Started should be in the players.yml file or something

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Jun 1, 2015
  19. Offline

    teej107

    You only need one cooldown time. You can either have it be the start time and compare the difference with it and the current time or just store the time when it should end by adding to the current time value and then comparing the current time when needed. Also you need to add milliseconds to the value, not ticks.
     
    MordorKing78 likes this.
  20. Offline

    MordorKing78

  21. Offline

    MordorKing78

    is the System.currentTimeMillis() in Ticks?
     
  22. yes
     
  23. Offline

    MordorKing78

    So i've made some code for another group command which I just f*cked up xD, This is my code..
    Dono why it ain't working but its messed up and I cant find my way anymore..

    Code:
                          ConfigurationSection players = player.getConfigurationSection("Players");
                          if(players.contains(p.getUniqueId().toString())){
                              long groupActivation = player.getLong(players + p.getUniqueId().toString() + ".Time");
                              String group = player.getString(players + p.getUniqueId().toString() + ".Group");
                              ConfigurationSection playerGroup = config.getConfigurationSection("Groups." + group);
                              int groupD = playerGroup.getInt("Seconds");  int groupDuration = groupD*20;
                              long totalTime = groupDuration;
                            
                              if(System.currentTimeMillis()+-groupActivation >= totalTime){
                                  Bukkit.broadcastMessage("test, You should be able..");
                                  return;
                              }
                          }else{
                              return;
                          }
    Here is what I did

    Code:
    ConfigurationSection players = player.getConfigurationSection("Players"); //Get all players who are in the file
                          if(players.contains(p.getUniqueId().toString())){ //Checks if the players uuid is in the file
                              long groupActivation = player.getLong(players + p.getUniqueId().toString() + ".Time"); //The time the player has joined the group
                              String group = player.getString(players + p.getUniqueId().toString() + ".Group"); //Gets the group the player is in
                              ConfigurationSection playerGroup = config.getConfigurationSection("Groups." + group); //Gets the group the player is in
                              int groupD = playerGroup.getInt("Seconds"); int groupDuration = groupD*20; //Gets the delay when a player has to be removed from the group times 20 (ticks)
                              long totalTime = groupDuration; //making it a long
                             
                              if(System.currentTimeMillis()+-groupActivation >= totalTime){ //Current Time minus when the player joined the group and check if its more then the given ticks
                                  Bukkit.broadcastMessage("test, You should be able.."); //test message
                                  return;
                              }
                          }else{
                              return;
                          }
     
    Last edited: Jun 2, 2015
  24. Offline

    teej107

    No it's not. It's in milliseconds.
    @MaTaMoR_ doesn't know what he is talking about.
     
  25. i dont know why i read milliseconds, and i perfectly know what is it, shuch a toxic person you're
     
  26. Offline

    nverdier

    You said that the method returns ticks. It does not. It returns milliseconds.
     
  27. Offline

    MordorKing78

  28. Offline

    teej107

    @MordorKing78 What exactly isn't working. Is it your test message?
     
  29. I didn't say that, i read "does System.current... returns milliseconds" ? and i responded "yes", it didn't notice that it wasn't "milliseconds", it actually was "ticks".
     
  30. @MordorKing78 Your code is a little hard to follow because of the format. However, a couple of things: You turn the int into a long, which is fine, but you do so after already multiplying it. If it was gonna overflow, it already would have done by that point. Also, you seem to be using 'seconds' but never convert from milliseconds to seconds. Instead you convert it to ticks, which aren't the same.
     
Thread Status:
Not open for further replies.

Share This Page