mirror of
https://github.com/Melledy/Grasscutter.git
synced 2024-11-24 01:21:30 +00:00
Task Improvement
This commit is contained in:
parent
4480ce14a7
commit
bca7aea95b
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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<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();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user