Util Simple cooldown manager!

Discussion in 'Resources' started by joao_maxinho, Jan 24, 2016.

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

    joao_maxinho

    Class:

    Code:
    public class Cooldown {
    
        public Cooldown(int seconds) {
            this.seconds = seconds;
        }
    
        private int seconds = 0;
    
        private HashMap<UUID, Long> hashmap = new HashMap();
    
        public void putInCooldown(Player player) {
            hashmap.put(player.getUniqueId(), System.currentTimeMillis() + (seconds * 1000L);
        }
    
        public boolean isOnCooldown(Player player) {
            UUID uuid = player.getUniqueId();
            return (hashmap.get(uuid) != null && hashmap.get(uuid) > System.currentTimeMillis())
        }
    
        public int getCooldownSeconds(Player player) {
            UUID uuid = player.getUniqueId();
            return (int)  ((hashmap.get(uuid) - System.currentTimeMillis()) / 1000L)
        }
    }
    Utilies:

    putInCooldown(player); // put the player in cooldown
    isOnCooldown(player); // return if player is on cooldown
    getCooldownSeconds(player); // return player reaming cooldown in seconds

    Code:
    Cooldown cd = new Cooldown(30);
    :)
    if the code has a error, tell me, because a maked it on notepad!
     
  2. Offline

    Zombie_Striker

    @joao_maxinho
    Interesting util. I know a few people that may use this. It works, but please consider applying the following changes.
    1. We should not have to import all these objects when the copy and paste this into our plugins. Post the imports as well.
    2. Don't you want to document your code? You don't want to put any markers saying that you are the creator of this resource? It up to you, but I image you want some form of credit.
    3. Why would you create a single amount of time to be put into the cooldown? Why can't we control how long we want the cooldown to last when we actually put them into the hashmap?
      Please use hashmap.contains(Object) instead.
    4. You never remove the player's uuid from the hashmap. This will take up more memory.
    5. Please do not be lazy and add the parameters for the hashmap.
    6. There is no need to create the "uuid" instance for all the methods. If it's only going to be used once, then you should not have to create the variable.
     
  3. Offline

    xTrollxDudex

    You don't want that double get right there, using a local variable can remove the other get entirely.
     
  4. Offline

    joao_maxinho

    People talk a lot in this forum by unnecessary things, this is my last topic here.
     
  5. Offline

    Zombie_Striker

    @joao_maxinho
    We are trying to tell you about how you can improve your code. How is that unnecessary?
     
    Mrs. bwfctower likes this.
  6. Offline

    Mrs. bwfctower

    @joao_maxinho Just about every other post here has posts making suggestions, and saying what could be better about their class/api/whatever. If you don't want to have people say what you could do to make your code better, then don't post it here.

    Also, on another note, there's already so many, very similar, cooldown managers out there.
    https://bukkit.org/threads/cooldown-manager.290459/
    https://bukkit.org/threads/creating-cooldown-in-event.377111/
    https://bukkit.org/threads/util-cooldownmanager-easy-cooldown-management.302020/
    https://bukkit.org/threads/tutorial-simple-cooldown-timer-newb-friendly.156283/
    https://bukkit.org/threads/tutorial-easy-cooldown-with-messages-to-show-the-time-left.240447/
    https://bukkit.org/threads/simple-c...torial-coding-for-noobs-and-others-d.209990/\
    https://bukkit.org/threads/cooldowns-tutorial-super-easy-newbie-friendly.300820/#post-2727368
    https://bukkit.org/threads/easycooldown.128219/\
    https://bukkit.org/threads/easycooldown.128219/#post-1654259

    Just on the first page when you search "cooldown".
     
    cococow123 and teej107 like this.
  7. Offline

    xTrollxDudex

    [​IMG]
    If you confuse suggestions with "unnecessary things" you might as well tell us how to improve our suggestions so that you would understand them better.
     
    WolfMage1 likes this.
  8. Offline

    teej107

    I agree. We suggest things to make your code better. I personally think this utility is a lot better than some of the ones I've seen lately. A few of the cooldown managers you listed are absolutely terrible either in code or the tutorial.
     
Thread Status:
Not open for further replies.

Share This Page