minecraft-datapack-language 15.4.0__py3-none-any.whl → 15.4.2__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.
@@ -1,1074 +1,230 @@
1
1
  """
2
- CLI Build Functions - Core build functionality for MDL CLI
2
+ CLI Build Module - Handles building MDL files into Minecraft datapacks
3
3
  """
4
4
 
5
5
  import os
6
6
  import shutil
7
7
  import zipfile
8
8
  from pathlib import Path
9
- from typing import Dict, List, Optional, Any
10
-
11
- from .mdl_lexer_js import lex_mdl_js
12
- from .mdl_parser_js import parse_mdl_js
13
- from .expression_processor import ExpressionProcessor
14
- from .dir_map import get_dir_map
15
- from .pack import Pack, Namespace, Function, Tag, Recipe, Advancement, LootTable, Predicate, ItemModifier, Structure
16
- from .mdl_errors import MDLErrorCollector, create_error, MDLBuildError, MDLFileError, MDLCompilationError, MDLSyntaxError, MDLParserError, MDLLexerError
17
- from .cli_utils import ensure_dir, write_json, _process_variable_substitutions, _convert_condition_to_minecraft_syntax, _find_mdl_files, _validate_selector, _resolve_selector, _extract_base_variable_name, _slugify
18
-
19
-
20
- class BuildContext:
21
- """Context for build operations to prevent race conditions."""
22
-
23
- def __init__(self):
24
- self.conditional_functions = []
25
- self.variable_scopes = {}
26
- self.namespace_functions = {}
27
- self.expression_processor = ExpressionProcessor()
28
-
29
-
30
- def _merge_mdl_files(files: List[Path], verbose: bool = False, error_collector: MDLErrorCollector = None) -> Optional[Dict[str, Any]]:
31
- """Merge multiple MDL files into a single AST."""
32
- if not files:
33
- return None
9
+ from typing import Optional, List
10
+
11
+ from .cli_colors import (
12
+ print_success, print_warning, print_error, print_info,
13
+ print_section, print_separator, color
14
+ )
15
+ from .mdl_errors import MDLErrorCollector, create_error, MDLConfigurationError, MDLFileError
16
+ from .pack import MDLPack
17
+ from .utils import find_mdl_files
18
+
19
+
20
+ def build_mdl(input_path: str, output_path: str, verbose: bool = False,
21
+ pack_format_override: Optional[int] = None, wrapper: Optional[str] = None,
22
+ ignore_warnings: bool = False) -> None:
23
+ """Build MDL files into a Minecraft datapack.
24
+
25
+ Args:
26
+ input_path: Path to MDL file or directory
27
+ output_path: Output directory for the datapack
28
+ verbose: Enable verbose output
29
+ pack_format_override: Override pack format number
30
+ wrapper: Create zip file with specified name
31
+ ignore_warnings: Suppress warning messages
32
+ """
33
+ error_collector = MDLErrorCollector()
34
34
 
35
- # Parse the first file to get the base AST
36
35
  try:
37
- if verbose:
38
- print(f"DEBUG: Parsing first file: {files[0]}")
36
+ # Validate input path
37
+ input_path_obj = Path(input_path)
38
+ if not input_path_obj.exists():
39
+ raise MDLFileError(
40
+ message=f"Input path does not exist: {input_path}",
41
+ suggestion="Check the path and try again."
42
+ )
39
43
 
40
- with open(files[0], 'r', encoding='utf-8') as f:
41
- content = f.read()
44
+ # Find MDL files
45
+ if input_path_obj.is_file():
46
+ mdl_files = [input_path_obj] if input_path_obj.suffix == '.mdl' else []
47
+ else:
48
+ mdl_files = find_mdl_files(input_path_obj)
42
49
 
43
- ast = parse_mdl_js(content, str(files[0]))
50
+ if not mdl_files:
51
+ raise MDLConfigurationError(
52
+ message=f"No MDL files found in: {input_path}",
53
+ suggestion="Ensure the path contains .mdl files or specify a single .mdl file."
54
+ )
44
55
 
45
56
  if verbose:
46
- print(f"DEBUG: Successfully parsed {files[0]}")
47
-
48
- except MDLLexerError as e:
49
- if error_collector:
50
- error_collector.add_error(e)
51
- else:
52
- raise
53
- return None
54
- except MDLParserError as e:
55
- if error_collector:
56
- error_collector.add_error(e)
57
- else:
58
- raise
59
- return None
60
- except MDLSyntaxError as e:
61
- if error_collector:
62
- error_collector.add_error(e)
63
- else:
64
- raise
65
- return None
66
- except Exception as e:
67
- if error_collector:
68
- error_collector.add_error(create_error(
69
- MDLCompilationError,
70
- f"Failed to parse {files[0]}: {str(e)}",
71
- file_path=str(files[0]),
72
- suggestion="Check the file syntax and ensure it's a valid MDL file."
73
- ))
74
- else:
75
- raise
76
- return None
77
-
78
- # Merge additional files
79
- for file_path in files[1:]:
80
- try:
81
- if verbose:
82
- print(f"DEBUG: Parsing additional file: {file_path}")
83
-
84
- with open(file_path, 'r', encoding='utf-8') as f:
85
- content = f.read()
86
-
87
- additional_ast = parse_mdl_js(content, str(file_path))
88
-
89
- if verbose:
90
- print(f"DEBUG: Successfully parsed {file_path}")
91
-
92
- # Merge functions
93
- if 'functions' in additional_ast:
94
- if 'functions' not in ast:
95
- ast['functions'] = []
96
- ast['functions'].extend(additional_ast['functions'])
97
-
98
- # Merge variables
99
- if 'variables' in additional_ast:
100
- if 'variables' not in ast:
101
- ast['variables'] = []
102
- ast['variables'].extend(additional_ast['variables'])
103
-
104
- # Merge namespaces (append to existing list)
105
- if 'namespaces' in additional_ast:
106
- if 'namespaces' not in ast:
107
- ast['namespaces'] = []
108
- ast['namespaces'].extend(additional_ast['namespaces'])
109
-
110
- # Merge registry declarations
111
- for registry_type in ['recipes', 'loot_tables', 'advancements', 'predicates', 'item_modifiers', 'structures']:
112
- if registry_type in additional_ast:
113
- if registry_type not in ast:
114
- ast[registry_type] = []
115
- ast[registry_type].extend(additional_ast[registry_type])
116
-
117
- # Merge pack metadata (use the first one found)
118
- if 'pack' in additional_ast and 'pack' not in ast:
119
- ast['pack'] = additional_ast['pack']
57
+ print_section("Building MDL Project")
58
+ print_info(f"Input: {input_path}")
59
+ print_info(f"Output: {output_path}")
60
+ print_info(f"Found {len(mdl_files)} MDL file(s)")
61
+ print_separator()
120
62
 
