1515# assigned in on_load
1616from stats_helper .quick_scoreboard import Scoreboard
1717
18+
19+ class Config (Serializable ):
20+ save_world_on_query : bool = False
21+ save_world_on_rank : bool = False
22+ save_world_on_scoreboard : bool = True
23+
24+
25+ config : Config
1826PLUGIN_ID = None # type: Optional[str]
19- HelpMessage = None # type: Optional[str ]
27+ HelpMessage = None # type: Optional[RTextBase ]
2028
2129uuid_list : Dict [str , str ] = {} # player -> uuid
2230UUID_LIST_ITEM = Tuple [str , str ]
2331flag_save_all = False
2432flag_unload = False
2533
26- stored = quick_scoreboard .stored
34+ quick_scoreboards = quick_scoreboard .quick_scoreboards
2735
2836
2937def refresh_uuid_list (server : ServerInterface ):
@@ -76,8 +84,8 @@ def save_uuid_list():
7684 json .dump (uuid_list , file , indent = 4 )
7785
7886
79- def tr (translation_key : str , * args , ** kwargs ):
80- return ServerInterface .get_instance ().tr ('{}.{}' .format (PLUGIN_ID , translation_key ), * args , ** kwargs )
87+ def tr (translation_key : str , * args , ** kwargs ) -> RTextMCDRTranslation :
88+ return ServerInterface .get_instance ().rtr ('{}.{}' .format (PLUGIN_ID , translation_key ), * args , ** kwargs )
8189
8290
8391def print_message (source : CommandSource , msg : Union [str , RTextBase ], is_tell : bool = True ):
@@ -111,15 +119,16 @@ def trigger_save_all(server: ServerInterface):
111119def show_help (source : CommandSource ):
112120 help_msg_rtext = RTextList ()
113121 symbol = 0
114- for line in HelpMessage .splitlines (True ):
115- result = re .search (r'(?<=§7)' + constants .Prefix + r'[\S ]*?(?=§)' , line )
116- if result is not None and symbol != 2 :
117- help_msg_rtext .append (RText (line ).c (RAction .suggest_command , result .group ()).h (tr ('click_to_fill' , result .group ())))
118- symbol = 1
119- else :
120- help_msg_rtext .append (line )
121- if symbol == 1 :
122- symbol += 1
122+ with source .preferred_language_context ():
123+ for line in HelpMessage .to_plain_text ().splitlines (True ):
124+ result = re .search (r'(?<=§7)' + constants .Prefix + r'[\S ]*?(?=§)' , line )
125+ if result is not None and symbol != 2 :
126+ help_msg_rtext .append (RText (line ).c (RAction .suggest_command , result .group ()).h (tr ('click_to_fill' , result .group ())))
127+ symbol = 1
128+ else :
129+ help_msg_rtext .append (line )
130+ if symbol == 1 :
131+ symbol += 1
123132 source .reply (help_msg_rtext )
124133
125134
@@ -132,11 +141,15 @@ def show_stat(source: CommandSource, name: str, cls: str, target: str, is_uuid:
132141 uuid = name if is_uuid else uuid_list .get (name , None )
133142 if uuid is None :
134143 print_message (source , tr ('player_uuid_not_found' , name ), is_tell = is_tell )
144+ if config .save_world_on_query :
145+ trigger_save_all (source .get_server ())
135146 msg = tr ('player_stat_display' , name , get_display_text (cls , target ), utils .get_stat_data (uuid , cls , target ))
136147 print_message (source , msg , is_tell = is_tell )
137148
138149
139150def show_rank (source : CommandSource , cls : str , target : str , list_bot : bool , is_tell : bool , is_all : bool , is_called : bool = False ):
151+ if config .save_world_on_rank and not is_called :
152+ trigger_save_all (source .get_server ())
140153 player_list = get_player_list (list_bot )
141154 arr = []
142155 sum = 0
@@ -186,7 +199,8 @@ def hide_scoreboard(server: ServerInterface):
186199def build_scoreboard (source : CommandSource , cls : str , target : str , title : Optional [str ] = None , list_bot : bool = False ):
187200 server = source .get_server ()
188201 player_list = get_player_list (list_bot )
189- trigger_save_all (server )
202+ if config .save_world_on_scoreboard :
203+ trigger_save_all (server )
190204 server .execute ('scoreboard objectives remove {}' .format (constants .ScoreboardName ))
191205 if title is None :
192206 title = get_display_text (cls , target )
@@ -202,23 +216,23 @@ def build_scoreboard(source: CommandSource, cls: str, target: str, title: Option
202216
203217def save_scoreboard (source : CommandSource , alias : str , cls : str , target : str , title : Optional [str ] = None ):
204218 to_save = quick_scoreboard .Scoreboard (alias , cls , target , title )
205- is_succeeded = stored .append (to_save )
219+ is_succeeded = quick_scoreboards .append (to_save )
206220 if is_succeeded :
207221 source .reply (tr ('save_scoreboard.done' , alias ))
208222 else :
209223 source .reply (RText (tr ('save_scoreboard.duplicated' , alias )).c (RAction .run_command , f'{ constants .Prefix } list' ).h (tr ('list_scoreboard.promote' )))
210224
211225
212226def rm_scoreboard (source : CommandSource , alias : str ):
213- is_succeeded = stored .remove (alias )
227+ is_succeeded = quick_scoreboards .remove (alias )
214228 if is_succeeded :
215229 source .reply (tr ('rm_scoreboard.done' , alias ))
216230 else :
217231 source .reply (RText (tr ('rm_scoreboard.not_found' , alias )).c (RAction .run_command , f'{ constants .Prefix } list' ).h (tr ('list_scoreboard.promote' )))
218232
219233
220234def list_quick_scoreboard (source : CommandSource , is_tell : bool ):
221- saved_list = stored .list_scoreboard ()
235+ saved_list = quick_scoreboards .list_scoreboard ()
222236 print_text = RTextList ()
223237 print_text .append (tr ('list_scoreboard.summary' ) + RText ('[+]' , color = RColor .green ).c (RAction .suggest_command , f'{ constants .Prefix } save ' ).h ('list_scoreboard.add' ) + '\n ' )
224238 num = 0
@@ -256,7 +270,7 @@ def add_player_to_uuid_list(source: CommandSource, player: str):
256270
257271
258272def register_command (server : PluginServerInterface ):
259- def exe (node : ArgumentNode , callback : Callable [[CommandContext , Arguments ], Any ]) -> ArgumentNode :
273+ def exe (node : AbstractNode , callback : Callable [[CommandContext , Arguments ], Any ]) -> AbstractNode :
260274 return node .runs (lambda src , ctx : callback (ctx , Arguments .empty ())).\
261275 then (ArgumentEnding ('args' ).runs (lambda src , ctx : callback (ctx , ctx ['args' ])))
262276
@@ -372,11 +386,12 @@ def on_player_joined(server: PluginServerInterface, player: str, info: Info):
372386
373387
374388def on_load (server : PluginServerInterface , old_module ):
375- global PLUGIN_ID , HelpMessage
389+ global PLUGIN_ID , HelpMessage , config
376390 PLUGIN_ID = server .get_self_metadata ().id
377- server .register_help_message (constants .Prefix , tr ( 'summary_help' ) )
391+ config = server .load_config_simple (constants .ConfigFile , in_data_folder = False , target_class = Config )
378392 HelpMessage = tr ('help_message' , name = server .get_self_metadata ().name , version = server .get_self_metadata ().version , prefix = constants .Prefix )
379- stored .load (server .logger )
393+ quick_scoreboards .load (server .logger )
380394 refresh_uuid_list (server )
381395 server .logger .info ('UUID list size: {}' .format (len (uuid_list )))
396+ server .register_help_message (constants .Prefix , tr ('summary_help' ))
382397 register_command (server )
0 commit comments