Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.sean0402.nightvision;
- import java.io.File;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map.Entry;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.scheduler.BukkitRunnable;
- /**
- * @author ZiixS (TheDarkPlay
- * Created in 22/08/2018
- * 2.0 Version
- * Please do not remove the credits, thank you :)
- */
- public class ZiCooldownUtil {
- private static ZiCooldownUtil inst;
- private HashMap<String, Cooldown> cooldownslist = new HashMap<String, Cooldown>();
- public static ZiCooldownUtil getCooldown() {
- return inst == null ? inst = new ZiCooldownUtil() : inst;
- }
- public Cooldown getCooldownID(String id) {
- return cooldownslist.get(id);
- }
- public static void setCooldowns() {
- ZiCooldownUtil cu = getCooldown();
- cu.createCooldown("Test", 10);
- }
- public void deleteCooldown(String id) {
- cooldownslist.remove(id);
- }
- public Cooldown createCooldown(String id, long seconds) {
- if (!cooldownslist.containsKey(id)) {
- Cooldown cooldown = new Cooldown(id, seconds);
- cooldownslist.put(id, cooldown);
- return cooldown;
- }
- return cooldownslist.get(id);
- }
- public void startVerify(JavaPlugin plugin, int ticks) {
- new BukkitRunnable() {
- @Override
- public void run() {
- if (cooldownslist == null || cooldownslist.isEmpty()) return;
- long time = System.currentTimeMillis();
- for (Entry<String, Cooldown> cooldowns : cooldownslist.entrySet()) {
- Cooldown cooldown = cooldowns.getValue();
- HashMap<String, Long> cool = cooldown.getAllPlayers();
- if (cool.isEmpty()) continue;
- for (Entry<String, Long> uuid : cool.entrySet()) {
- if (uuid.getValue() - time <= 0) {
- cool.remove(uuid.getKey());
- }
- }
- }
- }
- }.runTaskTimer(plugin, 0, ticks);
- }
- public void saveCooldowns(JavaPlugin plugin) throws IOException {
- File coolFile = new File(plugin.getDataFolder(), "cooldowns.yml");
- if (!coolFile.getParentFile().exists()) {
- coolFile.getParentFile().mkdirs();
- }
- if (!coolFile.exists()) {
- coolFile.createNewFile();
- }
- FileConfiguration coolConfig = YamlConfiguration.loadConfiguration(coolFile);
- coolConfig.set(" ", null);
- if (cooldownslist.isEmpty()) return;
- for (Entry<String, Cooldown> cooldowns : cooldownslist.entrySet()) {
- String id = cooldowns.getKey();
- coolConfig.createSection(id);
- for (Entry<String, Long> uuid : cooldowns.getValue().getAllPlayers().entrySet()) {
- coolConfig.set(id + "." + uuid.getKey(), uuid.getValue().intValue());
- }
- }
- coolConfig.save(coolFile);
- }
- public void loadCooldowns(JavaPlugin plugin) {
- File coolFile = new File(plugin.getDataFolder(), "cooldowns.yml");
- if (!coolFile.exists()) return;
- FileConfiguration coolConfig = YamlConfiguration.loadConfiguration(coolFile);
- if (coolConfig == null) return;
- if (coolConfig.getConfigurationSection("").getKeys(false) == null) return;
- long time = System.currentTimeMillis();
- for (String s : coolConfig.getConfigurationSection("").getKeys(false)) {
- Cooldown cooldown = this.getCooldownID(s);
- if (cooldown == null) continue;
- if (coolConfig.getConfigurationSection(s).getKeys(false) == null) continue;
- for (String ss : coolConfig.getConfigurationSection(s).getKeys(false)) {
- if (coolConfig.getLong(s + "." + ss) - time <= 0) continue;
- cooldown.addPlayerLong(ss, coolConfig.getLong(s + "." + ss) + time);
- }
- }
- }
- public class Cooldown {
- private final String id;
- private final long time;
- private final HashMap<String, Long> cooldowns = new HashMap<String, Long>();
- public Cooldown(String id, long time) {
- this.id = id;
- this.time = time * 1000;
- }
- public String getID() {
- return id;
- }
- public long getTime() {
- return time;
- }
- public void addPlayer(String name) {
- cooldowns.put(name, time + System.currentTimeMillis());
- }
- public void addPlayerLong(String name, long time) {
- cooldowns.put(name, time);
- }
- public void removePlayer(String name) {
- cooldowns.remove(name);
- }
- public boolean hasPlayer(String name) {
- if (cooldowns != null && cooldowns.containsKey(name)) {
- return true;
- }
- return false;
- }
- public long getRemainingLong(String name) {
- if (hasPlayer(name)) {
- return cooldowns.get(name) - System.currentTimeMillis();
- }
- return 0;
- }
- public String getRemainingText(String name) {
- if (hasPlayer(name)) {
- return toYYYYHHmmssS(cooldowns.get(name) - System.currentTimeMillis());
- }
- return null;
- }
- public HashMap<String, Long> getAllPlayers() {
- return cooldowns;
- }
- }
- private static void prependTimeAndUnit(StringBuffer timeBuf, long time, String unit) {
- if (time < 1) {
- return;
- }
- if (timeBuf.length() > 0) {
- timeBuf.insert(0, " ");
- }
- timeBuf.insert(0, unit);
- timeBuf.insert(0, time);
- }
- public static String toYYYYHHmmssS(long timeInMillis) {
- if (timeInMillis < 1) {
- return String.valueOf(timeInMillis);
- }
- StringBuffer timeBuf = new StringBuffer();
- // second (1000ms) & above
- long time = timeInMillis / 1000;
- if (time < 1) {
- return "less than 1 second";
- }
- long seconds = time % 60;
- prependTimeAndUnit(timeBuf, seconds, "s");
- // minute(60s) & above
- time = time / 60;
- if (time < 1) {
- return timeBuf.toString();
- }
- long minutes = time % 60;
- prependTimeAndUnit(timeBuf, minutes, "m");
- // hour(60m) & above
- time = time / 60;
- if (time < 1) {
- return timeBuf.toString();
- }
- long hours = time % 24;
- prependTimeAndUnit(timeBuf, hours, "h");
- // day(24h) & above
- time = time / 24;
- if (time < 1) {
- return timeBuf.toString();
- }
- long day = time % 365;
- prependTimeAndUnit(timeBuf, day, "d");
- // year(365d) ...
- time = time / 365;
- if (time < 1) {
- return timeBuf.toString();
- }
- prependTimeAndUnit(timeBuf, time, "y");
- return timeBuf.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement