From 4aefc2b728482040a90200cd9e9c5612ed9726a5 Mon Sep 17 00:00:00 2001 From: Andy Zhang <37402126+AnzhiZhang@users.noreply.github.com> Date: Fri, 8 Jul 2022 15:25:47 +0800 Subject: [PATCH] =?UTF-8?q?feat(more=5Fcommand=5Fnodes):=20=F0=9F=8E=89=20?= =?UTF-8?q?release=20MoreCommandNodes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Release-As: 1.0.0 --- .vscode/settings.json | 1 + more_command_nodes/mcdreforged.plugin.json | 11 +++ .../more_command_nodes/__init__.py | 91 +++++++++++++++++++ more_command_nodes/readme.md | 39 ++++++++ plugin_list.json | 1 + 5 files changed, 143 insertions(+) create mode 100644 more_command_nodes/mcdreforged.plugin.json create mode 100644 more_command_nodes/more_command_nodes/__init__.py create mode 100644 more_command_nodes/readme.md diff --git a/.vscode/settings.json b/.vscode/settings.json index f89e201..0807337 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,6 +6,7 @@ "database_api", "gamemode", "info", + "more_command_nodes", "qq_chat", "start_stop_helper_r", "uuid_api", diff --git a/more_command_nodes/mcdreforged.plugin.json b/more_command_nodes/mcdreforged.plugin.json new file mode 100644 index 0000000..bc2de24 --- /dev/null +++ b/more_command_nodes/mcdreforged.plugin.json @@ -0,0 +1,11 @@ +{ + "id": "more_command_nodes", + "version": "1.0.0", + "name": "MoreCommandNodes", + "description": { + "en_us": "More command nodes", + "zh_cn": "更多的指令节点" + }, + "author": "Andy Zhang", + "link": "https://github.com/AnzhiZhang/MCDReforgedPlugins/tree/master/more_command_nodes" +} diff --git a/more_command_nodes/more_command_nodes/__init__.py b/more_command_nodes/more_command_nodes/__init__.py new file mode 100644 index 0000000..af08352 --- /dev/null +++ b/more_command_nodes/more_command_nodes/__init__.py @@ -0,0 +1,91 @@ +from enum import Enum +from typing import Type, Union, Iterable + +from mcdreforged.api.command import * +from mcdreforged.command.builder.command_builder_util import get_element + +__all__ = [ + 'FloatsArgument', + 'Position', + 'Facing', + 'EnumeratedText' +] + + +class IllegalFloat(CommandSyntaxError): + def __init__(self, char_read: int): + super().__init__('Invalid Point', char_read) + + +class IncompleteFloat(CommandSyntaxError): + def __init__(self, char_read: int): + super().__init__('Incomplete Point', char_read) + + +class FloatsArgument(ArgumentNode): + """ + An argument with custom continuous floats. + """ + + def __init__(self, name: str, number: int): + super().__init__(name) + self.__number = number + + def parse(self, text: str) -> ParseResult: + total_read = 0 + coords = [] + for i in range(3): + value, read = command_builder_util.get_float(text[total_read:]) + if read == 0: + raise IllegalFloat(total_read) + total_read += read + if value is None: + raise IllegalFloat(total_read) + coords.append(value) + return ParseResult(coords, total_read) + + +class Position(FloatsArgument): + """ + A position argument, it has three continues floats. + """ + + def __init__(self, name: str): + super().__init__(name, 3) + + +class Facing(FloatsArgument): + """ + A facing argument, it has two continues floats. + """ + + def __init__(self, name: str): + super().__init__(name, 2) + + +class InvalidEnumeratedText(IllegalArgument): + def __init__(self, char_read: Union[int, str]): + super().__init__('Invalid enumerated text', char_read) + + +class EnumeratedText(ArgumentNode): + """ + Same as MCDR's Enumeration, + but it uses Enum's value as argument text instead of key. + """ + + def __init__(self, name: str, enum_class: Type[Enum]): + super().__init__(name) + self.__enum_class: Type[Enum] = enum_class + + def _get_suggestions(self, context: CommandContext) -> Iterable[str]: + return map(lambda e: e.value, self.__enum_class) + + def parse(self, text: str) -> ParseResult: + arg = get_element(text) + try: + enum = self.__enum_class(arg) + except ValueError: + raise InvalidEnumeratedText(arg) from None + else: + return ParseResult(enum, len(arg)) diff --git a/more_command_nodes/readme.md b/more_command_nodes/readme.md new file mode 100644 index 0000000..b57558b --- /dev/null +++ b/more_command_nodes/readme.md @@ -0,0 +1,39 @@ +# MoreCommandNodes + +> 更多指令节点 + +如果您想要添加更多自定义节点,欢迎提交 PR! + +## 节点列表 + +```mermaid +classDiagram + class FloatsArgument + class Position + class Facing + class EnumeratedText + + FloatsArgument : +__init__(String name, int number) + FloatsArgument <|-- Position + Position : +__init__(String name) + FloatsArgument <|-- Facing + Facing : +__init__(String name) + + EnumeratedText : +__init__(String name, Type[Enum] enum_class) +``` + +### FloatsArgument + +连续的多个浮点数节点。 + +### Position + +坐标节点,连续的三个浮点数。 + +### Facing + +朝向节点,连续的两个浮点数。 + +### EnumeratedText + +与 MCDR 的 Enumeration 类似,但是使用 Enum 的值而不是名称作为节点文本。 diff --git a/plugin_list.json b/plugin_list.json index 5b0d613..229fd5e 100644 --- a/plugin_list.json +++ b/plugin_list.json @@ -5,6 +5,7 @@ "database_api", "gamemode", "info", + "more_command_nodes", "qq_chat", "start_stop_helper_r", "uuid_api",