1- import json
21import os
32import 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
2213counter = None
2314count_start_time = None
2415is_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
2720def 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 ()))
0 commit comments