Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vote restart #5

Merged
merged 21 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,33 @@
![GitHub pull requests](https://img.shields.io/github/issues-pr/stevei5mc/AutoRestart?style=plastic)
## **插件介绍**
### **功能介绍**
1. **支持多语言(根据玩家客户端的语言进行匹配)**
2. **支持播放音效提醒玩家(暂时只支持mc原有的)**
3. **支持取消自动重启任务**
4. **支持多种重启任务(查看任务类型获取现在支持的重启任务),更多类型敬请期待**
- [x] **多语言(根据玩家客户端的语言进行匹配)**
- [x] **播放音效提醒玩家(暂时只支持mc原有的)**
- [x] **取消自动重启任务**
- [ ] **暂停任务 (敬请期待)**
### **任务类型**
- [x] **定时重启**
- [x] **手动重启(手动重启的时间为配置文件中的提示时间)**
- [x] **服务器无人时自动重启**
- [ ] **投票重启 (敬请期待)**
### **命令与权限**
|命令|权限节点|命令/权限介绍|默认权限|
|:-:|:-:|:-:|:-:|
|:-:|:-:|:-|:-:|
|/autorestart|autorestart.admin|主命令、打开GUI|OP|
|/autorestart reload|autorestart.admin.reload|重载配置文件|OP|
|/autorestart cancel|autorestart.admin.cancel|取消重启任务|OP|
|/autorestart restart manual|autorestart.admin.restart|手动重启服务器|OP|
|/autorestart restart no-player|autorestart.admin.restart|在服务器没有玩家在线时自动重启服务器|OP|
- **任务类型介绍:**
- **manual 手动重启时间为配置文件中`tips_time`设定的时间**
- **no-player 在服务器没有玩家在线的时自动候重启服务器**
|/voterestart|autorestart.user.vote|主命令、打开GUI|player|
|/voterestart initiate|autorestart.user.vote|发起投票|player|
|/voterestart vote approval|autorestart.user.vote|投出赞成票|player|
|/voterestart vote oppose|autorestart.user.vote|投出反对票|player|
|/voterestart vote veto|autorestart.admin.vote.veto|使用一票否决|OP|
||autorestart.admin.vote.force|在调试模式中强制发起投票|OP|
### **配置文件介绍**
```yml
#配置文件版本(勿动!!!)
version: 2
#默认语言
default_language: zh_CN
#重启时间(分钟 min)
Expand All @@ -51,18 +57,25 @@ sound:
#重启前执行的命令(&con为控制台执行 @p 代表玩家名)
runcommand: true
commands:
- "say hello world&con"
- "help"
- "say hello @p&con"
#发起重启投票的最低人数(最低只能为3)
vote_start_player: 3
#投票的时间限制(最高只能为5)
vote_time: 5
#调试模式
debug: false
```
### **支持的语言**
- **顺序按照支持的顺序排序**
- [x] zh_CN 中文(简体)
- [x] zh_TW 中文(繁體)
- [x] en_US English (United States)

## **使用方法**
### **注意事项**
- **自动重启还需要脚本的配合才能实现相关脚本在[Actions](https://github.com/stevei5mc/NewTipsVariables/actions)编译完成后会一并给出,你也可以到对应[GITHUB 仓库](https://github.com/stevei5mc/McStartServer)获取,[Releases](https://github.com/stevei5mc/AutoRestart/releases)中也可以获取得到但只会在版本发布后才会有相关脚本(Windows用.bat后缀的脚本,Linux请用.sh后缀的脚本,另外每种系统的脚本都有两种根据你的需求选择),如果你已经有了相关脚本则可以忽略**
- **本插件的重启时间会受服务器TPS导致重启时间不会按现实的时间进行而是按照服务器TPS的计算的时间进行的**

### **脚本使用说明**
- **此内容使用于配套的启动脚本,可配置项已在下方列出**
Expand Down Expand Up @@ -114,8 +127,8 @@ jar_name="server.jar" #开服核心的名字记得加.jar后缀名
|2|将本插件放进`plugins`文件夹,并确保安装前置插件[MemoriesOfTime-GameCore](https://motci.cn/job/GameCore/)后启动服务器 <br> 安装前置插件[Tips](https://motci.cn/job/Tips/)可让变量`{restart-remainder}`生效(这是可选的)|
|3|如果不使用默认配置则修改好配置文件后重启服务器让新的配置生效(由于自动重启任务的配置读取是在服务器启动阶段进行的,所以无法通过重载配置文件的操作让新的配置完全生效只能通过重启服务器来让新的配置生效)|
## **效果预览**
|![1](docs/image/1.jpg)|![2](docs/image/2.jpg)|
|-|-|
|![3](docs/image/3.jpg)|![4](docs/image/4.jpg)|
|![5](docs/image/5.jpg)|![6](docs/image/6.jpg)|
|![1](docs/image/1.jpg)|![2](docs/image/2.jpg)|![3](docs/image/3.jpg)|
|-|-|-|
|![4](docs/image/4.jpg)|![5](docs/image/5.jpg)|![6](docs/image/6.jpg)|
|![7](docs/image/7.jpg)|![8](docs/image/8.jpg)|![9](docs/image/9.jpg)|
## **[开发文档](docs/dev-doc.md)**
15 changes: 10 additions & 5 deletions docs/dev-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@
}
```
#### **讲解**
```java
Utils.runRestartTask(); // 运行重启任务
Utils.getRestartUseTime(); // 获取重启任务的重启时间
Utils.getRestartTipTime(); // 获取提示时间(在还剩多少秒的时候通知玩家)
Utils.cancelTask(); // 取消重启任务
```
Utils
- runRestartTask(int type) 运行重启任务
- runRestartTask(int restartTime,int type) 运行重启任务
- getRestartUseTime() 获取重启任务的重启时间
- getRestartTipTime() 获取提示时间,在还剩多少秒的时候通知玩家
- cancelTask() 取消重启任务
- getRemainder(Player player) 获取剩余时间
- runVoteTask() 发起重启任务
- cancelVoteTask() 取消重启任务
```
---
Binary file added docs/image/7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/image/8.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/image/9.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<modelVersion>4.0.0</modelVersion>
<groupId>cn.stevei5mc</groupId>
<artifactId>AutoRestart</artifactId>
<version>1.0.0-beta3</version>
<version>1.0.0-beta4</version>
<properties>
<java.version>1.8</java.version>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<compiler.encoding>UTF-8</compiler.encoding>
</properties>
<repositories>
<repository>
Expand Down Expand Up @@ -55,7 +55,7 @@
<version>24.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<dependency>
<groupId>com.smallaswater.tips</groupId>
<artifactId>Tips</artifactId>
<version>2.1.8</version>
Expand All @@ -71,7 +71,7 @@
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${maven.compiler.encoding}</encoding>
<encoding>${compiler.encoding}</encoding>
</configuration>
</plugin>
<plugin>
Expand All @@ -95,7 +95,7 @@
<value>version: "${project.version}"</value>
</replacement>
</replacements>
<encoding>${maven.compiler.encoding}</encoding>
<encoding>${compiler.encoding}</encoding>
</configuration>
</execution>
</executions>
Expand Down
41 changes: 32 additions & 9 deletions src/main/java/cn/stevei5mc/autorestart/AutoRestartPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import cn.nukkit.utils.Config;
import cn.lanink.gamecore.utils.Language;
import cn.nukkit.command.CommandSender;
import cn.stevei5mc.autorestart.command.AutoRestartCommand;
import cn.stevei5mc.autorestart.command.admin.AdminMain;
import cn.stevei5mc.autorestart.command.vote.VoteMain;
import cn.stevei5mc.autorestart.Utils;
import tip.utils.Api;
import cn.stevei5mc.autorestart.TipsVar;
Expand All @@ -33,14 +34,18 @@ public void onLoad() {
instance = this;
saveDefaultConfig();
this.config = new Config(this.getDataFolder() + "/config.yml", Config.YAML);
saveLanguageFile();
for(String lang: languages){
saveResource("language/"+lang+".yml",false);
}
updataConfig();
}

public void onEnable() {
if (this.getServer().getPluginManager().getPlugin("MemoriesOfTime-GameCore") != null) {
tips = false;
tips = false; //这是为了防止一些意外的情况准备的
loadLanguage();
this.getServer().getCommandMap().register("", new AutoRestartCommand());//注册命令
this.getServer().getCommandMap().register("", new AdminMain());//注册命令
this.getServer().getCommandMap().register("", new VoteMain());
int i = Utils.getRestartUseTime();
Utils.runRestartTask(i,1);
if (this.getServer().getPluginManager().getPlugin("Tips") != null) {
Expand Down Expand Up @@ -68,11 +73,6 @@ public void onDisable() {
this.getLogger().info("已停止运行,感谢你的使用");
}

private void saveLanguageFile() {
for(String lang: languages){
saveResource("language/"+lang+".yml",false);
}
}
//使用(有改动)https://github.com/MemoriesOfTime/CrystalWars/blob/master/src/main/java/cn/lanink/crystalwars/CrystalWars.java
private void loadLanguage() {
this.defaultLanguage = this.config.getString("default_language", "zh_CN");
Expand Down Expand Up @@ -107,4 +107,27 @@ public Language getLang(CommandSender sender) {
public void reload() {
this.config = new Config(this.getDataFolder() + "/config.yml", Config.YAML);
}

public void updataConfig() {
int ver = config.getInt("version", 1);
if (ver < 2) {
config.set("version", 2);
if (!config.exists("vote_start_player")) {
config.set("vote_start_player",3);
}
if (!config.exists("vote_time")) {
config.set("vote_time",5);
}
if (!config.exists("runcommand")) {
config.set("runcommand",true);
}
if (!config.exists("commands")) {
config.set("commands",Arrays.asList("help", "say hello \"@p\"&con"));
}
if (!config.exists("debug")) {
config.set("debug",false);
}
config.save();
}
}
}
79 changes: 15 additions & 64 deletions src/main/java/cn/stevei5mc/autorestart/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,18 @@
import cn.stevei5mc.autorestart.AutoRestartPlugin;
import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.level.Sound;
import cn.stevei5mc.autorestart.tasks.RestartTask;
import cn.nukkit.scheduler.TaskHandler;
import java.util.*;
import cn.stevei5mc.autorestart.tasks.VoteTask;
//import java.util.*;

public class Utils {
private static AutoRestartPlugin main = AutoRestartPlugin.getInstance();
public static boolean taskState = false;//任务状态,默认为 false
public static boolean voteTaskState = false;
public static int taskType = 0;//任务类型,默认编号为 0
private static int taskId;

/**
* 在指定时间内发送服务器需要重启的消息及播放音效
* @param seconds 传入剩余秒数
*/
public static void sendRestartMsg(int seconds) {
for (Player player : main.getServer().getOnlinePlayers().values()) {
if (main.getConfig().getBoolean("show_title",true)) {
player.sendTitle(
(main.getLang(player).translateString("restart_msg_title", seconds, main.getLang(player).translateString("time_unit_seconds"))),
(main.getLang(player).translateString("restart_msg_subtitle", seconds, main.getLang(player).translateString("time_unit_seconds"))),
0, 20, 0);
}
if (main.getConfig().getBoolean("show_tip",true)) {
player.sendTip(main.getLang(player).translateString("restrat_msg_tip", seconds, main.getLang(player).translateString("time_unit_seconds")));
}
if (main.getConfig().getBoolean("play_sound",true)) {
//参考和使用部分代码 https://github.com/glorydark/CustomForm/blob/main/src/main/java/glorydark/nukkit/customform/scriptForms/data/SoundData.java
//读取配置中音效的设置
String soundName = main.getConfig().getString("sound.name","random.toast");
float volume = (float) main.getConfig().getDouble("sound.volume",1.0);
float pitch = (float) main.getConfig().getDouble("sound.pitch",1.0);
Optional<Sound> find = Arrays.stream(Sound.values()).filter(get -> get.getSound().equals(soundName)).findAny();// 获取音效对象
Sound sound = find.orElse(null);
// 检查音效是否存在
if (sound != null) {
player.getLevel().addSound(player.getLocation(), sound, volume, pitch);// 播放音效给玩家
}
}
}
}


// 踢出玩家及关闭服务器
public static void shutdownServer() {
cancelTask();
Server.getInstance().getScheduler().scheduleDelayedTask(main, () -> {
if (main.getConfig().getBoolean("runcommand",true) && taskType <= 2) {
for (Player player : main.getServer().getOnlinePlayers().values()) {
ArrayList<String> commands;
commands = new ArrayList<>(main.getConfig().getStringList("commands"));
for (String s : commands) {
String[] cmd = s.split("&");
if ((cmd.length > 1) && ("con".equals(cmd[1]))) {
main.getServer().dispatchCommand(main.getServer().getConsoleSender(), cmd[0].replace("@p", player.getName()));
}else {
main.getServer().dispatchCommand(player, cmd[0].replace("@p", player.getName()));
}
}
}
}
if (main.getConfig().getBoolean("kick_player",true) && taskType <= 2) {
for (Player player : main.getServer().getOnlinePlayers().values()) {
player.kick((main.getLang(player).translateString("kick_player_msg")), false);
}
}
main.getServer().shutdown(); // 关闭服务器 注意:这里不会自动重启,你需要配置服务器管理工具或脚本来自动重启服务器进程
}, 0);
}
private static int voteTaskId;

/**
* 获取在重启前的开始提示的时间
Expand Down Expand Up @@ -99,7 +42,6 @@ public static int getRestartUseTime() {
return time;
}


/**
* 运行重启任务
* @param type 重启任务类型
Expand Down Expand Up @@ -166,12 +108,10 @@ public static String getRemainder(Player player) {
int hours = time / 3600;
int minutes = (time % 3600) / 60;
int seconds = time % 60;

String hourUnit = main.getLang(player).translateString("time_unit_hour");
String minuteUnit = main.getLang(player).translateString("time_unit_minutes");
String secondUnit = main.getLang(player).translateString("time_unit_seconds");
String timee = "";

if (hours > 0) {
timee = String.valueOf(hours) + hourUnit + String.valueOf(minutes) + minuteUnit + String.valueOf(seconds) + secondUnit;
} else if (minutes > 0) {
Expand All @@ -182,4 +122,15 @@ public static String getRemainder(Player player) {
String remainder = main.getLang(player).translateString("variable_remainder",timee);
return remainder;
}

public static void runVoteTask(int time,String vote) {
TaskHandler taskHandler = main.getServer().getScheduler().scheduleRepeatingTask(main, new VoteTask(time,vote), 20, true);
voteTaskId = taskHandler.getTaskId();
voteTaskState = true;
}

public static void cancelVoteTask() {
main.getServer().getScheduler().cancelTask(voteTaskId);
voteTaskState = false;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package cn.stevei5mc.autorestart.command;
package cn.stevei5mc.autorestart.command.admin;

import cn.nukkit.Player;
import cn.nukkit.command.CommandSender;
import cn.stevei5mc.autorestart.command.BaseCommand;
import cn.stevei5mc.autorestart.command.sub.Reload;
import cn.stevei5mc.autorestart.command.sub.Cancel;
import cn.stevei5mc.autorestart.command.sub.RunRestartTask;
import cn.stevei5mc.autorestart.command.base.BaseCommand;
import cn.stevei5mc.autorestart.command.admin.Reload;
import cn.stevei5mc.autorestart.command.admin.Cancel;
import cn.stevei5mc.autorestart.command.admin.RunRestartTask;
import cn.lanink.gamecore.utils.Language;
import cn.stevei5mc.autorestart.AutoRestartPlugin;
import cn.stevei5mc.autorestart.GUI;
import cn.stevei5mc.autorestart.gui.Admin;

public class AutoRestartCommand extends BaseCommand {
public class AdminMain extends BaseCommand {
protected AutoRestartPlugin main = AutoRestartPlugin.getInstance();
public AutoRestartCommand() {
public AdminMain() {
super("autorestart", "AutoRestart Command");
this.setPermission("autorestart.admin");
this.addSubCommand(new Reload("reload"));
Expand All @@ -24,15 +24,14 @@ public AutoRestartCommand() {
public void sendHelp(CommandSender sender) {
Language lang = main.getLang(sender);
String cmdname = "§a/autorestart ";
sender.sendMessage("§b=== AutoRestart Command List ===");
sender.sendMessage("§b=== AutoRestart admin command list ===");
sender.sendMessage(cmdname+"reload "+lang.translateString("command_help_reload"));
sender.sendMessage(cmdname+"cancel "+lang.translateString("command_help_cancel"));
sender.sendMessage(cmdname+"restart <manual|no-player> "+lang.translateString("command_help_restart"));
}

@Override
public void sendUI(Player player) {
GUI.sendMain(player);
Admin.sendMain(player);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cn.stevei5mc.autorestart.command.sub;
package cn.stevei5mc.autorestart.command.admin;

import cn.nukkit.command.CommandSender;
import cn.nukkit.command.data.CommandParameter;
import cn.stevei5mc.autorestart.command.BaseSubCommand;
import cn.stevei5mc.autorestart.command.base.BaseSubCommand;
import cn.nukkit.Player;
import cn.stevei5mc.autorestart.Utils;

Expand Down
Loading