diff --git a/src/main/java/net/t2code/automatedMessages/config/Config.java b/src/main/java/net/t2code/automatedMessages/config/Config.java index f3c9ad5..798d4e2 100644 --- a/src/main/java/net/t2code/automatedMessages/config/Config.java +++ b/src/main/java/net/t2code/automatedMessages/config/Config.java @@ -1,12 +1,17 @@ package net.t2code.automatedMessages.config; +import org.bukkit.Sound; + public enum Config { updateCheckOnJoin("plugin.updateCheck.onJoin", true, ConfigParam.BOOLEAN), updateCheckSeePreReleaseUpdates("plugin.updateCheck.seePreReleaseUpdates", true, ConfigParam.BOOLEAN), updateCheckTimeInterval("plugin.updateCheck.timeInterval", 60, ConfigParam.INTEGER), - prefix("plugin.prefix", "", ConfigParam.STRING), + prefix("plugin.prefix", "[T2C-AutomatedMessages]", ConfigParam.STRING), + sendConsole("plugin.broadcastInConsole", true, ConfigParam.BOOLEAN), + + timeFormat("plugin.timeFormat", "HH:mm:ss yyyy/MM/dd", ConfigParam.STRING), msgReloadStart("plugin.messages.reloadStart", "Reload start", ConfigParam.STRING), msgReloadEnd("plugin.messages.reloadEnd", "Reload end", ConfigParam.STRING), @@ -14,12 +19,19 @@ public enum Config { message("messages", "messages.KEY.enable", false, "messages.KEY.message", "", + "messages.KEY.sound.enable", true, + "messages.KEY.sound.sound",FileBuild.sound(), "messages.KEY.intervalInMin", 0, - "messages.KEY.useCronJob.enable", false, - "messages.KEY.useCronJob.value", "", + "messages.KEY.exactTime.enable", false, + "messages.KEY.exactTime.timeMinute", "*/5", + "messages.KEY.exactTime.timeHour", "*", + "messages.KEY.exactTime.timeDayOfMonth", "*", + "messages.KEY.exactTime.timeMonth", "*", + "messages.KEY.exactTime.timeDayOfWeek", "*", ConfigParam.MESSAGE ); + public String path; public String valueString; public Boolean valueBoolean; @@ -31,12 +43,33 @@ public enum Config { public String msgMsgPath; public String msgMsg; + + public String soundEnablePath; + public Boolean soundEnable; + + public String soundPath; + public Sound sound; + public String msgIntervalPath; public Integer msgInterval; - public String msgCronJobPath; - public Boolean msgCronJob; - public String msgCronJobStringPath; - public String msgCronJobString; + + public String exactTimePath; + public Boolean exactTime; + + public String timeMinutePath; + public String timeMinute; + + public String timeHourPath; + public String timeHour; + + public String timeDayOfMonthPath; + public String timeDayOfMonth; + + public String timeMonthPath; + public String timeMonth; + + public String timeDayOfWeekPath; + public String timeDayOfWeek; public ConfigParam configParam; @@ -61,21 +94,41 @@ public enum Config { Config(String msgPath, String msgEnablePath, Boolean msgEnable, String msgMsgPath, String msgMsg, + String soundEnablePath, Boolean soundEnable, + String soundPath , Sound sound, String msgIntervalPath, Integer msgInterval, - String msgCronJobPath, Boolean msgCronJob, - String msgCronJobStringPath, String msgCronJobString, + String exactTimePath, Boolean exactTime, String timeMinutePath, String timeMinute, + String timeHourPath, String timeHour, + String timeDayOfMonthPath, String timeDayOfMonth, + String timeMonthPath, String timeMonth, + String timeDayOfWeekPath, String timeDayOfWeek, ConfigParam configParam) { this.msgPath = msgPath; this.msgEnablePath = msgEnablePath; this.msgEnable = msgEnable; this.msgMsgPath = msgMsgPath; this.msgMsg = msgMsg; + + this.soundEnablePath = soundEnablePath; + this.soundEnable=soundEnable; + this.soundPath=soundPath; + this.sound=sound; + this.msgIntervalPath = msgIntervalPath; this.msgInterval = msgInterval; - this.msgCronJobPath = msgCronJobPath; - this.msgCronJob = msgCronJob; - this.msgCronJobStringPath = msgCronJobStringPath; - this.msgCronJobString = msgCronJobString; + this.exactTimePath = exactTimePath; + this.exactTime = exactTime; + this.timeMinutePath = timeMinutePath; + this.timeMinute = timeMinute; + this.timeHourPath = timeHourPath; + this.timeHour = timeHour; + + this.timeDayOfMonthPath = timeDayOfMonthPath; + this.timeDayOfMonth = timeDayOfMonth; + this.timeMonthPath = timeMonthPath; + this.timeMonth = timeMonth; + this.timeDayOfWeekPath = timeDayOfWeekPath; + this.timeDayOfWeek = timeDayOfWeek; this.configParam = configParam; } } diff --git a/src/main/java/net/t2code/automatedMessages/config/FileBuild.java b/src/main/java/net/t2code/automatedMessages/config/FileBuild.java index 6a88216..0460c8c 100644 --- a/src/main/java/net/t2code/automatedMessages/config/FileBuild.java +++ b/src/main/java/net/t2code/automatedMessages/config/FileBuild.java @@ -6,6 +6,8 @@ import net.t2code.automatedMessages.objects.Message; import net.t2code.automatedMessages.system.Main; import net.t2code.t2codelib.SPIGOT.api.messages.T2Creplace; import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend; +import net.t2code.t2codelib.SPIGOT.api.minecraftVersion.T2CmcVersion; +import org.bukkit.Sound; import org.bukkit.configuration.file.YamlConfiguration; import java.io.File; @@ -50,21 +52,33 @@ public class FileBuild { if (!yamlConfiguration.contains(value.msgPath)) { setExample("exampleDiscord", true, "[prefix] If you need help, just contact us on our dc.t2code.net'>T2Code Support Discord server.", - 20, false, "0 0/5 * ? * * *", yamlConfiguration); + true, sound(), 20, false, "*", "*", "*", "*", "*", yamlConfiguration); setExample("exampleTime", true, "[prefix] Es ist jetzt [time]", - 0, true, "0 0/20 * ? * * *", yamlConfiguration); + true, sound(),0, true, "0/15/30/45", "*", "*", "*", "*", yamlConfiguration); } for (String key : yamlConfiguration.getConfigurationSection(value.msgPath).getKeys(false)) { Boolean enable = yamlConfiguration.getBoolean(value.msgEnablePath.replace("KEY", key)); String message = yamlConfiguration.getString(value.msgMsgPath.replace("KEY", key)); + Boolean soundEnable = yamlConfiguration.getBoolean(value.soundEnablePath.replace("KEY", key)); + + Sound sound; + try { + sound = Sound.valueOf(yamlConfiguration.getString(value.soundPath.replace("KEY", key))); + } catch (Exception e) { + sound = sound(); + } + + Integer interval = yamlConfiguration.getInt(value.msgIntervalPath.replace("KEY", key)); - Boolean cronJob = yamlConfiguration.getBoolean(value.msgCronJobPath.replace("KEY", key)); - String cronJobString = yamlConfiguration.getString(value.msgCronJobStringPath.replace("KEY", key)); - Message msg = new Message(key, enable, message, interval, cronJob, cronJobString); - T2Csend.debugmsg(Main.getPlugin(),key); - T2Csend.debugmsg(Main.getPlugin(), String.valueOf(cronJob)); - T2Csend.debugmsg(Main.getPlugin(),cronJobString); + + Boolean exactTime = yamlConfiguration.getBoolean(value.exactTimePath.replace("KEY", key)); + String timeMinute = yamlConfiguration.getString(value.timeMinutePath.replace("KEY", key)); + String timeHour = yamlConfiguration.getString(value.timeHourPath.replace("KEY", key)); + String timeDayOfMonth = yamlConfiguration.getString(value.timeDayOfMonthPath.replace("KEY", key)); + String timeMonth = yamlConfiguration.getString(value.timeMonthPath.replace("KEY", key)); + String timeDayOfWeek = yamlConfiguration.getString(value.timeDayOfWeekPath.replace("KEY", key)); + Message msg = new Message(key, enable, message, soundEnable, sound, interval, exactTime, timeMinute, timeHour, timeDayOfMonth, timeMonth, timeDayOfWeek); messageHashMap.put(key, msg); } break; @@ -77,11 +91,26 @@ public class FileBuild { } } - private static void setExample(String key, Boolean enable, String msg, Integer interval, Boolean useCronJobEnable, String useCronJobValue,YamlConfiguration yamlConfiguration) { + static Sound sound() { + if (T2CmcVersion.isMc1_8()) { + return Sound.valueOf("NOTE_PIANO"); + } else if (T2CmcVersion.isMc1_9() || T2CmcVersion.isMc1_10() || T2CmcVersion.isMc1_11() || T2CmcVersion.isMc1_12()) { + return Sound.valueOf("BLOCK_NOTE_HARP"); + } else return Sound.BLOCK_NOTE_BLOCK_HARP; + } + + private static void setExample(String key, Boolean enable, String msg,Boolean soundEnable,Sound sound, Integer interval, Boolean exactTime, String timeMinute, String timeHour, String timeDayOfMonth, + String timeMonth, String timeDayOfWeek, YamlConfiguration yamlConfiguration) { yamlConfiguration.set("messages." + key + ".enable", enable); yamlConfiguration.set("messages." + key + ".message", msg); + yamlConfiguration.set("messages." + key + ".sound.enable", soundEnable); + yamlConfiguration.set("messages." + key + ".sound.sound", sound.toString()); yamlConfiguration.set("messages." + key + ".intervalInMin", interval); - yamlConfiguration.set("messages." + key + ".useCronJob.enable", useCronJobEnable); - yamlConfiguration.set("messages." + key + ".useCronJob.value", useCronJobValue); + yamlConfiguration.set("messages." + key + ".exactTime.enable", exactTime); + yamlConfiguration.set("messages." + key + ".exactTime.timeMinute", timeMinute); + yamlConfiguration.set("messages." + key + ".exactTime.timeHour", timeHour); + yamlConfiguration.set("messages." + key + ".exactTime.timeDayOfMonth", timeDayOfMonth); + yamlConfiguration.set("messages." + key + ".exactTime.timeMonth", timeMonth); + yamlConfiguration.set("messages." + key + ".exactTime.timeDayOfWeek", timeDayOfWeek); } } diff --git a/src/main/java/net/t2code/automatedMessages/messages/CronJob.java b/src/main/java/net/t2code/automatedMessages/messages/CronJob.java index d5bb24a..0c0ced9 100644 --- a/src/main/java/net/t2code/automatedMessages/messages/CronJob.java +++ b/src/main/java/net/t2code/automatedMessages/messages/CronJob.java @@ -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; } } + diff --git a/src/main/java/net/t2code/automatedMessages/messages/Management.java b/src/main/java/net/t2code/automatedMessages/messages/Management.java index a4885b4..11e9747 100644 --- a/src/main/java/net/t2code/automatedMessages/messages/Management.java +++ b/src/main/java/net/t2code/automatedMessages/messages/Management.java @@ -9,15 +9,15 @@ import java.util.Map; public class Management { private static final List timerJobList = new ArrayList<>(); + private static final List cronJobs = new ArrayList<>(); public static void start() { for (Map.Entry value : FileBuild.getMessageHashMap().entrySet()) { Message message = value.getValue(); if (!message.enable) continue; - if (message.cronJob) { - startCronJob(message); - - + if (message.exactTime) { + CronJob cronJob = new CronJob(message); + cronJobs.add(cronJob); } else { Timer job = new Timer(message); timerJobList.add(job); @@ -25,37 +25,12 @@ public class Management { } } - public static void startCronJob(Message message) { - try { - CronJob cronJob = new CronJob(message); - JobDetail job = JobBuilder.newJob(cronJob.getClass()) - .withIdentity(message.key, message.key).build(); - Trigger trigger = TriggerBuilder.newTrigger() - .withIdentity(message.key, message.key) - .withSchedule(CronScheduleBuilder.cronSchedule(message.cronJobString)) - //.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) - .build(); - Scheduler scheduler = new StdSchedulerFactory().getScheduler(); - scheduler.start(); - scheduler.scheduleJob(job, trigger); - jobList.add(scheduler); - - } catch (Exception e) { - e.printStackTrace(); - } - - } - public static void end() { for (Timer job : timerJobList) { job.disable = true; } - for (Scheduler scheduler : jobList) { - try { - scheduler.shutdown(); - } catch (SchedulerException e) { - throw new RuntimeException(e); - } + for (CronJob cronJob : cronJobs) { + cronJob.shutdown(); } } } diff --git a/src/main/java/net/t2code/automatedMessages/messages/SendMessage.java b/src/main/java/net/t2code/automatedMessages/messages/SendMessage.java index 377b49e..0448c7f 100644 --- a/src/main/java/net/t2code/automatedMessages/messages/SendMessage.java +++ b/src/main/java/net/t2code/automatedMessages/messages/SendMessage.java @@ -1,15 +1,21 @@ package net.t2code.automatedMessages.messages; -import net.t2code.automatedMessages.system.Main; +import net.t2code.automatedMessages.config.Config; +import net.t2code.automatedMessages.objects.Message; import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend; import org.bukkit.Bukkit; import org.bukkit.entity.Player; +import java.text.SimpleDateFormat; +import java.util.Calendar; + public class SendMessage { - public static void send(String msg){ - T2Csend.debugmsg(Main.getPlugin(),msg); + public static void send(Message message) { + String timeStamp = new SimpleDateFormat(Config.timeFormat.valueString).format(Calendar.getInstance().getTime()); + if (Config.sendConsole.valueBoolean) T2Csend.console(message.message.replace("[time]", timeStamp).replace("[prefix]",Config.prefix.valueString)); for (Player player : Bukkit.getOnlinePlayers()) { - T2Csend.player(player, msg); + T2Csend.player(player, message.message.replace("[time]", timeStamp).replace("[prefix]",Config.prefix.valueString)); + player.playSound(player.getLocation(), message.sound, 3, 1); } } } diff --git a/src/main/java/net/t2code/automatedMessages/messages/Timer.java b/src/main/java/net/t2code/automatedMessages/messages/Timer.java index c26e45f..e6f8dd3 100644 --- a/src/main/java/net/t2code/automatedMessages/messages/Timer.java +++ b/src/main/java/net/t2code/automatedMessages/messages/Timer.java @@ -1,10 +1,14 @@ package net.t2code.automatedMessages.messages; +import net.t2code.automatedMessages.config.Config; import net.t2code.automatedMessages.messages.SendMessage; import net.t2code.automatedMessages.objects.Message; import net.t2code.automatedMessages.system.Main; import org.bukkit.Bukkit; +import java.text.SimpleDateFormat; +import java.util.Calendar; + public class Timer { public Timer(Message message) { @@ -12,9 +16,10 @@ public class Timer { @Override public void run() { if (disable) return; - SendMessage.send(message.message); + SendMessage.send(message); } }, 0, 20L * 60 * message.interval); } + public Boolean disable = false; } diff --git a/src/main/java/net/t2code/automatedMessages/objects/Message.java b/src/main/java/net/t2code/automatedMessages/objects/Message.java index c4a390f..7e819c8 100644 --- a/src/main/java/net/t2code/automatedMessages/objects/Message.java +++ b/src/main/java/net/t2code/automatedMessages/objects/Message.java @@ -1,5 +1,8 @@ package net.t2code.automatedMessages.objects; +import org.bukkit.Sound; + +import javax.sound.midi.Soundbank; import java.util.List; public class Message { @@ -7,21 +10,42 @@ public class Message { public String key; public Boolean enable; public String message; + + public Boolean soundEnable; + public Sound sound; public Integer interval; - public Boolean cronJob; - public String cronJobString; + public Boolean exactTime; + public String timeMinute; + public String timeHour; + public String timeDayOfMonth; + public String timeMonth; + public String timeDayOfWeek; public Message(String key, Boolean enable, String message, + Boolean soundEnable, + Sound sound, Integer interval, - Boolean cronJob, - String cronJobString) { + + Boolean exactTime, + String timeMinute, + String timeHour, + String timeDayOfMonth, + String timeMonth, + String timeDayOfWeek) { this.key = key; this.enable = enable; this.message = message; + this.soundEnable = soundEnable; + this.sound=sound; this.interval = interval; - this.cronJob = cronJob; - this.cronJobString=cronJobString; + this.exactTime = exactTime; + this.timeMinute = timeMinute; + this.timeHour = timeHour; + this.timeDayOfMonth = timeDayOfMonth; + this.timeMonth = timeMonth; + this.timeDayOfWeek = timeDayOfWeek; + } }