minecraft-datapack-language 17.0.10__py3-none-any.whl → 17.0.12__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.
- minecraft_datapack_language/_embedded/docs/404.html +42 -0
- minecraft_datapack_language/_embedded/docs/Gemfile +26 -0
- minecraft_datapack_language/_embedded/docs/README.md +84 -0
- minecraft_datapack_language/_embedded/docs/_config.yml +74 -0
- minecraft_datapack_language/_embedded/docs/_data/version.yml +3 -0
- minecraft_datapack_language/_embedded/docs/_docs/cli-reference.md +506 -0
- minecraft_datapack_language/_embedded/docs/_docs/contributing.md +352 -0
- minecraft_datapack_language/_embedded/docs/_docs/docs-hub.md +266 -0
- minecraft_datapack_language/_embedded/docs/_docs/documentation.md +135 -0
- minecraft_datapack_language/_embedded/docs/_docs/examples.md +194 -0
- minecraft_datapack_language/_embedded/docs/_docs/getting-started.md +230 -0
- minecraft_datapack_language/_embedded/docs/_docs/language-reference.md +1637 -0
- minecraft_datapack_language/_embedded/docs/_docs/multi-file-projects.md +221 -0
- minecraft_datapack_language/_embedded/docs/_docs/python-bindings.md +446 -0
- minecraft_datapack_language/_embedded/docs/_docs/vscode-extension.md +381 -0
- minecraft_datapack_language/_embedded/docs/_includes/head-custom.html +983 -0
- minecraft_datapack_language/_embedded/docs/_includes/navigation.html +362 -0
- minecraft_datapack_language/_embedded/docs/_layouts/default.html +27 -0
- minecraft_datapack_language/_embedded/docs/_layouts/page.html +281 -0
- minecraft_datapack_language/_embedded/docs/_plugins/test_version.rb +13 -0
- minecraft_datapack_language/_embedded/docs/_plugins/version_reader.rb +37 -0
- minecraft_datapack_language/_embedded/docs/assets/css/style.css +211 -0
- minecraft_datapack_language/_embedded/docs/docs.md +134 -0
- minecraft_datapack_language/_embedded/docs/downloads.md +444 -0
- minecraft_datapack_language/_embedded/docs/icons/favicon-16.png +0 -0
- minecraft_datapack_language/_embedded/docs/icons/favicon-32.png +0 -0
- minecraft_datapack_language/_embedded/docs/icons/favicon-48.png +0 -0
- minecraft_datapack_language/_embedded/docs/icons/favicon-64.png +0 -0
- minecraft_datapack_language/_embedded/docs/icons/icon-1024.png +0 -0
- minecraft_datapack_language/_embedded/docs/icons/icon-128.png +0 -0
- minecraft_datapack_language/_embedded/docs/icons/icon-256.png +0 -0
- minecraft_datapack_language/_embedded/docs/icons/icon-512.png +0 -0
- minecraft_datapack_language/_embedded/docs/icons/icon-64.png +0 -0
- minecraft_datapack_language/_embedded/docs/index.md +378 -0
- minecraft_datapack_language/_version.py +2 -2
- minecraft_datapack_language/cli.py +5 -1
- {minecraft_datapack_language-17.0.10.dist-info → minecraft_datapack_language-17.0.12.dist-info}/METADATA +1 -1
- minecraft_datapack_language-17.0.12.dist-info/RECORD +56 -0
- minecraft_datapack_language-17.0.10.dist-info/RECORD +0 -22
- {minecraft_datapack_language-17.0.10.dist-info → minecraft_datapack_language-17.0.12.dist-info}/WHEEL +0 -0
- {minecraft_datapack_language-17.0.10.dist-info → minecraft_datapack_language-17.0.12.dist-info}/entry_points.txt +0 -0
- {minecraft_datapack_language-17.0.10.dist-info → minecraft_datapack_language-17.0.12.dist-info}/licenses/LICENSE +0 -0
- {minecraft_datapack_language-17.0.10.dist-info → minecraft_datapack_language-17.0.12.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,221 @@
|
|
1
|
+
---
|
2
|
+
layout: page
|
3
|
+
title: Multi-File Projects
|
4
|
+
permalink: /docs/multi-file-projects/
|
5
|
+
---
|
6
|
+
|
7
|
+
MDL supports organizing code across multiple files for better project structure.
|
8
|
+
|
9
|
+
## Basic Multi-File Setup
|
10
|
+
|
11
|
+
### Main File (with pack declaration)
|
12
|
+
|
13
|
+
Only the main entry file needs a pack declaration:
|
14
|
+
|
15
|
+
```mdl
|
16
|
+
// main.mdl
|
17
|
+
pack "my_project" "A multi-file project" 82;
|
18
|
+
namespace "core";
|
19
|
+
|
20
|
+
var num playerCount<global> = 0;
|
21
|
+
|
22
|
+
function "init" {
|
23
|
+
playerCount<global> = 0;
|
24
|
+
say "Core system initialized!";
|
25
|
+
}
|
26
|
+
|
27
|
+
on_load core:init;
|
28
|
+
```
|
29
|
+
|
30
|
+
### Additional Files (no pack declaration)
|
31
|
+
|
32
|
+
Other files don't need pack declarations:
|
33
|
+
|
34
|
+
```mdl
|
35
|
+
// ui.mdl
|
36
|
+
namespace "ui";
|
37
|
+
|
38
|
+
function ui:show_hud {
|
39
|
+
tellraw @a {"text":"Players: $playerCount<global>$","color":"green"};
|
40
|
+
}
|
41
|
+
|
42
|
+
function ui:update_hud {
|
43
|
+
exec ui:show_hud<@a>;
|
44
|
+
}
|
45
|
+
```
|
46
|
+
|
47
|
+
```mdl
|
48
|
+
// game.mdl
|
49
|
+
namespace "game";
|
50
|
+
|
51
|
+
function game:start {
|
52
|
+
say "Game started!";
|
53
|
+
exec ui:update_hud;
|
54
|
+
}
|
55
|
+
```
|
56
|
+
|
57
|
+
## Building Multi-File Projects
|
58
|
+
|
59
|
+
### Build All Files Together
|
60
|
+
|
61
|
+
```bash
|
62
|
+
mdl build --mdl . -o dist
|
63
|
+
```
|
64
|
+
|
65
|
+
### Build Entire Directory
|
66
|
+
|
67
|
+
```bash
|
68
|
+
mdl build --mdl myproject/ -o dist
|
69
|
+
```
|
70
|
+
|
71
|
+
### Build with Wildcards
|
72
|
+
|
73
|
+
```bash
|
74
|
+
mdl build --mdl "*.mdl" -o dist
|
75
|
+
```
|
76
|
+
|
77
|
+
## Namespace Organization
|
78
|
+
|
79
|
+
Each file can have its own namespace to prevent conflicts:
|
80
|
+
|
81
|
+
```mdl
|
82
|
+
// combat.mdl
|
83
|
+
namespace "combat";
|
84
|
+
|
85
|
+
var num damage = 10; // Defaults to player-specific scope
|
86
|
+
|
87
|
+
function "attack" {
|
88
|
+
say Attack damage: $damage$;
|
89
|
+
}
|
90
|
+
```
|
91
|
+
|
92
|
+
```mdl
|
93
|
+
// magic.mdl
|
94
|
+
namespace "magic";
|
95
|
+
|
96
|
+
var num mana = 100; // Defaults to player-specific scope
|
97
|
+
|
98
|
+
function magic:cast_spell {
|
99
|
+
if $mana$ >= 20 {
|
100
|
+
mana = $mana$ - 20;
|
101
|
+
say "Spell cast! Mana: $mana$";
|
102
|
+
}
|
103
|
+
}
|
104
|
+
```
|
105
|
+
|
106
|
+
## Variable Sharing
|
107
|
+
|
108
|
+
Variables from all files are automatically merged:
|
109
|
+
|
110
|
+
```mdl
|
111
|
+
// core.mdl
|
112
|
+
var num globalTimer<global> = 0;
|
113
|
+
var num playerScore = 0; // Defaults to player-specific scope
|
114
|
+
```
|
115
|
+
|
116
|
+
```mdl
|
117
|
+
// ui.mdl
|
118
|
+
namespace "ui";
|
119
|
+
function ui:show_stats {
|
120
|
+
tellraw @a {"text":"Timer: $globalTimer<global>$, Score: $playerScore$","color":"gold"};
|
121
|
+
}
|
122
|
+
```
|
123
|
+
|
124
|
+
<!-- Removed redundant Explicit Scopes in Conditions section as requested -->
|
125
|
+
|
126
|
+
## Complete Multi-File Example
|
127
|
+
|
128
|
+
Here's a complete example with multiple files:
|
129
|
+
|
130
|
+
**`main.mdl`**:
|
131
|
+
```mdl
|
132
|
+
pack "adventure" "Adventure game" 82;
|
133
|
+
namespace "core";
|
134
|
+
|
135
|
+
var num gameState<@a> = 0;
|
136
|
+
var num playerLevel = 1; // Defaults to player-specific scope
|
137
|
+
|
138
|
+
function "init" {
|
139
|
+
gameState<@a> = 0;
|
140
|
+
playerLevel<@s> = 1;
|
141
|
+
say Adventure game initialized!;
|
142
|
+
}
|
143
|
+
|
144
|
+
on_load "core:init";
|
145
|
+
```
|
146
|
+
|
147
|
+
**`combat.mdl`**:
|
148
|
+
```mdl
|
149
|
+
namespace "combat";
|
150
|
+
|
151
|
+
var num playerHealth = 20; // Defaults to player-specific scope
|
152
|
+
|
153
|
+
function "attack" {
|
154
|
+
say Attacking! Health: $playerHealth$;
|
155
|
+
}
|
156
|
+
|
157
|
+
function "heal" {
|
158
|
+
if "$playerHealth$ < 20" {
|
159
|
+
playerHealth<@s> = playerHealth<@s> + 5;
|
160
|
+
say Healed! Health: $playerHealth$;
|
161
|
+
}
|
162
|
+
}
|
163
|
+
```
|
164
|
+
|
165
|
+
**`ui.mdl`**:
|
166
|
+
```mdl
|
167
|
+
namespace "ui";
|
168
|
+
|
169
|
+
function "show_status" {
|
170
|
+
tellraw @a {"text":"Level: $playerLevel$, Health: $playerHealth$","color":"aqua"};
|
171
|
+
}
|
172
|
+
|
173
|
+
function "update_ui" {
|
174
|
+
function "ui:show_status<@a>";
|
175
|
+
}
|
176
|
+
```
|
177
|
+
|
178
|
+
**`game.mdl`**:
|
179
|
+
```mdl
|
180
|
+
namespace "game";
|
181
|
+
|
182
|
+
function game:start {
|
183
|
+
gameState<global> = 1;
|
184
|
+
say "Game started!";
|
185
|
+
exec ui:update_ui;
|
186
|
+
}
|
187
|
+
|
188
|
+
function game:level_up {
|
189
|
+
if "$playerLevel$ < 10" {
|
190
|
+
playerLevel<@s> = playerLevel<@s> + 1;
|
191
|
+
say "Level up! New level: $playerLevel$";
|
192
|
+
}
|
193
|
+
}
|
194
|
+
```
|
195
|
+
|
196
|
+
Build the project:
|
197
|
+
```bash
|
198
|
+
mdl build --mdl . -o dist
|
199
|
+
```
|
200
|
+
|
201
|
+
## Best Practices
|
202
|
+
|
203
|
+
1. **One namespace per file**: Keep related functionality together
|
204
|
+
2. **Main file first**: Put the file with pack declaration first
|
205
|
+
3. **Clear naming**: Use descriptive file and namespace names
|
206
|
+
4. **Shared variables**: Declare shared variables in the main file
|
207
|
+
5. **Function organization**: Group related functions in the same file
|
208
|
+
6. **Proper scoping**: Use `<global>` for server-wide variables, omit scope for player-specific (defaults to @s)
|
209
|
+
7. **Explicit scope access**: Omit scope for @s; use `<...>` only when you need a different selector
|
210
|
+
|
211
|
+
## File Structure Example
|
212
|
+
|
213
|
+
```
|
214
|
+
my_project/
|
215
|
+
├── main.mdl # Pack declaration, core variables
|
216
|
+
├── combat.mdl # Combat system
|
217
|
+
├── magic.mdl # Magic system
|
218
|
+
├── ui.mdl # User interface
|
219
|
+
├── game.mdl # Game logic
|
220
|
+
└── utils.mdl # Utility functions
|
221
|
+
```
|
@@ -0,0 +1,446 @@
|
|
1
|
+
---
|
2
|
+
layout: page
|
3
|
+
title: Python Bindings
|
4
|
+
permalink: /docs/python-bindings/
|
5
|
+
---
|
6
|
+
|
7
|
+
The MDL Python bindings provide a clean, programmatic way to create Minecraft datapacks. They're fully compatible with the MDL language and support variables, control flow, nested functions, and MDL's scope system (default `@s`, explicit selectors, and `<global>`).
|
8
|
+
|
9
|
+
## Quick Start
|
10
|
+
|
11
|
+
```python
|
12
|
+
from minecraft_datapack_language import Pack
|
13
|
+
|
14
|
+
# Create a datapack
|
15
|
+
p = Pack("My Pack", "A cool datapack", 82)
|
16
|
+
|
17
|
+
# Add a namespace
|
18
|
+
ns = p.namespace("example")
|
19
|
+
|
20
|
+
# Add functions
|
21
|
+
ns.function("hello", "say Hello World!")
|
22
|
+
ns.function("welcome", "tellraw @a {\"text\":\"Welcome!\",\"color\":\"green\"}")
|
23
|
+
|
24
|
+
# Hook into Minecraft lifecycle
|
25
|
+
p.on_load("example:hello")
|
26
|
+
p.on_tick("example:welcome")
|
27
|
+
|
28
|
+
# Build the datapack
|
29
|
+
p.build("dist")
|
30
|
+
```
|
31
|
+
|
32
|
+
## Core Classes
|
33
|
+
|
34
|
+
### Pack
|
35
|
+
|
36
|
+
The main class for creating datapacks.
|
37
|
+
|
38
|
+
```python
|
39
|
+
from minecraft_datapack_language import Pack
|
40
|
+
|
41
|
+
# Create a pack
|
42
|
+
p = Pack(
|
43
|
+
name="My Pack", # Pack name
|
44
|
+
description="Description", # Optional description
|
45
|
+
pack_format=82 # Minecraft pack format
|
46
|
+
)
|
47
|
+
```
|
48
|
+
|
49
|
+
**Methods:**
|
50
|
+
- `namespace(name)` - Create a namespace
|
51
|
+
- `on_load(function_id)` - Hook function to world load
|
52
|
+
- `on_tick(function_id)` - Hook function to tick
|
53
|
+
- `tag(registry, name, values=[], replace=False)` - Create tags
|
54
|
+
- `build(output_dir)` - Build the datapack
|
55
|
+
|
56
|
+
### Namespace
|
57
|
+
|
58
|
+
Represents a namespace for organizing functions.
|
59
|
+
|
60
|
+
```python
|
61
|
+
ns = p.namespace("example")
|
62
|
+
|
63
|
+
# Add functions to the namespace
|
64
|
+
ns.function("function_name", "command1", "command2", ...)
|
65
|
+
```
|
66
|
+
|
67
|
+
**Methods:**
|
68
|
+
- `function(name, *commands)` - Add a function with commands
|
69
|
+
|
70
|
+
### Control Flow and Expressions (Bindings)
|
71
|
+
|
72
|
+
Bindings include helpers to compose expressions and control flow identical to MDL:
|
73
|
+
|
74
|
+
```python
|
75
|
+
from minecraft_datapack_language import Pack
|
76
|
+
from minecraft_datapack_language.python_api import num, var_read, binop
|
77
|
+
|
78
|
+
p = Pack("Control Flow", "desc", 82)
|
79
|
+
ns = p.namespace("game")
|
80
|
+
|
81
|
+
def build_logic(fb):
|
82
|
+
# declare variable
|
83
|
+
fb.declare_var("counter", "<@s>", 0)
|
84
|
+
|
85
|
+
# counter<@s> = $counter<@s>$ + 1
|
86
|
+
fb.set("counter", "<@s>", binop(var_read("counter", "<@s>"), "PLUS", num(1)))
|
87
|
+
|
88
|
+
# if $counter<@s>$ > 5 { say "hi" } else { say "low" }
|
89
|
+
cond = binop(var_read("counter", "<@s>"), "GREATER", num(5))
|
90
|
+
fb.if_(cond, lambda t: t.say("hi"), lambda e: e.say("low"))
|
91
|
+
|
92
|
+
# while $counter<@s>$ < 10 { counter<@s> = $counter<@s>$ + 1 }
|
93
|
+
wcond = binop(var_read("counter", "<@s>"), "LESS", num(10))
|
94
|
+
fb.while_(wcond, lambda b: b.set("counter", "<@s>", binop(var_read("counter", "<@s>"), "PLUS", num(1))))
|
95
|
+
|
96
|
+
ns.function("main", build_logic)
|
97
|
+
p.build("dist")
|
98
|
+
```
|
99
|
+
|
100
|
+
## Basic Examples
|
101
|
+
|
102
|
+
### Hello World
|
103
|
+
|
104
|
+
```python
|
105
|
+
from minecraft_datapack_language import Pack
|
106
|
+
|
107
|
+
def create_hello_world():
|
108
|
+
p = Pack("Hello World", "A simple hello world datapack", 82)
|
109
|
+
|
110
|
+
ns = p.namespace("example")
|
111
|
+
ns.function("hello",
|
112
|
+
"say Hello, Minecraft!",
|
113
|
+
"tellraw @a {\"text\":\"Welcome to my datapack!\",\"color\":\"green\"}"
|
114
|
+
)
|
115
|
+
|
116
|
+
# Hook into world load
|
117
|
+
p.on_load("example:hello")
|
118
|
+
|
119
|
+
return p
|
120
|
+
|
121
|
+
# Create and build
|
122
|
+
pack = create_hello_world()
|
123
|
+
pack.build("dist")
|
124
|
+
```
|
125
|
+
|
126
|
+
### Particle Effects
|
127
|
+
|
128
|
+
```python
|
129
|
+
from minecraft_datapack_language import Pack
|
130
|
+
|
131
|
+
def create_particle_pack():
|
132
|
+
p = Pack("Particle Effects", "Creates particle effects around players", 82)
|
133
|
+
|
134
|
+
ns = p.namespace("particles")
|
135
|
+
|
136
|
+
ns.function("tick",
|
137
|
+
"execute as @a run particle minecraft:end_rod ~ ~ ~ 0.1 0.1 0.1 0.01 1",
|
138
|
+
"execute as @a run particle minecraft:firework ~ ~ ~ 0.2 0.2 0.2 0.02 2"
|
139
|
+
)
|
140
|
+
|
141
|
+
ns.function("init", "say Particle effects enabled!")
|
142
|
+
|
143
|
+
# Hook into lifecycle
|
144
|
+
p.on_load("particles:init")
|
145
|
+
p.on_tick("particles:tick")
|
146
|
+
|
147
|
+
return p
|
148
|
+
```
|
149
|
+
|
150
|
+
## Advanced Features
|
151
|
+
|
152
|
+
### Variables and Control Flow
|
153
|
+
The bindings support MDL's scope model (default @s and explicit selectors):
|
154
|
+
|
155
|
+
```python
|
156
|
+
from minecraft_datapack_language import Pack
|
157
|
+
|
158
|
+
def create_advanced_pack():
|
159
|
+
p = Pack("Advanced Features", "Demonstrates advanced features", 82)
|
160
|
+
|
161
|
+
ns = p.namespace("advanced")
|
162
|
+
|
163
|
+
# Functions with variables and control flow
|
164
|
+
ns.function("variable_demo",
|
165
|
+
"var num counter<global> = 0",
|
166
|
+
"counter<global> = 10",
|
167
|
+
"counter<global> = counter<global> + 5",
|
168
|
+
"if $counter<global>$ >= 15 {",
|
169
|
+
" say Counter is 15!",
|
170
|
+
" counter<global> = counter<global> - 5",
|
171
|
+
"}"
|
172
|
+
)
|
173
|
+
|
174
|
+
ns.function("control_flow_demo",
|
175
|
+
"var num playerHealth = 20",
|
176
|
+
"if $playerHealth$ < 10 {",
|
177
|
+
" say Health is low!",
|
178
|
+
" playerHealth = playerHealth + 5",
|
179
|
+
"} else {",
|
180
|
+
" say Health is good",
|
181
|
+
"}"
|
182
|
+
)
|
183
|
+
|
184
|
+
ns.function("loop_demo",
|
185
|
+
"var num countdown<global> = 5",
|
186
|
+
"while $countdown<global>$ > 0 {",
|
187
|
+
" say Countdown: $countdown<global>$",
|
188
|
+
" countdown<global> = countdown<global> - 1",
|
189
|
+
"}",
|
190
|
+
"say Blast off!"
|
191
|
+
)
|
192
|
+
|
193
|
+
p.on_tick("advanced:variable_demo")
|
194
|
+
p.on_tick("advanced:control_flow_demo")
|
195
|
+
p.on_tick("advanced:loop_demo")
|
196
|
+
|
197
|
+
return p
|
198
|
+
```
|
199
|
+
|
200
|
+
### Function Calls and Cross-Namespace References
|
201
|
+
|
202
|
+
```python
|
203
|
+
from minecraft_datapack_language import Pack
|
204
|
+
|
205
|
+
def create_function_pack():
|
206
|
+
p = Pack("Function Calls", "Demonstrates function calls", 82)
|
207
|
+
|
208
|
+
# Core namespace
|
209
|
+
core = p.namespace("core")
|
210
|
+
core.function("init", "say Initializing...")
|
211
|
+
core.function("tick", "say Tick...")
|
212
|
+
|
213
|
+
# Utility namespace
|
214
|
+
util = p.namespace("util")
|
215
|
+
util.function("helper", "say Helper function")
|
216
|
+
util.function("helper2", "say Another helper")
|
217
|
+
|
218
|
+
# Main namespace with cross-namespace calls
|
219
|
+
main = p.namespace("main")
|
220
|
+
main.function("start",
|
221
|
+
"say Starting...",
|
222
|
+
"function core:init",
|
223
|
+
"function util:helper"
|
224
|
+
)
|
225
|
+
|
226
|
+
main.function("update",
|
227
|
+
"function core:tick",
|
228
|
+
"function util:helper2"
|
229
|
+
)
|
230
|
+
|
231
|
+
# Lifecycle hooks
|
232
|
+
p.on_load("main:start")
|
233
|
+
p.on_tick("main:update")
|
234
|
+
|
235
|
+
return p
|
236
|
+
```
|
237
|
+
|
238
|
+
### Tags and Data
|
239
|
+
|
240
|
+
```python
|
241
|
+
from minecraft_datapack_language import Pack
|
242
|
+
|
243
|
+
def create_tag_pack():
|
244
|
+
p = Pack("Tags and Data", "Demonstrates tags and data", 82)
|
245
|
+
|
246
|
+
ns = p.namespace("example")
|
247
|
+
ns.function("init", "say Tags initialized!")
|
248
|
+
|
249
|
+
# Function tags
|
250
|
+
p.tag("function", "minecraft:load", values=["example:init"])
|
251
|
+
p.tag("function", "minecraft:tick", values=["example:tick"])
|
252
|
+
|
253
|
+
# Item tags
|
254
|
+
p.tag("item", "example:swords", values=[
|
255
|
+
"minecraft:diamond_sword",
|
256
|
+
"minecraft:netherite_sword"
|
257
|
+
])
|
258
|
+
|
259
|
+
# Block tags
|
260
|
+
p.tag("block", "example:glassy", values=[
|
261
|
+
"minecraft:glass",
|
262
|
+
"minecraft:tinted_glass"
|
263
|
+
])
|
264
|
+
|
265
|
+
return p
|
266
|
+
```
|
267
|
+
|
268
|
+
## Integration with MDL Files
|
269
|
+
You can use the Python bindings alongside MDL files:
|
270
|
+
|
271
|
+
```python
|
272
|
+
from minecraft_datapack_language import Pack
|
273
|
+
from minecraft_datapack_language.mdl_parser import MDLParser
|
274
|
+
|
275
|
+
def create_hybrid_pack():
|
276
|
+
# Parse MDL file
|
277
|
+
with open("my_functions.mdl", "r") as f:
|
278
|
+
mdl_content = f.read()
|
279
|
+
|
280
|
+
ast = MDLParser().parse(mdl_content)
|
281
|
+
|
282
|
+
# Create pack via Python bindings
|
283
|
+
p = Pack("Hybrid Pack", "Combines MDL and Python", 82)
|
284
|
+
|
285
|
+
# Add functions from MDL (placeholder emission; compile MDL separately when mixing)
|
286
|
+
for func in ast.functions:
|
287
|
+
ns = p.namespace(func.namespace)
|
288
|
+
ns.function(func.name, "say Imported from MDL via parser")
|
289
|
+
|
290
|
+
# Add additional functions via Python bindings
|
291
|
+
ns = p.namespace("python")
|
292
|
+
ns.function("python_func", "say Created via Python API!")
|
293
|
+
|
294
|
+
return p
|
295
|
+
```
|
296
|
+
|
297
|
+
## Best Practices
|
298
|
+
|
299
|
+
### 1. Organize by Namespace
|
300
|
+
|
301
|
+
```python
|
302
|
+
def create_organized_pack():
|
303
|
+
p = Pack("Organized Pack", "Well-organized datapack", 82)
|
304
|
+
|
305
|
+
# Core systems
|
306
|
+
core = p.namespace("core")
|
307
|
+
core.function("init", "say Initializing...")
|
308
|
+
core.function("tick", "say Tick...")
|
309
|
+
|
310
|
+
# Feature modules
|
311
|
+
combat = p.namespace("combat")
|
312
|
+
ui = p.namespace("ui")
|
313
|
+
data = p.namespace("data")
|
314
|
+
|
315
|
+
# Each namespace handles its own functionality
|
316
|
+
return p
|
317
|
+
```
|
318
|
+
|
319
|
+
### 2. Use Function Composition
|
320
|
+
|
321
|
+
```python
|
322
|
+
def create_composable_pack():
|
323
|
+
p = Pack("Composable Pack", "Uses function composition", 82)
|
324
|
+
|
325
|
+
ns = p.namespace("example")
|
326
|
+
|
327
|
+
# Small, focused functions
|
328
|
+
ns.function("check_player", "execute if entity @s[type=minecraft:player]")
|
329
|
+
ns.function("give_effect", "effect give @s minecraft:speed 10 1")
|
330
|
+
ns.function("send_message", "tellraw @s {\"text\":\"Effect applied!\",\"color\":\"green\"}")
|
331
|
+
|
332
|
+
# Compose functions
|
333
|
+
ns.function("player_effects",
|
334
|
+
"function example:check_player run function example:give_effect",
|
335
|
+
"function example:check_player run function example:send_message"
|
336
|
+
)
|
337
|
+
|
338
|
+
return p
|
339
|
+
```
|
340
|
+
|
341
|
+
### 3. Error Handling
|
342
|
+
|
343
|
+
```python
|
344
|
+
def create_robust_pack():
|
345
|
+
p = Pack("Robust Pack", "Handles errors gracefully", 82)
|
346
|
+
|
347
|
+
ns = p.namespace("robust")
|
348
|
+
|
349
|
+
# Always check conditions before operations
|
350
|
+
ns.function("safe_operation",
|
351
|
+
"execute if entity @a run say Players found",
|
352
|
+
"execute unless entity @a run say No players found",
|
353
|
+
"execute if entity @a run effect give @a minecraft:speed 5 1"
|
354
|
+
)
|
355
|
+
|
356
|
+
return p
|
357
|
+
```
|
358
|
+
|
359
|
+
## Complete Example
|
360
|
+
Here's a complete example that demonstrates all features including the explicit scope system:
|
361
|
+
|
362
|
+
```python
|
363
|
+
from minecraft_datapack_language import Pack
|
364
|
+
|
365
|
+
def create_complete_pack():
|
366
|
+
"""Create a complete datapack demonstrating all features."""
|
367
|
+
|
368
|
+
# Create the pack
|
369
|
+
p = Pack("Complete Example", "Demonstrates all MDL features", 82)
|
370
|
+
|
371
|
+
# Core namespace
|
372
|
+
core = p.namespace("core")
|
373
|
+
core.function("init",
|
374
|
+
"var num gameState<global> = 0",
|
375
|
+
"var num playerLevel = 1",
|
376
|
+
"gameState<global> = 0",
|
377
|
+
"playerLevel = 1",
|
378
|
+
"say [core:init] Initializing Complete Example...",
|
379
|
+
"tellraw @a {\"text\":\"Complete Example loaded!\",\"color\":\"green\"}",
|
380
|
+
"scoreboard objectives add example_counter dummy \"Example Counter\""
|
381
|
+
)
|
382
|
+
|
383
|
+
core.function("tick",
|
384
|
+
"gameState<global> = gameState<global> + 1",
|
385
|
+
"say [core:tick] Core systems running... Game state: $gameState<global>$",
|
386
|
+
"execute as @a run particle minecraft:end_rod ~ ~ ~ 0.1 0.1 0.1 0.01 1"
|
387
|
+
)
|
388
|
+
|
389
|
+
# Combat namespace
|
390
|
+
combat = p.namespace("combat")
|
391
|
+
combat.function("weapon_effects",
|
392
|
+
"execute as @a[nbt={SelectedItem:{id:\"minecraft:diamond_sword\"}}] run effect give @s minecraft:strength 1 0 true",
|
393
|
+
"execute as @a[nbt={SelectedItem:{id:\"minecraft:golden_sword\"}}] run effect give @s minecraft:speed 1 0 true"
|
394
|
+
)
|
395
|
+
|
396
|
+
combat.function("update_combat",
|
397
|
+
"function core:tick",
|
398
|
+
"function combat:weapon_effects"
|
399
|
+
)
|
400
|
+
|
401
|
+
# UI namespace
|
402
|
+
ui = p.namespace("ui")
|
403
|
+
ui.function("hud",
|
404
|
+
"title @a actionbar {\"text\":\"Complete Example Active - Level: $playerLevel$\",\"color\":\"gold\"}"
|
405
|
+
)
|
406
|
+
|
407
|
+
ui.function("update_ui",
|
408
|
+
"function ui:hud",
|
409
|
+
"function combat:update_combat"
|
410
|
+
)
|
411
|
+
|
412
|
+
# Data namespace
|
413
|
+
data = p.namespace("data")
|
414
|
+
data.function("setup_data",
|
415
|
+
"say Setting up data..."
|
416
|
+
)
|
417
|
+
|
418
|
+
# Lifecycle hooks
|
419
|
+
p.on_load("core:init")
|
420
|
+
p.on_tick("ui:update_ui")
|
421
|
+
|
422
|
+
# Function tags
|
423
|
+
p.tag("function", "minecraft:load", values=["core:init"])
|
424
|
+
p.tag("function", "minecraft:tick", values=["ui:update_ui"])
|
425
|
+
|
426
|
+
# Data tags
|
427
|
+
p.tag("item", "example:swords", values=[
|
428
|
+
"minecraft:diamond_sword",
|
429
|
+
"minecraft:netherite_sword"
|
430
|
+
])
|
431
|
+
|
432
|
+
p.tag("block", "example:glassy", values=[
|
433
|
+
"minecraft:glass",
|
434
|
+
"minecraft:tinted_glass"
|
435
|
+
])
|
436
|
+
|
437
|
+
return p
|
438
|
+
|
439
|
+
# Create and build the pack
|
440
|
+
if __name__ == "__main__":
|
441
|
+
pack = create_complete_pack()
|
442
|
+
pack.build("dist")
|
443
|
+
print("Complete example pack built successfully!")
|
444
|
+
```
|
445
|
+
|
446
|
+
The Python bindings provide a powerful, flexible way to create Minecraft datapacks with full support for the MDL language features including scopes and control flow.
|