Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. package nl.Rektmeneer.PremiumWands.Wands.Cooldown;
  2.  
  3. import java.util.Comparator;
  4. import java.util.HashMap;
  5. import java.util.PriorityQueue;
  6. import java.util.UUID;
  7.  
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.scheduler.BukkitRunnable;
  10.  
  11. import nl.Rektmeneer.PremiumWands.Spell;
  12. import nl.Rektmeneer.PremiumWands.WandsPremium;
  13.  
  14. public class Cooldown
  15. {
  16. public static HashMap<UUID, HashMap<String, Cooldown>> Cooldowns = new HashMap<UUID, HashMap<String, Cooldown>>();
  17. public static Cooldown plugin = new Cooldown();
  18.  
  19. public static final PriorityQueue<Cooldown> REVERT_QUEUE = new PriorityQueue<Cooldown>(100, new Comparator<Cooldown>()
  20. {
  21. @Override
  22. public int compare(Cooldown t1, Cooldown t2)
  23. {
  24. return (int) (t1.removeCooldownTime - t2.removeCooldownTime);
  25. }
  26. });
  27.  
  28. private Spell s;
  29. private long removeCooldownTime;
  30. private Player p;
  31. private boolean active;
  32.  
  33. public Cooldown()
  34. {
  35.  
  36. }
  37.  
  38. public Cooldown(Player p, Spell s, long cooldown)
  39. {
  40. this.s = s;
  41. this.p = p;
  42. this.removeCooldownTime = System.currentTimeMillis() + cooldown;
  43. this.active = true;
  44.  
  45. HashMap<String, Cooldown> value = new HashMap<String, Cooldown>();
  46. value.put(s.getClass().getSimpleName(), this);
  47.  
  48. Cooldowns.put(p.getUniqueId(), value);
  49. REVERT_QUEUE.add(this);
  50. }
  51.  
  52. public static boolean hasCooldown(Player p, Spell s)
  53. {
  54. for(Cooldown c : REVERT_QUEUE)
  55. {
  56. if(c.getPlayer().getUniqueId().equals(p.getUniqueId()))
  57. {
  58. if(c.getSpell().equals(s))
  59. {
  60. if(c.isActive())
  61. {
  62. return true;
  63. }
  64. }
  65. }
  66. }
  67. return false;
  68. }
  69.  
  70. public Spell getSpell()
  71. {
  72. return this.s;
  73. }
  74.  
  75. public Player getPlayer()
  76. {
  77. return this.p;
  78. }
  79.  
  80. public boolean isActive()
  81. {
  82. return this.active;
  83. }
  84.  
  85. public long getCooldown()
  86. {
  87. return this.removeCooldownTime;
  88. }
  89.  
  90. public long getTimeLeft(Player p, Spell s)
  91. {
  92. Cooldown c = getCooldown(p, s);
  93. long t = c.getCooldown() - System.currentTimeMillis();
  94. long tim = t/1000;
  95. long time = Math.round(tim);
  96. return time;
  97. }
  98.  
  99. public void remove()
  100. {
  101. this.active = false;
  102. }
  103.  
  104. public static void startCheck()
  105. {
  106. new BukkitRunnable()
  107. {
  108. public void run()
  109. {
  110. long currentTime = System.currentTimeMillis();
  111. while (!Cooldown.REVERT_QUEUE.isEmpty())
  112. {
  113. Cooldown cooldown = (Cooldown) Cooldown.REVERT_QUEUE.peek();
  114. if (currentTime < cooldown.removeCooldownTime)
  115. {
  116. break;
  117. }
  118. Cooldown.REVERT_QUEUE.poll();
  119. cooldown.remove();
  120. }
  121. }
  122. }.runTaskTimer(WandsPremium.plugin, 0L, 1L);
  123. }
  124.  
  125. public Cooldown getCooldown(Player p, Spell s)
  126. {
  127. if(Cooldowns.containsKey(p.getUniqueId()))
  128. {
  129. if(Cooldowns.get(p.getUniqueId()).containsKey(s.getClass().getSimpleName()))
  130. {
  131. if(Cooldowns.get(p.getUniqueId()).get(s.getClass().getSimpleName()).isActive())
  132. {
  133. return Cooldowns.get(p.getUniqueId()).get(s.getClass().getSimpleName());
  134. }
  135. }
  136. }
  137. return null;
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement