Skip to content

Commit

Permalink
chore: add extra info
Browse files Browse the repository at this point in the history
  • Loading branch information
Subilan committed Aug 7, 2024
1 parent e478185 commit 92e866c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import cc.seati.SeatiCore.Commands.Abstract.Command;
import cc.seati.SeatiCore.Main;
import cc.seati.SeatiCore.Tasks.Task;
import cc.seati.SeatiCore.Tasks.TaskType;
import cc.seati.SeatiCore.Utils.CommonUtil;
import cc.seati.SeatiCore.Utils.TextUtil;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.MutableComponent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.time.LocalDateTime;
Expand All @@ -21,25 +23,18 @@ public CommandTaskInfo(String taskName) {

@Override
public int handle(CommandContext<CommandSourceStack> ctx) {
MutableComponent component = buildTaskInfo(this.taskName);
if (component == null) {
TaskType type = TaskType.of(this.taskName);
if (type == null) {
CommonUtil.sendMessage(ctx, "&c找不到 &e" + this.taskName + "&c 的相关信息");
} else {
CommonUtil.sendMessage(ctx, component);
return 1;
}
Task task = type.toMainTask();
MutableComponent component = buildTaskInfo(task).append(task.getExtraInfo());
CommonUtil.sendMessage(ctx, component);
return 1;
}

public @Nullable MutableComponent buildTaskInfo(String taskName) {
Task targetTask = switch (taskName) {
case "backupServer" -> Main.backupServerTask;
case "emptyServer" -> Main.emptyServerTask;
case "playerSnapshot" -> Main.playersSnapshotTask;
default -> null;
};

if (targetTask == null) return null;

public MutableComponent buildTaskInfo(@NotNull Task targetTask) {
boolean isRunning = targetTask.isRunning();
int interval = targetTask.getInterval();
LocalDateTime lastExecution = targetTask.getLastExecution();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public int handle(CommandContext<CommandSourceStack> ctx, String module, String
case "verify" -> new CommandVerify().handle(ctx);
case "archive" -> new CommandArchive().handle(ctx);
case "backup" -> new CommandBackup().handle(ctx);
case "taskinfo", "taskrun", "taskstop" -> {
case "taskinfo", "taskrun", "taskstop", "ti", "tu", "td", "taskup", "taskdown", "taskstart" -> {
if (argArray.length < 2) {
CommonUtil.sendMessage(ctx, "&c参数不足");
yield 1;
} else {
yield switch (action) {
case "taskinfo" -> new CommandTaskInfo(argArray[1]).handle(ctx);
case "taskrun" -> new CommandTaskRunStop(argArray[1], true).handle(ctx);
case "taskstop" -> new CommandTaskRunStop(argArray[1], false).handle(ctx);
case "taskrun", "taskup", "tu", "taskstart" -> new CommandTaskRunStop(argArray[1], true).handle(ctx);
case "taskstop", "taskdown", "td" -> new CommandTaskRunStop(argArray[1], false).handle(ctx);
default -> throw new IllegalStateException("Unexpected value: " + action);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cc.seati.SeatiCore.Utils.CommonUtil;
import cc.seati.SeatiCore.Utils.ConfigUtil;
import cc.seati.SeatiCore.Utils.OSSUtil;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.MinecraftServer;
import org.jetbrains.annotations.Nullable;

Expand Down
14 changes: 10 additions & 4 deletions src/main/java/cc/seati/SeatiCore/Tasks/EmptyServerTask.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cc.seati.SeatiCore.Tasks;

import cc.seati.SeatiCore.Main;
import cc.seati.SeatiCore.Utils.CommonUtil;
import cc.seati.SeatiCore.Utils.ConfigUtil;
import cc.seati.SeatiCore.Utils.LabUtil;
import cc.seati.SeatiCore.Utils.OSSUtil;
import cc.seati.SeatiCore.Utils.*;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.MinecraftServer;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -62,4 +60,12 @@ public int getInterval() {
public TaskType getType() {
return TaskType.EMPTY_SERVER;
}

@Override
public MutableComponent getExtraInfo() {
return TextUtil.literal(
"&f空置时间:&e" + this.emptyTime + "s\n" +
"距离释放:&c" + (ConfigUtil.getMaxEmptyTime() - this.emptyTime) + "s\n"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cc.seati.SeatiCore.Database.Model.OnlinePlayerSnapshot;
import cc.seati.SeatiCore.Main;
import cc.seati.SeatiCore.Utils.DBUtil;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.MinecraftServer;
import org.jetbrains.annotations.Nullable;

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/cc/seati/SeatiCore/Tasks/Task.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package cc.seati.SeatiCore.Tasks;

import cc.seati.SeatiCore.Utils.TextUtil;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import org.jetbrains.annotations.Nullable;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -51,4 +54,8 @@ public int getUptime() {
public abstract @Nullable LocalDateTime getLastExecution();

public abstract TaskType getType();

public MutableComponent getExtraInfo() {
return TextUtil.literal("&7此任务没有其它关键参数");
}
}
7 changes: 6 additions & 1 deletion src/main/java/cc/seati/SeatiCore/Tasks/TaskType.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ public enum TaskType {
this.name = name;
}

/**
* 根据 value 的值返回一个 TaskType
* @param value 与 TaskType#toString 所对应的值,不区分大小写
* @return 如果有匹配,返回对应的 TaskType,否则返回 null
*/
public static @Nullable TaskType of(String value) {
try {
return Arrays.stream(TaskType.values())
.filter(x -> x.toString().equals(value))
.filter(x -> x.toString().equalsIgnoreCase(value))
.toList()
.get(0);
} catch (IndexOutOfBoundsException e) {
Expand Down

0 comments on commit 92e866c

Please sign in to comment.