System.currentTimeMillis();

Discussion in 'Plugin Development' started by CraftCreeper6, Oct 20, 2014.

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

    CraftCreeper6

    Hey! I was just wondering how I would use System.currentTimeMillis(); to make a cooldown. Here is what I have to far:
    Code:java
    1. long time = System.currentTimeMillis();
    2.  
    3. if(!cooldown.containsKey(p.getName())) {
    4.  
    5. cooldown.put(p.getName(), (long) 7);
    6.  
    7. }
    8.  
    9. long untilUse = cooldown.get(p.getName());
    10.  
    11. if(((untilUse/1000)+7)-(time/1000) > time) {
    12.  
    13. p.sendMessage("§9You are still on cooldown for another §4" + untilUse + "§9 seconds!");
    14.  
    15. return;
    16.  
    17. }


    But that quite literally does nothing. Can anyone help me? :)
     
  2. Offline

    mrCookieSlime

    CraftCreeper6
    It does nothing because currentTimeMillis returns milliseconds.
    So it does work... for 7 milliseconds ^^
    So you want to use 7000 instead of 7.
     
  3. Offline

    CraftCreeper6

    mrCookieSlime
    Oh, damn, lol.

    EDIT: Doesn't work :( (COOLDOWN is 7000)
    Code:java
    1. if(!cooldown.containsKey(p.getName())) {
    2.  
    3. cooldown.put(p.getName(), COOLDOWN);
    4.  
    5. }
    6.  
    7. long untilUse = cooldown.get(p.getName());
    8.  
    9. if(((untilUse/1000)+COOLDOWN)-(time/1000) > time) {
    10.  
    11. p.sendMessage("§9You are still on cooldown for another §4" + untilUse + "§9 seconds!");
    12.  
    13. return;
    14.  
    15. }
     
  4. Offline

    Totom3

    CraftCreeper6 The point of using this approach is to store the last time the player did the thing on cooldown, therefore you have to store the current time instead of the duration of the cooldown. Then when you want to test it, you check the difference between the current time, and what was stored in the map/collection/db/etc...
    Code:java
    1.  
    2. // Add an entry in the map
    3. map.put(p.getName(), System.currentTimeMillis());
    4.  
    5. // Get the remaining time in the map
    6. long remainingTime =
    7. System.currentTimeMillis() - map.get(p.getName())
    8.  

    Then you simply check if remainingTime is bigger than the cooldown time.
     
    Gnat008 likes this.
  5. Offline

    CraftCreeper6

    Totom3
    Still doesn't work.
    Code:java
    1. long time = System.currentTimeMillis();
    2.  
    3. if(!cooldown.containsKey(p.getName())) {
    4.  
    5. cooldown.put(p.getName(), time);
    6.  
    7. }
    8.  
    9. long remainingTime = System.currentTimeMillis() - cooldown.get(p.getName());
    10.  
    11. if(((remainingTime/1000)+COOLDOWN)-(time/1000) > time) {
    12.  
    13. p.sendMessage("§9You are still on cooldown for another §4" + remainingTime + "§9 seconds!");
    14.  
    15. return;
    16.  
    17. }
     
  6. Offline

    Totom3

    CraftCreeper6 Ya know... there is a reason why remainingTime is called remainingTime, lol.

    Code:java
    1. if (remainingTime > COOLDOW_TIME) {
    2. // Allowed
    3. }
     
  7. Offline

    FerusGrim

    I'm going to do a little bit of re-writing of this. One, because a couple of the variables are confusingly written out. Tell me if this works:

    Code:java
    1. public final long cooldownTime = 7000; // 7 seconds
    2.  
    3. public boolean isCoolingDown(Player p) {
    4. final long currentTime = System.currentTimeMillis();
    5. final UUID uuid = p.getUuid();
    6.  
    7. if (!cooldown.containsKey(uuid) {
    8. cooldown.put(uuid, currentTime);
    9. return false;
    10. } else {
    11. final long surpassedTime = System.currentTimeMillis() - cooldown.get(uuid);
    12. if (surpassedTime >= cooldownTime) {
    13. cooldown.remove(uuid);
    14. return false;
    15. } else {
    16. p.sendMessage("Blah blah, you have " + (cooldownTime - surpassedTime) / 1000 + " seconds left!";
    17. return true;
    18. }
    19. }
    20. }
     
  8. Offline

    Totom3

    FerusGrim Public final field ? I think you forgot the static or the private or no final. Also, in a method that is supposed to check if the player's cooldown is over, I wouldn't send any messages or change the collection. Oh, also, your method returns the opposite of what it's name says it should.
     
  9. Offline

    FerusGrim

    Yes? I make it public because it's not within a method, and final because it's... well, final. Obviously, he could modify that bit if he plans on having a configuration that changes.

    Did you really just suggest that I make something static, when there's no need to make a static call to it?

    ...Why? Do you have any reasoning behind this? Or are you just saying it...?

    No, it doesn't. The method call is 'isCoolingDown'. There are two if() checks. One to see if they're NOT in the cooldown map, and one to see if they've surpassed or are equal to the cooldown time. In those cases, the method returns false, to mean that the player isn't cooling down. The one place where the method returns TRUE, to state that the player IS cooling down, is if they're name is in the cooldown map, and the time hasn't surpassed the cooldown time.
     
  10. Offline

    Totom3

    FerusGrim Well, what I meant with the field is that if it's a constant (final), you might as well just set it to static because then it's accessible from other classes that do not hold a reference to.. argh... whatever. It just comes down to a personal preference/coding style. Also, my bad for the method, I misread the name, sorry.
     
  11. Offline

    Gater12

    Maybe he was referring to making a constant. We know the cooldown time is 7 seconds and that will never change. Doesn't make sense to make it per instance if we know it will always be 7 seconds so we make it static.

    Code:java
    1. public static final int COOLDOWN_TIME = 7000;


    EDIT: Yes it does come down to your coding style and personal preference.
     
  12. Offline

    FerusGrim

    Gater12 Totom3

    That makes sense. I hadn't considered a constant value, because I assumed in production, it would be set by a config file.
     
  13. Offline

    Rocoty

    FerusGrim That field should be static. It would make no sense for it not to be, as it has nothing to do with any object's state.

    EDIT: People seem to believe static is some sort of evilness that should never be used. It's not. It's just a terrible idea to use it as a way of making things accessible to other classes or the main class and whatnot, which is quite sadly what most newbies tend to do. That's why we say it's bad.
     
  14. Offline

    FerusGrim

    Did you not read the last three messages? :)
     
  15. Offline

    Rocoty

    Yeah I did....I just sort of applied some reasoning onto the subject
     
  16. Offline

    FerusGrim

    Ah. The reasoning behind why it should be static was understood, as a constant value. Just, as I was saying, I hadn't thought on it, because I was assuming in production that value would be set by a configuration value.
     
  17. Offline

    _Filip

  18. Offline

    CraftCreeper6

    FerusGrim
    Thanks! Works fine! :D

    EDIT: Doesn't show the cooldown finished until your right click again. :confused:
     
  19. Offline

    FerusGrim

    You're right, it doesn't. The method of cooldown I gave you doesn't require a Runnable, but lacks the notification when you can actually use it again. Nothing to fix that except a Runnable, I'm afraid.
     
Thread Status:
Not open for further replies.

Share This Page