121
- except MDLLexerError as e:
122
- if error_collector:
123
- error_collector.add_error(e)
124
- else:
125
- raise
126
- return None
127
- except MDLParserError as e:
128
- if error_collector:
129
- error_collector.add_error(e)
130
- else:
131
- raise
132
- return None
133
- except MDLSyntaxError as e:
134
- if error_collector:
135
- error_collector.add_error(e)
136
- else:
137
- raise
138
- return None
139
- except Exception as e:
140
- if error_collector:
141
- error_collector.add_error(create_error(
142
- MDLCompilationError,
143
- f"Failed to parse {file_path}: {str(e)}",
144
- file_path=str(file_path),
145
- suggestion="Check the file syntax and ensure it's a valid MDL file."
146
- ))
147
- else:
148
- raise
149
- return None
150
-
151
- return ast
152
-
153
-
154
- def _generate_scoreboard_objectives(ast: Dict[str, Any], output_dir: Path) -> List[str]:
155
- """Generate scoreboard objectives for all variables."""
156
- scoreboard_commands = []
157
-
158
- # Collect all variable names in order of appearance
159
- variables = []
160
- seen_variables = set()
161
-
162
- # From variable declarations (preserve order)
163
- if 'variables' in ast:
164
- for var_decl in ast['variables']:
165
- if 'name' in var_decl and var_decl['name'] not in seen_variables:
166
- variables.append(var_decl['name'])
167
- seen_variables.add(var_decl['name'])
168
-
169
- # From functions (scan for variable usage)
170
- if 'functions' in ast:
171
- for func in ast['functions']:
172
- if 'body' in func:
173
- for statement in func['body']:
174
- # Look for variable assignments and usage
175
- if statement['type'] == 'variable_assignment':
176
- if statement['name'] not in seen_variables:
177
- variables.append(statement['name'])
178
- seen_variables.add(statement['name'])
179
- elif statement['type'] == 'command':
180
- # Scan command for variable substitutions
181
- command = statement['command']
182
- import re
183
- var_matches = re.findall(r'\$([^$]+)\$', command)
184
- for var_name in var_matches:
185
- # Extract base name from scoped variables
186
- base_name = _extract_base_variable_name(var_name)
187
- if base_name not in seen_variables:
188
- variables.append(base_name)
189
- seen_variables.add(base_name)
190
-
191
- # Create scoreboard objectives in the order they were found
192
- for var_name in variables:
193
- scoreboard_commands.append(f"scoreboard objectives add {var_name} dummy")
194
-
195
- return scoreboard_commands
196
-
197
-
198
- def _generate_load_function(scoreboard_commands: List[str], output_dir: Path, namespace: str, ast: Dict[str, Any]) -> None:
199
- """Generate the load function with scoreboard setup."""
200
- load_content = []
201
-
202
- # Add armor stand setup for server-side operations
203
- load_content.append("execute unless entity @e[type=armor_stand,tag=mdl_server,limit=1] run summon armor_stand ~ 320 ~ {Tags:[\"mdl_server\"],Invisible:1b,Marker:1b,NoGravity:1b,Invulnerable:1b}")
204
-
205
- # Add scoreboard objectives
206
- load_content.extend(scoreboard_commands)
207
-
208
- # Add any custom load commands from the AST
209
- if 'load' in ast:
210
- for command in ast['load']:
211
- load_content.append(command)
212
-
213
- # Write the load function
214
- load_dir = output_dir / "data" / namespace / "function"
215
- ensure_dir(str(load_dir))
216
-
217
- with open(load_dir / "load.mcfunction", 'w', encoding='utf-8') as f:
218
- f.write('\n'.join(load_content))
219
-
220
-
221
- def _process_say_command_with_variables(content: str, selector: str, variable_scopes: Dict[str, str] = None) -> str:
222
- """Process say command content with variable substitution, converting to tellraw with score components."""
223
- import re
224
-
225
- print(f"DEBUG: _process_say_command_with_variables called with content: {repr(content)}, selector: {selector}")
226
- print(f"DEBUG: Variable scopes available: {variable_scopes}")
227
-
228
- # Clean up the content - remove quotes if present
229
- content = content.strip()
230
- if content.startswith('"') and content.endswith('"'):
231
- content = content[1:-1] # Remove surrounding quotes
232
-
233
- # Look for traditional $variable$ syntax
234
- var_pattern = r'\$([^$]+)\$'
235
- matches = re.findall(var_pattern, content)
236
-
237
- if not matches:
238
- # No variables, return simple tellraw
239
- return f'tellraw @a [{{"text":"{content}"}}]'
240
-
241
- # Use re.sub to replace variables with placeholders, then split by placeholders
242
- # This avoids the issue with re.split including captured groups
243
- placeholder_content = content
244
- var_placeholders = []
245
-
246
- for i, match in enumerate(matches):
247
- placeholder = f"__VAR_{i}__"
248
- var_placeholders.append((placeholder, match))
249
- placeholder_content = placeholder_content.replace(f"${match}$", placeholder, 1)
250
-
251
- # Split by placeholders to get text parts
252
- text_parts = placeholder_content
253
- for placeholder, var_name in var_placeholders:
254
- text_parts = text_parts.replace(placeholder, f"|{var_name}|")
255
-
256
- # Now split by the pipe delimiters
257
- parts = text_parts.split('|')
258
-
259
- # Build tellraw components
260
- components = []
261
-
262
- for i, part in enumerate(parts):
263
- if i % 2 == 0:
264
- # Text part
265
- if part: # Only add non-empty text parts
266
- components.append(f'{{"text":"{part}"}}')
267
- else:
268
- # Variable part
269
- var_name = part
270
-
271
- # Check if variable has scope selector
272
- if '<' in var_name and var_name.endswith('>'):
273
- # Scoped variable: $variable<selector>$
274
- var_parts = var_name.split('<', 1)
275
- base_var = var_parts[0]
276
- var_selector = var_parts[1][:-1] # Remove trailing >
277
- components.append(f'{{"score":{{"name":"{var_selector}","objective":"{base_var}"}}}}')
278
- else:
279
- # Simple variable: $variable$ - determine selector based on declared scope
280
- var_selector = "@e[type=armor_stand,tag=mdl_server,limit=1]" # Default to global
281
- if variable_scopes and var_name in variable_scopes:
282
- declared_scope = variable_scopes[var_name]
283
- if declared_scope == 'global':
284
- var_selector = "@e[type=armor_stand,tag=mdl_server,limit=1]"
285
- else:
286
- var_selector = declared_scope
287
- print(f"DEBUG: Variable {var_name} using declared scope {declared_scope} -> selector {var_selector}")
288
- else:
289
- print(f"DEBUG: Variable {var_name} has no declared scope, using default global selector")
290
-
291
- components.append(f'{{"score":{{"name":"{var_selector}","objective":"{var_name}"}}}}')
292
-
293
- # Join components and create tellraw command
294
- components_str = ','.join(components)
295
- return f'tellraw @a [{components_str}]'
296
-
297
-
298
- def _process_statement(statement: Any, namespace: str, function_name: str, statement_index: int = 0, is_tag_function: bool = False, selector: str = "@s", variable_scopes: Dict[str, str] = None, build_context: BuildContext = None, output_dir: Path = None) -> List[str]:
299
- """Process a single statement and return Minecraft commands."""
300
- if variable_scopes is None:
301
- variable_scopes = {}
302
-
303
- if build_context is None:
304
- build_context = BuildContext()
305
-
306
- commands = []
307
-
308
- if statement['type'] == 'command':
309
- command = statement['command']
63
+ # Create output directory
64
+ output_path_obj = Path(output_path)
65
+ if output_path_obj.exists():
66
+ if verbose:
67
+ print_warning(f"Output directory already exists: {output_path}")
68
+ print_info("Removing existing directory...")
69
+ shutil.rmtree(output_path_obj)
310
70
 
