From bca7aea95b5475c86a9dc66102e0e49df2996b63 Mon Sep 17 00:00:00 2001 From: Yazawazi <47273265+Yazawazi@users.noreply.github.com> Date: Wed, 27 Apr 2022 13:10:50 +0800 Subject: [PATCH] Task Improvement --- src/main/java/emu/grasscutter/task/Task.java | 4 + .../emu/grasscutter/task/TaskHandler.java | 19 +++-- .../java/emu/grasscutter/task/TaskMap.java | 73 +++++++++++++++---- .../emu/grasscutter/task/tasks/MoonCard.java | 13 ++-- 4 files changed, 80 insertions(+), 29 deletions(-) diff --git a/src/main/java/emu/grasscutter/task/Task.java b/src/main/java/emu/grasscutter/task/Task.java index 34638a777..1f35d16ce 100644 --- a/src/main/java/emu/grasscutter/task/Task.java +++ b/src/main/java/emu/grasscutter/task/Task.java @@ -1,5 +1,7 @@ package emu.grasscutter.task; +import org.quartz.JobDataMap; + import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -27,4 +29,6 @@ public @interface Task { String taskName() default "NO_NAME"; String taskCronExpression() default "0 0 0 0 0 ?"; String triggerName() default "NO_NAME"; + boolean executeImmediatelyAfterReset() default false; + boolean executeImmediately() default false; } diff --git a/src/main/java/emu/grasscutter/task/TaskHandler.java b/src/main/java/emu/grasscutter/task/TaskHandler.java index e1a160a07..c61f45534 100644 --- a/src/main/java/emu/grasscutter/task/TaskHandler.java +++ b/src/main/java/emu/grasscutter/task/TaskHandler.java @@ -1,11 +1,18 @@ package emu.grasscutter.task; -import org.quartz.Job; -import org.quartz.JobExecutionContext; -import org.quartz.JobExecutionException; +import org.quartz.*; -public interface TaskHandler extends Job { - default void execute(JobExecutionContext context) throws JobExecutionException { - +@PersistJobDataAfterExecution +public class TaskHandler implements Job { + + public void restartExecute() throws JobExecutionException { + execute(null); } + + @Override + public void execute(JobExecutionContext context) throws JobExecutionException { + // TODO Auto-generated method stub + + } + } diff --git a/src/main/java/emu/grasscutter/task/TaskMap.java b/src/main/java/emu/grasscutter/task/TaskMap.java index 077ff1f59..9e6c1efca 100644 --- a/src/main/java/emu/grasscutter/task/TaskMap.java +++ b/src/main/java/emu/grasscutter/task/TaskMap.java @@ -1,20 +1,9 @@ package emu.grasscutter.task; import emu.grasscutter.Grasscutter; -import emu.grasscutter.game.Account; -import emu.grasscutter.game.player.Player; -import org.quartz.CronScheduleBuilder; -import org.quartz.CronTrigger; -import org.quartz.JobBuilder; -import org.quartz.JobDetail; -import org.quartz.Scheduler; -import org.quartz.SchedulerException; -import org.quartz.SchedulerFactory; -import org.quartz.Trigger; -import org.quartz.TriggerBuilder; +import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; -import org.quartz.spi.MutableTrigger; import org.reflections.Reflections; import java.util.*; @@ -23,6 +12,7 @@ import java.util.*; public final class TaskMap { private final Map tasks = new HashMap<>(); private final Map annotations = new HashMap<>(); + private final Map afterReset = new HashMap<>(); private final SchedulerFactory schedulerFactory = new StdSchedulerFactory(); public TaskMap() { @@ -37,6 +27,44 @@ public final class TaskMap { return Grasscutter.getGameServer().getTaskMap(); } + public void resetNow() { + // Unregister all tasks + for (TaskHandler task : this.tasks.values()) { + unregisterTask(task.getClass().getAnnotation(Task.class).taskName()); + } + + // Run all afterReset tasks + for (TaskHandler task : this.afterReset.values()) { + try { + task.restartExecute(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + // Remove all afterReset tasks + this.afterReset.clear(); + + // Register all tasks + for (TaskHandler task : this.tasks.values()) { + registerTask(task.getClass().getAnnotation(Task.class).taskName(), task); + } + } + + public TaskMap unregisterTask(String taskName) { + this.tasks.remove(taskName); + this.annotations.remove(taskName); + + try { + Scheduler scheduler = schedulerFactory.getScheduler(); + scheduler.deleteJob(new JobKey(taskName)); + } catch (SchedulerException e) { + e.printStackTrace(); + } + + return this; + } + public TaskMap registerTask(String taskName, TaskHandler task) { Task annotation = task.getClass().getAnnotation(Task.class); this.annotations.put(taskName, annotation); @@ -56,7 +84,10 @@ public final class TaskMap { .build(); scheduler.scheduleJob(job, convTrigger); - scheduler.start(); + + if (annotation.executeImmediately()) { + task.execute(null); + } } catch (SchedulerException e) { e.printStackTrace(); } @@ -83,12 +114,24 @@ public final class TaskMap { try { Task taskData = annotated.getAnnotation(Task.class); Object object = annotated.newInstance(); - if (object instanceof TaskHandler) + if (object instanceof TaskHandler) { this.registerTask(taskData.taskName(), (TaskHandler) object); - else Grasscutter.getLogger().error("Class " + annotated.getName() + " is not a TaskHandler!"); + if (taskData.executeImmediatelyAfterReset()) { + this.afterReset.put(taskData.taskName(), (TaskHandler) object); + } + } else { + Grasscutter.getLogger().error("Class " + annotated.getName() + " is not a TaskHandler!"); + } } catch (Exception exception) { Grasscutter.getLogger().error("Failed to register task handler for " + annotated.getSimpleName(), exception); } }); + try { + Scheduler scheduler = schedulerFactory.getScheduler(); + scheduler.start(); + } catch (SchedulerException e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/emu/grasscutter/task/tasks/MoonCard.java b/src/main/java/emu/grasscutter/task/tasks/MoonCard.java index 45c04586e..21ac60c97 100644 --- a/src/main/java/emu/grasscutter/task/tasks/MoonCard.java +++ b/src/main/java/emu/grasscutter/task/tasks/MoonCard.java @@ -1,27 +1,24 @@ package emu.grasscutter.task.tasks; -import emu.grasscutter.database.DatabaseManager; -import emu.grasscutter.game.player.Player; +import emu.grasscutter.Grasscutter; import emu.grasscutter.task.Task; import emu.grasscutter.task.TaskHandler; -import java.util.List; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; @Task(taskName = "MoonCard", taskCronExpression = "0 0 0 * * ?", triggerName = "MoonCardTrigger") // taskCronExpression: Fixed time period: 0:0:0 every day (twenty-four hour system) -public final class MoonCard implements TaskHandler { +public class MoonCard extends TaskHandler { @Override - public void execute(JobExecutionContext context) throws JobExecutionException { - List players = DatabaseManager.getDatastore().find(Player.class).stream().toList(); - for (Player player : players) { + public synchronized void execute(JobExecutionContext context) throws JobExecutionException { + Grasscutter.getGameServer().getPlayers().forEach((uid, player) -> { if (player.isOnline()) { if (player.inMoonCard()) { player.getTodayMoonCard(); } } - } + }); } }