Util CooldownAPI for beginners

Discussion in 'Resources' started by joao_maxinho, Feb 11, 2015.

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

    joao_maxinho

    1. Create one class in a package like... "your.name.plugin.Util"

    2. In this class. Paste this

    UUID VERSION :
    Code:
        private static HashMap<UUID, Long> cooldown = new HashMap<UUID, Long>();
      
        public static void putCooldown(Player p, int Seconds) {
        cooldown.put(p.getUniqueId(), System.currentTimeMillis() + (Seconds * 1000L));
        }
      
        public static int getCooldown(Player p) {
        if (cooldown.containsKey(p.getUniqueId())) {
        return Long.valueOf(((System.currentTimeMillis() - cooldown.get(p.getUniqueId())) / 1000)).intValue();
        } else {
        return 0;
        }
        }
      
        public static boolean isOnCooldown(Player p) {
        if (cooldown.containsKey(p.getUniqueId()) && cooldown.get(p.getUniqueId()) > System.currentTimeMillis()) {
        return true;
        } else {
        return false;
        }
        }
      
        public static void removeCooldown(Player p) {
        if (cooldown.containsKey(p.getUniqueId())) {
        cooldown.remove(p.getUniqueId());
        }
        }
    
    STRING VERSION :

    Code:
        private static HashMap<String, Long> cooldown = new HashMap<String, Long>();
     
        public static void putCooldown(Player p, int Seconds) {
        cooldown.put(p.getName(), System.currentTimeMillis() + (Seconds * 1000L));
        }
     
        public static int getCooldown(Player p) {
        if (cooldown.containsKey(p.getName())) {
        return Long.valueOf(((System.currentTimeMillis() - cooldown.get(p.getName())) / 1000)).intValue();
        } else {
        return 0;
        }
        }
     
        public static boolean isOnCooldown(Player p) {
        if (cooldown.containsKey(p.getName()) && cooldown.get(p.getName()) > System.currentTimeMillis()) {
        return true;
        } else {
        return false;
        }
        }
     
        public static void removeCooldown(Player p) {
        if (cooldown.containsKey(p.getName())) {
        cooldown.remove(p.getName());
        }
        }
    Now. Import this class in one Listener for use the Cooldown API!
    Simple!

    ApiClassName.removeCooldown(Player p)
    ApiClassName.isOnCooldown(Player p)
    ApiClassName.getCooldown(Player p)
    ApiClassName.putCooldown(Player p, int Seconds)

    I hope to help some people * - *

    Note :
    Sorry for my english ;-;
    Brazilian :c
     
    Last edited: Feb 11, 2015
    ChipDev likes this.
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    joao_maxinho

    Usually cooldowns are small, 10-minute or one second, think unnecessary use UUID for this, but if you want to change this is very simple API.
     
  4. Offline

    xTrollxDudex

    The boxing is totally unnecessary
     
  5. Offline

    Skionz

  6. Offline

    teej107

    When are you going to remove the static? I desperately want more cooldowns per player!!!!
     
  7. Offline

    joao_maxinho

    Create more than one Class for Cooldown's

    ?

    It's for be sexy

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Feb 12, 2015
  8. Offline

    Skionz

    This class literally wraps around a few methods in the already existing Map class.
     
  9. Offline

    teej107

    No thank you. I think I'll stick with OOP.
     
    Skionz and timtower like this.
Thread Status:
Not open for further replies.

Share This Page