From d4be2522550222329625646e2ac2b547ed975705 Mon Sep 17 00:00:00 2001 From: Andy Zhang <37402126+AnzhiZhang@users.noreply.github.com> Date: Mon, 22 Jul 2024 12:41:42 +0100 Subject: [PATCH] =?UTF-8?q?feat(command=5Faliases):=20=E2=9C=A8=20add=20ne?= =?UTF-8?q?w=20plugin=20command=5Faliases?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .release-please/release-please-config.json | 3 ++ .../command_aliases/__init__.py | 31 +++++++++++++++++++ src/command_aliases/mcdreforged.plugin.json | 11 +++++++ src/command_aliases/readme.md | 20 ++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 src/command_aliases/command_aliases/__init__.py create mode 100644 src/command_aliases/mcdreforged.plugin.json create mode 100644 src/command_aliases/readme.md diff --git a/.release-please/release-please-config.json b/.release-please/release-please-config.json index 2de711e..dd1e45d 100644 --- a/.release-please/release-please-config.json +++ b/.release-please/release-please-config.json @@ -19,6 +19,9 @@ "src/bot": { "package-name": "bot" }, + "src/command_aliases": { + "package-name": "command_aliases" + }, "src/database_api": { "package-name": "database_api" }, diff --git a/src/command_aliases/command_aliases/__init__.py b/src/command_aliases/command_aliases/__init__.py new file mode 100644 index 0000000..759ff64 --- /dev/null +++ b/src/command_aliases/command_aliases/__init__.py @@ -0,0 +1,31 @@ +from mcdreforged.api.command import * +from mcdreforged.api.types import PluginServerInterface +from mcdreforged.api.utils.serializer import Serializable + + +class Config(Serializable): + alias: dict[str, str] = {} + + +def on_load(server: PluginServerInterface, prev_module): + config = server.load_config_simple(target_class=Config) + + for alias, command in config.alias.items(): + server.register_command( + Literal(alias) + .runs(get_handler(command)) + .then( + GreedyText('content') + .runs(get_handler(command)) + ) + ) + + +def get_handler(command): + def handler(src, ctx): + src.get_server().execute_command( + f'{command} {ctx.get("content", "")}', + src + ) + + return handler diff --git a/src/command_aliases/mcdreforged.plugin.json b/src/command_aliases/mcdreforged.plugin.json new file mode 100644 index 0000000..c27268f --- /dev/null +++ b/src/command_aliases/mcdreforged.plugin.json @@ -0,0 +1,11 @@ +{ + "id": "command_aliases", + "version": "1.0.0", + "name": "Command Aliases", + "description": { + "en_us": "Aliases commands", + "zh_cn": "为命令添加别名" + }, + "author": "Andy Zhang", + "link": "https://github.com/AnzhiZhang/MCDReforgedPlugins/tree/master/src/command_aliases" +} diff --git a/src/command_aliases/readme.md b/src/command_aliases/readme.md new file mode 100644 index 0000000..e84f7d5 --- /dev/null +++ b/src/command_aliases/readme.md @@ -0,0 +1,20 @@ +# Command Aliases + +> Alias commands by config + +## Usage + +You need to add all commands you would like to alias in the config file, where keys are the alias and values are the original commands. + +```json +{ + "alias": { + "!!mcdr": "!!MCDR" + } +} +``` + +Now you can use `!!mcdr` as an alias for `!!MCDR`. + +> [!NOTE] +> There will be no completion for alias commands. If you want to use completion, you need to use original commands. It does not make sense if you need to use alias in an environment where you have completion.