|
2 | 2 |
|
3 | 3 | import asyncio |
4 | 4 | import contextlib |
| 5 | +from pathlib import Path |
5 | 6 | from typing import Any |
6 | 7 |
|
7 | 8 | from .commands.async_build import AsyncBuildCommands |
|
10 | 11 | from .commands.async_navigation import AsyncNavigationCommands |
11 | 12 | from .commands.async_waypoints import AsyncWaypointsCommands |
12 | 13 | from .commands.async_world import AsyncWorldCommands |
| 14 | +from .commands._types import CommandDispatchResult |
13 | 15 | from .discovery import resolve_bridge_info |
14 | 16 | from .models import BridgeError, BridgeInfo |
15 | 17 | from .protocol import decode_line, encode_line, new_request |
| 18 | +from .schematic_paths import normalize_build_coords, normalize_schematic_path |
16 | 19 | from .settings import AsyncSettingsNamespace |
17 | 20 |
|
18 | 21 |
|
@@ -139,6 +142,42 @@ async def wait_for_task(self, task_id: str) -> dict[str, Any]: |
139 | 142 | if isinstance(event_name, str) and event_name in self.TERMINAL_TASK_EVENTS: |
140 | 143 | return event |
141 | 144 |
|
| 145 | + async def build_file( |
| 146 | + self, |
| 147 | + path: str | Path, |
| 148 | + *coords: int, |
| 149 | + base_dir: str | Path | None = None, |
| 150 | + ) -> CommandDispatchResult: |
| 151 | + """Dispatch Baritone `build` using a local schematic path. |
| 152 | +
|
| 153 | + Relative paths resolve from the calling Python file directory by default. |
| 154 | + Use `base_dir` to override that base path. |
| 155 | +
|
| 156 | + Coordinate args must be either: |
| 157 | + - none (build at player position), or |
| 158 | + - exactly three ints `(x, y, z)`. |
| 159 | + """ |
| 160 | + normalized_path = normalize_schematic_path(path, base_dir=base_dir) |
| 161 | + normalized_coords = normalize_build_coords(coords) |
| 162 | + return await self.build(normalized_path, *normalized_coords) |
| 163 | + |
| 164 | + async def build_file_wait( |
| 165 | + self, |
| 166 | + path: str | Path, |
| 167 | + *coords: int, |
| 168 | + base_dir: str | Path | None = None, |
| 169 | + ) -> dict[str, Any]: |
| 170 | + """Dispatch `build_file` and wait for terminal task event. |
| 171 | +
|
| 172 | + Raises `BridgeError(code=\"BAD_RESPONSE\", ...)` when dispatch response |
| 173 | + does not include a `task_id`. |
| 174 | + """ |
| 175 | + dispatch = await self.build_file(path, *coords, base_dir=base_dir) |
| 176 | + task_id = dispatch.get("task_id") |
| 177 | + if not task_id: |
| 178 | + raise BridgeError("BAD_RESPONSE", "No task_id returned for command: build", dispatch["raw"]) |
| 179 | + return await self.wait_for_task(task_id) |
| 180 | + |
142 | 181 | async def _request(self, method: str, params: dict[str, Any]) -> dict[str, Any]: |
143 | 182 | self._ensure_connected() |
144 | 183 | assert self._writer is not None |
|
0 commit comments