dfpyre 0.4.6__py3-none-any.whl → 0.5.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of dfpyre might be problematic. Click here for more details.
- dfpyre/items.py +70 -152
- dfpyre/pyre.py +135 -209
- dfpyre/style.py +14 -14
- {dfpyre-0.4.6.dist-info → dfpyre-0.5.0.dist-info}/METADATA +77 -79
- dfpyre-0.5.0.dist-info/RECORD +9 -0
- dfpyre-0.4.6.dist-info/RECORD +0 -9
- {dfpyre-0.4.6.dist-info → dfpyre-0.5.0.dist-info}/LICENSE +0 -0
- {dfpyre-0.4.6.dist-info → dfpyre-0.5.0.dist-info}/WHEEL +0 -0
dfpyre/pyre.py
CHANGED
|
@@ -7,9 +7,6 @@ By Amp
|
|
|
7
7
|
|
|
8
8
|
import base64
|
|
9
9
|
import gzip
|
|
10
|
-
import socket
|
|
11
|
-
import websocket
|
|
12
|
-
import time
|
|
13
10
|
import json
|
|
14
11
|
import os
|
|
15
12
|
from difflib import get_close_matches
|
|
@@ -51,7 +48,7 @@ class Target(Enum):
|
|
|
51
48
|
ALL_MOBS = 9
|
|
52
49
|
LAST_ENTITY = 10
|
|
53
50
|
|
|
54
|
-
def
|
|
51
|
+
def get_string_value(self):
|
|
55
52
|
return TARGETS[self.value]
|
|
56
53
|
|
|
57
54
|
DEFAULT_TARGET = Target.SELECTION
|
|
@@ -69,35 +66,35 @@ def _warn(message):
|
|
|
69
66
|
print(f'{COL_WARN}! WARNING ! {message}{COL_RESET}')
|
|
70
67
|
|
|
71
68
|
|
|
72
|
-
def
|
|
73
|
-
close = get_close_matches(
|
|
69
|
+
def _warn_unrecognized_name(codeblock_type: str, codeblock_name: str):
|
|
70
|
+
close = get_close_matches(codeblock_name, TAGDATA[codeblock_type].keys())
|
|
74
71
|
if close:
|
|
75
|
-
_warn(f'Code block name "{
|
|
72
|
+
_warn(f'Code block name "{codeblock_name}" not recognized. Did you mean "{close[0]}"?')
|
|
76
73
|
else:
|
|
77
|
-
_warn(f'Code block name "{
|
|
74
|
+
_warn(f'Code block name "{codeblock_name}" not recognized. Try spell checking or retyping without spaces.')
|
|
78
75
|
|
|
79
76
|
|
|
80
|
-
def
|
|
81
|
-
|
|
77
|
+
def _load_codeblock_data() -> Tuple:
|
|
78
|
+
tag_data = {}
|
|
82
79
|
if os.path.exists(CODEBLOCK_DATA_PATH):
|
|
83
80
|
with open(CODEBLOCK_DATA_PATH, 'r') as f:
|
|
84
|
-
|
|
81
|
+
tag_data = json.load(f)
|
|
85
82
|
else:
|
|
86
83
|
_warn('data.json not found -- Item tags and error checking will not work.')
|
|
87
84
|
return ({}, set(), set())
|
|
88
85
|
|
|
89
|
-
del
|
|
86
|
+
del tag_data['meta']
|
|
90
87
|
|
|
91
|
-
|
|
88
|
+
all_names = [x for l in [d.keys() for d in tag_data.values()] for x in l] # flatten list
|
|
92
89
|
return (
|
|
93
|
-
|
|
94
|
-
set(
|
|
95
|
-
set(
|
|
90
|
+
tag_data,
|
|
91
|
+
set(tag_data['extras'].keys()),
|
|
92
|
+
set(all_names)
|
|
96
93
|
)
|
|
97
94
|
|
|
98
|
-
TAGDATA, TAGDATA_EXTRAS_KEYS, ALL_CODEBLOCK_NAMES =
|
|
95
|
+
TAGDATA, TAGDATA_EXTRAS_KEYS, ALL_CODEBLOCK_NAMES = _load_codeblock_data()
|
|
99
96
|
|
|
100
|
-
def
|
|
97
|
+
def _add_inverted(data, inverted):
|
|
101
98
|
"""
|
|
102
99
|
If inverted is true, add 'inverted': 'NOT' to data.
|
|
103
100
|
"""
|
|
@@ -105,86 +102,86 @@ def _addInverted(data, inverted):
|
|
|
105
102
|
data['inverted'] = 'NOT'
|
|
106
103
|
|
|
107
104
|
|
|
108
|
-
def
|
|
109
|
-
|
|
105
|
+
def _convert_data_types(args):
|
|
106
|
+
converted_args = []
|
|
110
107
|
for value in args:
|
|
111
108
|
if type(value) in {int, float}:
|
|
112
|
-
|
|
109
|
+
converted_args.append(num(value))
|
|
113
110
|
elif type(value) is str:
|
|
114
111
|
if value[0] == VAR_SHORTHAND_CHAR and value[1] in VAR_SCOPES:
|
|
115
|
-
|
|
116
|
-
|
|
112
|
+
var_object = var(value[2:], VAR_SCOPES[value[1]])
|
|
113
|
+
converted_args.append(var_object)
|
|
117
114
|
else:
|
|
118
|
-
|
|
115
|
+
converted_args.append(text(value))
|
|
119
116
|
else:
|
|
120
|
-
|
|
121
|
-
return tuple(
|
|
117
|
+
converted_args.append(value)
|
|
118
|
+
return tuple(converted_args)
|
|
122
119
|
|
|
123
120
|
|
|
124
|
-
def
|
|
121
|
+
def _reformat_codeblock_tags(tags, codeblock_type: str, codeblock_name: str):
|
|
125
122
|
"""
|
|
126
123
|
Turns data.json tag items into DiamondFire formatted tag items
|
|
127
124
|
"""
|
|
128
|
-
|
|
129
|
-
for
|
|
130
|
-
|
|
131
|
-
|
|
125
|
+
reformatted_tags = []
|
|
126
|
+
for tag_item in tags:
|
|
127
|
+
action_value = codeblock_name if 'action' not in tag_item else tag_item['action']
|
|
128
|
+
new_tag_item = {
|
|
132
129
|
'item': {
|
|
133
130
|
'id': 'bl_tag',
|
|
134
131
|
'data': {
|
|
135
|
-
'option':
|
|
136
|
-
'tag':
|
|
137
|
-
'action':
|
|
138
|
-
'block':
|
|
132
|
+
'option': tag_item['option'],
|
|
133
|
+
'tag': tag_item['tag'],
|
|
134
|
+
'action': action_value,
|
|
135
|
+
'block': codeblock_type
|
|
139
136
|
}
|
|
140
137
|
},
|
|
141
|
-
'slot':
|
|
138
|
+
'slot': tag_item['slot']
|
|
142
139
|
}
|
|
143
|
-
|
|
144
|
-
return
|
|
140
|
+
reformatted_tags.append(new_tag_item)
|
|
141
|
+
return reformatted_tags
|
|
145
142
|
|
|
146
143
|
|
|
147
|
-
def
|
|
144
|
+
def _get_codeblock_tags(codeblock_type: str, codeblock_name: str):
|
|
148
145
|
"""
|
|
149
146
|
Get tags for the specified codeblock type and name
|
|
150
147
|
"""
|
|
151
148
|
tags = None
|
|
152
|
-
if
|
|
153
|
-
tags = TAGDATA['extras'][
|
|
149
|
+
if codeblock_type in TAGDATA_EXTRAS_KEYS:
|
|
150
|
+
tags = TAGDATA['extras'][codeblock_type]
|
|
154
151
|
else:
|
|
155
|
-
tags = TAGDATA[
|
|
156
|
-
return
|
|
152
|
+
tags = TAGDATA[codeblock_type].get(codeblock_name)
|
|
153
|
+
return _reformat_codeblock_tags(tags, codeblock_type, codeblock_name)
|
|
157
154
|
|
|
158
155
|
|
|
159
|
-
def
|
|
156
|
+
def _build_block(codeblock: CodeBlock, include_tags: bool):
|
|
160
157
|
"""
|
|
161
158
|
Builds a properly formatted block from a CodeBlock object.
|
|
162
159
|
"""
|
|
163
|
-
|
|
164
|
-
|
|
160
|
+
final_block = codeblock.data.copy()
|
|
161
|
+
codeblock_type = codeblock.data.get('block')
|
|
165
162
|
|
|
166
163
|
# add target if necessary ('Selection' is the default when 'target' is blank)
|
|
167
|
-
if
|
|
168
|
-
|
|
164
|
+
if codeblock_type in TARGET_CODEBLOCKS and codeblock.target != DEFAULT_TARGET:
|
|
165
|
+
final_block['target'] = codeblock.target.get_string_value()
|
|
169
166
|
|
|
170
167
|
# add items into args
|
|
171
|
-
|
|
168
|
+
final_args = [arg.format(slot) for slot, arg in enumerate(codeblock.args) if arg.type in VARIABLE_TYPES]
|
|
172
169
|
|
|
173
170
|
# check for unrecognized name, add tags
|
|
174
|
-
if
|
|
175
|
-
if
|
|
176
|
-
|
|
177
|
-
elif
|
|
178
|
-
tags =
|
|
179
|
-
if len(
|
|
180
|
-
|
|
181
|
-
|
|
171
|
+
if codeblock_type is not None: # for brackets
|
|
172
|
+
if codeblock_type not in TAGDATA_EXTRAS_KEYS and codeblock.name not in ALL_CODEBLOCK_NAMES:
|
|
173
|
+
_warn_unrecognized_name(codeblock_type, codeblock.name)
|
|
174
|
+
elif include_tags:
|
|
175
|
+
tags = _get_codeblock_tags(codeblock_type, codeblock.name)
|
|
176
|
+
if len(final_args) + len(tags) > 27:
|
|
177
|
+
final_args = final_args[:(27-len(tags))] # trim list if over 27 elements
|
|
178
|
+
final_args.extend(tags) # add tags to end
|
|
182
179
|
|
|
183
|
-
|
|
184
|
-
return
|
|
180
|
+
final_block['args'] = {'items': final_args}
|
|
181
|
+
return final_block
|
|
185
182
|
|
|
186
183
|
|
|
187
|
-
def
|
|
184
|
+
def _df_encode(jsonString: str) -> str:
|
|
188
185
|
"""
|
|
189
186
|
Encodes a stringified json.
|
|
190
187
|
"""
|
|
@@ -192,12 +189,12 @@ def _dfEncode(jsonString: str) -> str:
|
|
|
192
189
|
return base64.b64encode(encodedString).decode('utf-8')
|
|
193
190
|
|
|
194
191
|
|
|
195
|
-
def
|
|
192
|
+
def _get_template_item(template_code: str, name: str, author: str) -> NbtItem:
|
|
196
193
|
now = datetime.datetime.now()
|
|
197
194
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
195
|
+
template_item = NbtItem('yellow_shulker_box')
|
|
196
|
+
template_item.set_name(f'&x&f&f&5&c&0&0>> &x&f&f&c&7&0&0{name}')
|
|
197
|
+
template_item.set_lore([
|
|
201
198
|
f'&8Author: {author}',
|
|
202
199
|
f'&8Date: {now.strftime("%Y-%m-%d")}',
|
|
203
200
|
'',
|
|
@@ -205,81 +202,13 @@ def getTemplateItem(templateCode: str, name: str, author: str) -> NbtItem:
|
|
|
205
202
|
'&7https://github.com/Amp63/pyre'
|
|
206
203
|
])
|
|
207
204
|
|
|
208
|
-
|
|
209
|
-
'hypercube:codetemplatedata': f'{{"author":"{author}","name":"{name}","version": 1,"code":"{
|
|
205
|
+
pbv_tag = {
|
|
206
|
+
'hypercube:codetemplatedata': f'{{"author":"{author}","name":"{name}","version": 1,"code":"{template_code}"}}',
|
|
210
207
|
'hypercube:pyre_creation_timestamp': now.timestamp()
|
|
211
208
|
}
|
|
212
|
-
|
|
209
|
+
template_item.set_tag('PublicBukkitValues', pbv_tag, raw=True)
|
|
213
210
|
|
|
214
|
-
return
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
def sendRecode(templateCode: str, name: str='Unnamed Template', author: str='pyre') -> int:
|
|
218
|
-
"""
|
|
219
|
-
Sends a template to DiamondFire via recode item api.
|
|
220
|
-
|
|
221
|
-
:param str templateCode: The code for the template as a base64 string.
|
|
222
|
-
:param str name: The name of the template.
|
|
223
|
-
:param str author: The author of the template.
|
|
224
|
-
|
|
225
|
-
:return: status code
|
|
226
|
-
- `0` = Success
|
|
227
|
-
- `1` = Connection refused
|
|
228
|
-
- `2` = Other socket error
|
|
229
|
-
"""
|
|
230
|
-
|
|
231
|
-
templateItem = getTemplateItem(templateCode, name, author)
|
|
232
|
-
data = {'type': 'nbt', 'source': f'pyre Template - {name}', 'data': templateItem.get_nbt()}
|
|
233
|
-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
234
|
-
try:
|
|
235
|
-
s.connect(('127.0.0.1', 31372))
|
|
236
|
-
except ConnectionRefusedError:
|
|
237
|
-
print(f"""{COL_ERROR}Could not connect to recode item API. Possible problems:
|
|
238
|
-
- Minecraft is not open
|
|
239
|
-
- Recode is not installed (get it here: https://modrinth.com/mod/recode or join the discord here: https://discord.gg/GWxWtcwA2C){COL_RESET}""")
|
|
240
|
-
s.close()
|
|
241
|
-
return 1
|
|
242
|
-
|
|
243
|
-
s.send((str(data) + '\n').encode('utf-8'))
|
|
244
|
-
received = json.loads(s.recv(1024).decode())
|
|
245
|
-
status = received['status']
|
|
246
|
-
s.close()
|
|
247
|
-
time.sleep(0.5)
|
|
248
|
-
|
|
249
|
-
if status == 'success':
|
|
250
|
-
print(f'{COL_SUCCESS}Template sent to client successfully.{COL_RESET}')
|
|
251
|
-
return 0
|
|
252
|
-
error = received['error']
|
|
253
|
-
print(f'{COL_ERROR}Error sending template: {error}{COL_RESET}')
|
|
254
|
-
return 2
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
def sendCodeClient(templateCode: str, name: str='Unnamed Template', author: str='pyre') -> int:
|
|
258
|
-
try:
|
|
259
|
-
ws = websocket.WebSocket()
|
|
260
|
-
ws.connect(CODECLIENT_URL)
|
|
261
|
-
print(f'{COL_SUCCESS}Connected. {COL_WARN}Please run /auth in game.{COL_RESET}')
|
|
262
|
-
|
|
263
|
-
ws.recv() # auth response
|
|
264
|
-
|
|
265
|
-
templateItem = getTemplateItem(templateCode, name, author)
|
|
266
|
-
command = f'give {templateItem.get_nbt()}'
|
|
267
|
-
ws.send(command)
|
|
268
|
-
ws.close()
|
|
269
|
-
|
|
270
|
-
print(f'{COL_SUCCESS}Template sent to client successfully.{COL_RESET}')
|
|
271
|
-
return 0
|
|
272
|
-
|
|
273
|
-
except Exception as e:
|
|
274
|
-
if isinstance(e, ConnectionRefusedError):
|
|
275
|
-
print(f'{COL_ERROR}Could not connect to CodeClient API. Possible problems:')
|
|
276
|
-
print(f' - Minecraft is not open')
|
|
277
|
-
print(f' - CodeClient is not installed (get it here: https://modrinth.com/mod/codeclient)')
|
|
278
|
-
print(f' - CodeClient API is not enabled (enable it in CodeClient general settings)')
|
|
279
|
-
return 1
|
|
280
|
-
|
|
281
|
-
print(f'Connection failed: {e}')
|
|
282
|
-
return 2
|
|
211
|
+
return template_item
|
|
283
212
|
|
|
284
213
|
|
|
285
214
|
class DFTemplate:
|
|
@@ -287,56 +216,53 @@ class DFTemplate:
|
|
|
287
216
|
Represents a DiamondFire code template.
|
|
288
217
|
"""
|
|
289
218
|
def __init__(self, name: str=None, author: str='pyre'):
|
|
290
|
-
self.
|
|
219
|
+
self.codeblocks: List[CodeBlock] = []
|
|
291
220
|
self.closebracket = None
|
|
292
221
|
self.name = name
|
|
293
222
|
self.author = author
|
|
294
223
|
|
|
295
224
|
|
|
296
|
-
def
|
|
225
|
+
def _set_template_name(self, first_block):
|
|
297
226
|
if self.name is not None:
|
|
298
227
|
return
|
|
299
|
-
if 'data' in
|
|
300
|
-
self.name =
|
|
228
|
+
if 'data' in first_block:
|
|
229
|
+
self.name = first_block['data']
|
|
301
230
|
if not self.name:
|
|
302
231
|
self.name = 'Unnamed Template'
|
|
303
232
|
else:
|
|
304
|
-
self.name =
|
|
233
|
+
self.name = first_block['block'] + '_' + first_block['action']
|
|
305
234
|
|
|
306
235
|
|
|
307
|
-
def build(self,
|
|
236
|
+
def build(self, include_tags: bool=True) -> str:
|
|
308
237
|
"""
|
|
309
238
|
Build this template.
|
|
310
239
|
|
|
311
240
|
:param bool includeTags: If True, include item tags in code blocks. Otherwise omit them.
|
|
312
241
|
:return: String containing encoded template data.
|
|
313
242
|
"""
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
if
|
|
243
|
+
template_dict_blocks = [_build_block(codeblock, include_tags) for codeblock in self.codeblocks]
|
|
244
|
+
template_dict = {'blocks': template_dict_blocks}
|
|
245
|
+
first_block = template_dict_blocks[0]
|
|
246
|
+
if first_block['block'] not in TEMPLATE_STARTERS:
|
|
318
247
|
_warn('Template does not start with an event, function, or process.')
|
|
319
248
|
|
|
320
|
-
self.
|
|
249
|
+
self._set_template_name(first_block)
|
|
321
250
|
|
|
322
251
|
print(f'{COL_SUCCESS}Template built successfully.{COL_RESET}')
|
|
323
252
|
|
|
324
|
-
|
|
325
|
-
return
|
|
253
|
+
json_string = json.dumps(template_dict, separators=(',', ':'))
|
|
254
|
+
return _df_encode(json_string)
|
|
326
255
|
|
|
327
256
|
|
|
328
|
-
def
|
|
257
|
+
def build_and_send(self, method: Literal['recode', 'codeclient'], includeTags: bool=True) -> int:
|
|
329
258
|
"""
|
|
330
259
|
Builds this template and sends it to DiamondFire automatically.
|
|
331
260
|
|
|
332
261
|
:param bool includeTags: If True, include item tags in code blocks. Otherwise omit them.
|
|
333
262
|
"""
|
|
334
263
|
templateCode = self.build(includeTags)
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
if sendMethod == 'codeclient':
|
|
338
|
-
return sendCodeClient(templateCode, name=self.name, author=self.author)
|
|
339
|
-
return -1
|
|
264
|
+
templateItem = _get_template_item(templateCode, self.name, self.author)
|
|
265
|
+
return templateItem.send_to_minecraft(method)
|
|
340
266
|
|
|
341
267
|
|
|
342
268
|
def clear(self):
|
|
@@ -348,132 +274,132 @@ class DFTemplate:
|
|
|
348
274
|
|
|
349
275
|
def _openbracket(self, btype: Literal['norm', 'repeat']='norm'):
|
|
350
276
|
bracket = CodeBlock('Bracket', data={'id': 'bracket', 'direct': 'open', 'type': btype})
|
|
351
|
-
self.
|
|
277
|
+
self.codeblocks.append(bracket)
|
|
352
278
|
self.closebracket = btype
|
|
353
279
|
|
|
354
280
|
|
|
355
281
|
# command methods
|
|
356
|
-
def
|
|
282
|
+
def player_event(self, name: str):
|
|
357
283
|
cmd = CodeBlock(name, data={'id': 'block', 'block': 'event', 'action': name})
|
|
358
|
-
self.
|
|
284
|
+
self.codeblocks.append(cmd)
|
|
359
285
|
|
|
360
286
|
|
|
361
|
-
def
|
|
287
|
+
def entity_event(self, name: str):
|
|
362
288
|
cmd = CodeBlock(name, data={'id': 'block', 'block': 'entity_event', 'action': name})
|
|
363
|
-
self.
|
|
289
|
+
self.codeblocks.append(cmd)
|
|
364
290
|
|
|
365
291
|
|
|
366
292
|
def function(self, name: str, *args):
|
|
367
|
-
args =
|
|
293
|
+
args = _convert_data_types(args)
|
|
368
294
|
cmd = CodeBlock('function', args, data={'id': 'block', 'block': 'func', 'data': name})
|
|
369
|
-
self.
|
|
295
|
+
self.codeblocks.append(cmd)
|
|
370
296
|
|
|
371
297
|
|
|
372
298
|
def process(self, name: str):
|
|
373
299
|
cmd = CodeBlock('process', data={'id': 'block', 'block': 'process', 'data': name})
|
|
374
|
-
self.
|
|
300
|
+
self.codeblocks.append(cmd)
|
|
375
301
|
|
|
376
302
|
|
|
377
|
-
def
|
|
378
|
-
args =
|
|
303
|
+
def call_function(self, name: str, *args):
|
|
304
|
+
args = _convert_data_types(args)
|
|
379
305
|
cmd = CodeBlock('call_func', args, data={'id': 'block', 'block': 'call_func', 'data': name})
|
|
380
|
-
self.
|
|
306
|
+
self.codeblocks.append(cmd)
|
|
381
307
|
|
|
382
308
|
|
|
383
|
-
def
|
|
309
|
+
def start_process(self, name: str):
|
|
384
310
|
cmd = CodeBlock('start_process', data={'id': 'block', 'block': 'start_process', 'data': name})
|
|
385
|
-
self.
|
|
311
|
+
self.codeblocks.append(cmd)
|
|
386
312
|
|
|
387
313
|
|
|
388
|
-
def
|
|
389
|
-
args =
|
|
314
|
+
def player_action(self, name: str, *args, target: Target=DEFAULT_TARGET):
|
|
315
|
+
args = _convert_data_types(args)
|
|
390
316
|
cmd = CodeBlock(name, args, target=target, data={'id': 'block', 'block': 'player_action', 'action': name})
|
|
391
|
-
self.
|
|
317
|
+
self.codeblocks.append(cmd)
|
|
392
318
|
|
|
393
319
|
|
|
394
|
-
def
|
|
395
|
-
args =
|
|
320
|
+
def game_action(self, name: str, *args):
|
|
321
|
+
args = _convert_data_types(args)
|
|
396
322
|
cmd = CodeBlock(name, args, data={'id': 'block', 'block': 'game_action', 'action': name})
|
|
397
|
-
self.
|
|
323
|
+
self.codeblocks.append(cmd)
|
|
398
324
|
|
|
399
325
|
|
|
400
|
-
def
|
|
401
|
-
args =
|
|
326
|
+
def entity_action(self, name: str, *args, target: Target=DEFAULT_TARGET):
|
|
327
|
+
args = _convert_data_types(args)
|
|
402
328
|
cmd = CodeBlock(name, args, target=target, data={'id': 'block', 'block': 'entity_action', 'action': name})
|
|
403
|
-
self.
|
|
329
|
+
self.codeblocks.append(cmd)
|
|
404
330
|
|
|
405
331
|
|
|
406
|
-
def
|
|
407
|
-
args =
|
|
332
|
+
def if_player(self, name: str, *args, target: Target=DEFAULT_TARGET, inverted: bool=False):
|
|
333
|
+
args = _convert_data_types(args)
|
|
408
334
|
data = {'id': 'block', 'block': 'if_player', 'action': name}
|
|
409
|
-
|
|
335
|
+
_add_inverted(data, inverted)
|
|
410
336
|
cmd = CodeBlock(name, args, target=target, data=data)
|
|
411
|
-
self.
|
|
337
|
+
self.codeblocks.append(cmd)
|
|
412
338
|
self._openbracket()
|
|
413
339
|
|
|
414
340
|
|
|
415
|
-
def
|
|
416
|
-
args =
|
|
341
|
+
def if_variable(self, name: str, *args, inverted: bool=False):
|
|
342
|
+
args = _convert_data_types(args)
|
|
417
343
|
data = {'id': 'block', 'block': 'if_var', 'action': name}
|
|
418
|
-
|
|
344
|
+
_add_inverted(data, inverted)
|
|
419
345
|
cmd = CodeBlock(name, args, data=data)
|
|
420
|
-
self.
|
|
346
|
+
self.codeblocks.append(cmd)
|
|
421
347
|
self._openbracket()
|
|
422
348
|
|
|
423
349
|
|
|
424
|
-
def
|
|
425
|
-
args =
|
|
350
|
+
def if_game(self, name: str, *args, inverted: bool=False):
|
|
351
|
+
args = _convert_data_types(args)
|
|
426
352
|
data = {'id': 'block', 'block': 'if_game', 'action': name}
|
|
427
|
-
|
|
353
|
+
_add_inverted(data, inverted)
|
|
428
354
|
cmd = CodeBlock(name, args, data=data)
|
|
429
|
-
self.
|
|
355
|
+
self.codeblocks.append(cmd)
|
|
430
356
|
self._openbracket()
|
|
431
357
|
|
|
432
358
|
|
|
433
|
-
def
|
|
434
|
-
args =
|
|
359
|
+
def if_entity(self, name: str, *args, target: Target=DEFAULT_TARGET, inverted: bool=False):
|
|
360
|
+
args = _convert_data_types(args)
|
|
435
361
|
data = {'id': 'block', 'block': 'if_entity', 'action': name}
|
|
436
|
-
|
|
362
|
+
_add_inverted(data, inverted)
|
|
437
363
|
cmd = CodeBlock(name, args, target=target, data=data)
|
|
438
|
-
self.
|
|
364
|
+
self.codeblocks.append(cmd)
|
|
439
365
|
self._openbracket()
|
|
440
366
|
|
|
441
367
|
|
|
442
368
|
def else_(self):
|
|
443
369
|
cmd = CodeBlock('else', data={'id': 'block', 'block': 'else'})
|
|
444
|
-
self.
|
|
370
|
+
self.codeblocks.append(cmd)
|
|
445
371
|
self._openbracket()
|
|
446
372
|
|
|
447
373
|
|
|
448
|
-
def repeat(self, name: str, *args,
|
|
449
|
-
args =
|
|
374
|
+
def repeat(self, name: str, *args, sub_action: str=None):
|
|
375
|
+
args = _convert_data_types(args)
|
|
450
376
|
data = {'id': 'block', 'block': 'repeat', 'action': name}
|
|
451
|
-
if
|
|
452
|
-
data['subAction'] =
|
|
377
|
+
if sub_action is not None:
|
|
378
|
+
data['subAction'] = sub_action
|
|
453
379
|
cmd = CodeBlock(name, args, data=data)
|
|
454
|
-
self.
|
|
380
|
+
self.codeblocks.append(cmd)
|
|
455
381
|
self._openbracket('repeat')
|
|
456
382
|
|
|
457
383
|
|
|
458
384
|
def bracket(self, *args):
|
|
459
|
-
args =
|
|
385
|
+
args = _convert_data_types(args)
|
|
460
386
|
cmd = CodeBlock('Bracket', data={'id': 'bracket', 'direct': 'close', 'type': self.closebracket})
|
|
461
|
-
self.
|
|
387
|
+
self.codeblocks.append(cmd)
|
|
462
388
|
|
|
463
389
|
|
|
464
390
|
def control(self, name: str, *args):
|
|
465
|
-
args =
|
|
391
|
+
args = _convert_data_types(args)
|
|
466
392
|
cmd = CodeBlock(name, args, data={'id': 'block', 'block': 'control', 'action': name})
|
|
467
|
-
self.
|
|
393
|
+
self.codeblocks.append(cmd)
|
|
468
394
|
|
|
469
395
|
|
|
470
|
-
def
|
|
471
|
-
args =
|
|
396
|
+
def select_object(self, name: str, *args):
|
|
397
|
+
args = _convert_data_types(args)
|
|
472
398
|
cmd = CodeBlock(name, args, data={'id': 'block', 'block': 'select_obj', 'action': name})
|
|
473
|
-
self.
|
|
399
|
+
self.codeblocks.append(cmd)
|
|
474
400
|
|
|
475
401
|
|
|
476
|
-
def
|
|
477
|
-
args =
|
|
402
|
+
def set_variable(self, name: str, *args):
|
|
403
|
+
args = _convert_data_types(args)
|
|
478
404
|
cmd = CodeBlock(name, args, data={'id': 'block', 'block': 'set_var', 'action': name})
|
|
479
|
-
self.
|
|
405
|
+
self.codeblocks.append(cmd)
|
dfpyre/style.py
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import re
|
|
2
2
|
from mcitemlib.style import STYLE_CODE_REGEX, FORMAT_CODES, StyledString
|
|
3
3
|
|
|
4
|
-
def
|
|
4
|
+
def is_ampersand_coded(s: str) -> bool:
|
|
5
5
|
return bool(re.match(STYLE_CODE_REGEX, s))
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
def
|
|
8
|
+
def ampersand_to_minimessage(ampersand_code: str) -> str:
|
|
9
9
|
ampersand_code = ampersand_code.replace('&r', '<reset>') # bad but should work most of the time
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
for substring in
|
|
13
|
-
|
|
14
|
-
for
|
|
15
|
-
if
|
|
16
|
-
|
|
17
|
-
if
|
|
18
|
-
|
|
10
|
+
styled_string = StyledString.from_codes(ampersand_code)
|
|
11
|
+
formatted_string_list = []
|
|
12
|
+
for substring in styled_string.substrings:
|
|
13
|
+
formatted_substring_list = []
|
|
14
|
+
for style_type, value in substring.data.items():
|
|
15
|
+
if style_type in FORMAT_CODES.values() and value:
|
|
16
|
+
formatted_substring_list.append(f'<{style_type}>')
|
|
17
|
+
if style_type == 'color':
|
|
18
|
+
formatted_substring_list.append(f'<{value}>')
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return ''.join(
|
|
20
|
+
formatted_substring_list.append(substring.data['text'])
|
|
21
|
+
formatted_string_list.append(''.join(formatted_substring_list))
|
|
22
|
+
return ''.join(formatted_string_list)
|