Commit d32f706c authored by KingRainbow44's avatar KingRainbow44
Browse files

Merge branch 'development' into api

# Conflicts:
#	src/main/java/emu/grasscutter/plugin/api/ServerHook.java
parents 47d8fd3c f0d728b4
package emu.grasscutter.task;
import org.quartz.*;
@PersistJobDataAfterExecution
public class TaskHandler implements Job {
public void restartExecute() throws JobExecutionException {
execute(null);
}
public void onEnable() {
}
public void onDisable() {
}
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
// TODO Auto-generated method stub
}
}
package emu.grasscutter.task;
import emu.grasscutter.Grasscutter;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.reflections.Reflections;
import java.util.*;
@SuppressWarnings({"UnusedReturnValue", "unused"})
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() {
this(false);
}
public TaskMap(boolean scan) {
if (scan) this.scan();
}
public static TaskMap getInstance() {
return Grasscutter.getGameServer().getTaskMap();
}
public void resetNow() {
// Unregister all tasks
for (TaskHandler task : this.tasks.values()) {
unregisterTask(task);
}
// 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(TaskHandler task) {
this.tasks.remove(task.getClass().getAnnotation(Task.class).taskName());
this.annotations.remove(task.getClass().getAnnotation(Task.class).taskName());
try {
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.deleteJob(new JobKey(task.getClass().getAnnotation(Task.class).taskName()));
} catch (SchedulerException e) {
e.printStackTrace();
}
task.onDisable();
return this;
}
public TaskMap registerTask(String taskName, TaskHandler task) {
Task annotation = task.getClass().getAnnotation(Task.class);
this.annotations.put(taskName, annotation);
this.tasks.put(taskName, task);
// register task
try {
Scheduler scheduler = schedulerFactory.getScheduler();
JobDetail job = JobBuilder
.newJob(task.getClass())
.withIdentity(taskName)
.build();
Trigger convTrigger = TriggerBuilder.newTrigger()
.withIdentity(annotation.triggerName())
.withSchedule(CronScheduleBuilder.cronSchedule(annotation.taskCronExpression()))
.build();
scheduler.scheduleJob(job, convTrigger);
if (annotation.executeImmediately()) {
task.execute(null);
}
task.onEnable();
} catch (SchedulerException e) {
e.printStackTrace();
}
return this;
}
public List<TaskHandler> getHandlersAsList() {
return new LinkedList<>(this.tasks.values());
}
public HashMap<String, TaskHandler> getHandlers() {
return new LinkedHashMap<>(this.tasks);
}
public TaskHandler getHandler(String taskName) {
return this.tasks.get(taskName);
}
private void scan() {
Reflections reflector = Grasscutter.reflector;
Set<Class<?>> classes = reflector.getTypesAnnotatedWith(Task.class);
classes.forEach(annotated -> {
try {
Task taskData = annotated.getAnnotation(Task.class);
Object object = annotated.newInstance();
if (object instanceof TaskHandler) {
this.registerTask(taskData.taskName(), (TaskHandler) object);
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.Grasscutter;
import emu.grasscutter.task.Task;
import emu.grasscutter.task.TaskHandler;
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 class MoonCard extends TaskHandler {
@Override
public void onEnable() {
Grasscutter.getLogger().info("[Task] MoonCard task enabled.");
}
@Override
public void onDisable() {
Grasscutter.getLogger().info("[Task] MoonCard task disabled.");
}
@Override
public synchronized void execute(JobExecutionContext context) throws JobExecutionException {
Grasscutter.getGameServer().getPlayers().forEach((uid, player) -> {
if (player.isOnline()) {
if (player.inMoonCard()) {
player.getTodayMoonCard();
}
}
});
}
}
......@@ -18,9 +18,9 @@ import java.util.stream.Collectors;
import com.google.gson.reflect.TypeToken;
import emu.grasscutter.GenshinConstants;
import emu.grasscutter.GameConstants;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.GenshinData;
import emu.grasscutter.data.GameData;
import emu.grasscutter.data.ResourceLoader;
import emu.grasscutter.data.def.AvatarData;
import emu.grasscutter.data.def.ItemData;
......@@ -45,48 +45,48 @@ public final class Tools {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
writer.println("// Genshin Impact " + GenshinConstants.VERSION + " GM Handbook");
writer.println("// Grasscutter " + GameConstants.VERSION + " GM Handbook");
writer.println("// Created " + dtf.format(now) + System.lineSeparator() + System.lineSeparator());
list = new ArrayList<>(GenshinData.getAvatarDataMap().keySet());
list = new ArrayList<>(GameData.getAvatarDataMap().keySet());
Collections.sort(list);
writer.println("// Avatars");
for (Integer id : list) {
AvatarData data = GenshinData.getAvatarDataMap().get(id);
AvatarData data = GameData.getAvatarDataMap().get(id);
writer.println(data.getId() + " : " + map.get(data.getNameTextMapHash()));
}
writer.println();
list = new ArrayList<>(GenshinData.getItemDataMap().keySet());
list = new ArrayList<>(GameData.getItemDataMap().keySet());
Collections.sort(list);
writer.println("// Items");
for (Integer id : list) {
ItemData data = GenshinData.getItemDataMap().get(id);
ItemData data = GameData.getItemDataMap().get(id);
writer.println(data.getId() + " : " + map.get(data.getNameTextMapHash()));
}
writer.println();
writer.println("// Scenes");
list = new ArrayList<>(GenshinData.getSceneDataMap().keySet());
list = new ArrayList<>(GameData.getSceneDataMap().keySet());
Collections.sort(list);
for (Integer id : list) {
SceneData data = GenshinData.getSceneDataMap().get(id);
SceneData data = GameData.getSceneDataMap().get(id);
writer.println(data.getId() + " : " + data.getScriptData());
}
writer.println();
writer.println("// Monsters");
list = new ArrayList<>(GenshinData.getMonsterDataMap().keySet());
list = new ArrayList<>(GameData.getMonsterDataMap().keySet());
Collections.sort(list);
for (Integer id : list) {
MonsterData data = GenshinData.getMonsterDataMap().get(id);
MonsterData data = GameData.getMonsterDataMap().get(id);
writer.println(data.getId() + " : " + map.get(data.getNameTextMapHash()));
}
}
......
......@@ -34,7 +34,7 @@ public final class Crypto {
public static void extractSecretKeyBuffer(byte[] data) {
try {
GetPlayerTokenRsp p = GetPlayerTokenRsp.parseFrom(data);
FileUtils.write(Grasscutter.getConfig().KEY_FOLDER + "secretKeyBuffer.bin", p.getSecretKeyBuffer().toByteArray());
FileUtils.write(Grasscutter.getConfig().KEY_FOLDER + "secretKeyBuffer.bin", p.getSecretKeyBytes().toByteArray());
Grasscutter.getLogger().info("Secret Key: " + p.getSecretKey());
} catch (Exception e) {
Grasscutter.getLogger().error("Crypto error.", e);
......
package emu.grasscutter.utils;
import java.util.Date;
import java.util.Calendar;
public final class DateHelper {
public static Date onlyYearMonthDay(Date now) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
}
......@@ -174,11 +174,11 @@ public final class Utils {
// Check for resources folder.
if(!fileExists(resourcesFolder)) {
logger.info("Creating resources folder...");
logger.info("Place a copy of 'GenshinData' in the resources folder.");
logger.info("Place a copy of 'BinOutput' and 'ExcelBinOutput' in the resources folder.");
createFolder(resourcesFolder); exit = true;
}
// Check for GenshinData.
// Check for BinOutput + ExcelBinOuput.
if(!fileExists(resourcesFolder + "BinOutput") ||
!fileExists(resourcesFolder + "ExcelBinOutput")) {
logger.info("Place a copy of 'BinOutput' and 'ExcelBinOutput' in the resources folder.");
......
......@@ -33,7 +33,7 @@ if not "%JAVA_PATH%" == "DO_NOT_CHECK_PATH" (
goto :EXIT
)
) else set JAVA_PATH=
if not exist "%SERVER_PATH%grasscutter.jar" (
if not exist "%SERVER_PATH%%SERVER_JAR_NAME%" (
call :LOG [ERROR] Server jar not found.
goto :EXIT
)
......@@ -129,7 +129,7 @@ del /f /q "%temp%\db.vbs" >nul 2>nul
:GAME
call :LOG [INFO] Starting server...
"%JAVA_PATH%java.exe" -jar "%SERVER_PATH%grasscutter.jar"
"%JAVA_PATH%java.exe" -jar "%SERVER_PATH%%SERVER_JAR_NAME%"
call :LOG [INFO] Server stopped
:EXIT
......
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