Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package nl.Rektmeneer.PremiumWands.Wands.Cooldown;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.PriorityQueue;
- import java.util.UUID;
- import org.bukkit.entity.Player;
- import org.bukkit.scheduler.BukkitRunnable;
- import nl.Rektmeneer.PremiumWands.Spell;
- import nl.Rektmeneer.PremiumWands.WandsPremium;
- public class Cooldown
- {
- public static HashMap<UUID, HashMap<String, Cooldown>> Cooldowns = new HashMap<UUID, HashMap<String, Cooldown>>();
- public static Cooldown plugin = new Cooldown();
- public static final PriorityQueue<Cooldown> REVERT_QUEUE = new PriorityQueue<Cooldown>(100, new Comparator<Cooldown>()
- {
- @Override
- public int compare(Cooldown t1, Cooldown t2)
- {
- return (int) (t1.removeCooldownTime - t2.removeCooldownTime);
- }
- });
- private Spell s;
- private long removeCooldownTime;
- private Player p;
- private boolean active;
- public Cooldown()
- {
- }
- public Cooldown(Player p, Spell s, long cooldown)
- {
- this.s = s;
- this.p = p;
- this.removeCooldownTime = System.currentTimeMillis() + cooldown;
- this.active = true;
- HashMap<String, Cooldown> value = new HashMap<String, Cooldown>();
- value.put(s.getClass().getSimpleName(), this);
- Cooldowns.put(p.getUniqueId(), value);
- REVERT_QUEUE.add(this);
- }
- public static boolean hasCooldown(Player p, Spell s)
- {
- for(Cooldown c : REVERT_QUEUE)
- {
- if(c.getPlayer().getUniqueId().equals(p.getUniqueId()))
- {
- if(c.getSpell().equals(s))
- {
- if(c.isActive())
- {
- return true;
- }
- }
- }
- }
- return false;
- }
- public Spell getSpell()
- {
- return this.s;
- }
- public Player getPlayer()
- {
- return this.p;
- }
- public boolean isActive()
- {
- return this.active;
- }
- public long getCooldown()
- {
- return this.removeCooldownTime;
- }
- public long getTimeLeft(Player p, Spell s)
- {
- Cooldown c = getCooldown(p, s);
- long t = c.getCooldown() - System.currentTimeMillis();
- long tim = t/1000;
- long time = Math.round(tim);
- return time;
- }
- public void remove()
- {
- this.active = false;
- }
- public static void startCheck()
- {
- new BukkitRunnable()
- {
- public void run()
- {
- long currentTime = System.currentTimeMillis();
- while (!Cooldown.REVERT_QUEUE.isEmpty())
- {
- Cooldown cooldown = (Cooldown) Cooldown.REVERT_QUEUE.peek();
- if (currentTime < cooldown.removeCooldownTime)
- {
- break;
- }
- Cooldown.REVERT_QUEUE.poll();
- cooldown.remove();
- }
- }
- }.runTaskTimer(WandsPremium.plugin, 0L, 1L);
- }
- public Cooldown getCooldown(Player p, Spell s)
- {
- if(Cooldowns.containsKey(p.getUniqueId()))
- {
- if(Cooldowns.get(p.getUniqueId()).containsKey(s.getClass().getSimpleName()))
- {
- if(Cooldowns.get(p.getUniqueId()).get(s.getClass().getSimpleName()).isActive())
- {
- return Cooldowns.get(p.getUniqueId()).get(s.getClass().getSimpleName());
- }
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement