Cooldown Manager

Discussion in 'Resources' started by LCastr0, Jul 12, 2014.

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


    Cooldown Manager
    First of all, yes, I know there is some Cooldown resources out there already, but this one is a little bit different, where you don't need to use more than one class for the Manager and you still can get the time left.​
    • Boolean to check if a player is in Cooldown
    • Multiple Cooldowns per player
    • Named Cooldowns
    • Integer to get the time left of a player in the specified Cooldown
    • Now without tasks/schedulers!
    • UUID Ready!
    1. import java.util.HashMap;
    2. import java.util.Map;
    3. import java.util.UUID;
    5. import org.bukkit.entity.Player;
    6. public class Cooldown {
    8. private static Map<String, Cooldown> cooldowns = new HashMap<String, Cooldown>();
    9. private long start;
    10. private final int timeInSeconds;
    11. private final UUID id;
    12. private final String cooldownName;
    14. public Cooldown(UUID id, String cooldownName, int timeInSeconds){
    15. = id;
    16. this.cooldownName = cooldownName;
    17. this.timeInSeconds = timeInSeconds;
    18. }
    20. public static boolean isInCooldown(UUID id, String cooldownName){
    21. if(getTimeLeft(id, cooldownName)>=1){
    22. return true;
    23. } else {
    24. stop(id, cooldownName);
    25. return false;
    26. }
    27. }
    29. private static void stop(UUID id, String cooldownName){
    30. Cooldown.cooldowns.remove(id+cooldownName);
    31. }
    33. private static Cooldown getCooldown(UUID id, String cooldownName){
    34. return cooldowns.get(id.toString()+cooldownName);
    35. }
    37. public static int getTimeLeft(UUID id, String cooldownName){
    38. Cooldown cooldown = getCooldown(id, cooldownName);
    39. int f = -1;
    40. if(cooldown!=null){
    41. long now = System.currentTimeMillis();
    42. long cooldownTime = cooldown.start;
    43. int totalTime = cooldown.timeInSeconds;
    44. int r = (int) (now - cooldownTime) / 1000;
    45. f = (r - totalTime) * (-1);
    46. }
    47. return f;
    48. }
    50. public void start(){
    51. this.start = System.currentTimeMillis();
    52. cooldowns.put(, this);
    53. }
    55. }


    Start a Cooldown for a player/Check if a Player is in a Cooldown
    1. //Checks if the player is in a cooldown
    2. if(!Cooldown.isinCooldown(<uuid>, <name>)){
    3. //If isn't, start it
    4. Cooldown c = new Cooldown(<uuid>, <name>, <time>);
    5. c.start();
    6. }

    Get the time left (int)
    1. int timeLeft = Cooldown.getTimeLeft(<uuid>, <name>);

    P.S.: <name> is the name of the Cooldown, not the name of the player!

    Thanks for reading! If you have any suggestions, questions, or anything else, just tell me.
  2. Offline


    That's a nice cooldown, Not sure if it works per player though, since the times are fields. I'll test it later.
  3. Offline


    Oh yea I forgot this... I will fix as soon as I get in My computer, and remove the start method :p
    Just tested with 2 players and it works
  4. Offline


    Why would you use a task for this? Surely it would be much more efficient, as well as simpler, to do this:

    1. public final class Cooldowns {
    2. private final Map<UUID, Cooldown> cooldowns;
    4. public Cooldowns() {
    5. cooldowns = new HashMap<>();
    6. }
    8. public void add(final Cooldown cooldown) {
    9. final UUID player = cooldown.getPlayer();
    10. if (cooldowns.containsKey(player)) {
    11. cooldowns.remove(player);
    12. }
    13. cooldowns.put(player, cooldown);
    14. }
    16. public void remove(final UUID player) {
    17. cooldowns.remove(player);
    18. }
    20. public boolean hasCooldown(final UUID player) {
    21. final Cooldown cooldown = cooldowns.get(player);
    22. if (cooldown == null) {
    23. return false;
    24. }
    25. if (cooldown.isExpired()) {
    26. cooldowns.remove(player);
    27. return false;
    28. }
    29. return true;
    30. }
    31. }

    1. public final class Cooldown {
    2. private final UUID player;
    3. private final long length;
    4. private final long time;
    6. public Cooldown(final UUID player, final long length, final long time) {
    7. this.player = player;
    8. this.length = length;
    9. this.time = time;
    10. }
    12. public UUID getPlayer() {
    13. return player;
    14. }
    16. public boolean isExpired() {
    17. return System.currentTimeMillis() >= time + length;
    18. }
    19. }
    Garris0n likes this.
  5. Offline


    Because this one doesn't show the time left.
  6. Offline


    You can easily make it so you can. The pro of the one PandazNWafflez provided is you do not have a task for each cooldown. The examples provided are both good cooldown manager examples.
  7. Offline


    Yeah because it's so difficult for you to type:

    1. public long getTimeRemaining() {
    2. return (time + length) - System.currentTimeMillis();
    3. }

    Without me doing it for you...
    Garris0n likes this.
  8. Offline


    Oh, also, my class removes the cooldown immediatelly when the time passes. Your one would need a scheduler to remove the player.
  9. Offline


    Oh god, why...

    I'll be back tomorrow to rant about this, it's 4 am.
    ArthurMaker likes this.
  10. Offline


    Stop complaining about my class, if you didn't like it, don't use it!
    I didn't make it in the purpose of BIG things, if you have a better way to do it, do it by yourself, don't come here to complain how my code is bad. I am sure this would help a lot of new coders.
  11. Offline


    Bejin no ombro pro recalq passar longe :v
  12. Offline


    Só acho q n é lgl o povo vir aqui e comentar coisas ruins sobre o meu código. Eu demorei pra fazer a class (Tudo bem que aquele código não demorou, mas eu não comecei a class com aquilo, eu tinha feito de outra maneira, que tinha demorado bem mais... Ai eu pensei "como sou burro", e fiz aquilo.), e eu sei que ela podia ser bem melhor (E será (8) (que merda)), mas eu não sou o melhor coder do mundo, e postei aqui porque sabia que muita gente que está começando deve precisar de algo simples, sem ter que eles mesmos usarem um Map pra isso. A primeira vez que tive que usar um Cooldown aqui do Bukkit, demorei umas horas pra entender o que Map fazia, e porque estava lá, além do que os tutoriais de Cooldown aqui não são lá muito "begginers"... Se é que vc me entende :p
  13. Offline


    LCastr0 It's just some criticism from the community. It's better to improve.
  14. Offline


    No it wouldn't... Mine removes the player when the method is called which checks whether they are in cooldown. Mine has the exact same functionality but is better from a standards perspective (object-orientated) and also doesn't require loads and loads of tasks running all the time.
  15. Offline


    Okay, here goes...


    This whole scheduler-list thing was detailed by everyone's favorite (dripping sarcasm) Bukkit tutorial channel, TheBCBroz.

    Here's a link to what is probably most terrible YouTube tutorial video in existence. I feel the need to point out that you should absolutely not follow or listen to anything said in that video. Because it is bad. Very, very bad.

    Using a scheduler and list to track a tutorial is a terrible, terrible idea. Said video also recommends using a list of players, but I'm not even going to go there right now. Just don't do that.

    Using a scheduler will force you to actively track the time until the cooldown is finished. Which is a complete waste of CPU. Instead, you should do it "lazily", which is a term you can google if you feel like reading about it. Actually, here. Now you don't even have to search for it yourself. I would explain how to do this, but PandazNWafflez already posted a perfectly good example, and, while I have worries about it being copy-pasted, I'd rather somebody copy-paste that code than the OP's code.

    Just remove it when the player leaves, having a UUID and long in a map isn't going to kill anyone. It's certainly better than wasting CPU running 50 schedulers, not to mention a HashMap is going to have better performance than an ArrayList (although you really should've used a HashSet, so that's a bit of a moot point).


    I'm more worried about people who don't know any better using it.

    If you stubbornly want to use inefficient code, fine, but I'm here because you're recommending that others use inefficient code, which, for obvious reasons, I have a problem with.

    Surprisingly enough (more sarcasm), giving new coders bad code is doing the exact opposite of helping them.

    I don't speak portuguese, so I can't be sure exactly what this says, however, from a pretty bad google translation, you seem to have the misconception that "new coders don't know what maps are" means "teach them to do it in an extremely inefficient way". In reality, it means "teach new coders what maps are".
  16. Offline


    Garris0n This is the way I found to do it, I'm sorry if I didn't do a Java course, I don't have enough money for it, I just wanted to help the comunity. If you think it will not help, I will gladly clear the OP (Original Post, not Poster.) and ask someone to lock this thread as it's useless.
    Thank you for your feedback, and sorry for anything.
  17. Offline


    You shouldn't be offended by this. Everyone is always learning. Almost every programmer will look back at code they wrote and think 'wow, why did I do it like that' as they learn, and it's a good thing to learn.
    darkness1999 and samus1221 like this.
  18. Offline


    I am preparing a better version of this resource since a lot of people didn't like it. It will not use schedulers/tasks, only system time.
    Thanks for the suggestions/feedback.
  19. Offline


    totally invalid "tutorial" that teach nothing useful.

    everything already explained multiple times, including "why tasks are invalid", at your current skill you just can't write anything new or superior to articles already present on forum.

    only way to write superior tutorial is special implementation of hashmap designed exclusively to handle countdowns or special implementation of task\timer that handle countdowns without wasting any CPU time or special stack based implementation that track time in completely different way
    all such ways require knowledge far beyond "java for dummies ep.1"

    i wont ask you to remove this thread or clear first post, negative experience still experience and still useful.

    if you going to make some tutorial - at least perform basic research of subject, do benchmarks, read stackoverflow read source code of methods at play...
  20. Offline


    If you can please READ ALL THE REPLIES you would know I am updating this thread. Thanks for your inpatience.

    The main post was updated!​

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


    RawCode Mhm.. What does it help you or other to flame on every thread? I think that every try to contribute is at least more than you do. Sorry, to be that offensive right now but I really don't like that behavior.

    MCForger, DotDash and samus1221 like this.
  22. Offline


    if person is wrong i state directly "you are wrong" walking around and trying to make this in "non offensive" way is not my style.
    if you dont like it - its your personal problem, also you can hit "ignore" button and make things simple for everyone.

    and you did error that i explained before you ever updated, becouse i know how people think and how they act in advance, reason is simple, everyone act same.

    Your current version of code feature nothing new, it's just copypaste of reply #4.
    Its not ever your research, you just copypasted it without any understanding of internals.

    You can check this link (it greatly outdated by this moment, but still explain how lazy tracking is done)

    and this link (it is not superior to stuff on forum, but feature original reseach implementation of few things, like remaining time output or input parsing)

    Posting my plugin probably will teach something new, your current tutorial - not.
  23. Offline


    RawCode correction: your plugin would teach an ungodly amount of bad practice.
    For starters, you fail to use the oncommand event for commands.

    You should learn bukkit API before claiming you know it so well.
    Slikey likes this.
  24. Offline


    Please explain what exactly "bad" in my plugin.
    As for onCommand - completely idiotic "innovation", making things only harder to understand- i seen billion threads about users who forgot to register command in plugin.yml but none about "how to shadow\redirect command".

    As for bukkit API - i dont need to learn stuff i not going to ever use, all injections to server core known to me
  25. Offline


    RawCode oh yes, trying to make bukkit API plugins without proper knowledge of bukkit API, yet you have the guts to tell people they don't know how to code plugins.
    As per the last thread I told you, onCommand for compatibility and proper command registration, but apparentally you keep thinking you know better than the people who made the bukkit API.
    Slikey likes this.
  26. Offline


    API is not bible or similar book everyone must follow and trust without any questions.

    i know better how to handle stuff in my plugins and see no reason to support plugin.yml related features.
  27. Offline


    RawCode good luck.
    Don't post your code, though. People may try to follow you coding practices.
    Seeing as the past threads where you refused to actually listen to reason and tried to force your bad practices (Eg: preprocess instead of oncommand, broadcast instead of to simulate chat, infinite loops) shows me that you aren't planning to actually learn how to code, and you shouldn't post here.
    Slikey and MCForger like this.
  28. Offline


    do you ever opened cbukkit source code
    do you know place where onCommand is passed to plugins
    do you know how "chat" delivered to playerd
    do you know that server handled by infinite loop with sleep(50)

    i know internals and check implementation directly, there is no reason to ever listen someone who call everything he do not understand "bad practice"

    i will be happy if people follow my "bad practice" and will start to think self, without trusting blindly to api or random youtube videos
  29. Offline


    RawCode Makes me wonder why you arent using all NMS classes if you think the entire bukkit API is useless.
    Slikey likes this.
  30. RawCode Man. Everything in your repository is horrible. I don't even. Oh my god. Period.
Thread Status:
Not open for further replies.

Share This Page