-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(command_aliases): ✨ add new plugin command_aliases
- Loading branch information
1 parent
5c19321
commit d4be252
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |