Skip to content

Commit

Permalink
feat(more_command_nodes): 🎉 release MoreCommandNodes
Browse files Browse the repository at this point in the history
Release-As: 1.0.0
  • Loading branch information
AnzhiZhang committed Jul 8, 2022
1 parent 5afe188 commit 4aefc2b
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"database_api",
"gamemode",
"info",
"more_command_nodes",
"qq_chat",
"start_stop_helper_r",
"uuid_api",
Expand Down
11 changes: 11 additions & 0 deletions more_command_nodes/mcdreforged.plugin.json
Original file line number Diff line number Diff line change
@@ -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"
}
91 changes: 91 additions & 0 deletions more_command_nodes/more_command_nodes/__init__.py
Original file line number Diff line number Diff line change
@@ -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))
39 changes: 39 additions & 0 deletions more_command_nodes/readme.md
Original file line number Diff line number Diff line change
@@ -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 的值而不是名称作为节点文本。
1 change: 1 addition & 0 deletions plugin_list.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"database_api",
"gamemode",
"info",
"more_command_nodes",
"qq_chat",
"start_stop_helper_r",
"uuid_api",
Expand Down

0 comments on commit 4aefc2b

Please sign in to comment.