Make my util a better working/look!

Discussion in 'Spigot Plugin Development' started by jusjus112, Mar 13, 2017.

  1. Hey guys,

    I recently started again to write plugins.
    Well I needed a Util to handle cooldowns and use it easy, so I made below an Util. Small but beautiful I always say.

    AND YES, it's working GOOD. No errors or anything

    Code (Text):
    private static HashMap<Object, HashMap<Integer, Boolean>> cd = new HashMap<Object, HashMap<Integer, Boolean>>();
        public void run() {
            Iterator<Object> iter = cd.keySet().iterator();
            while (iter.hasNext()){
                Object ob =;
                if (cd.containsKey(ob)) {
                    HashMap<Integer, Boolean> map = cd.get(ob);
                    if (map.values().contains(Boolean.TRUE)){
                        int i = 0;
                        for(int k : map.keySet())
                        if (i<=0){
                        else map.put(i, true);
        public void addCooldown(Object ob, int seconds){
            if (cd.containsKey(ob))return;
            HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>();
            map.put(seconds, true);
            cd.put(ob, map);
        public boolean isOnCooldown(Object ob){
            HashMap<Integer, Boolean> map = cd.get(ob);
            boolean value = false;
            if (map == null || map.isEmpty())
                return false;
            for (boolean i : map.values())
            return value;
        public Integer getLastSeconds(Object ob){
            if (!cd.containsKey(ob))
                return 0;
            HashMap<Integer, Boolean> map = cd.get(ob);
            int key = 0;
            for (int i : map.keySet())
            return key;
    Now I ask to improve your opinion of this util to be faster and more efficiently. I know, it's not the best but I thought I'd just throw it in this group.

    Thanks in advance.

  2. Isn't it better to use Strings or UUIDs instead of Objects for the cooldown hashmap keys?

    And are you using a scheduler here? Cooldowns cannot use schedulers, you will end up with horrendous lag if it's stacked up.

    If you're using schedulers, it's working, but it's far from good.
    #2 Hex_27, Mar 13, 2017
    Last edited: Mar 13, 2017
  3. Depends if it's asynchronous/sync

    A) Minecraft includes the Guava library which has a Multimap, that will clean up your code. B) Don't use a scheduler, get the current time, add the cooldowns and store that time. Then to see if expired or on cooldowns see if current time is past the end time.

    End time = current time + cooldowns
    Expired = current time > end time
  4. No it doesn't depend on if it's async or sync. Apparently running tasks in the first place takes up more than running a piece of code on its own
  5. Well no shit but your have a lot less lag doing asynchronous rather then sync. From the look of things he is running the code every second, you would need thousands of cooldowns to start experiencing lag on the server and many many more doing it async. (This of course depends on the CPU)

    Also @OP, if you do the scheduler and run it async then you will need to account for concurrency
  6. Your code is kinda inefficient, you should do how @SystemUpdate_ said, just have a HashMap like this:
    Code (Text):

    HashMap<String, Long> cooldowns = new HashMap<String, Long>();
    //add cooldown with:
    cooldowns.put(player.getName(), System.currentTimeMillis();
    //check if a player is on cooldown:
    if (System.currentTimeMillis() - cooldowns.get(player.getName() > cooldownTime)
        //player isnt on cooldown
       //player is on cooldown
    //Note: cooldownTime has to be in milliseconds