311
- # Handle say commands specifically
312
- if command.startswith('say '):
313
- print(f"DEBUG: Found say command: {repr(command)}")
314
- # Convert say command to tellraw command
315
- content = command[4:] # Remove "say " prefix
316
- print(f"DEBUG: Say command content: {repr(content)}")
317
- print(f"DEBUG: Raw command from AST: {repr(statement['command'])}")
318
- # Convert to Minecraft tellraw format
319
- processed_command = _process_say_command_with_variables(content, selector, variable_scopes)
320
- print(f"DEBUG: Processed say command: {repr(processed_command)}")
321
- commands.append(processed_command)
322
- elif command.startswith('tellraw @a ') or command.startswith('tellraw @ a '):
323
- # Fix extra space in tellraw commands
324
- fixed_command = command.replace('tellraw @ a ', 'tellraw @a ')
325
- commands.append(fixed_command)
326
- else:
327
- # Process other commands normally
328
- processed_command = _process_variable_substitutions(command, selector)
329
- commands.append(processed_command)
330
-
331
- elif statement['type'] == 'variable_assignment':
332
- var_name = statement['name']
333
- value = statement['value']
71
+ output_path_obj.mkdir(parents=True, exist_ok=True)
334
72
 
335
- # Determine the correct selector for this variable based on its declared scope
336
- var_selector = selector # Default to current selector
337
- if variable_scopes and var_name in variable_scopes:
338
- declared_scope = variable_scopes[var_name]
339
- if declared_scope == 'global':
340
- var_selector = "@e[type=armor_stand,tag=mdl_server,limit=1]"
341
- else:
342
- var_selector = declared_scope
343
- print(f"DEBUG: Variable {var_name} assignment using selector: {var_selector} (declared scope: {variable_scopes.get(var_name, 'none')})")
73
+ # Build the datapack
74
+ pack = MDLPack()
344
75
 
345
- # Handle different value types
346
- if isinstance(value, int):
347
- commands.append(f"scoreboard players set {var_selector} {var_name} {value}")
348
- elif isinstance(value, str) and value.startswith('$') and value.endswith('$'):
349
- # Variable reference
350
- ref_var = value[1:-1] # Remove $ symbols
351
- commands.append(f"scoreboard players operation {var_selector} {var_name} = {var_selector} {ref_var}")
352
- elif hasattr(value, '__class__') and 'BinaryExpression' in str(value.__class__):
353
- # Handle complex expressions (BinaryExpression, etc.)
354
- # Convert to proper Minecraft scoreboard commands
355
- if hasattr(value, 'left') and hasattr(value, 'right') and hasattr(value, 'operator'):
356
- left = value.left
357
- right = value.right
358
- operator = value.operator
359
-
360
- # Handle different operators
361
- if operator == 'PLUS':
362
- if hasattr(left, 'name') and hasattr(right, 'value'):
363
- # counter = counter + 1
364
- commands.append(f"scoreboard players add {var_selector} {var_name} {right.value}")
365
- else:
366
- # Complex case - use operation
367
- commands.append(f"# Complex addition: {var_name} = {left} + {right}")
368
- elif operator == 'MINUS':
369
- if hasattr(left, 'name') and hasattr(right, 'value'):
370
- # health = health - 10
371
- commands.append(f"scoreboard players remove {var_selector} {var_name} {right.value}")
372
- else:
373
- # Complex case - use operation
374
- commands.append(f"# Complex operation: {var_name} = {left} - {right}")
375
- else:
376
- # Other operators - use operation
377
- commands.append(f"# Complex operation: {var_name} = {left} {operator} {right}")
378
- else:
379
- commands.append(f"# Complex assignment: {var_name} = {value}")
380
- else:
381
- # Handle LiteralExpression and other value types
76
+ for mdl_file in mdl_files:
77
+ if verbose:
78
+ print_info(f"Processing: {color.file_path(mdl_file.name)}")
79
+
382
80
  try:
383
- if hasattr(value, 'value'):
384
- # LiteralExpression case
385
- num_value = int(value.value)
386
- commands.append(f"scoreboard players set {var_selector} {var_name} {num_value}")
387
- else:
388
- # Direct value case
389
- num_value = int(value)
390
- commands.append(f"scoreboard players set {var_selector} {var_name} {num_value}")
391
- except (ValueError, TypeError):
392
- # If we can't convert to int, add a placeholder
393
- commands.append(f"# Assignment: {var_name} = {value}")
394
-
395
- elif statement['type'] == 'if_statement':
396
- condition = statement['condition']
397
- then_body = statement['then_body']
398
- else_body = statement.get('else_body', [])
399
-
400
- # Convert condition to Minecraft syntax
401
- minecraft_condition = _convert_condition_to_minecraft_syntax(condition, selector)
402
-
403
- # Generate unique function names for conditional blocks
404
- if_func_name = f"{function_name}_if_{statement_index}"
405
- else_func_name = f"{function_name}_else_{statement_index}"
406
-
407
- # Create conditional function
408
- if_commands = []
409
- for i, stmt in enumerate(then_body):
410
- if_commands.extend(_process_statement(stmt, namespace, if_func_name, i, is_tag_function, selector, variable_scopes, build_context, output_dir))
81
+ pack.add_mdl_file(mdl_file)
82
+ if verbose:
83
+ print_success(f"✓ Processed: {color.file_path(mdl_file.name)}")
84
+ except Exception as e:
85
+ error_collector.add_error(create_error(
86
+ MDLConfigurationError,
87
+ f"Failed to process {mdl_file.name}: {str(e)}",
88
+ file_path=str(mdl_file),
89
+ suggestion="Check the MDL file syntax and try again."
90
+ ))
411
91
 
