Skip to content

Commit 97dc913

Browse files
committed
MCDR 2.0 format
1 parent 9f4002b commit 97dc913

4 files changed

Lines changed: 27 additions & 41 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.idea
2+
*.mcdr

config/CrashRestart.json

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
1-
import json
21
import os
32
import time
43

5-
PLUGIN_METADATA = {
6-
'id': 'crash_restart',
7-
'version': '1.0.0',
8-
'name': 'Crash Restart',
9-
'author': 'Fallen_Breath',
10-
'link': 'https://github.com/MCDReforged/CrashRestart',
11-
}
4+
from mcdreforged.api.all import *
125

13-
CONFIG_FILE = os.path.join('config', 'CrashRestart.json')
146

15-
# MAXIMUM: 3 crashes within 5min
16-
config = {
17-
"MAX_COUNT": 3,
18-
"COUNTING_TIME": 300 # seconds
19-
}
20-
default_config = config.copy()
7+
class Config(Serializable):
8+
MAX_COUNT: int = 3
9+
COUNTING_TIME: int = 300
10+
2111

12+
config: Config
2213
counter = None
2314
count_start_time = None
2415
is_crash = False
16+
PLUGIN_METADATA = ServerInterface.get_instance().as_plugin_server_interface().get_self_metadata()
17+
CONFIG_FILE = os.path.join('config', 'CrashRestart.json')
2518

2619

2720
def on_server_startup(server):
@@ -40,9 +33,9 @@ def on_server_stop(server, return_code):
4033
global counter, count_start_time, is_crash
4134
if return_code == 0 and not is_crash:
4235
return
43-
max_count = config['MAX_COUNT']
44-
counting_time = config['COUNTING_TIME']
45-
reason = 'the return code of the server is {}'.format(return_code) if return_code != 0 else 'a crash report has been created'
36+
max_count = config.MAX_COUNT
37+
counting_time = config.COUNTING_TIME
38+
reason = 'The return code of the server is {}'.format(return_code) if return_code != 0 else 'a crash report has been created'
4639
server.logger.info('Seems like a crash has happened since {}'.format(reason))
4740
current_time = time.time()
4841
if count_start_time is not None and current_time - count_start_time <= counting_time:
@@ -57,23 +50,7 @@ def on_server_stop(server, return_code):
5750
server.start()
5851

5952

60-
def on_load(server, old):
61-
global counter, count_start_time, is_crash
62-
if old is not None:
63-
counter = old.counter
64-
count_start_time = old.count_start_time
65-
is_crash = old.is_crash
66-
53+
def on_load(server: PluginServerInterface, old):
6754
global config
68-
try:
69-
with open(CONFIG_FILE) as file:
70-
js = json.load(file)
71-
for key in config.keys():
72-
config[key] = js[key]
73-
except:
74-
server.logger.info('fail to read config file, using default value')
75-
config = default_config
76-
with open(CONFIG_FILE, 'w') as file:
77-
json.dump(config, file, indent=4)
78-
79-
server.logger.info('Config info: {}'.format(config))
55+
config = server.load_config_simple(CONFIG_FILE, in_data_folder=False, target_class=Config)
56+
server.logger.info('Config info: {}'.format(config.serialize()))

mcdreforged.plugin.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"id": "crash_restart",
3+
"version": "1.0.0",
4+
"name": "Crash Restart",
5+
"description": {
6+
"en_us": "Automatically restart the server after the server crashed",
7+
"zh_cn": "在服务端崩溃后自动重启服务器的插件"
8+
},
9+
"author": "Fallen_Breath",
10+
"link": "https://github.com/MCDReforged/CrashRestart",
11+
"resources": ["LICENSE"]
12+
}

0 commit comments

Comments
 (0)