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.