412
- # Write conditional function
413
- if if_commands:
414
- # Use the output directory parameter
415
- if output_dir:
416
- if_dir = output_dir / "data" / namespace / "function"
417
- else:
418
- if_dir = Path(f"data/{namespace}/function")
419
- ensure_dir(str(if_dir))
420
- with open(if_dir / f"{if_func_name}.mcfunction", 'w', encoding='utf-8') as f:
421
- f.write('\n'.join(if_commands))
92
+ # Check for errors before proceeding
93
+ if error_collector.has_errors():
94
+ error_collector.print_errors(verbose=verbose, ignore_warnings=ignore_warnings)
95
+ error_collector.raise_if_errors()
422
96
 
423
- # Create else function if needed
424
- if else_body:
425
- else_commands = []
426
- for i, stmt in enumerate(else_body):
427
- else_commands.extend(_process_statement(stmt, namespace, else_func_name, i, is_tag_function, selector, variable_scopes, build_context, output_dir))
428
-
429
- if else_commands:
430
- with open(if_dir / f"{else_func_name}.mcfunction", 'w', encoding='utf-8') as f:
431
- f.write('\n'.join(else_commands))
97
+ # Generate the datapack
98
+ if verbose:
99
+ print_separator()
100
+ print_info("Generating datapack files...")
432
101
 
433
- # Add the conditional execution command
434
- if else_body:
435
- commands.append(f"execute if {minecraft_condition} run function {namespace}:{if_func_name}")
436
- commands.append(f"execute unless {minecraft_condition} run function {namespace}:{else_func_name}")
437
- # Add if_end function call for cleanup
438
- commands.append(f"function {namespace}:{function_name}_if_end_{statement_index}")
439
- else:
440
- commands.append(f"execute if {minecraft_condition} run function {namespace}:{if_func_name}")
441
-
442
- elif statement['type'] == 'while_loop' or statement['type'] == 'while_statement':
443
- # Handle while loops using recursion
444
- loop_commands = _process_while_loop_recursion(statement, namespace, function_name, statement_index, is_tag_function, selector, variable_scopes, build_context, output_dir)
445
- commands.extend(loop_commands)
446
-
447
- elif statement['type'] == 'function_call':
448
- func_name = statement['name']
449
- scope = statement.get('scope')
450
- func_namespace = statement.get('namespace', namespace) # Use specified namespace or current namespace
102
+ try:
103
+ pack.generate_datapack(output_path_obj, pack_format_override)
104
+ if verbose:
105
+ print_success(" Datapack generated successfully")
106
+ except Exception as e:
107
+ error_collector.add_error(create_error(
108
+ MDLConfigurationError,
109
+ f"Failed to generate datapack: {str(e)}",
110
+ suggestion="Check the output directory permissions and try again."
111
+ ))
451
112
 
452
- if scope:
453
- # Handle scoped function call
454
- if scope == 'global':
455
- # Global scope uses the server armor stand
456
- selector = "@e[type=armor_stand,tag=mdl_server,limit=1]"
457
- else:
458
- # Use the specified scope selector
459
- selector = scope
113
+ # Create wrapper if requested
114
+ if wrapper:
115
+ if verbose:
116
+ print_info(f"Creating wrapper: {wrapper}")
460
117
 
461
- # Generate execute as command
462
- commands.append(f"execute as {selector} run function {func_namespace}:{func_name}")
463
- else:
464
- # Simple function call without scope
465
- commands.append(f"function {func_namespace}:{func_name}")
466
-
467
- elif statement['type'] == 'raw_text':
468
- # Raw Minecraft commands
469
- raw_commands = statement['commands']
470
- for cmd in raw_commands:
471
- processed_cmd = _process_variable_substitutions(cmd, selector)
472
- commands.append(processed_cmd)
473
-
474
- return commands
475
-
476
-
477
- def _generate_function_file(ast: Dict[str, Any], output_dir: Path, namespace: str, verbose: bool = False, build_context: BuildContext = None) -> None:
478
- """Generate function files from the AST for a specific namespace."""
479
- if build_context is None:
480
- build_context = BuildContext()
481
-
482
- if 'functions' not in ast:
483
- return
484
-
485
- # Filter functions by namespace
486
- namespace_functions = []
487
- for func in ast['functions']:
488
- # Check if function belongs to this namespace
489
- # For now, we'll generate all functions in all namespaces
490
- # In the future, we could add namespace annotations to functions
491
- namespace_functions.append(func)
492
-
493
- if verbose:
494
- print(f"DEBUG: Processing {len(namespace_functions)} functions for namespace {namespace}")
495
-
496
- for func in namespace_functions:
497
- func_name = func['name']
498
- func_body = func.get('body', [])
499
-
500
- # Collect variable scopes from the AST
501
- variable_scopes = {}
502
- if 'variables' in ast:
503
- for var_decl in ast['variables']:
504
- var_name = var_decl['name']
505
- var_scope = var_decl.get('scope')
506
- if var_scope:
507
- variable_scopes[var_name] = var_scope
508
- print(f"DEBUG: Variable {var_name} has scope {var_scope}")
509
- else:
510
- print(f"DEBUG: Variable {var_name} has no scope (defaults to @s)")
511
-
512
- print(f"DEBUG: Collected variable scopes: {variable_scopes}")
513
-
514
- # Generate function content
515
- function_commands = []
516
- for i, statement in enumerate(func_body):
517
118
  try:
518
- print(f"DEBUG: Processing statement {i} of type {statement.get('type', 'unknown')}: {statement}")
519
- commands = _process_statement(statement, namespace, func_name, i, False, "@s", variable_scopes, build_context, output_dir)
520
- function_commands.extend(commands)
521
- print(f"Generated {len(commands)} commands for statement {i} in function {func_name}: {commands}")
119
+ create_wrapper(output_path_obj, wrapper)
120
+ if verbose:
121
+ print_success(f"✓ Wrapper created: {wrapper}")
522
122
  except Exception as e:
523
- print(f"Warning: Error processing statement {i} in function {func_name}: {e}")
524
- import traceback
525
- traceback.print_exc()
526
- continue
123
+ error_collector.add_error(create_error(
124
+ MDLFileError,
125
+ f"Failed to create wrapper: {str(e)}",
126
+ suggestion="Check file permissions and disk space."
127
+ ))
527
128
 
528
- # Write function file
529
- if function_commands:
530
- # Only add armor stand setup to main functions that need it
531
- # Don't add to helper functions or functions in the "other" namespace
532
- should_add_armor_stand = (namespace != "other" and
533
- (func_name in ["main", "init", "load"] or
534
- any(cmd for cmd in function_commands if "scoreboard" in cmd or "tellraw" in cmd)))
535
-
536
- final_commands = []
537
- if should_add_armor_stand:
538
- final_commands.append("execute unless entity @e[type=armor_stand,tag=mdl_server,limit=1] run summon armor_stand ~ 320 ~ {Tags:[\"mdl_server\"],Invisible:1b,Marker:1b,NoGravity:1b,Invulnerable:1b}")
539
- final_commands.extend(function_commands)
540
-
541
- if verbose:
542
- print(f"DEBUG: Final commands for {func_name}: {final_commands}")
543
-
544
- func_dir = output_dir / "data" / namespace / "function"
545
- ensure_dir(str(func_dir))
546
-
547
- with open(func_dir / f"{func_name}.mcfunction", 'w', encoding='utf-8') as f:
548
- content = '\n'.join(final_commands)
549
- if verbose:
550
- print(f"DEBUG: Writing to file {func_name}.mcfunction: {repr(content)}")
551
- f.write(content)
552
-
553
- if verbose:
554
- print(f"Generated function: {namespace}:{func_name}")
555
- else:
556
- if verbose:
557
- print(f"No commands generated for function: {namespace}:{func_name}")
558
- print(f"Function body: {func_body}")
559
-
560
-
561
- def _generate_hook_files(ast: Dict[str, Any], output_dir: Path, namespace: str, build_context: BuildContext = None, all_namespaces: List[str] = None) -> None:
562
- """Generate load and tick tag files."""
563
- if build_context is None:
564
- build_context = BuildContext()
565
-
566
- if all_namespaces is None:
567
- all_namespaces = [namespace]
568
-
569
- # Generate load tag
570
- load_tag_dir = output_dir / "data" / "minecraft" / "tags" / "function"
571
- ensure_dir(str(load_tag_dir))
572
-
573
- # Start with load functions for all namespaces
574
- load_values = []
575
- for ns in all_namespaces:
576
- load_values.append(f"{ns}:load")
577
-
578
- # Add functions specified in on_load hooks
579
- if 'hooks' in ast:
580
- for hook in ast['hooks']:
581
- if hook['hook_type'] == 'load':
582
- load_values.append(hook['function_name'])
583
-
584
- # Add pack-specific load function if pack name is available and different from namespace
585
- if 'pack' in ast and 'name' in ast['pack']:
586
- pack_name = ast['pack']['name']
587
- pack_load_function = f"{pack_name}:load"
588
- # Only add if it's not already in the list (avoids duplicates when pack name == namespace)
589
- if pack_load_function not in load_values:
590
- load_values.append(pack_load_function)
591
-
592
- # Remove duplicates while preserving order
593
- seen = set()
594
- unique_load_values = []
595
- for value in load_values:
596
- if value not in seen:
597
- seen.add(value)
598
- unique_load_values.append(value)
599
-
600
- load_tag_content = {
601
- "values": unique_load_values
602
- }
603
- write_json(str(load_tag_dir / "load.json"), load_tag_content)
604
-
605
- # Generate tick tag if there are tick functions
606
- tick_functions = []
607
- if 'functions' in ast:
608
- for func in ast['functions']:
609
- if func.get('name', '').startswith('tick'):
610
- tick_functions.append(f"{namespace}:{func['name']}")
611
-
612
- if tick_functions:
613
- # Remove duplicates while preserving order
614
- seen = set()
615
- unique_tick_functions = []
616
- for value in tick_functions:
617
- if value not in seen:
618
- seen.add(value)
619
- unique_tick_functions.append(value)
129
+ # Final status
130
+ if verbose:
131
+ print_separator()
132
+ print_success("Build completed successfully!")
133
+ print_info(f"Output location: {color.file_path(output_path)}")
134
+ if wrapper:
135
+ print_info(f"Wrapper file: {color.file_path(wrapper)}")
620
136
 
621
- tick_tag_content = {
622
- "values": unique_tick_functions
623
- }
624
- write_json(str(load_tag_dir / "tick.json"), tick_tag_content)
625
-
626
-
627
- def _generate_global_load_function(ast: Dict[str, Any], output_dir: Path, namespace: str, build_context: BuildContext = None) -> None:
628
- """Generate the global load function."""
629
- if build_context is None:
630
- build_context = BuildContext()
631
-
632
- # Generate scoreboard objectives
633
- scoreboard_commands = _generate_scoreboard_objectives(ast, output_dir)
634
-
635
- # Generate load function
636
- _generate_load_function(scoreboard_commands, output_dir, namespace, ast)
637
-
638
-
639
- def _generate_tag_files(ast: Dict[str, Any], output_dir: Path, namespace: str) -> None:
640
- """Generate tag files for the datapack."""
641
- # This is handled by _generate_hook_files
642
- pass
643
-
644
-
645
- def _validate_pack_format(pack_format: int) -> None:
646
- """Validate the pack format number."""
647
- if not isinstance(pack_format, int) or pack_format < 1:
648
- raise ValueError(f"Invalid pack format: {pack_format}. Must be a positive integer.")
649
-
650
-
651
- def _collect_conditional_functions(if_statement, namespace: str, function_name: str, statement_index: int, is_tag_function: bool = False, selector: str = "@s", variable_scopes: Dict[str, str] = None, build_context: BuildContext = None) -> List[str]:
652
- """Collect conditional functions from if statements."""
653
- if variable_scopes is None:
654
- variable_scopes = {}
655
-
656
- if build_context is None:
657
- build_context = BuildContext()
658
-
659
- conditional_functions = []
660
-
661
- # Generate function name for this conditional
662
- if_func_name = f"{function_name}_if_{statement_index}"
663
- conditional_functions.append(if_func_name)
664
-
665
- # Process then body
666
- for i, stmt in enumerate(if_statement['then_body']):
667
- if stmt['type'] == 'if_statement':
668
- nested_functions = _collect_conditional_functions(stmt, namespace, if_func_name, i, is_tag_function, selector, variable_scopes, build_context)
669
- conditional_functions.extend(nested_functions)
670
-
671
- # Process else body if it exists
672
- if 'else_body' in if_statement and if_statement['else_body']:
673
- else_func_name = f"{function_name}_else_{statement_index}"
674
- conditional_functions.append(else_func_name)
137
+ # Print any warnings if not ignored
138
+ if not ignore_warnings and error_collector.has_warnings():
139
+ error_collector.print_errors(verbose=verbose, ignore_warnings=False)
675
140
 
