public class SchedulerUtils { private Spleef instance; private ChatUtils chat; private File scheduleFile; private FileConfiguration scheduleConfig; public SchedulerUtils(Spleef instance) { this.instance = instance; this.chat = instance.getChat(); this.scheduleFile = instance.getScheduleFile(); this.scheduleConfig = instance.getScheduleConfig(); } public void loadScheduledSpleefEvents() { // Loads all of the events List scheduledEvents = scheduleConfig.getStringList("schedule.daily"); if (scheduledEvents == null || scheduledEvents.size() == 0) return; for (String events : scheduledEvents) { scheduleSpleefEvents(events); } } public void scheduleSpleefEvents(String time) { // Schedules each individual event List scheduledEvents = scheduleConfig.getStringList("schedule.daily"); FileConfiguration config = instance.getConfig(); Spleef.State state = instance.getState(); if (!scheduledEvents.contains(time)) { scheduledEvents.add(time); scheduleConfig.set("schedule.daily", scheduledEvents); } Timer timer = new Timer(); String[] eventTime = time.split(":"); String hour = eventTime[0]; String minute = eventTime[1]; if (minute.toLowerCase().contains("pm") && !hour.equals("12")) { hour = String.valueOf(Integer.parseInt(hour) + 12); } if (minute.toLowerCase().contains("am") && hour.equals("12")) { hour = String.valueOf(0); } int minute1 = Integer.parseInt(minute.replace("am", "").replace("pm", "")); LocalDateTime coreStart = LocalDateTime.of(LocalDate.now(), LocalTime.of(Integer.parseInt(hour), minute1)); ZoneId timeZone = ZoneId.of(instance.getConfig().getString("spleef-schedule.timezone")); ZonedDateTime timeZoneDate = ZonedDateTime.of(coreStart, timeZone); ZonedDateTime timeZoneNow = ZonedDateTime.ofInstant(Instant.now(), timeZone); Date date = Date.from(timeZoneDate.toInstant()); if (timeZoneDate.isBefore(timeZoneNow) && (timeZoneDate.getDayOfMonth() == timeZoneNow.getDayOfMonth())) { date = Date.from(timeZoneDate.toInstant().plusSeconds(86400)); } timer.schedule(new TimerTask() { @Override public void run() { if (instance.getSpawnLocation().getWorld() == null) { chat.sendConsoleMessage(config.getString("spleef-spawn.invalid-location")); return; } if (state == Spleef.State.WAITING) { chat.sendConsoleMessage(config.getString("spleef-start.already-starting")); return; } if (state == Spleef.State.IN_ARENA || state == Spleef.State.FIGHTING) { chat.sendConsoleMessage(config.getString("already-in-game")); return; } for (String str : config.getStringList("spleef-start.success")) { chat.sendBroadcast(str.replace("%PLAYER%", "CONSOLE")); } instance.setState(Spleef.State.WAITING); new SpleefStart(instance).runTaskTimer(instance, 20L, 20L); scheduleSpleefEvents(time); } }, date); instance.getTimers().put(time, timer); } public void stopAllTimers() { HashMap timers = instance.getTimers(); for (Timer timer : timers.values()) { timer.cancel(); } } public List getScheduledSpleefEvents() { List scheduledEvents = scheduleConfig.getStringList("schedule.daily"); if (scheduledEvents == null || scheduledEvents.size() == 0) return null; List events = new ArrayList<>(scheduledEvents); events.sort((o1, o2) -> { try { return new SimpleDateFormat("hh:mma").parse(o1).compareTo(new SimpleDateFormat("hh:mma").parse(o2)); } catch (ParseException e) { return 0; } }); return events; } public List getScheduleSpleefEventsMessage() { List scheduledEvents = scheduleConfig.getStringList("schedule.daily"); if (scheduledEvents == null || scheduledEvents.size() == 0) return null; List eventTimes = new ArrayList<>(); for (int i = 0; i < getScheduledSpleefEvents().size(); i++) { eventTimes.add(instance.getConfig().getString("spleef-list-schedule.values").replace("%TIME%", getScheduledSpleefEvents().get(i))); } return eventTimes; } public Map.Entry getClosestSpleefEventsDate() { List scheduledEvents = scheduleConfig.getStringList("schedule.daily"); if (scheduledEvents == null || scheduledEvents.size() == 0) return null; TreeMap sortedDates = new TreeMap<>(); for (String event : scheduledEvents) { String[] eventTime = event.split(":"); String hour = eventTime[0]; String minute = eventTime[1]; if (minute.toLowerCase().contains("pm") && !hour.equals("12")) { hour = String.valueOf(Integer.parseInt(hour) + 12); } if (minute.toLowerCase().contains("am") && hour.equals("12")) { hour = String.valueOf(0); } int minute1 = Integer.parseInt(minute.replace("am", "").replace("pm", "")); LocalDateTime coreStart = LocalDateTime.of(LocalDate.now(), LocalTime.of(Integer.parseInt(hour), minute1)); ZoneId timeZone = ZoneId.of(instance.getConfig().getString("spleef-schedule.timezone")); ZonedDateTime timeZoneDate = ZonedDateTime.of(coreStart, timeZone); ZonedDateTime timeZoneNow = ZonedDateTime.ofInstant(Instant.now(), timeZone); Date date = Date.from(timeZoneDate.toInstant()); System.out.println("Original Date: " + date); if (timeZoneDate.isBefore(timeZoneNow) && (timeZoneDate.getDayOfMonth() == timeZoneNow.getDayOfMonth())) { date = Date.from(timeZoneDate.toInstant().plusSeconds(86400)); System.out.println("Date Changed: " + date); } sortedDates.put(date, event); } System.out.println(sortedDates); return sortedDates.firstEntry(); } public String getTimeUntilNextSpleefEvent() { Date now = new Date(); if (getClosestSpleefEventsDate() == null || getClosestSpleefEventsDate().getKey() == null) return null; Date closestSpleefEvents = getClosestSpleefEventsDate().getKey(); long hours = ChronoUnit.HOURS.between(now.toInstant(), closestSpleefEvents.toInstant()); long minutes = ChronoUnit.MINUTES.between(now.toInstant(), closestSpleefEvents.toInstant()) % 60; long seconds = ChronoUnit.SECONDS.between(now.toInstant(), closestSpleefEvents.toInstant()) % 60; String time = hours + "h, " + minutes + "m, " + seconds + "s"; if (hours == 1) time = hours + "h, " + minutes + "m, " + seconds + "s"; if (hours == 0) time = minutes + "m, " + seconds + "s"; if (hours == 0 && seconds == 0) time = minutes + "m"; if (hours == 0 && minutes == 0) time = seconds + "s"; if (minutes == 1 && seconds == 0) time = minutes + "m"; if (minutes == 1 && seconds > 0) time = minutes + "m, " + seconds + "s"; return time; } public void reloadScheduleFile() { try { scheduleConfig.load(scheduleFile); } catch (IOException | InvalidConfigurationException e) { e.printStackTrace(); } } public void saveScheduleFile() { try { scheduleConfig.save(scheduleFile); YamlConfiguration.loadConfiguration(scheduleFile); } catch (Exception error) { error.printStackTrace(); } }