Cooldowns? Hmm...

Discussion in 'Resources' started by ABDTGaming, Aug 21, 2014.

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



    *First off: Forgive me if this is in the wrong section, but this is where I see it occur most*

    I have seen cooldowns done wrong multiple times, and I know a few people that get REALLY annoyed by this. *Cough* Garris0n *Cough* One of the questions most asked after he states that they're wrong are, "Well, how do you do it then?". Well, this tutorial explains a few of the common errors like that that people make. He DID make a mistake in the video, but he cleared it up in the description. Turqmelon also did a video on this. Sorry for the short thread, but the video explains a lot, and I thought it was about time that it picked up.

    sgtcaze - Making the tutorial. Go check him out on youtube, because he make a bunch of epic tutorials
    turqmelon - Making a more in-depth tutorial.
    Garris0n likes this.
  2. Offline


    What about my lib? After I fixed it, there was no more complains...
  3. Offline


    Sorry, I don't see every tutorial. Feel free to post a link to it and I'll try putting it on the thread
  4. Offline


    I'd say yours is more of a library than a tutorial.
  5. Offline


    Fixed. Happy?
  6. Offline


    I haven't watched the videos but really we should all just use player metadata for this. That's what it's there for. We can also use lazy evaluation - I.E only check the metadata when we need to use it. This way there is a) practically no code written by us because Bukkit has metadata built in and b) a very fast way of doing it because there are no timers to set the cooldowns. We could even write a util class which has getCooldown which checks whether the player has a cooldowns removing he metadata if the cooldown has expired and setCooldown to add one. Realistically, those two methods are all you need to write if you use metadata.
  7. Offline


    Why give a link to a tutorial? Why not explain what it says/does?
  8. Offline


    Because it was 1:16 in the morning, and he explains it in the video. It also prevents copying and pasting.
  9. Offline


    A even more simple compact class by Comphenix.

    1. package com.comphenix.example;
    3. import org.bukkit.entity.Player;
    5. import;
    6. import;
    8. public class Cooldowns {
    9. private static Table<String, String, Long> cooldowns = HashBasedTable.create();
    11. /**
    12. * Retrieve the number of milliseconds left until a given cooldown expires.
    13. * <p>
    14. * Check for a negative value to determine if a given cooldown has expired. <br>
    15. * Cooldowns that have never been defined will return {@link Long#MIN_VALUE}.
    16. * @param player - the player.
    17. * @param key - cooldown to locate.
    18. * @return Number of milliseconds until the cooldown expires.
    19. */
    20. public static long getCooldown(Player player, String key) {
    21. return calculateRemainder(cooldowns.get(player.getName(), key));
    22. }
    24. /**
    25. * Update a cooldown for the specified player.
    26. * @param player - the player.
    27. * @param key - cooldown to update.
    28. * @param delay - number of milliseconds until the cooldown will expire again.
    29. * @return The previous number of milliseconds until expiration.
    30. */
    31. public static long setCooldown(Player player, String key, long delay) {
    32. return calculateRemainder(
    33. cooldowns.put(player.getName(), key, System.currentTimeMillis() + delay));
    34. }
    36. /**
    37. * Determine if a given cooldown has expired. If it has, refresh the cooldown. If not, do nothing.
    38. * @param player - the player.
    39. * @param key - cooldown to update.
    40. * @param delay - number of milliseconds until the cooldown will expire again.
    41. * @return TRUE if the cooldown was expired/unset and has now been reset, FALSE otherwise.
    42. */
    43. public static boolean tryCooldown(Player player, String key, long delay) {
    44. if (getCooldown(player, key) <= 0) {
    45. setCooldown(player, key, delay);
    46. return true;
    47. }
    48. return false;
    49. }
    51. /**
    52. * Remove any cooldowns associated with the given player.
    53. * @param player - the player we will reset.
    54. */
    55. public static void removeCooldowns(Player player) {
    56. cooldowns.row(player.getName()).clear();
    57. }
    59. private static long calculateRemainder(Long expireTime) {
    60. return expireTime != null ? expireTime - System.currentTimeMillis() : Long.MIN_VALUE;
    61. }
    62. }

    Reference & usage:
    Comphenix likes this.
  10. Offline


    Hi can you like make a cooldown class and start a cooldown from another class ?

    I need 1 a cooldown class for like 5 sec and an other class starting the cooldown with an IF statement is that possable ?
    if it is possable can you give me an example pls.
Thread Status:
Not open for further replies.

Share This Page