Util CooldownManager

Discussion in 'Resources' started by PhantomUnicorns, Mar 28, 2018.

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

    PhantomUnicorns

    Some quick classes, speaks for itself. It's possible one of the best ways to manage cooldowns, I think :p. Oh and I fit it with some documentation for those that don't understand what everything does. Anything wrong with it or anything just reply.

    Nonstatic-Based Class (Short Term Cool-downs) (open)

    Code:
    import java.util.IdentityHashMap;
    import java.util.Map;
    
    import org.bukkit.entity.Player;
    
    /**
    * A class to manage cool-downs for your plugins
    *
    * @author PhantomUnicorns
    */
    public class CooldownManager {
    
        private final Map<Player, Long> _COOLDOWNS;
    
        public CooldownManager() {
            _COOLDOWNS = new IdentityHashMap<>();
        }
    
        /**
        * Puts a duration in milliseconds
        *
        * @param player
        *            The player to add a cool-down to
        * @param key
        *            The key you would like to place on the cool-down
        * @param duration
        *            The time it takes for the cool-down to end (Milliseconds)
        * @return If it successfully added the cool-down
        */
        public boolean put(Player player, long duration) {
            if (!_COOLDOWNS.containsKey(player)) {
                _COOLDOWNS.put(player, System.currentTimeMillis() + duration);
                return true;
            }
            return false;
        }
    
        /**
        * Gets the duration left in milliseconds
        *
        * @param player
        *            The player to get the cool-down of
        * @param key
        *            The key the cool-down is associated with
        * @return The duration left (Milliseconds)
        */
        public long get(Player player) {
            if (_COOLDOWNS.containsKey(player)) {
                long durationLeft = _COOLDOWNS.get(player);
                if (durationLeft > System.currentTimeMillis()) {
                    return durationLeft - System.currentTimeMillis();
                } else {
                    _COOLDOWNS.remove(player);
                }
            }
            return -1;
        }
    
        /**
        * Gets and removes the cool-down from the player
        *
        * @param player
        *            The player to get-remove the cool-down of
        * @param key
        *            The key the cool-down is associated with
        * @return The duration left (Milliseconds)
        */
        public long remove(Player player, String key) {
            if (_COOLDOWNS.containsKey(player)) {
                long durationLeft = _COOLDOWNS.remove(player);
                if (durationLeft > System.currentTimeMillis()) {
                    return durationLeft - System.currentTimeMillis();
                } else {
                    _COOLDOWNS.remove(player);
                }
            }
            return -1;
        }
    }
    


    Static-Based Class (Short Term Cool-downs) (open)

    Code:
    import java.util.HashMap;
    import java.util.IdentityHashMap;
    import java.util.Map;
    
    import org.bukkit.entity.Player;
    
    /**
    * A (static-based) class to manage cool-downs for your plugins
    *
    * @author PhantomUnicorns
    */
    public class CooldownManagerStatic {
    
        private static final Map<Player, Map<String, Long>> _COOLDOWNS;
    
        static {
            _COOLDOWNS = new IdentityHashMap<>();
        }
    
        /**
        * Puts a duration in milliseconds
        *
        * @param player
        *            The player to add a cool-down to
        * @param key
        *            The key you would like to place on the cool-down
        * @param duration
        *            The time it takes for the cool-down to end (Milliseconds)
        * @return If it successfully added the cool-down
        */
        public static boolean put(Player player, String key, long duration) {
            if (_COOLDOWNS.containsKey(player)) {
                Map<String, Long> playerCooldowns = _COOLDOWNS.get(player);
                if (!playerCooldowns.containsKey(key)) {
                    playerCooldowns.put(key, System.currentTimeMillis() + duration);
                } else {
                    return false;
                }
            } else {
                Map<String, Long> playerCooldowns = new HashMap<>();
                playerCooldowns.put(key, System.currentTimeMillis() + duration);
                _COOLDOWNS.put(player, playerCooldowns);
            }
            return true;
        }
    
        /**
        * Gets the duration left in milliseconds
        *
        * @param player
        *            The player to get the cool-down of
        * @param key
        *            The key the cool-down is associated with
        * @return The duration left (Milliseconds)
        */
        public static long get(Player player, String key) {
            if (_COOLDOWNS.containsKey(player)) {
                Map<String, Long> playerCooldowns = _COOLDOWNS.get(player);
                if (playerCooldowns.containsKey(key)) {
                    long durationLeft = playerCooldowns.get(key);
                    if (durationLeft > System.currentTimeMillis()) {
                        return durationLeft - System.currentTimeMillis();
                    } else {
                        playerCooldowns.remove(key);
                        if (playerCooldowns.isEmpty()) {
                            _COOLDOWNS.remove(player);
                        }
                        return -1;
                    }
                } else {
                    return -1;
                }
            } else {
                return -1;
            }
        }
    
        /**
        * Gets and removes the cool-down from the player
        *
        * @param player
        *            The player to get-remove the cool-down of
        * @param key
        *            The key the cool-down is associated with
        * @return The duration left (Milliseconds)
        */
        public static long remove(Player player, String key) {
            if (_COOLDOWNS.containsKey(player)) {
                Map<String, Long> playerCooldowns = _COOLDOWNS.get(player);
                if (playerCooldowns.containsKey(key)) {
                    long durationLeft = playerCooldowns.remove(key);
                    if (durationLeft > System.currentTimeMillis()) {
                        return durationLeft - System.currentTimeMillis();
                    } else {
                        playerCooldowns.remove(key);
                        if (playerCooldowns.isEmpty()) {
                            _COOLDOWNS.remove(player);
                        }
                        return -1;
                    }
                } else {
                    return -1;
                }
            } else {
                return -1;
            }
        }
    }
    


    Nonstatic-Based Class (Long Term Cool-downs) (open)

    Code:
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;
    
    import org.bukkit.entity.Player;
    
    /**
    * A class to manage cool-downs for your plugins
    *
    * @author PhantomUnicorns
    */
    public class CooldownManager {
    
        private final Map<UUID, Long> _COOLDOWNS;
    
        public CooldownManager() {
            _COOLDOWNS = new HashMap<>();
        }
    
        /**
        * Puts a duration in milliseconds
        *
        * @param player
        *            The player to add a cool-down to
        * @param key
        *            The key you would like to place on the cool-down
        * @param duration
        *            The time it takes for the cool-down to end (Milliseconds)
        * @return If it successfully added the cool-down
        */
        public boolean put(Player player, long duration) {
            return put(player.getUniqueId(), duration);
        }
    
        /**
        * Puts a duration in milliseconds
        *
        * @param uuid
        *            The player's UUID to add a cool-down to
        * @param key
        *            The key you would like to place on the cool-down
        * @param duration
        *            The time it takes for the cool-down to end (Milliseconds)
        * @return If it successfully added the cool-down
        */
        public boolean put(UUID uuid, long duration) {
            if (!_COOLDOWNS.containsKey(uuid)) {
                _COOLDOWNS.put(uuid, System.currentTimeMillis() + duration);
                return true;
            }
            return false;
        }
    
        /**
        * Gets the duration left in milliseconds
        *
        * @param player
        *            The player to get the cool-down of
        * @param key
        *            The key the cool-down is associated with
        * @return The duration left (Milliseconds)
        */
        public long get(Player player) {
            return get(player.getUniqueId());
        }
    
        /**
        * Gets the duration left in milliseconds
        *
        * @param uuid
        *            The player's UUID to get the cool-down of
        * @param key
        *            The key the cool-down is associated with
        * @return The duration left (Milliseconds)
        */
        public long get(UUID uuid) {
            if (_COOLDOWNS.containsKey(uuid)) {
                long durationLeft = _COOLDOWNS.get(uuid);
                if (durationLeft > System.currentTimeMillis()) {
                    return durationLeft - System.currentTimeMillis();
                } else {
                    _COOLDOWNS.remove(uuid);
                }
            }
            return -1;
        }
    
        /**
        * Gets and removes the cool-down from the player
        *
        * @param player
        *            The player to get-remove the cool-down of
        * @param key
        *            The key the cool-down is associated with
        * @return The duration left (Milliseconds)
        */
        public long remove(Player player, String key) {
            return remove(player.getUniqueId(), key);
        }
    
        /**
        * Gets and removes the cool-down from the player
        *
        * @param player
        *            The player to get-remove the cool-down of
        * @param key
        *            The key the cool-down is associated with
        * @return The duration left (Milliseconds)
        */
        public long remove(UUID uuid, String key) {
            if (_COOLDOWNS.containsKey(uuid)) {
                long durationLeft = _COOLDOWNS.remove(uuid);
                if (durationLeft > System.currentTimeMillis()) {
                    return durationLeft - System.currentTimeMillis();
                } else {
                    _COOLDOWNS.remove(uuid);
                }
            }
            return -1;
        }
    }
    


    Static-Based Class (Long Term Cool-downs) (open)

    Code:
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;
    
    import org.bukkit.entity.Player;
    
    /**
    * A class to manage cooldowns for your plugins
    *
    * @author PhantomUnicorns
    */
    public class CooldownManager {
    
        private static final Map<UUID, Map<String, Long>> _COOLDOWNS;
    
        static {
            _COOLDOWNS = new HashMap<>();
        }
    
        /**
        * Puts a duration in milliseconds
        *
        * @param player
        *            The player to add a cooldown to
        * @param key
        *            The key you would like to place on the cooldown
        * @param duration
        *            The time it takes for the cooldown to end (Milliseconds)
        * @return If it successfully added the cooldown
        */
        public static boolean put(Player player, String key, long duration) {
            return put(player.getUniqueId(), key, duration);
        }
    
        /**
        * Puts a duration in milliseconds
        *
        * @param uuid
        *            The player's uuid to add a cooldown to
        * @param key
        *            The key you would like to place on the cooldown
        * @param duration
        *            The time it takes for the cooldown to end (Milliseconds)
        * @return If it successfully added the cooldown
        */
        public static boolean put(UUID uuid, String key, long duration) {
            if (_COOLDOWNS.containsKey(uuid)) {
                Map<String, Long> playerCooldowns = _COOLDOWNS.get(uuid);
                if (!playerCooldowns.containsKey(key)) {
                    playerCooldowns.put(key, System.currentTimeMillis() + duration);
                } else {
                    return false;
                }
            } else {
                Map<String, Long> playerCooldowns = new HashMap<>();
                playerCooldowns.put(key, System.currentTimeMillis() + duration);
                _COOLDOWNS.put(uuid, playerCooldowns);
            }
            return true;
        }
    
        /**
        * Gets the duration left in milliseconds
        *
        * @param player
        *            The player to get the cooldown of
        * @param key
        *            The key the cooldown is associated with
        * @return The duration left (Milliseconds)
        */
        public static long get(Player player, String key) {
            return get(player.getUniqueId(), key);
        }
    
        /**
        * Gets the duration left in milliseconds
        *
        * @param player
        *            The player to get the cooldown of
        * @param key
        *            The key the cooldown is associated with
        * @return The duration left (Milliseconds)
        */
        public static long get(UUID uuid, String key) {
            if (_COOLDOWNS.containsKey(uuid)) {
                Map<String, Long> playerCooldowns = _COOLDOWNS.get(uuid);
                if (playerCooldowns.containsKey(key)) {
                    long durationLeft = playerCooldowns.get(key);
                    if (durationLeft > System.currentTimeMillis()) {
                        return durationLeft - System.currentTimeMillis();
                    } else {
                        playerCooldowns.remove(key);
                        if (playerCooldowns.isEmpty()) {
                            _COOLDOWNS.remove(uuid);
                        }
                        return -1;
                    }
                } else {
                    return -1;
                }
            } else {
                return -1;
            }
        }
    
        /**
        * Gets and removes the cooldown from the player
        *
        * @param player
        *            The player to get-remove the cooldown of
        * @param key
        *            The key the cooldown is associated with
        * @return The duration left (Milliseconds)
        */
        public static long remove(Player player, String key) {
            return remove(player.getUniqueId(), key);
        }
    
        /**
        * Gets and removes the cooldown from the player
        *
        * @param player
        *            The player to get-remove the cooldown of
        * @param key
        *            The key the cooldown is associated with
        * @return The duration left (Milliseconds)
        */
        public static long remove(UUID uuid, String key) {
            if (_COOLDOWNS.containsKey(uuid)) {
                Map<String, Long> playerCooldowns = _COOLDOWNS.get(uuid);
                if (playerCooldowns.containsKey(key)) {
                    long durationLeft = playerCooldowns.remove(key);
                    if (durationLeft > System.currentTimeMillis()) {
                        return durationLeft - System.currentTimeMillis();
                    } else {
                        playerCooldowns.remove(key);
                        if (playerCooldowns.isEmpty()) {
                            _COOLDOWNS.remove(uuid);
                        }
                        return -1;
                    }
                } else {
                    return -1;
                }
            } else {
                return -1;
            }
        }
    }
    
     
    Last edited: Mar 28, 2018
Thread Status:
Not open for further replies.

Share This Page