Skip to content

Commit be67780

Browse files
committed
v7.2.0
MCDR 2.1+ translation logic config file: select if save-all command is used when executing stuffs
1 parent 86212d8 commit be67780

File tree

5 files changed

+41
-25
lines changed

5 files changed

+41
-25
lines changed

mcdreforged.plugin.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "stats_helper",
3-
"version": "7.1.3",
3+
"version": "7.2.0",
44
"name": "Stats Helper",
55
"description": {
66
"en_us": "A Minecraft statistic helper",
@@ -11,7 +11,7 @@
1111
],
1212
"link": "https://github.com/TISUnion/StatsHelper",
1313
"dependencies": {
14-
"mcdreforged": ">=2.0.0-beta.7"
14+
"mcdreforged": ">=2.1.0-beta.1"
1515
},
1616
"resources": [
1717
"lang",

stats_helper/__init__.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,23 @@
1515
# assigned in on_load
1616
from 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
1826
PLUGIN_ID = None # type: Optional[str]
19-
HelpMessage = None # type: Optional[str]
27+
HelpMessage = None # type: Optional[RTextBase]
2028

2129
uuid_list: Dict[str, str] = {} # player -> uuid
2230
UUID_LIST_ITEM = Tuple[str, str]
2331
flag_save_all = False
2432
flag_unload = False
2533

26-
stored = quick_scoreboard.stored
34+
quick_scoreboards = quick_scoreboard.quick_scoreboards
2735

2836

2937
def 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

8391
def print_message(source: CommandSource, msg: Union[str, RTextBase], is_tell: bool = True):
@@ -111,15 +119,16 @@ def trigger_save_all(server: ServerInterface):
111119
def 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

139150
def 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):
186199
def 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

203217
def 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

212226
def 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

220234
def 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

258272
def 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

374388
def 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)

stats_helper/cmd_node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from stats_helper import quick_scoreboard
44

5-
stored = quick_scoreboard.stored
5+
stored = quick_scoreboard.quick_scoreboards
66

77

88
class Arguments:

stats_helper/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
UUIDFile = os.path.join('config', PluginName, 'uuid.json')
99
UUIDFilePrev = os.path.join('plugins', PluginName, 'uuid.json')
1010
QuickScoreboardFile = os.path.join('config', PluginName, 'quick_scoreboard.json')
11+
ConfigFile = os.path.join('config', PluginName, 'config.json')
1112
RankAmount = 15
1213
rankColor = ['§b', '§d', '§e', '§f']

stats_helper/quick_scoreboard.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@ def list_scoreboard(self) -> List[Scoreboard]:
8484
return list(self.saved.values())
8585

8686

87-
stored = QuickScoreboards(constants.QuickScoreboardFile)
87+
quick_scoreboards = QuickScoreboards(constants.QuickScoreboardFile)
8888

0 commit comments

Comments
 (0)