This commit is contained in:
2022-11-17 23:44:45 +01:00
parent 66103a26e4
commit afe4297640
7 changed files with 233 additions and 71 deletions

View File

@@ -1,15 +1,85 @@
package net.t2code.automatedMessages.messages;
import net.t2code.automatedMessages.objects.Message;
import net.t2code.automatedMessages.system.Main;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
import org.bukkit.Bukkit;
public class CronJob{
public Message message;
import java.time.LocalDateTime;
public class CronJob {
private Message message;
private Boolean shutdown = false;
public CronJob(Message message) {
this.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();
}
public void execute() {
SendMessage.send(message.message);
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(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;
}
}