dfpyre 0.8.9__tar.gz → 0.8.10__tar.gz

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.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dfpyre
3
- Version: 0.8.9
3
+ Version: 0.8.10
4
4
  Summary: A package for creating and modifying code templates for the DiamondFire Minecraft server.
5
5
  Home-page: https://github.com/Amp63/pyre
6
6
  License: MIT
@@ -444,18 +444,21 @@ class Parameter:
444
444
  return f'{self.__class__.__name__}({self.name}, type: {raw_type})'
445
445
 
446
446
 
447
- def item_from_dict(item_dict: dict) -> Any:
448
- item_id = item_dict['id']
449
- item_data = item_dict['data']
447
+ def item_from_dict(item_dict: dict, preserve_item_slots: bool) -> Any:
448
+ item_id = item_dict['item']['id']
449
+ item_data = item_dict['item']['data']
450
+ item_slot = item_dict['slot'] if preserve_item_slots else None
450
451
 
451
452
  if item_id == 'item':
452
- return Item.from_snbt(item_data['item'])
453
+ item = Item.from_snbt(item_data['item'])
454
+ item.slot = item_slot
455
+ return item
453
456
 
454
457
  elif item_id == 'txt':
455
- return String(item_data['name'])
458
+ return String(item_data['name'], item_slot)
456
459
 
457
460
  elif item_id == 'comp':
458
- return Text(item_data['name'])
461
+ return Text(item_data['name'], item_slot)
459
462
 
460
463
  elif item_id == 'num':
461
464
  num_value = item_data['name']
@@ -463,30 +466,30 @@ def item_from_dict(item_dict: dict) -> Any:
463
466
  num_value = float(item_data['name'])
464
467
  if num_value % 1 == 0:
465
468
  num_value = int(num_value)
466
- return Number(num_value)
467
- return Number(num_value)
469
+ return Number(num_value, item_slot)
470
+ return Number(num_value, item_slot)
468
471
 
469
472
  elif item_id == 'loc':
470
473
  item_loc = item_data['loc']
471
- return Location(item_loc['x'], item_loc['y'], item_loc['z'], item_loc['pitch'], item_loc['yaw'])
474
+ return Location(item_loc['x'], item_loc['y'], item_loc['z'], item_loc['pitch'], item_loc['yaw'], item_slot)
472
475
 
473
476
  elif item_id == 'var':
474
- return Variable(item_data['name'], item_data['scope'])
477
+ return Variable(item_data['name'], item_data['scope'], item_slot)
475
478
 
476
479
  elif item_id == 'snd':
477
- return Sound(item_data['sound'], item_data['pitch'], item_data['vol'])
480
+ return Sound(item_data['sound'], item_data['pitch'], item_data['vol'], item_slot)
478
481
 
479
482
  elif item_id == 'part':
480
- return Particle(item_data)
483
+ return Particle(item_data, item_slot)
481
484
 
482
485
  elif item_id == 'pot':
483
- return Potion(item_data['pot'], item_data['dur'], item_data['amp'])
486
+ return Potion(item_data['pot'], item_data['dur'], item_data['amp'], item_slot)
484
487
 
485
488
  elif item_id == 'g_val':
486
- return GameValue(item_data['type'], item_data['target'])
489
+ return GameValue(item_data['type'], item_data['target'], item_slot)
487
490
 
488
491
  elif item_id == 'vec':
489
- return Vector(item_data['x'], item_data['y'], item_data['z'])
492
+ return Vector(item_data['x'], item_data['y'], item_data['z'], item_slot)
490
493
 
491
494
  elif item_id == 'pn_el':
492
495
  description = item_data.get('description') or ''
@@ -494,9 +497,10 @@ def item_from_dict(item_dict: dict) -> Any:
494
497
  param_type = ParameterType(PARAMETER_TYPE_LOOKUP.index(item_data['type']))
495
498
  if item_data['optional']:
496
499
  if 'default_value' in item_data:
497
- return Parameter(item_data['name'], param_type, item_data['plural'], True, description, note, item_from_dict(item_data['default_value']))
498
- return Parameter(item_data['name'], param_type, item_data['plural'], True, description, note)
499
- return Parameter(item_data['name'], param_type, item_data['plural'], False, description, note)
500
+ default_value_item = item_from_dict(item_data['default_value'], preserve_item_slots)
501
+ return Parameter(item_data['name'], param_type, item_data['plural'], True, description, note, default_value_item, item_slot)
502
+ return Parameter(item_data['name'], param_type, item_data['plural'], True, description, note, slot=item_slot)
503
+ return Parameter(item_data['name'], param_type, item_data['plural'], False, description, note, slot=item_slot)
500
504
 
501
505
  elif item_id in {'bl_tag', 'hint'}: # Ignore tags and hints
502
506
  return
@@ -244,9 +244,12 @@ class DFTemplate:
244
244
 
245
245
 
246
246
  @staticmethod
247
- def from_code(template_code: str):
247
+ def from_code(template_code: str, preserve_item_slots: bool=False):
248
248
  """
249
249
  Create a template object from an existing template code.
250
+
251
+ :param str template_code: The base64 string to create a template from.
252
+ :param bool preserve_item_slots: If True, the positions of items within chests will be saved.
250
253
  """
251
254
  template_dict = json.loads(df_decode(template_code))
252
255
  codeblocks: list[CodeBlock] = []
@@ -258,7 +261,7 @@ class DFTemplate:
258
261
  if item_dict['item'].get('id') == 'bl_tag':
259
262
  tag_data = item_dict['item']['data']
260
263
  block_tags[tag_data['tag']] = tag_data['option']
261
- parsed_item = item_from_dict(item_dict['item'])
264
+ parsed_item = item_from_dict(item_dict, preserve_item_slots)
262
265
  if parsed_item is not None:
263
266
  block_args.append(parsed_item)
264
267
  block_target = Target(TARGETS.index(block_dict['target'])) if 'target' in block_dict else DEFAULT_TARGET
@@ -329,7 +332,7 @@ class DFTemplate:
329
332
  return template_item.send_to_minecraft()
330
333
 
331
334
 
332
- def generate_script(self, output_path: str, indent_size: int=4, literal_shorthand: bool=True, var_shorthand: bool=False):
335
+ def generate_script(self, output_path: str, indent_size: int=4, literal_shorthand: bool=True, var_shorthand: bool=False, preserve_slots: bool=False):
333
336
  """
334
337
  Generate an equivalent python script for this template.
335
338
 
@@ -337,8 +340,9 @@ class DFTemplate:
337
340
  :param int indent_size: The multiple of spaces to add when indenting lines.
338
341
  :param bool literal_shorthand: If True, `text` and `num` items will be written as strings and ints respectively.
339
342
  :param bool var_shorthand: If True, all variables will be written using variable shorthand.
343
+ :param bool preserve_slots: If True, the positions of items within chests will be saved.
340
344
  """
341
- flags = GeneratorFlags(indent_size, literal_shorthand, var_shorthand)
345
+ flags = GeneratorFlags(indent_size, literal_shorthand, var_shorthand, preserve_slots)
342
346
  with open(output_path, 'w', encoding='utf-8') as f:
343
347
  f.write(generate_script(self, flags))
344
348
 
@@ -37,39 +37,43 @@ class GeneratorFlags:
37
37
  indent_size: int
38
38
  literal_shorthand: bool
39
39
  var_shorthand: bool
40
+ preserve_slots: bool
40
41
 
41
42
 
42
- def item_to_string(class_name: str, i: Item):
43
+ def item_to_string(class_name: str, i: Item, slot_argument: str):
43
44
  i.nbt.pop('DF_NBT', None)
44
45
  stripped_id = i.get_id().replace('minecraft:', '')
45
46
  if set(i.nbt.keys()) == {'id', 'count'}:
46
47
  if i.get_count() == 1:
47
- return f'{class_name}("{stripped_id}")'
48
- return f'{class_name}("{stripped_id}", {i.get_count()})'
48
+ return f'{class_name}("{stripped_id}"{slot_argument})'
49
+ return f'{class_name}("{stripped_id}", {i.get_count()}{slot_argument})'
49
50
  return f'{class_name}.from_snbt("""{i.get_snbt()}""")'
50
51
 
51
52
 
52
53
  def argument_item_to_string(flags: GeneratorFlags, arg_item: object) -> str:
53
54
  class_name = arg_item.__class__.__name__
55
+ has_slot = arg_item.slot is not None and flags.preserve_slots
56
+ slot_argument = f', slot={arg_item.slot}' if has_slot else ''
57
+
54
58
  if isinstance(arg_item, Item):
55
- return item_to_string(class_name, arg_item)
59
+ return item_to_string(class_name, arg_item, slot_argument)
56
60
 
57
61
  if isinstance(arg_item, String):
58
62
  value = arg_item.value.replace('\n', '\\n')
59
- if flags.literal_shorthand:
63
+ if not has_slot and flags.literal_shorthand:
60
64
  return f'"{value}"'
