Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. package me.sean0402.nightvision;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.HashMap;
  5. import java.util.Map.Entry;
  6.  
  7. import org.bukkit.configuration.file.FileConfiguration;
  8. import org.bukkit.configuration.file.YamlConfiguration;
  9. import org.bukkit.plugin.java.JavaPlugin;
  10. import org.bukkit.scheduler.BukkitRunnable;
  11.  
  12. /**
  13. * @author ZiixS (TheDarkPlay
  14. * Created in 22/08/2018
  15. * 2.0 Version
  16. * Please do not remove the credits, thank you :)
  17. */
  18.  
  19. public class ZiCooldownUtil {
  20.  
  21. private static ZiCooldownUtil inst;
  22. private HashMap<String, Cooldown> cooldownslist = new HashMap<String, Cooldown>();
  23.  
  24. public static ZiCooldownUtil getCooldown() {
  25. return inst == null ? inst = new ZiCooldownUtil() : inst;
  26. }
  27.  
  28. public Cooldown getCooldownID(String id) {
  29. return cooldownslist.get(id);
  30. }
  31.  
  32. public static void setCooldowns() {
  33. ZiCooldownUtil cu = getCooldown();
  34. cu.createCooldown("Test", 10);
  35. }
  36.  
  37. public void deleteCooldown(String id) {
  38. cooldownslist.remove(id);
  39. }
  40.  
  41. public Cooldown createCooldown(String id, long seconds) {
  42. if (!cooldownslist.containsKey(id)) {
  43. Cooldown cooldown = new Cooldown(id, seconds);
  44. cooldownslist.put(id, cooldown);
  45. return cooldown;
  46. }
  47. return cooldownslist.get(id);
  48. }
  49.  
  50. public void startVerify(JavaPlugin plugin, int ticks) {
  51. new BukkitRunnable() {
  52. @Override
  53. public void run() {
  54. if (cooldownslist == null || cooldownslist.isEmpty()) return;
  55.  
  56. long time = System.currentTimeMillis();
  57.  
  58. for (Entry<String, Cooldown> cooldowns : cooldownslist.entrySet()) {
  59.  
  60. Cooldown cooldown = cooldowns.getValue();
  61. HashMap<String, Long> cool = cooldown.getAllPlayers();
  62.  
  63. if (cool.isEmpty()) continue;
  64.  
  65. for (Entry<String, Long> uuid : cool.entrySet()) {
  66. if (uuid.getValue() - time <= 0) {
  67. cool.remove(uuid.getKey());
  68. }
  69. }
  70.  
  71. }
  72. }
  73. }.runTaskTimer(plugin, 0, ticks);
  74. }
  75.  
  76. public void saveCooldowns(JavaPlugin plugin) throws IOException {
  77.  
  78. File coolFile = new File(plugin.getDataFolder(), "cooldowns.yml");
  79.  
  80. if (!coolFile.getParentFile().exists()) {
  81. coolFile.getParentFile().mkdirs();
  82. }
  83.  
  84. if (!coolFile.exists()) {
  85. coolFile.createNewFile();
  86. }
  87.  
  88. FileConfiguration coolConfig = YamlConfiguration.loadConfiguration(coolFile);
  89. coolConfig.set(" ", null);
  90.  
  91. if (cooldownslist.isEmpty()) return;
  92.  
  93. for (Entry<String, Cooldown> cooldowns : cooldownslist.entrySet()) {
  94.  
  95. String id = cooldowns.getKey();
  96.  
  97. coolConfig.createSection(id);
  98.  
  99. for (Entry<String, Long> uuid : cooldowns.getValue().getAllPlayers().entrySet()) {
  100. coolConfig.set(id + "." + uuid.getKey(), uuid.getValue().intValue());
  101. }
  102. }
  103.  
  104. coolConfig.save(coolFile);
  105. }
  106.  
  107. public void loadCooldowns(JavaPlugin plugin) {
  108.  
  109. File coolFile = new File(plugin.getDataFolder(), "cooldowns.yml");
  110. if (!coolFile.exists()) return;
  111.  
  112. FileConfiguration coolConfig = YamlConfiguration.loadConfiguration(coolFile);
  113. if (coolConfig == null) return;
  114. if (coolConfig.getConfigurationSection("").getKeys(false) == null) return;
  115.  
  116. long time = System.currentTimeMillis();
  117.  
  118. for (String s : coolConfig.getConfigurationSection("").getKeys(false)) {
  119.  
  120. Cooldown cooldown = this.getCooldownID(s);
  121. if (cooldown == null) continue;
  122.  
  123. if (coolConfig.getConfigurationSection(s).getKeys(false) == null) continue;
  124.  
  125. for (String ss : coolConfig.getConfigurationSection(s).getKeys(false)) {
  126.  
  127. if (coolConfig.getLong(s + "." + ss) - time <= 0) continue;
  128.  
  129. cooldown.addPlayerLong(ss, coolConfig.getLong(s + "." + ss) + time);
  130. }
  131. }
  132. }
  133.  
  134. public class Cooldown {
  135.  
  136. private final String id;
  137. private final long time;
  138. private final HashMap<String, Long> cooldowns = new HashMap<String, Long>();
  139.  
  140. public Cooldown(String id, long time) {
  141. this.id = id;
  142. this.time = time * 1000;
  143. }
  144.  
  145. public String getID() {
  146. return id;
  147. }
  148.  
  149. public long getTime() {
  150. return time;
  151. }
  152.  
  153. public void addPlayer(String name) {
  154. cooldowns.put(name, time + System.currentTimeMillis());
  155. }
  156.  
  157. public void addPlayerLong(String name, long time) {
  158. cooldowns.put(name, time);
  159. }
  160.  
  161. public void removePlayer(String name) {
  162. cooldowns.remove(name);
  163. }
  164.  
  165. public boolean hasPlayer(String name) {
  166. if (cooldowns != null && cooldowns.containsKey(name)) {
  167. return true;
  168. }
  169. return false;
  170. }
  171.  
  172. public long getRemainingLong(String name) {
  173. if (hasPlayer(name)) {
  174. return cooldowns.get(name) - System.currentTimeMillis();
  175. }
  176. return 0;
  177. }
  178.  
  179. public String getRemainingText(String name) {
  180. if (hasPlayer(name)) {
  181. return toYYYYHHmmssS(cooldowns.get(name) - System.currentTimeMillis());
  182. }
  183. return null;
  184. }
  185.  
  186. public HashMap<String, Long> getAllPlayers() {
  187. return cooldowns;
  188. }
  189. }
  190.  
  191. private static void prependTimeAndUnit(StringBuffer timeBuf, long time, String unit) {
  192. if (time < 1) {
  193. return;
  194. }
  195.  
  196. if (timeBuf.length() > 0) {
  197. timeBuf.insert(0, " ");
  198. }
  199.  
  200. timeBuf.insert(0, unit);
  201. timeBuf.insert(0, time);
  202. }
  203.  
  204.  
  205. public static String toYYYYHHmmssS(long timeInMillis) {
  206.  
  207. if (timeInMillis < 1) {
  208. return String.valueOf(timeInMillis);
  209. }
  210.  
  211. StringBuffer timeBuf = new StringBuffer();
  212.  
  213. // second (1000ms) & above
  214. long time = timeInMillis / 1000;
  215. if (time < 1) {
  216. return "less than 1 second";
  217. }
  218.  
  219. long seconds = time % 60;
  220. prependTimeAndUnit(timeBuf, seconds, "s");
  221.  
  222. // minute(60s) & above
  223. time = time / 60;
  224. if (time < 1) {
  225. return timeBuf.toString();
  226. }
  227.  
  228. long minutes = time % 60;
  229. prependTimeAndUnit(timeBuf, minutes, "m");
  230.  
  231. // hour(60m) & above
  232. time = time / 60;
  233. if (time < 1) {
  234. return timeBuf.toString();
  235. }
  236.  
  237. long hours = time % 24;
  238. prependTimeAndUnit(timeBuf, hours, "h");
  239.  
  240. // day(24h) & above
  241. time = time / 24;
  242. if (time < 1) {
  243. return timeBuf.toString();
  244. }
  245.  
  246. long day = time % 365;
  247. prependTimeAndUnit(timeBuf, day, "d");
  248.  
  249. // year(365d) ...
  250. time = time / 365;
  251. if (time < 1) {
  252. return timeBuf.toString();
  253. }
  254.  
  255. prependTimeAndUnit(timeBuf, time, "y");
  256.  
  257. return timeBuf.toString();
  258. }
  259.  
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement