dfpyre 0.4.6__py3-none-any.whl → 0.6.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 CHANGED
@@ -3,8 +3,17 @@ Contains class definitions for code items.
3
3
  """
4
4
 
5
5
  from enum import Enum
6
- from typing import Literal, Dict
7
- from dfpyre.style import isAmpersandCoded, ampersandToMinimessage
6
+ import re
7
+ from typing import Literal, Dict, Any
8
+ from dfpyre.style import is_ampersand_coded, ampersand_to_minimessage
9
+ from mcitemlib.itemlib import Item as NbtItem
10
+
11
+
12
+ NUMBER_REGEX = r'-?\d*\.?\d+'
13
+
14
+
15
+ class PyreException(Exception):
16
+ pass
8
17
 
9
18
 
10
19
  def _add_slot(d: Dict, slot: int|None):
@@ -12,27 +21,16 @@ def _add_slot(d: Dict, slot: int|None):
12
21
  d['slot'] = slot
13
22
 
14
23
 
15
- class item:
24
+ class item(NbtItem):
16
25
  """
17
26
  Represents a Minecraft item.
18
27
  """
19
28
  type = 'item'
20
29
 
21
- def __init__(self, itemID: str, count: int=1):
22
- self.id = itemID
23
- self.count = count
24
-
25
30
  def format(self, slot: int|None):
26
- formattedDict = {
27
- "item": {
28
- "id": self.type,
29
- "data": {
30
- "item": f"{{DF_NBT:2586,id:\"{self.id}\",Count:{self.count}b}}"
31
- }
32
- }
33
- }
34
- _add_slot(formattedDict, slot)
35
- return formattedDict
31
+ formatted_dict = {"item": {"id": self.type, "data": {"item": self.get_nbt()}}}
32
+ _add_slot(formatted_dict, slot)
33
+ return formatted_dict
36
34
 
37
35
 
38
36
  class string:
@@ -45,16 +43,9 @@ class string:
45
43
  self.value = value
46
44
 
47
45
  def format(self, slot: int|None):
48
- formattedDict = {
49
- "item": {
50
- "id": self.type,
51
- "data": {
52
- "name": self.value
53
- }
54
- }
55
- }
56
- _add_slot(formattedDict, slot)
57
- return formattedDict
46
+ formatted_dict = {"item": {"id": self.type, "data": {"name": self.value}}}
47
+ _add_slot(formatted_dict, slot)
48
+ return formatted_dict
58
49
 
59
50
 
60
51
  class text:
@@ -64,21 +55,14 @@ class text:
64
55
  type = 'comp'
65
56
 
66
57
  def __init__(self, value: str):
67
- if isAmpersandCoded(value):
68
- value = ampersandToMinimessage(value)
58
+ if is_ampersand_coded(value):
59
+ value = ampersand_to_minimessage(value)
69
60
  self.value = value
70
-
61
+
71
62
  def format(self, slot: int|None):
72
- formattedDict = {
73
- "item": {
74
- "id": self.type,
75
- "data": {
76
- "name": self.value
77
- }
78
- }
79
- }
80
- _add_slot(formattedDict, slot)
81
- return formattedDict
63
+ formatted_dict = {"item": {"id": self.type, "data": {"name": self.value}}}
64
+ _add_slot(formatted_dict, slot)
65
+ return formatted_dict
82
66
 
83
67
 
84
68
  class num:
@@ -87,20 +71,13 @@ class num:
87
71
  """
88
72
  type = 'num'
89
73
 
90
- def __init__(self, num: int|float):
74
+ def __init__(self, num: int|float|str):
91
75
  self.value = num
92
76
 
93
77
  def format(self, slot: int|None):
94
- formattedDict = {
95
- "item": {
96
- "id": self.type,
97
- "data": {
98
- "name": str(self.value)
99
- }
100
- }
101
- }
102
- _add_slot(formattedDict, slot)
103
- return formattedDict
78
+ formatted_dict = {"item": {"id": self.type, "data": {"name": str(self.value)}}}
79
+ _add_slot(formatted_dict, slot)
80
+ return formatted_dict
104
81
 
105
82
 
106
83
  class loc:
@@ -117,23 +94,21 @@ class loc:
117
94
  self.yaw = float(yaw)
118
95
 
119
96
  def format(self, slot: int|None):
120
- formattedDict = {
121
- "item": {
122
- "id": self.type,
123
- "data": {
97
+ formatted_dict = {"item": {
98
+ "id": self.type,
99
+ "data": {
124
100
  "isBlock": False,
125
101
  "loc": {
126
- "x": self.x,
127
- "y": self.y,
128
- "z": self.z,
129
- "pitch": self.pitch,
130
- "yaw": self.yaw
102
+ "x": self.x,
103
+ "y": self.y,
104
+ "z": self.z,
105
+ "pitch": self.pitch,
106
+ "yaw": self.yaw
131
107
  }
132
- }
133
108
  }
134
- }
135
- _add_slot(formattedDict, slot)
136
- return formattedDict
109
+ }}
110
+ _add_slot(formatted_dict, slot)
111
+ return formatted_dict
137
112
 
138
113
 
139
114
  class var:
@@ -147,17 +122,9 @@ class var:
147
122
  self.scope = scope
148
123
 
149
124
  def format(self, slot: int|None):
150
- formattedDict = {
151
- "item": {
152
- "id": self.type,
153
- "data": {
154
- "name": self.name,
155
- "scope": self.scope
156
- }
157
- }
158
- }
159
- _add_slot(formattedDict, slot)
160
- return formattedDict
125
+ formatted_dict = {"item": {"id": self.type,"data": {"name": self.name, "scope": self.scope}}}
126
+ _add_slot(formatted_dict, slot)
127
+ return formatted_dict
161
128
 
162
129
 
163
130
  class sound:
@@ -172,18 +139,9 @@ class sound:
172
139
  self.vol = vol
173
140
 
174
141
  def format(self, slot: int|None):
175
- formattedDict = {
176
- "item": {
177
- "id": self.type,
178
- "data": {
179
- "sound": self.name,
180
- "pitch": self.pitch,
181
- "vol": self.vol
182
- }
183
- }
184
- }
185
- _add_slot(formattedDict, slot)
186
- return formattedDict
142
+ formatted_dict = {"item": {"id": self.type,"data": {"sound": self.name, "pitch": self.pitch, "vol": self.vol}}}
143
+ _add_slot(formatted_dict, slot)
144
+ return formatted_dict
187
145
 
188
146
 
189
147
  class particle:
@@ -191,39 +149,13 @@ class particle:
191
149
  Represents a DiamondFire particle object.
192
150
  """
193
151
  type = 'part'
194
- def __init__(self, name: str='Cloud', amount: int=1, horizontal: float=0.0, vertical: float=0.0,
195
- x: float=1.0, y: float=0.0, z: float=0.0, motionVariation: float=100):
196
- self.name = name
197
- self.amount = amount
198
- self.horizontal = horizontal
199
- self.vertical = vertical
200
- self.x = x
201
- self.y = y
202
- self.z = z
203
- self.motionVariation = motionVariation
152
+ def __init__(self, particle_data: dict):
153
+ self.particle_data = particle_data
204
154
 
205
155
  def format(self, slot: int|None):
206
- formattedDict = {
207
- "item": {
208
- "id": self.type,
209
- "data": {
210
- "particle": self.name,
211
- "cluster": {
212
- "amount": self.amount,
213
- "horizontal": self.horizontal,
214
- "vertical": self.vertical
215
- },
216
- "data": {
217
- "x": self.x,
218
- "y": self.y,
219
- "z": self.z,
220
- "motionVariation": self.motionVariation
221
- }
222
- }
223
- }
224
- }
225
- _add_slot(formattedDict, slot)
226
- return formattedDict
156
+ formatted_dict = {"item": {"id": self.type, "data": self.particle_data}}
157
+ _add_slot(formatted_dict, slot)
158
+ return formatted_dict
227
159
 
228
160
 
229
161
  class potion:
@@ -238,18 +170,9 @@ class potion:
238
170
  self.amp = amp
239
171
 
240
172
  def format(self, slot: int|None):
241
- formattedDict = {
242
- "item": {
243
- "id": self.type,
244
- "data": {
245
- "pot": self.name,
246
- "dur": self.dur,
247
- "amp": self.amp
248
- }
249
- }
250
- }
251
- _add_slot(formattedDict, slot)
252
- return formattedDict
173
+ formatted_dict = {"item": {"id": self.type,"data": {"pot": self.name, "dur": self.dur, "amp": self.amp}}}
174
+ _add_slot(formatted_dict, slot)
175
+ return formatted_dict
253
176
 
254
177
 
255
178
  class gamevalue:
@@ -263,17 +186,9 @@ class gamevalue:
263
186
  self.target = target
264
187
 
265
188
  def format(self, slot: int|None):
266
- formattedDict = {
267
- "item": {
268
- "id": self.type,
269
- "data": {
270
- "type": self.name,
271
- "target": self.target
272
- }
273
- }
274
- }
275
- _add_slot(formattedDict, slot)
276
- return formattedDict
189
+ formatted_dict = {"item": {"id": self.type, "data": {"type": self.name, "target": self.target}}}
190
+ _add_slot(formatted_dict, slot)
191
+ return formatted_dict
277
192
 
278
193
 
279
194
  class vector:
@@ -288,18 +203,9 @@ class vector:
288
203
  self.z = float(z)
289
204
 
290
205
  def format(self, slot: int|None):
291
- formattedDict = {
292
- "item": {
293
- "id": self.type,
294
- "data": {
295
- "x": self.x,
296
- "y": self.y,
297
- "z": self.z
298
- }
299
- }
300
- }
301
- _add_slot(formattedDict, slot)
302
- return formattedDict
206
+ formatted_dict = {"item": {"id": self.type, "data": {"x": self.x, "y": self.y, "z": self.z}}}
207
+ _add_slot(formatted_dict, slot)
208
+ return formatted_dict
303
209
 
304
210
 
305
211
  PARAMETER_TYPE_LOOKUP = ['txt', 'comp', 'num', 'loc', 'vec', 'snd', 'part', 'pot', 'item', 'any', 'var', 'list', 'dict']
@@ -328,34 +234,89 @@ class parameter:
328
234
  """
329
235
  type = 'pn_el'
330
236
 
331
- def __init__(self, name: str, paramType: ParameterType, plural: bool=False, optional: bool=False, description: str="", note: str="", defaultValue=None):
237
+ def __init__(self, name: str, param_type: ParameterType, plural: bool=False, optional: bool=False, description: str="", note: str="", default_value=None):
332
238
  self.name = name
333
- self.paramType = paramType
239
+ self.param_type = param_type
334
240
  self.plural = plural
335
241
  self.optional = optional
336
242
  self.description = description
337
243
  self.note = note
338
- self.defaultValue = defaultValue
244
+ self.default_value = default_value
339
245
 
340
246
 
341
247
  def format(self, slot: int):
342
- formattedDict = {
343
- "item": {
344
- "id": self.type,
345
- "data": {
248
+ formatted_dict = {"item": {
249
+ "id": self.type,
250
+ "data": {
346
251
  "name": self.name,
347
- "type": self.paramType.get_string_value(),
252
+ "type": self.param_type.get_string_value(),
348
253
  "plural": self.plural,
349
254
  "optional": self.optional,
350
- }
351
- },
255
+ }},
352
256
  "slot": slot
353
- }
257
+ }
354
258
  if self.description:
355
- formattedDict['item']['data']['description'] = self.description
259
+ formatted_dict['item']['data']['description'] = self.description
356
260
  if self.note:
357
- formattedDict['item']['data']['note'] = self.note
358
- if self.defaultValue is not None and not self.plural and self.optional:
359
- formattedDict['item']['data']['default_value'] = self.defaultValue.format(None)['item']
261
+ formatted_dict['item']['data']['note'] = self.note
262
+ if self.default_value is not None and not self.plural and self.optional:
263
+ formatted_dict['item']['data']['default_value'] = self.default_value.format(None)['item']
360
264
 
361
- return formattedDict
265
+ return formatted_dict
266
+
267
+
268
+ def _some_or(value: Any, none_value: Any):
269
+ """
270
+ Returns `none_value` if `value` is None, otherwise returns `value`.
271
+ """
272
+ if value is None:
273
+ return none_value
274
+ return value
275
+
276
+
277
+ def item_from_dict(item_dict: Dict) -> object:
278
+ item_id = item_dict['id']
279
+ item_data = item_dict['data']
280
+
281
+ if item_id == 'item':
282
+ return item.from_nbt(item_data['item'])
283
+ elif item_id == 'txt':
284
+ return string(item_data['name'])
285
+ elif item_id == 'comp':
286
+ return text(item_data['name'])
287
+ elif item_id == 'num':
288
+ num_value = item_data['name']
289
+ if re.match(NUMBER_REGEX, num_value):
290
+ num_value = float(item_data['name'])
291
+ if num_value % 1 == 0:
292
+ num_value = int(num_value)
293
+ return num(num_value)
294
+ return num(num_value)
295
+ elif item_id == 'loc':
296
+ item_loc = item_data['loc']
297
+ return loc(item_loc['x'], item_loc['y'], item_loc['z'], item_loc['pitch'], item_loc['yaw'])
298
+ elif item_id == 'var':
299
+ return var(item_data['name'], item_data['scope'])
300
+ elif item_id == 'snd':
301
+ return sound(item_data['sound'], item_data['pitch'], item_data['vol'])
302
+ elif item_id == 'part':
303
+ return particle(item_data)
304
+ elif item_id == 'pot':
305
+ return potion(item_data['pot'], item_data['dur'], item_data['amp'])
306
+ elif item_id == 'g_val':
307
+ return gamevalue(item_data['type'], item_data['target'])
308
+ elif item_id == 'vec':
309
+ return vector(item_data['x'], item_data['y'], item_data['z'])
310
+ elif item_id == 'pn_el':
311
+ description = _some_or(item_data.get('description'), '')
312
+ note = _some_or(item_data.get('note'), '')
313
+ param_type = ParameterType(PARAMETER_TYPE_LOOKUP.index(item_data['type']))
314
+ if item_data['optional']:
315
+ param = parameter(item_data['name'], param_type, item_data['plural'], True, description, note, item_from_dict(item_data['default_value']))
316
+ else:
317
+ param = parameter(item_data['name'], param_type, item_data['plural'], False, description, note)
318
+ return param
319
+ elif item_id == 'bl_tag':
320
+ return
321
+ else:
322
+ raise PyreException(f'Unrecognized item id `{item_id}`')