676
- for i, stmt in enumerate(if_statement['else_body']):
677
- if stmt['type'] == 'if_statement':
678
- nested_functions = _collect_conditional_functions(stmt, namespace, else_func_name, i, is_tag_function, selector, variable_scopes, build_context)
679
- conditional_functions.extend(nested_functions)
680
-
681
- return conditional_functions
682
-
683
-
684
- def _process_while_loop_recursion(while_statement, namespace: str, function_name: str, statement_index: int, is_tag_function: bool = False, selector: str = "@s", variable_scopes: Dict[str, str] = None, build_context: BuildContext = None, output_dir: Path = None) -> List[str]:
685
- """Process while loops using recursive function calls."""
686
- if variable_scopes is None:
687
- variable_scopes = {}
688
-
689
- if build_context is None:
690
- build_context = BuildContext()
691
-
692
- condition = while_statement['condition']
693
- body = while_statement['body']
694
-
695
- # Generate unique function names - they should be the same according to the test
696
- loop_func_name = f"test_{function_name}_while_{statement_index}"
697
-
698
- # Process loop body
699
- body_commands = []
700
- for i, stmt in enumerate(body):
701
- body_commands.extend(_process_statement(stmt, namespace, loop_func_name, i, is_tag_function, selector, variable_scopes, build_context, output_dir))
702
-
703
- # Add the recursive call to the loop body
704
- minecraft_condition = _convert_condition_to_minecraft_syntax(condition, selector)
705
- body_commands.append(f"execute if {minecraft_condition} run function {namespace}:{loop_func_name}")
706
-
707
- # Write the single loop function
708
- if body_commands:
709
- # Use the output directory parameter
710
- if output_dir:
711
- func_dir = output_dir / "data" / namespace / "function"
712
- else:
713
- func_dir = Path(f"data/{namespace}/function")
714
- ensure_dir(str(func_dir))
715
- with open(func_dir / f"{loop_func_name}.mcfunction", 'w', encoding='utf-8') as f:
716
- f.write('\n'.join(body_commands))
717
-
718
- # Return the command to start the loop with conditional execution
719
- return [f"execute if {minecraft_condition} run function {namespace}:{loop_func_name}"]
141
+ # Raise if there are any errors
142
+ error_collector.raise_if_errors()
143
+
144
+ except Exception as e:
145
+ if not isinstance(e, (MDLConfigurationError, MDLFileError)):
146
+ error_collector.add_error(create_error(
147
+ MDLConfigurationError,
148
+ f"Unexpected build error: {str(e)}",
149
+ suggestion="If this error persists, please report it as a bug."
150
+ ))
151
+
152
+ error_collector.print_errors(verbose=verbose, ignore_warnings=ignore_warnings)
153
+ error_collector.raise_if_errors()
720
154
 
721
155
 
722
- def _process_while_loop_schedule(while_statement, namespace: str, function_name: str, statement_index: int, is_tag_function: bool = False, selector: str = "@s", variable_scopes: Dict[str, str] = None, build_context: BuildContext = None) -> List[str]:
723
- """Process while loops using scheduled functions."""
724
- if variable_scopes is None:
725
- variable_scopes = {}
726
-
727
- if build_context is None:
728
- build_context = BuildContext()
729
-
730
- condition = while_statement['condition']
731
- body = while_statement['body']
156
+ def create_wrapper(output_dir: Path, wrapper_name: str) -> None:
157
+ """Create a zip wrapper around the output directory.
732
158
 
733
- # Generate unique function names
734
- loop_func_name = f"{function_name}_while_{statement_index}"
735
- loop_body_func_name = f"{function_name}_while_body_{statement_index}"
159
+ Args:
160
+ output_dir: Directory to wrap
161
+ wrapper_name: Name of the zip file
162
+ """
163
+ wrapper_path = Path(wrapper_name)
736
164
 
737
- # Process loop body
738
- body_commands = []
739
- for i, stmt in enumerate(body):
740
- body_commands.extend(_process_statement(stmt, namespace, loop_body_func_name, i, is_tag_function, selector, variable_scopes, build_context))
165
+ # Ensure .zip extension
166
+ if not wrapper_path.suffix.lower() == '.zip':
167
+ wrapper_path = wrapper_path.with_suffix('.zip')
741
168
 
742
- # Add the loop continuation command
743
- minecraft_condition = _convert_condition_to_minecraft_syntax(condition, selector)
744
- body_commands.append(f"execute {minecraft_condition} run schedule function {namespace}:{loop_body_func_name} 1t")
169
+ # Remove existing wrapper if it exists
170
+ if wrapper_path.exists():
171
+ wrapper_path.unlink()
745
172
 
