Created
May 5, 2017 23:54
-
-
Save missionarydev/20966c4542bf44dd8ee31e49c39f0a02 to your computer and use it in GitHub Desktop.
CooldownTimers Utility class from SinixPvP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.surgehcf.core.hcf; | |
import java.util.HashMap; | |
import java.util.UUID; | |
import org.bukkit.entity.Player; | |
public class CooldownTimers | |
{ | |
public static void createCooldown(String k) | |
{ | |
if (cooldown.containsKey(k)) { | |
throw new IllegalArgumentException("Cooldown already exists."); | |
} | |
cooldown.put(k, new HashMap()); | |
} | |
public static HashMap<UUID, Long> getCooldownMap(String k) | |
{ | |
if (cooldown.containsKey(k)) { | |
return cooldown.get(k); | |
} | |
return null; | |
} | |
public static void addCooldown(String k, Player p, int seconds) | |
{ | |
if (!cooldown.containsKey(k)) { | |
throw new IllegalArgumentException(k + " does not exist"); | |
} | |
long next = System.currentTimeMillis() + seconds * 1000L; | |
((HashMap)cooldown.get(k)).put(p.getUniqueId(), Long.valueOf(next)); | |
} | |
public static boolean isOnCooldown(String k, Player p) | |
{ | |
return (cooldown.containsKey(k)) && (cooldown.get(k).containsKey(p.getUniqueId())) && (System.currentTimeMillis() <= ((Long)((HashMap)cooldown.get(k)).get(p.getUniqueId())).longValue()); | |
} | |
public static int getCooldownForPlayerInt(String k, Player p) | |
{ | |
return (int)((Long) ((HashMap) cooldown.get(k)).get(p.getUniqueId()) - System.currentTimeMillis()) / 1000; | |
} | |
public static long getCooldownForPlayerLong(String k, Player p) | |
{ | |
return (int)(((Long)((HashMap)cooldown.get(k)).get(p.getUniqueId())).longValue() - System.currentTimeMillis()); | |
} | |
public static void removeCooldown(String k, Player p) | |
{ | |
if (!cooldown.containsKey(k)) { | |
throw new IllegalArgumentException(k + " does not exist"); | |
} | |
((HashMap)cooldown.get(k)).remove(p.getUniqueId()); | |
} | |
private static HashMap<String, HashMap<UUID, Long>> cooldown = new HashMap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An example of unnecessary unboxing would be at line 46, return (int)(((Long)((HashMap)cooldown.get(k)).get(p.getUniqueId())).longValue() - System.currentTimeMillis());
You do not need to get the #longValue() because it already returns a long.
Screenshot from IntelliJ IDEA: