11# -*- coding: utf-8 -*-
2- import json
32import os
43import shutil
54import time
6- from json import JSONDecodeError
7- from typing import List , Tuple , Callable , Any , Optional
5+ from typing import Dict , Iterable , List , Tuple , Optional , Union
86
97from mcdreforged .api .all import *
108
119PLUGIN_METADATA = ServerInterface .get_instance ().as_plugin_server_interface ().get_self_metadata ()
12- config = {
13- 'enabled' : True ,
14- 'source_world_directory' : './qb_multi/slot1/world' ,
15- 'destination_world_directory' : './server/world' ,
16- 'dimension_region_folder' : {
10+
11+
12+ class Config (Serializable ):
13+ enabled : bool = True ,
14+ source_world_directory : str = './qb_multi/slot1/world'
15+ destination_world_directory : str = './server/world'
16+ dimension_region_folder : Dict [str , Union [str , List [str ]]] = {
1717 '-1' : 'DIM-1/region' ,
1818 '0' : 'region' ,
1919 '1' : 'DIM1/region'
2020 }
21- }
22- DEFAULT_CONFIG = config .copy ()
23- CONFIG_FILE_PATH = os .path .join ('config' , '{}.json' .format (PLUGIN_METADATA .id ))
2421
22+
23+ config : Optional [Config ] = None
2524Prefix = '!!region'
2625PluginName = PLUGIN_METADATA .name
2726LogFilePath = os .path .join ('logs' , '{}.log' .format (PluginName ))
@@ -58,8 +57,17 @@ def __init__(self, x: int, z: int, dim: int):
5857 def to_file_name (self ):
5958 return 'r.{}.{}.mca' .format (self .x , self .z )
6059
61- def to_file_path (self ):
62- return os .path .join (config ['dimension_region_folder' ][str (self .dim )], self .to_file_name ())
60+ def to_file_list (self ):
61+ file_list = []
62+ folders = config .dimension_region_folder [str (self .dim )]
63+ if isinstance (folders , str ):
64+ file_list .append (os .path .join (folders , self .to_file_name ()))
65+ elif isinstance (folders , Iterable ):
66+ for folder in folders :
67+ file_list .append (os .path .join (folder , self .to_file_name ()))
68+ else :
69+ pass
70+ return file_list
6371
6472 def __eq__ (self , other ):
6573 if not isinstance (other , type (self )):
@@ -148,19 +156,20 @@ def region_update(source: CommandSource):
148156 print_log (source .get_server (), '{} 更新了 {} 个区域文件:' .format (source , len (regionList )))
149157 historyList .clear ()
150158 for region in regionList :
151- source_dir = os .path .join (config ['source_world_directory' ], region .to_file_path ())
152- destination = os .path .join (config ['destination_world_directory' ], region .to_file_path ())
153- try :
154- source .get_server ().logger .info ('- "{}" -> "{}"' .format (source_dir , destination ))
155- shutil .copyfile (source_dir , destination )
156- except Exception as e :
157- msg = '失败,错误信息:{}' .format (str (e ))
158- flag = False
159- else :
160- msg = '成功'
161- flag = True
162- historyList .append ((region , flag ))
163- print_log (source .get_server (), ' {}: {}' .format (region , msg ))
159+ for region_file in region .to_file_list ():
160+ source_dir = os .path .join (config .source_world_directory , region_file )
161+ destination = os .path .join (config .destination_world_directory , region_file )
162+ try :
163+ source .get_server ().logger .info ('- "{}" -> "{}"' .format (source_dir , destination ))
164+ shutil .copyfile (source_dir , destination )
165+ except Exception as e :
166+ msg = '失败,错误信息:{}' .format (str (e ))
167+ flag = False
168+ else :
169+ msg = '成功'
170+ flag = True
171+ historyList .append ((region , flag ))
172+ print_log (source .get_server (), ' {}: {}' .format (region , msg ))
164173
165174 regionList .clear ()
166175 time .sleep (1 )
@@ -184,7 +193,8 @@ def on_load(server: PluginServerInterface, old):
184193
185194def load_config (source : Optional [CommandSource ]):
186195 global config , server_inst
187- config = server_inst .load_config_simple (CONFIG_FILE_PATH , DEFAULT_CONFIG , in_data_folder = False , source_to_reply = source , echo_in_console = False )
196+ config_file_path = os .path .join ('config' , '{}.json' .format (PLUGIN_METADATA .id ))
197+ config = server_inst .load_config_simple (config_file_path , in_data_folder = False , source_to_reply = source , echo_in_console = False , target_class = Config )
188198
189199
190200def reload_config (source : CommandSource ):
@@ -213,7 +223,7 @@ def get_region_parm_node(callback):
213223 then (Literal ('history' ).runs (show_history )).
214224 then (
215225 Literal ('update' ).
216- requires (lambda : config [ ' enabled' ] ).
226+ requires (lambda : config . enabled ).
217227 on_error (RequirementNotMet , lambda src : src .reply ('{}未启用!请在配置文件中开启' .format (PLUGIN_METADATA .name )), handled = True ).
218228 runs (region_update )
219229 ).
0 commit comments