Hello everyone, I want to show you a different way to do cooldowns.. I have seen many things not very professional and I want to show you something better: at first as this is the class util: Code: private static HashMap<UUID, Long> cooldown = new HashMap<>(); public static void setCooldown(Player p) { cooldown.put(p.getUniqueId(), System.currentTimeMillis()); } public static boolean hasCooldown(Player p) { return cooldown.containsKey(p.getUniqueId()); } public static void removeCooldown(Player p) { cooldown.remove(p.getUniqueId()); } public static long getTime(Player p, int time) { if(hasCooldown(p)) return 0L; long x = ((cooldown.get(p.getUniqueId()) /1000) + time) - (System.currentTimeMillis()/100); if(x == 0) cooldown.remove(p.getUniqueId()); return x; } Now it must be used ! Code: public void playCooldown(Player p, int cooldownTime) { //To start without cooldown if(cooldownTime > 0) { //to remove the player if the cooldown is done if(UtilCooldown.hasCooldown(p)) { if(UtilCooldown.getTime(p, cooldownTime) == 0) { UtilCooldown.removeCooldown(p); } } if(!UtilCooldown.hasCooldown(p)) { UtilCooldown.setCooldown(p); //Start here --> p.playSound(p.getLocation(), Sound.LEVEL_UP,1, 1); } else { //Display cooldown second or seconds ? String s = (UtilCooldown.getTime(p, cooldownTime) == 1) ? "second" : "seconds"; if(UtilCooldown.getTime(p, cooldownTime) > 0) { p.sendMessage(UtilCooldown.getTime(p cooldownTime) + s + "left"); } } } else { //Start here --> instant start p.playSound(p.getLocation(), Sound.LEVEL_UP,1, 1); } } Now the problem of this is that you can only save one cooldow per player, to correct this you can simply create a double hashmap : this is an example, you can save something other than a class Code: private static HashMap<UUID, HashMap<Class<?>, Long>> cooldown = new HashMap<>(); public static void setCooldown(Player p ,Class<?> gadget) { if(!cooldown.containsKey(p.getUniqueId())) { HashMap<Class<?>, Long> cooldown2 = new HashMap<>(); cooldown2.put(gadget, System.currentTimeMillis()); cooldown.put(p.getUniqueId(), cooldown2); } else { cooldown.get(p.getUniqueId()).put(gadget, System.currentTimeMillis()); } } public static boolean hasCooldown(Player p,Class<?> gadget) { if(cooldown.containsKey(p.getUniqueId())) { return true; } else { return cooldown.get(p.getUniqueId()).containsKey(gadget)); } } public static void removeCooldown(Player p, Class<?> gadget) { cooldown.get(p.getUniqueId()).remove(gadget); } public static long getTime(Player p, Class<?> gadget, int time) { long stat = 0; if(hasCooldown(p, gadget)) { stat = ((cooldown.get(p.getUniqueId()).get(gadget) /1000) + time) - (System.currentTimeMillis()/1000); if(stat < 0) { cooldown.get(p.getUniqueId()).remove(gadget); } } return stat; } Happy coding !
@coco5843 What if you stored an object as the key instead of a UUID, that way it's not specific to just UUIDs.
@coco5843 I only looked at the first code snippet, because it was really broken. Please make sure your resources are actually at least functioning before posting it here, otherwise it's not useful to anybody Anyway, here are some problems: 1) Program to an interface not an implementation. Why are you declaring as HashMap rather than just as a Map? 2) Your logic is off for the getTime() method - it will either throw an NPE or return 0 3) Once you have fixed that, your maths logic for that method is off too. You don't correctly convert to seconds in one part.