-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathitem.py
More file actions
110 lines (95 loc) · 3.67 KB
/
item.py
File metadata and controls
110 lines (95 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from dataclasses import dataclass
import typing as t
from .. import Command, ParsedCommand, Parser
from ...exceptions import ConstructionException
from ...nodes import EntityNode, IntegerNode, ItemNode, PositionNode, RawNode
from ...parser_types import Any, Entity, Integer, Item, Literal, Position
from ...util import ensure_nodes
@dataclass()
class ParsedItemCommand(ParsedCommand):
command: str
target_type: RawNode
target: t.Union[PositionNode, EntityNode]
slot: RawNode
action: RawNode
source_type: RawNode = None
source: t.Union[PositionNode, EntityNode] = None
source_slot: RawNode = None
modifier: RawNode = None
item: ItemNode = None
count: IntegerNode = None
def __str__(self):
base = (f'{self.command} {self.target_type} {self.target} {self.slot} '
f'{self.action}')
if self.action.value == 'copy':
ensure_nodes(self, 'source_type', 'source', 'source_slot')
command = f'{base} {self.source_type} {self.source} ' \
f'{self.source_slot}'
if self.modifier is not None:
return f'{command} {self.modifier}'
return command
elif self.action.value == 'modify':
ensure_nodes(self, 'modifier')
return f'{base} {self.modifier}'
elif self.action.value == 'replace':
ensure_nodes(self, 'item')
if self.count is not None:
return f'{base} {self.item} {self.count}'
return f'{base} {self.item}'
else:
raise ConstructionException(
f'expected action to be \'copy\', \'modfiy\' or \'replace\', '
f'not {self.action.value!r}'
)
item = Command('item', parsed=ParsedItemCommand)
TYPES = (('block', Position()), ('entity', Entity()))
for target_type, target in TYPES:
# item <TARGET> <slot> copy <SOURCE> <slot> [<modifier>]
for source_type, source in TYPES:
# - item <TARGET> <slot> copy <SOURCE> <slot> <modifier>
item.add_variation(
Parser(Literal(target_type), 'target_type'),
Parser(target, 'target'),
Parser(Any(), 'slot'),
Parser(Literal('copy'), 'action'),
Parser(Literal(source_type), 'source_type'),
Parser(source, 'source'),
Parser(Any(), 'source_slot'),
Parser(Any(), 'modifier')
)
# - item <TARGET> <slot> copy <SOURCE> <slot>
item.add_variation(
Parser(Literal(target_type), 'target_type'),
Parser(target, 'target'),
Parser(Any(), 'slot'),
Parser(Literal('copy'), 'action'),
Parser(Literal(source_type), 'source_type'),
Parser(source, 'source'),
Parser(Any(), 'source_slot')
)
# item <TARGET> <slot> modify <modifier>
item.add_variation(
Parser(Literal(target_type), 'target_type'),
Parser(target, 'target'),
Parser(Any(), 'slot'),
Parser(Literal('modify'), 'action'),
Parser(Any(), 'modifier')
)
# item <TARGET> <slot> replace <item> [<count>]
# - item <TARGET> <slot> replace <item> <count>
item.add_variation(
Parser(Literal(target_type), 'target_type'),
Parser(target, 'target'),
Parser(Any(), 'slot'),
Parser(Literal('replace'), 'action'),
Parser(Item(), 'item'),
Parser(Integer(), 'count')
)
# - item <TARGET> <slot> replace <item>
item.add_variation(
Parser(Literal(target_type), 'target_type'),
Parser(target, 'target'),
Parser(Any(), 'slot'),
Parser(Literal('replace'), 'action'),
Parser(Item(), 'item')
)