[Tutorial] Easy cooldown with messages to show the time left

Discussion in 'Resources' started by bramhaag, Mar 4, 2014.

Thread Status:
Not open for further replies.
  1. Hi all,

    Welcome to my first tutorial on Bukkit. I'm going to show you how to make an easy cooldown for commands, events etc. with a message that shows the time the player still has to wait.
    Also look to the methode of SoThatsIt in the commands
    Lets begin
    Code:java
    1.  
    2. public HashMap<String, Long> cooldowns = new HashMap<String, Long>();
    3.  
    4. int cooldownTime = 60; // The number of seconds the player has to wait
    5. if(cooldowns.containsKey(p.getName())) {
    6.  
    7. //We divide the cooldown in milliseconds to seconds
    8. long secondsLeft = ((cooldowns.get(p.getName())/1000)+cooldownTime) - (System.currentTimeMillis()/1000);
    9.  
    10. //If the cooldown is still buzy
    11. if(secondsLeft>0) {
    12. sender.sendMessage("You cant do this for another "+ secondsLeft +" seconds!");
    13. return true;
    14. }
    15. }
    16. // No cooldown found or cooldown has expired, save new cooldown
    17. cooldowns.put(p.getName(), System.currentTimeMillis());
    18. // Do Command or Event Here
    19. }
    20. }


    Hope it helps,

    Bramhaag

    Wow, i clicked on the wrong sub-forum :/ Could an admin move this tread please?

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

    pigeontroll

    Do you think you can make it so that it shows x.y seconds left? Or does it already do that? For example, rather than "4 seconds" it would say "4.2 seconds".
     
  3. pigeontroll

    It makes it 4 seconds, because we divide it with 1000. You can change this and get the result you want Not true
     
  4. Offline

    SoThatsIt

    You get 4 seconds because you are converting the number to a long, which does not support decimals.

    One way to accomplish this is to get the timeleft as a float and then round it to a number of decimal places.
    Example:

    Code:java
    1. long timeSinceCreation = System.currentTimeMillis() - cooldowns.get(p.getName());
    2. float timeLeft = cooldownTime - (timeSinceCreation / 1000f);
    3. float secondsLeft = ((int) (timeLeft * 10)) / 10; //Round to one decimal place, to round to more simply use more 0's

    The problem with this method is that if there is 4 seconds left it will say just 4 instead of "4.0"

    Another way to accomplish this without this problem is to get the timeleft as an integer 10 times more than what it should be. We can accomplish this by dividing timeSinceCreation by 100 instead of 1000f.
    Example:

    Code:java
    1. long timeSinceCreation = System.currentTimeMillis() - cooldowns.get(p.getName());
    2. int timeLeft = cooldownTime - (int) (timeSinceCreation / 100);

    We would then convert this into a String and add a decimal point 1 before the end of the string.

    Code:java
    1. String secondsLeft = "" + timeLeft;
    2. secondsLeft = str.substring(0, str.length()-2) + "." + str.substring(str.length()-1);


    I hope this helped

    ~sothatsit

    Also, bramhaag this resource is quite incomplete and i think you should put the code into some methods so it is easier to read, also you never remove the player from the cooldown list when the cooldown is finished.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  5. Wow, you hijacked my tread :p Awsome methode!

    Well, I already noticed that the player doesn't get removed from the cooldown, but for some reason, it was working fine for me
     
Thread Status:
Not open for further replies.

Share This Page