Skip to content

Instantly share code, notes, and snippets.

@missionarydev
Created May 5, 2017 23:54
Show Gist options
  • Save missionarydev/20966c4542bf44dd8ee31e49c39f0a02 to your computer and use it in GitHub Desktop.
Save missionarydev/20966c4542bf44dd8ee31e49c39f0a02 to your computer and use it in GitHub Desktop.
CooldownTimers Utility class from SinixPvP
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();
}
@missionarydev
Copy link
Author

missionarydev commented May 5, 2017

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment