-
-
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(dict_command_registration): 🎉 new plugin `dict_command_registrat…
…ion`
- Loading branch information
1 parent
1f8226a
commit 9f7abd2
Showing
7 changed files
with
477 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
dict_command_registration/dict_command_registration/__init__.py
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,55 @@ | ||
from typing import Any, Dict | ||
|
||
from mcdreforged.api.types import PluginServerInterface | ||
|
||
from .exceptions import MissingRequiredAttribute | ||
from .node_type import NodeType | ||
from .node import Node | ||
|
||
__all__ = [ | ||
"MissingRequiredAttribute", | ||
"NodeType", | ||
"Node", | ||
"register", | ||
] | ||
|
||
|
||
def register( | ||
server: PluginServerInterface, | ||
command: Dict[str, Any], | ||
help_message: str = None, | ||
help_message_permission: int = 0 | ||
) -> None: | ||
""" | ||
Register a command. | ||
See also: https://github.com/AnzhiZhang/MCDReforgedPlugins/blob/master/dict_command_registration/readme.md#register | ||
:param PluginServerInterface server: the PluginServerInterface instance of | ||
your plugin, to ensure that this command is registered by your plugin. | ||
:param dict command: Command, please find more information in the document. | ||
:param str help_message: Provide a string value if you want register | ||
help message for this command. | ||
:param int help_message_permission: The minimum permission level to see | ||
this help message. See also in MCDReforged document. | ||
:return: None. | ||
""" | ||
# parse dict | ||
root_node = Node(command) | ||
root_node.to_mcdr_node().print_tree() | ||
|
||
# register command | ||
server.register_command(root_node.to_mcdr_node()) | ||
|
||
# register help message | ||
if help_message is not None: | ||
# get literal | ||
if isinstance(root_node.literal, str): | ||
literal = root_node.literal | ||
else: | ||
literal = root_node.literal[0] | ||
|
||
# register | ||
server.register_help_message( | ||
literal, | ||
help_message, | ||
help_message_permission | ||
) |
8 changes: 8 additions & 0 deletions
8
dict_command_registration/dict_command_registration/exceptions.py
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,8 @@ | ||
__all__ = [ | ||
"MissingRequiredAttribute", | ||
] | ||
|
||
|
||
class MissingRequiredAttribute(Exception): | ||
def __init__(self, name: str): | ||
super().__init__(f"Missing required attribute \"{name}\"") |
134 changes: 134 additions & 0 deletions
134
dict_command_registration/dict_command_registration/node.py
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,134 @@ | ||
from enum import Enum | ||
from typing import Any, Callable, Dict, Iterable, List, Type, Union | ||
|
||
from mcdreforged.api.command import * | ||
|
||
from .exceptions import MissingRequiredAttribute | ||
from .node_type import NodeType | ||
|
||
|
||
class Node: | ||
def __init__(self, data: Dict[str, Any]): | ||
self.__data = data | ||
|
||
# name | ||
self.__name: str = data.get("name") | ||
if self.__name is None: | ||
raise MissingRequiredAttribute("name") | ||
|
||
# node | ||
self.__node: Union[Literal, ArgumentNode] = data.get("node") | ||
|
||
# literal | ||
self.__literal: Union[str, Iterable[str]] = data.get( | ||
"literal", | ||
self.__name | ||
) | ||
|
||
# type | ||
self.__type: Union[NodeType, Type[ArgumentNode]] = data.get( | ||
"type", | ||
NodeType.LITERAL | ||
) | ||
|
||
# enumeration | ||
self.__enumeration: Dict[str, Any] = data.get("enumeration", {}) | ||
|
||
# args | ||
self.__args: List[Any] = data.get("args", []) | ||
|
||
# kwargs | ||
self.__kwargs: Dict[str, Any] = data.get("kwargs", {}) | ||
|
||
# runs | ||
self.__runs: Callable = data.get("runs") | ||
|
||
# requires | ||
self.__requires: Callable = data.get("requires") | ||
|
||
# redirects | ||
self.__redirects: AbstractNode = data.get("redirects") | ||
|
||
# suggests | ||
self.__suggests: Callable = data.get("suggests") | ||
|
||
# on_error | ||
self.__on_error: Dict[str, Any] = data.get("on_error") | ||
|
||
# on_child_error | ||
self.__on_child_error: Dict[str, Any] = data.get("on_child_error") | ||
|
||
# children | ||
self.__children: List[Node] = [] | ||
for i in data.get("children", []): | ||
self.__children.append(Node(i)) | ||
|
||
@property | ||
def literal(self) -> Union[str, Iterable[str]]: | ||
return self.__literal | ||
|
||
def to_mcdr_node(self) -> Union[Literal, ArgumentNode]: | ||
if self.__node is None: | ||
# instantiate node | ||
mcdr_node: Union[Literal, ArgumentNode] | ||
if type(self.__type) == NodeType: | ||
if self.__type == NodeType.LITERAL: | ||
mcdr_node = self.__type.value(self.__literal) | ||
elif self.__type == NodeType.ENUMERATION: | ||
mcdr_node = self.__type.value( | ||
self.__name, | ||
Enum(self.__name, self.__enumeration) | ||
) | ||
else: | ||
mcdr_node = self.__type.value(self.__name) | ||
else: | ||
mcdr_node = self.__type( | ||
self.__name, | ||
*self.__args, | ||
**self.__kwargs | ||
) | ||
|
||
# runs | ||
if self.__runs is not None: | ||
mcdr_node.runs(self.__runs) | ||
|
||
# requires | ||
if self.__requires is not None: | ||
mcdr_node.requires(self.__requires) | ||
|
||
# redirects | ||
if self.__redirects is not None: | ||
mcdr_node.redirects(self.__redirects) | ||
|
||
# suggests | ||
if self.__suggests is not None: | ||
mcdr_node.suggests(self.__suggests) | ||
|
||
# on_error | ||
if self.__on_error is not None: | ||
mcdr_node.on_error( | ||
self.__on_error.get("error_type", CommandError), | ||
self.__on_error.get("handler", lambda *args: None), | ||
handled=self.__on_error.get("handled", False), | ||
) | ||
|
||
# on_child_error | ||
if self.__on_child_error is not None: | ||
mcdr_node.on_child_error( | ||
self.__on_child_error.get("error_type", CommandError), | ||
self.__on_child_error.get("handler", lambda *args: None), | ||
handled=self.__on_child_error.get("handled", False), | ||
) | ||
|
||
# runs | ||
if self.__runs is not None: | ||
mcdr_node.runs(self.__runs) | ||
|
||
# add children | ||
for i in self.__children: | ||
mcdr_node.then(i.to_mcdr_node()) | ||
|
||
# return | ||
return mcdr_node | ||
else: | ||
return self.__node |
15 changes: 15 additions & 0 deletions
15
dict_command_registration/dict_command_registration/node_type.py
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,15 @@ | ||
from enum import Enum | ||
|
||
from mcdreforged.api.command import * | ||
|
||
|
||
class NodeType(Enum): | ||
LITERAL = Literal | ||
NUMBER = Number | ||
INTEGER = Integer | ||
FLOAT = Float | ||
TEXT = Text | ||
QUOTABLE_TEXT = QuotableText | ||
GREEDY_TEXT = GreedyText | ||
BOOLEAN = Boolean | ||
ENUMERATION = Enumeration |
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,14 @@ | ||
{ | ||
"id": "dict_command_registration", | ||
"version": "1.0.0", | ||
"name": "Dict Command Registration", | ||
"description": { | ||
"en_us": "Register your command by a python dict", | ||
"zh_cn": "用 Python å—典注册您的指令" | ||
}, | ||
"author": "Andy Zhang", | ||
"link": "https://github.com/AnzhiZhang/MCDReforgedPlugins/tree/master/dict_command_registration", | ||
"dependencies": { | ||
"mcdreforged": "^2.5.0" | ||
} | ||
} |
Oops, something went wrong.