Commit 5b6ecc72 authored by Yazawazi's avatar Yazawazi Committed by Melledy
Browse files

Task Improvement

parent ef2e159b
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;
}
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
}
}
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<String, TaskHandler> tasks = new HashMap<>();
private final Map<String, Task> annotations = new HashMap<>();
private final Map<String, TaskHandler> 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();
}
}
}
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<Player> 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();
}
}
}
});
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment