-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.py
159 lines (125 loc) · 5.61 KB
/
classes.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import regex as re
COMMANDS = [
"advancement", "attribute", "bossbar",
"clone", "data", "datapack",
"defaultgamemode", "difficulty", "effect",
"enchant", "execute", "experience",
"fill", "function", "gamemode",
"gamerule", "give", "help",
"kick", "kill", "list",
"locate", "particle", "reload",
"say", "scoreboard", "seed",
"setblock", "setworldspawn", "spawnpoint",
"spreadplayers", "stopsound", "summon",
"tag", "team", "teammsg",
"teleport", "tell", "tellraw",
"time", "title", "tp",
"trigger", "w", "weather",
"worldborder", "xp"
]
class IncompleteMacroError(Exception):
pass
class IncorrectIncludeError(Exception):
pass
class RelativeCoordinate:
def __init__(self, x: int | float, y: int | float, z: int | float):
self.x = x
self.y = y
self.z = z
def __str__(self):
return f"~{self.x} ~{self.y} ~{self.z}".replace("~0", "~")
class CommandBlockMinecart:
def __init__(self, command: str, passenger = None):
if type(command) == list:
self.command = command[0]
if len(command[1:]) > 0:
self.passenger = CommandBlockMinecart(command[1:])
else:
self.passenger = None
else:
self.command = command
self.passenger = passenger
def __str__(self):
resultant = '{id:"command_block_minecart",Command:"'
resultant += self.command
resultant += '"'
if self.passenger != None:
resultant += f',Passengers:[{self.passenger}]'
resultant += '}'
return resultant
class Command:
def __init__(self, command: str, conditional = False, comment = ""):
self.command = command
self.conditional = conditional
self.comment = comment
class CommandChain:
def __init__(self, commands: list[Command], origin = RelativeCoordinate(0, 0, 0), facing = "north", continued = False):
self.commands = commands
self.origin = origin
self.facing = facing
self.increment = 0
self.continued = continued
def to_list(self):
resultant = []
block_coordinate = self.origin
for i in range(len(self.commands)):
command = f"setblock {block_coordinate} "
command += "command_block" if i == 0 and not self.continued else "chain_command_block"
block_data = []
if self.facing != "north": block_data += [f"facing={self.facing}"]
if self.commands[i].conditional: block_data += ["conditional=true"]
if block_data != []: command += "[" + ",".join(block_data) + "]"
command += "{Command:'"
command += self.commands[i].command
command += "'"
if i > 0 or self.continued: command += ",auto:1b"
command += "}"
resultant += [command]
match self.facing:
case "north":
block_coordinate.z -= 1
case "south":
block_coordinate.z += 1
case "west":
block_coordinate.x -= 1
case "east":
block_coordinate.x += 1
case "down":
block_coordinate.y -= 1
case "up":
block_coordinate.y += 1
return resultant
class Macro:
def __init__(self, name: str, argument_names: list | None, contents: str):
if argument_names != None:
for argument in argument_names:
if argument in COMMANDS:
raise IncompleteMacroError(f"Macro '{name}' contains argument name '{argument}', which is a command. Change to avoid unexpected errors.")
self.name = name
self.argument_names = argument_names
self.contents = contents
def line_is_macro(self, line: str):
return line.strip().startswith(self.name)
def substitute(self, line: str):
if not self.line_is_macro(line):
return line
if self.argument_names != None:
arguments = re.findall(r"\(.*\)", line)
arguments = re.findall(r"(?<=[\(,])[^\s]*?[^,]+(?=[,\)])", arguments[0])
for i in range(len(arguments)):
arguments[i] = arguments[i].strip()
if len(arguments) != len(self.argument_names):
raise IncompleteMacroError(f"Missing argument for macro {self.name} ({len(arguments)} args instead of {len(self.argument_names)})")
substitute_dict = dict(zip(self.argument_names, arguments))
new_line = "\n".join(self.contents)
for key in substitute_dict:
substitute = substitute_dict[key].replace('(', '')
new_line = re.sub(rf"(?<!\w)(?<=[^@]){key}(?!\w)", substitute, new_line)
return new_line.splitlines()
return self.contents
class Sign:
def __init__(self, text: str, facing = "north"):
self.text = text
self.facing = facing
def __str__(self):
return