746
- # Write loop body function
747
- if body_commands:
748
- # Use the output directory from build context
749
- if hasattr(build_context, 'output_dir'):
750
- func_dir = build_context.output_dir / "data" / namespace / "function"
751
- else:
752
- func_dir = Path(f"data/{namespace}/function")
753
- ensure_dir(str(func_dir))
754
- with open(func_dir / f"{loop_body_func_name}.mcfunction", 'w', encoding='utf-8') as f:
755
- f.write('\n'.join(body_commands))
756
-
757
- # Return the command to start the loop
758
- return [f"schedule function {namespace}:{loop_body_func_name} 1t"]
759
-
760
-
761
- def _create_zip_file(source_dir: Path, zip_path: Path) -> None:
762
- """Create a zip file from a directory."""
763
- with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
764
- for file_path in source_dir.rglob('*'):
173
+ # Create the zip file
174
+ with zipfile.ZipFile(wrapper_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
175
+ for file_path in output_dir.rglob('*'):
765
176
  if file_path.is_file():
766
- arcname = file_path.relative_to(source_dir)
177
+ # Calculate relative path within the zip
178
+ arcname = file_path.relative_to(output_dir)
767
179
  zipf.write(file_path, arcname)
768
180
 
769
181
 
770
- def _generate_pack_mcmeta(ast: Dict[str, Any], output_dir: Path) -> None:
771
- """Generate the pack.mcmeta file."""
772
- pack_format = ast.get('pack', {}).get('format', 82)
773
- pack_description = ast.get('pack', {}).get('description', 'MDL Generated Datapack')
182
+ def build_single_file(mdl_file: Path, output_dir: Path, verbose: bool = False) -> None:
183
+ """Build a single MDL file.
774
184
 
775
- pack_meta = {
776
- "pack": {
777
- "pack_format": pack_format,
778
- "description": pack_description
779
- }
780
- }
185
+ Args:
186
+ mdl_file: Path to the MDL file
187
+ output_dir: Output directory
188
+ verbose: Enable verbose output
189
+ """
190
+ if verbose:
191
+ print_info(f"Building single file: {color.file_path(mdl_file.name)}")
192
+
193
+ pack = MDLPack()
194
+ pack.add_mdl_file(mdl_file)
195
+ pack.generate_datapack(output_dir)
781
196
 
782
- write_json(str(output_dir / "pack.mcmeta"), pack_meta)
197
+ if verbose:
198
+ print_success(f"✓ Built: {color.file_path(mdl_file.name)}")
783
199
 
784
200
 
785
- def _ast_to_pack(ast: Dict[str, Any], mdl_files: List[Path]) -> Pack:
786
- """Convert AST to Pack object."""
787
- pack_info = ast.get('pack', {})
788
- if pack_info is None:
789
- pack_info = {}
790
- pack_name = pack_info.get('name', 'mdl_pack')
791
- pack_format = pack_info.get('pack_format', 82) # Use pack_format instead of format
792
- pack_description = pack_info.get('description', 'MDL Generated Datapack')
201
+ def build_directory(input_dir: Path, output_dir: Path, verbose: bool = False) -> None:
202
+ """Build all MDL files in a directory.
793
203
 
794
- pack = Pack(pack_name, pack_description, pack_format)
795
-
796
- # Add namespaces and functions
797
- if 'functions' in ast:
798
- # Get namespace name from AST or use pack name
799
- namespace_info = ast.get('namespace', {})
800
- if namespace_info is None:
801
- namespace_info = {}
802
- namespace_name = namespace_info.get('name', pack_name)
803
- namespace = pack.namespace(namespace_name)
804
-
805
- for func in ast['functions']:
806
- function_name = func['name']
807
- # Create function and add commands if they exist
808
- function = namespace.function(function_name)
809
-
810
- # Add commands from function body if they exist
811
- if 'body' in func:
812
- for i, statement in enumerate(func['body']):
813
- try:
814
- # Use the same processing logic as the build system
815
- commands = _process_statement(statement, namespace_name, function_name, i, False, "@s", {}, BuildContext())
816
- function.commands.extend(commands)
817
- except Exception as e:
818
- # If processing fails, try to add as simple command
819
- if statement.get('type') == 'command':
820
- function.commands.append(statement['command'])
821
- elif statement.get('type') == 'function_call':
822
- func_name = statement['name']
823
- scope = statement.get('scope')
824
- func_namespace = statement.get('namespace', namespace_name) # Use specified namespace or current namespace
825
-
826
- if scope:
827
- # Handle scoped function call
828
- if scope == 'global':
829
- # Global scope uses the server armor stand
830
- selector = "@e[type=armor_stand,tag=mdl_server,limit=1]"
831
- else:
832
- # Use the specified scope selector
833
- selector = scope
834
-
835
- # Generate execute as command
836
- function.commands.append(f"execute as {selector} run function {func_namespace}:{func_name}")
837
- else:
838
- # Simple function call without scope
839
- function.commands.append(f"function {func_namespace}:{func_name}")
840
- elif statement.get('type') == 'variable_assignment':
841
- # Handle variable assignments
842
- var_name = statement['name']
843
- value = statement['value']
844
-
845
- # Determine selector based on variable scope
846
- var_selector = "@s" # Default
847
- if 'variables' in ast:
848
- for var_decl in ast['variables']:
849
- if var_decl.get('name') == var_name:
850
- var_scope = var_decl.get('scope')
851
- if var_scope == 'global':
852
- var_selector = "@e[type=armor_stand,tag=mdl_server,limit=1]"
853
- elif var_scope:
854
- var_selector = var_scope
855
- break
856
-
857
- if hasattr(value, 'value'):
858
- # Simple literal value
859
- function.commands.append(f"scoreboard players set {var_name} {var_selector} {value.value}")
860
- else:
861
- # Complex expression - add a placeholder
862
- function.commands.append(f"# Variable assignment: {var_name} = {value}")
863
- else:
864
- # Add a placeholder for other statement types
865
- function.commands.append(f"# Statement: {statement.get('type', 'unknown')}")
866
-
867
- # Add variables
868
- if 'variables' in ast:
869
- for var in ast['variables']:
870
- # Variables are handled during command processing
871
- pass
204
+ Args:
205
+ input_dir: Input directory containing MDL files
206
+ output_dir: Output directory
207
+ verbose: Enable verbose output
208
+ """
209
+ if verbose:
210
+ print_info(f"Building directory: {color.file_path(input_dir.name)}")
872
211
 
873
- # Add hooks
874
- if 'hooks' in ast:
875
- for hook in ast['hooks']:
876
- if hook['hook_type'] == 'load':
877
- pack.on_load(hook['function_name'])
878
- elif hook['hook_type'] == 'tick':
879
- pack.on_tick(hook['function_name'])
212
+ mdl_files = find_mdl_files(input_dir)
880
213
 
881
- # Add recipes
882
- if 'recipes' in ast:
883
- namespace_info = ast.get('namespace', {})
884
- if namespace_info is None:
885
- namespace_info = {}
886
- namespace_name = namespace_info.get('name', pack_name)
887
- namespace = pack.namespace(namespace_name)
888
-
889
- for recipe in ast['recipes']:
890
- recipe_name = recipe['name']
891
- recipe_data = recipe['data']
892
- # Create recipe object
893
- from .pack import Recipe
894
- recipe_obj = Recipe(recipe_name, recipe_data)
895
- namespace.recipes[recipe_name] = recipe_obj
214
+ if not mdl_files:
215
+ raise MDLConfigurationError(
216
+ message=f"No MDL files found in directory: {input_dir}",
217
+ suggestion="Ensure the directory contains .mdl files."
218
+ )
896
219
 
897
- # Add advancements
898
- if 'advancements' in ast:
899
- namespace_info = ast.get('namespace', {})
900
- if namespace_info is None:
901
- namespace_info = {}
902
- namespace_name = namespace_info.get('name', pack_name)
903
- namespace = pack.namespace(namespace_name)
904
-
905
- for advancement in ast['advancements']:
906
- advancement_name = advancement['name']
907
- advancement_data = advancement['data']
908
- # Create advancement object
909
- from .pack import Advancement
910
- advancement_obj = Advancement(advancement_name, advancement_data)
911
- namespace.advancements[advancement_name] = advancement_obj
220
+ pack = MDLPack()
912
221
 
913
- # Add loot tables
914
- if 'loot_tables' in ast:
915
- namespace_info = ast.get('namespace', {})
916
- if namespace_info is None:
917
- namespace_info = {}
918
- namespace_name = namespace_info.get('name', pack_name)
919
- namespace = pack.namespace(namespace_name)
920
-
921
- for loot_table in ast['loot_tables']:
922
- loot_table_name = loot_table['name']
923
- loot_table_data = loot_table['data']
924
- # Create loot table object
925
- from .pack import LootTable
926
- loot_table_obj = LootTable(loot_table_name, loot_table_data)
927
- namespace.loot_tables[loot_table_name] = loot_table_obj
928
-
929
- return pack
930
-
931
-
932
- def build_mdl(input_path: str, output_path: str, verbose: bool = False, pack_format_override: Optional[int] = None, wrapper: Optional[str] = None, ignore_warnings: bool = False) -> None:
933
- """Build MDL files into a Minecraft datapack."""
934
- error_collector = MDLErrorCollector()
935
-
936
- try:
937
- input_dir = Path(input_path)
938
- output_dir = Path(output_path)
939
-
940
- # Validate input directory exists
941
- if not input_dir.exists():
942
- error_collector.add_error(create_error(
943
- MDLFileError,
944
- f"Input path does not exist: {input_path}",
945
- file_path=input_path,
946
- suggestion="Check the path and ensure the file or directory exists."
947
- ))
948
- error_collector.print_errors(verbose=True, ignore_warnings=ignore_warnings)
949
- error_collector.raise_if_errors()
950
- return
951
-
952
- # Find MDL files
953
- mdl_files = _find_mdl_files(input_dir)
954
-
955
- if not mdl_files:
956
- error_collector.add_error(create_error(
957
- MDLFileError,
958
- f"No .mdl files found in {input_path}",
959
- file_path=input_path,
960
- suggestion="Ensure the directory contains .mdl files or specify a single .mdl file."
961
- ))
962
- error_collector.print_errors(verbose=True, ignore_warnings=ignore_warnings)
963
- error_collector.raise_if_errors()
964
- return
965
-
966
- if verbose:
967
- print(f"Found {len(mdl_files)} MDL file(s):")
968
- for file in mdl_files:
969
- print(f" - {file}")
970
-
971
- # Merge and parse MDL files
972
- ast = _merge_mdl_files(mdl_files, verbose, error_collector)
973
-
974
- if ast is None:
975
- error_collector.print_errors(verbose=True, ignore_warnings=ignore_warnings)
976
- error_collector.raise_if_errors()
977
- return
978
-
979
- # Override pack format if specified
980
- if pack_format_override is not None:
981
- _validate_pack_format(pack_format_override)
982
- if 'pack' not in ast:
983
- ast['pack'] = {}
984
- ast['pack']['format'] = pack_format_override
985
-
986
- # Create output directory
987
- ensure_dir(str(output_dir))
988
-
989
- # Generate pack.mcmeta
990
- _generate_pack_mcmeta(ast, output_dir)
991
-
992
- # Handle multiple namespaces from multiple files
993
- namespaces = []
994
-
995
- # Get namespaces from AST
996
- if 'namespaces' in ast:
997
- for ns in ast['namespaces']:
998
- if 'name' in ns:
999
- namespaces.append(_slugify(ns['name']))
1000
-
1001
- # If no explicit namespaces, use the pack name as default
1002
- if not namespaces:
1003
- default_namespace = ast.get('pack', {}).get('name', 'mdl_pack')
1004
- namespaces.append(_slugify(default_namespace))
1005
-
1006
- # Debug: Show what namespaces we're using
222
+ for mdl_file in mdl_files:
1007
223
  if verbose:
1008
- print(f"DEBUG: AST namespaces: {ast.get('namespaces', [])}")
1009
- print(f"DEBUG: AST pack: {ast.get('pack', {})}")
1010
- print(f"DEBUG: Using namespaces: {namespaces}")
1011
-
1012
- # Generate functions for each namespace
1013
- build_context = BuildContext()
1014
- for namespace in namespaces:
1015
- if verbose:
1016
- print(f"DEBUG: Processing namespace: {namespace}")
1017
- _generate_function_file(ast, output_dir, namespace, verbose, build_context)
1018
-
1019
- # Generate hook files (load/tick tags) - include all namespaces
1020
- primary_namespace = namespaces[0] if namespaces else 'mdl_pack'
1021
- _generate_hook_files(ast, output_dir, primary_namespace, build_context, all_namespaces=namespaces)
1022
-
1023
- # Generate load functions for all namespaces
1024
- for namespace in namespaces:
1025
- _generate_global_load_function(ast, output_dir, namespace, build_context)
1026
-
1027
- # Create zip file (always create one, use wrapper name if specified)
1028
- zip_name = wrapper if wrapper else output_dir.name
1029
- # When using wrapper, create zip in output directory; otherwise in parent directory
1030
- if wrapper:
1031
- zip_path = output_dir / f"{zip_name}.zip"
1032
- else:
1033
- zip_path = output_dir.parent / f"{zip_name}.zip"
1034
- _create_zip_file(output_dir, zip_path)
1035
- if verbose:
1036
- print(f"[ZIP] Created zip file: {zip_path}")
1037
-
1038
- print(f"[OK] Successfully built datapack: {output_path}")
1039
- if verbose:
1040
- print(f"[DIR] Output directory: {output_dir}")
1041
- print(f"[NS] Namespace: {namespace}")
224
+ print_info(f"Adding: {color.file_path(mdl_file.name)}")
225
+ pack.add_mdl_file(mdl_file)
1042
226
 
1043
- except MDLLexerError as e:
1044
- error_collector.add_error(e)
1045
- error_collector.print_errors(verbose=True, ignore_warnings=ignore_warnings)
1046
- error_collector.raise_if_errors()
1047
- except MDLParserError as e:
1048
- error_collector.add_error(e)
1049
- error_collector.print_errors(verbose=True, ignore_warnings=ignore_warnings)
1050
- error_collector.raise_if_errors()
1051
- except MDLSyntaxError as e:
1052
- error_collector.add_error(e)
1053
- error_collector.print_errors(verbose=True, ignore_warnings=ignore_warnings)
1054
- error_collector.raise_if_errors()
1055
- except MDLBuildError as e:
1056
- error_collector.add_error(e)
1057
- error_collector.print_errors(verbose=True, ignore_warnings=ignore_warnings)
1058
- error_collector.raise_if_errors()
1059
- except MDLFileError as e:
1060
- error_collector.add_error(e)
1061
- error_collector.print_errors(verbose=True, ignore_warnings=ignore_warnings)
1062
- error_collector.raise_if_errors()
1063
- except MDLCompilationError as e:
1064
- error_collector.add_error(e)
1065
- error_collector.print_errors(verbose=True, ignore_warnings=ignore_warnings)
1066
- error_collector.raise_if_errors()
1067
- except Exception as e:
1068
- error_collector.add_error(create_error(
1069
- MDLBuildError,
1070
- f"Unexpected error during build: {str(e)}",
1071
- suggestion="Check the input files and try again. If the problem persists, report this as a bug."
1072
- ))
1073
- error_collector.print_errors(verbose=True, ignore_warnings=ignore_warnings)
1074
- error_collector.raise_if_errors()
227
+ pack.generate_datapack(output_dir)
228
+
229
+ if verbose:
230
+ print_success(f"✓ Built {len(mdl_files)} file(s) from {color.file_path(input_dir.name)}")