61
- return f'{class_name}("{value}")'
65
+ return f'{class_name}("{value}"{slot_argument})'
62
66
 
63
67
  if isinstance(arg_item, Text):
64
68
  value = arg_item.value.replace('\n', '\\n')
65
- return f'{class_name}("{value}")'
69
+ return f'{class_name}("{value}"{slot_argument})'
66
70
 
67
71
  if isinstance(arg_item, Number):
68
72
  if not re.match(NUMBER_REGEX, str(arg_item.value)):
69
- return f'{class_name}("{arg_item.value}")'
70
- if flags.literal_shorthand:
73
+ return f'{class_name}("{arg_item.value}"{slot_argument})'
74
+ if not has_slot and flags.literal_shorthand:
71
75
  return str(arg_item.value)
72
- return f'{class_name}({arg_item.value})'
76
+ return f'{class_name}({arg_item.value}{slot_argument})'
73
77
 
74
78
  if isinstance(arg_item, Location):
75
79
  loc_components = [arg_item.x, arg_item.y, arg_item.z]
@@ -77,28 +81,28 @@ def argument_item_to_string(flags: GeneratorFlags, arg_item: object) -> str:
77
81
  loc_components.append(arg_item.pitch)
78
82
  if arg_item.yaw != 0:
79
83
  loc_components.append(arg_item.yaw)
80
- return f'{class_name}({", ".join(str(c) for c in loc_components)})'
84
+ return f'{class_name}({", ".join(str(c) for c in loc_components)}{slot_argument})'
81
85
 
82
86
  if isinstance(arg_item, Variable):
83
- if flags.var_shorthand:
87
+ if not has_slot and flags.var_shorthand:
84
88
  return f'"${VAR_SCOPES[arg_item.scope]} {arg_item.name}"'
85
89
  if arg_item.scope == 'unsaved':
86
- return f'{class_name}("{arg_item.name}")'
87
- return f'{class_name}("{arg_item.name}", "{arg_item.scope}")'
90
+ return f'{class_name}("{arg_item.name}"{slot_argument})'
91
+ return f'{class_name}("{arg_item.name}", "{arg_item.scope}"{slot_argument})'
88
92
 
89
93
  if isinstance(arg_item, Sound):
90
- return f'{class_name}("{arg_item.name}", {arg_item.pitch}, {arg_item.vol})'
94
+ return f'{class_name}("{arg_item.name}", {arg_item.pitch}, {arg_item.vol}{slot_argument})'
91
95
 
92
96
  if isinstance(arg_item, Particle):
93
97
  return f'{class_name}({arg_item.particle_data})'
94
98
 
95
99
  if isinstance(arg_item, Potion):
96
- return f'{class_name}("{arg_item.name}", {arg_item.dur}, {arg_item.amp})'
100
+ return f'{class_name}("{arg_item.name}", {arg_item.dur}, {arg_item.amp}{slot_argument})'
97
101
 
98
102
  if isinstance(arg_item, GameValue):
99
103
  if arg_item.target == 'Default':
100
- return f'{class_name}("{arg_item.name}")'
101
- return f'{class_name}("{arg_item.name}", "{arg_item.target}")'
104
+ return f'{class_name}("{arg_item.name}"{slot_argument})'
105
+ return f'{class_name}("{arg_item.name}", "{arg_item.target}"{slot_argument})'
102
106
 
103
107
  if isinstance(arg_item, Parameter):
104
108
  param_type_class_name = arg_item.param_type.__class__.__name__
@@ -113,10 +117,10 @@ def argument_item_to_string(flags: GeneratorFlags, arg_item: object) -> str:
113
117
  param_args.append(f'description="{arg_item.description}"')
114
118
  if arg_item.note:
115
119
  param_args.append(f'note="{arg_item.note}"')
116
- return f'{class_name}({", ".join(param_args)})'
120
+ return f'{class_name}({", ".join(param_args)}{slot_argument})'
117
121
 
118
122
  if isinstance(arg_item, Vector):
119
- return f'{class_name}({arg_item.x}, {arg_item.y}, {arg_item.z})'
123
+ return f'{class_name}({arg_item.x}, {arg_item.y}, {arg_item.z}{slot_argument})'
120
124
 
121
125
 
122
126
  def add_script_line(flags: GeneratorFlags, script_lines: list[str], indent_level: int, line: str, add_comma: bool=True):
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "dfpyre"
3
- version = "0.8.9"
3
+ version = "0.8.10"
4
4
  description = "A package for creating and modifying code templates for the DiamondFire Minecraft server."
5
5
  authors = ["Amp"]
6
6
  readme = "README.md"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes