86 lines
2.6 KiB
Java
86 lines
2.6 KiB
Java
package net.t2code.automatedMessages.messages;
|
|
|
|
import net.t2code.automatedMessages.objects.Message;
|
|
import net.t2code.automatedMessages.system.Main;
|
|
import org.bukkit.Bukkit;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
public class CronJob {
|
|
|
|
private Message message;
|
|
private Boolean shutdown = false;
|
|
|
|
public CronJob(@NotNull Message message) {
|
|
this.message = message;
|
|
String minuteString = message.timeMinute;
|
|
String hourString = message.timeHour;
|
|
|
|
if (minuteString.contains("*/")) {
|
|
timer(Integer.valueOf(minuteString.replace("*/", "")), 60);
|
|
return;
|
|
}
|
|
if (hourString.contains("*/")) {
|
|
timer(Integer.valueOf(hourString.replace("*/", "")), 60 * 60);
|
|
return;
|
|
}
|
|
job();
|
|
}
|
|
|
|
private void job() {
|
|
Bukkit.getScheduler().runTaskTimerAsynchronously(Main.getPlugin(), new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
if (shutdown) return;
|
|
executeJob();
|
|
}
|
|
}, 0, 60 * 20L);
|
|
}
|
|
|
|
private void executeJob() {
|
|
LocalDateTime dateTime = LocalDateTime.now();
|
|
|
|
if (check(message.timeMinute, dateTime.getMinute(), "minute")) return;
|
|
if (check(message.timeHour, dateTime.getHour(), "h")) return;
|
|
if (check(message.timeDayOfMonth, dateTime.getDayOfMonth(), "dom")) return;
|
|
if (check(message.timeMonth, dateTime.getMonth().getValue(), "mon")) return;
|
|
if (check(message.timeDayOfWeek, dateTime.getDayOfWeek().getValue(), "dow")) return;
|
|
|
|
SendMessage.send(message);
|
|
}
|
|
|
|
private boolean check(@NotNull String check, Integer value, String v) {
|
|
if (check.equals("*")) return false;
|
|
if (check.contains("/")) {
|
|
String[] strings = check.split("/");
|
|
for (String string : strings) {
|
|
if (string.equals("*")) return false;
|
|
if (Integer.parseInt(string) == value) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
try {
|
|
if (Integer.parseInt(check) == value) return false;
|
|
} catch (Exception ignored) {
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public void timer(Integer period, Integer value) {
|
|
Bukkit.getScheduler().scheduleAsyncRepeatingTask(Main.getPlugin(), new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
if (shutdown) return;
|
|
SendMessage.send(message);
|
|
}
|
|
}, 0, 20L * value * period);
|
|
}
|
|
|
|
public void shutdown() {
|
|
this.shutdown = true;
|
|
}
|
|
}
|
|
|