From 53626446ddeb6ae7c0ec567c97556647be27ebf7 Mon Sep 17 00:00:00 2001 From: Alex3236 <45303195+alex3236@users.noreply.github.com> Date: Sun, 26 Feb 2023 17:46:09 +0800 Subject: [PATCH 1/2] refactor --- .gitignore | 4 ++- lang/en_us.yml | 8 +++-- lang/zh_cn.yml | 8 +++-- seed/__init__.py | 85 ++++++++++++++++++++++++++++++------------------ 4 files changed, 67 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index 723ef36..ed81a6c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -.idea \ No newline at end of file +.idea/ +__pycache__/ +.vscode/ \ No newline at end of file diff --git a/lang/en_us.yml b/lang/en_us.yml index 8528299..76411e7 100644 --- a/lang/en_us.yml +++ b/lang/en_us.yml @@ -1,4 +1,6 @@ seed: - help_msg: Get world seed quickly! - get_seed: 'Seed:' - copy_to_clipboard: Click to copy to clipboard \ No newline at end of file + help_msg: Get world seed + get_seed: 'Seed: ' + copy_to_clipboard: Click to copy to clipboard + failed: Failed to get seed + \ No newline at end of file diff --git a/lang/zh_cn.yml b/lang/zh_cn.yml index 496bd3e..df83f79 100644 --- a/lang/zh_cn.yml +++ b/lang/zh_cn.yml @@ -1,4 +1,6 @@ seed: - help_msg: 更快速的获取服务器种子! - get_seed: 服务器种子: - copy_to_clipboard: 点击复制到剪贴板 \ No newline at end of file + help_msg: 获取世界种子 + get_seed: 种子: + copy_to_clipboard: 点击复制到剪贴板 + failed: 无法获取种子 + \ No newline at end of file diff --git a/seed/__init__.py b/seed/__init__.py index 1d628cd..8f6ec4b 100644 --- a/seed/__init__.py +++ b/seed/__init__.py @@ -1,44 +1,67 @@ +import time +from typing import Optional + from mcdreforged.api.all import * +from parse import parse -server_inst: ServerInterface +getting_seed = False +seed: Optional[str] = None +config: dict = None -get_seed = False -seed: str = '0' +def on_load(server: PluginServerInterface, old_module): + global config + config = server.load_config_simple(default_config={ + 'command': 'seed', + 'parser': 'Seed: [{}]' + }) + if server.is_server_running(): + get_seed(server) + server.register_command(Literal('!!seed').runs(print_seed)) + server.register_help_message('!!seed', server.tr('seed.help_msg')) -def on_load(server: ServerInterface, old_module): - global server_inst - server_inst = server - server.register_command(Literal('!!seed').runs(run)) - server.register_help_message('!!seed', server.tr('seed.help_msg')) +def on_server_startup(server: PluginServerInterface): + if seed is None: + get_seed(server) -def run(): - global get_seed, seed - if seed == '0': - get_seed = True - server_inst.execute('seed') +@new_thread('seed') +def get_seed(server: PluginServerInterface): + global seed, getting_seed + if getting_seed: + return else: - print_seed(seed) + getting_seed = True + if seed is None: + server.execute(config['command']) + for _ in range(50): + if seed is None: + time.sleep(0.1) + else: + getting_seed = False + return + server.logger.error(server.tr('seed.failed')) + getting_seed = False -# reference code https://github.com/MCDReforged/Seed def on_info(server: ServerInterface, info: Info): - global get_seed, seed - if info.content.startswith('Seed: [') and get_seed: - seed = info.content.split('[')[1].split(']')[0] - print_seed(seed) - get_seed = False - - -def print_seed(value: str): - server_inst.tell('@a', - RTextList( - RText(server_inst.tr('seed.get_seed'), color=RColor.yellow), - RText('[', color=RColor.white), - RText(value, color=RColor.green, styles=RStyle.underlined).set_hover_text( - server_inst.tr('seed.copy_to_clipboard')).set_click_event(RAction.copy_to_clipboard, value), - RText(']', color=RColor.white)) - ) + global getting_seed, seed + if getting_seed: + result = parse(config['parser'], info.content) + if result: + seed = result[0] + + +def print_seed(source: CommandSource): + if seed is None: + source.reply(RText(RTextMCDRTranslation('seed.failed'), RColor.red)) + source.reply(RTextList( + RTextMCDRTranslation('seed.get_seed', RColor.yellow), + RText('[', RColor.white), + RText(seed, RColor.green, RStyle.underlined). + h(RTextMCDRTranslation('seed.copy_to_clipboard')). + c(RAction.copy_to_clipboard, seed), + RText(']', RColor.white)) + ) From e8d37b660096bcc1c4f4895ca6fb0cd9906255e8 Mon Sep 17 00:00:00 2001 From: Alex3236 <45303195+alex3236@users.noreply.github.com> Date: Sun, 26 Feb 2023 17:56:19 +0800 Subject: [PATCH 2/2] Update README & description --- README.md | 15 +++++++++++++-- mcdreforged.plugin.json | 4 ++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bd84a3f..5371fc7 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,19 @@ # Seed -!!seed can get world seed +!!seed to get world seed. + +## Preview ![image](https://user-images.githubusercontent.com/106148777/221347169-1764fb31-db0c-40e5-82cf-77c1915a638c.png) +## Configure + +`config/seed/config.json` +- `command`: Command to get seed in console +- `parser`: Parser for the command result + +The default configuration already supports most server software (without plugin/mods). + +## More -(yes, a very simple plugin +This plugn was inspired by [`MCDReforged/Seed`](https://github.com/MCDReforged/Seed) diff --git a/mcdreforged.plugin.json b/mcdreforged.plugin.json index 70cc510..35258a9 100644 --- a/mcdreforged.plugin.json +++ b/mcdreforged.plugin.json @@ -1,8 +1,8 @@ { "id": "seed", - "version": "1.0.1", + "version": "1.1.0", "name": "Seed", - "description": "Get world seed quickly!", + "description": "Get world seed without op permission.", "author": [ "OptiJava" ],