-
-
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(more_command_nodes): 🎉 release MoreCommandNodes
Release-As: 1.0.0
- Loading branch information
1 parent
5afe188
commit 4aefc2b
Showing
5 changed files
with
143 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,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" | ||
} |
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,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)) |
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,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 的值而不是名称作为节点文本。 |
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