mirror of
https://github.com/Melledy/Grasscutter.git
synced 2024-11-27 21:10:54 +00:00
Task Improvement
This commit is contained in:
parent
4480ce14a7
commit
bca7aea95b
@ -1,5 +1,7 @@
|
|||||||
package emu.grasscutter.task;
|
package emu.grasscutter.task;
|
||||||
|
|
||||||
|
import org.quartz.JobDataMap;
|
||||||
|
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
|
||||||
@ -27,4 +29,6 @@ public @interface Task {
|
|||||||
String taskName() default "NO_NAME";
|
String taskName() default "NO_NAME";
|
||||||
String taskCronExpression() default "0 0 0 0 0 ?";
|
String taskCronExpression() default "0 0 0 0 0 ?";
|
||||||
String triggerName() default "NO_NAME";
|
String triggerName() default "NO_NAME";
|
||||||
|
boolean executeImmediatelyAfterReset() default false;
|
||||||
|
boolean executeImmediately() default false;
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,18 @@
|
|||||||
package emu.grasscutter.task;
|
package emu.grasscutter.task;
|
||||||
|
|
||||||
import org.quartz.Job;
|
import org.quartz.*;
|
||||||
import org.quartz.JobExecutionContext;
|
|
||||||
import org.quartz.JobExecutionException;
|
|
||||||
|
|
||||||
public interface TaskHandler extends Job {
|
@PersistJobDataAfterExecution
|
||||||
default void execute(JobExecutionContext context) throws JobExecutionException {
|
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;
|
package emu.grasscutter.task;
|
||||||
|
|
||||||
import emu.grasscutter.Grasscutter;
|
import emu.grasscutter.Grasscutter;
|
||||||
import emu.grasscutter.game.Account;
|
|
||||||
import emu.grasscutter.game.player.Player;
|
|
||||||
|
|
||||||
import org.quartz.CronScheduleBuilder;
|
import org.quartz.*;
|
||||||
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.impl.StdSchedulerFactory;
|
import org.quartz.impl.StdSchedulerFactory;
|
||||||
import org.quartz.spi.MutableTrigger;
|
|
||||||
import org.reflections.Reflections;
|
import org.reflections.Reflections;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -23,6 +12,7 @@ import java.util.*;
|
|||||||
public final class TaskMap {
|
public final class TaskMap {
|
||||||
private final Map<String, TaskHandler> tasks = new HashMap<>();
|
private final Map<String, TaskHandler> tasks = new HashMap<>();
|
||||||
private final Map<String, Task> annotations = new HashMap<>();
|
private final Map<String, Task> annotations = new HashMap<>();
|
||||||
|
private final Map<String, TaskHandler> afterReset = new HashMap<>();
|
||||||
private final SchedulerFactory schedulerFactory = new StdSchedulerFactory();
|
private final SchedulerFactory schedulerFactory = new StdSchedulerFactory();
|
||||||
|
|
||||||
public TaskMap() {
|
public TaskMap() {
|
||||||
@ -37,6 +27,44 @@ public final class TaskMap {
|
|||||||
return Grasscutter.getGameServer().getTaskMap();
|
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) {
|
public TaskMap registerTask(String taskName, TaskHandler task) {
|
||||||
Task annotation = task.getClass().getAnnotation(Task.class);
|
Task annotation = task.getClass().getAnnotation(Task.class);
|
||||||
this.annotations.put(taskName, annotation);
|
this.annotations.put(taskName, annotation);
|
||||||
@ -56,7 +84,10 @@ public final class TaskMap {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
scheduler.scheduleJob(job, convTrigger);
|
scheduler.scheduleJob(job, convTrigger);
|
||||||
scheduler.start();
|
|
||||||
|
if (annotation.executeImmediately()) {
|
||||||
|
task.execute(null);
|
||||||
|
}
|
||||||
} catch (SchedulerException e) {
|
} catch (SchedulerException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -83,12 +114,24 @@ public final class TaskMap {
|
|||||||
try {
|
try {
|
||||||
Task taskData = annotated.getAnnotation(Task.class);
|
Task taskData = annotated.getAnnotation(Task.class);
|
||||||
Object object = annotated.newInstance();
|
Object object = annotated.newInstance();
|
||||||
if (object instanceof TaskHandler)
|
if (object instanceof TaskHandler) {
|
||||||
this.registerTask(taskData.taskName(), (TaskHandler) object);
|
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) {
|
} catch (Exception exception) {
|
||||||
Grasscutter.getLogger().error("Failed to register task handler for " + annotated.getSimpleName(), 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;
|
package emu.grasscutter.task.tasks;
|
||||||
|
|
||||||
import emu.grasscutter.database.DatabaseManager;
|
import emu.grasscutter.Grasscutter;
|
||||||
import emu.grasscutter.game.player.Player;
|
|
||||||
import emu.grasscutter.task.Task;
|
import emu.grasscutter.task.Task;
|
||||||
import emu.grasscutter.task.TaskHandler;
|
import emu.grasscutter.task.TaskHandler;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.quartz.JobExecutionContext;
|
import org.quartz.JobExecutionContext;
|
||||||
import org.quartz.JobExecutionException;
|
import org.quartz.JobExecutionException;
|
||||||
|
|
||||||
@Task(taskName = "MoonCard", taskCronExpression = "0 0 0 * * ?", triggerName = "MoonCardTrigger")
|
@Task(taskName = "MoonCard", taskCronExpression = "0 0 0 * * ?", triggerName = "MoonCardTrigger")
|
||||||
// taskCronExpression: Fixed time period: 0:0:0 every day (twenty-four hour system)
|
// 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
|
@Override
|
||||||
public void execute(JobExecutionContext context) throws JobExecutionException {
|
public synchronized void execute(JobExecutionContext context) throws JobExecutionException {
|
||||||
List<Player> players = DatabaseManager.getDatastore().find(Player.class).stream().toList();
|
Grasscutter.getGameServer().getPlayers().forEach((uid, player) -> {
|
||||||
for (Player player : players) {
|
|
||||||
if (player.isOnline()) {
|
if (player.isOnline()) {
|
||||||
if (player.inMoonCard()) {
|
if (player.inMoonCard()) {
|
||||||
player.getTodayMoonCard();
|
player.getTodayMoonCard();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user