minerender 2.1.0-alpha.85 → 2.1.0-alpha.87

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.
@@ -18688,6 +18688,7 @@ var index_browser_exports = {};
18688
18688
  __export(index_browser_exports, {
18689
18689
  AXES: () => AXES,
18690
18690
  AnvilParser: () => AnvilParser,
18691
+ AnvilWorldSource: () => AnvilWorldSource,
18691
18692
  ArchiveAssetSource: () => ArchiveAssetSource,
18692
18693
  AssetKey: () => AssetKey,
18693
18694
  AssetLoadError: () => AssetLoadError,
@@ -18790,6 +18791,8 @@ __export(index_browser_exports, {
18790
18791
  Ticker: () => Ticker,
18791
18792
  TintColors: () => TintColors,
18792
18793
  UVMapper: () => UVMapper,
18794
+ VideoExporter: () => VideoExporter,
18795
+ WorldStreamer: () => WorldStreamer,
18793
18796
  WrappedImage: () => WrappedImage,
18794
18797
  addBox3WireframeToObject: () => addBox3WireframeToObject,
18795
18798
  addWireframeToMesh: () => addWireframeToMesh,
@@ -22165,6 +22168,131 @@ function copyDataTexture(source) {
22165
22168
  return new import_three19.Texture().copy(copySource);
22166
22169
  }
22167
22170
 
22171
+ // src/export/VideoExporter.ts
22172
+ var VideoExporter = class {
22173
+ constructor(source, options) {
22174
+ this.source = source;
22175
+ this.chunks = [];
22176
+ this.settled = false;
22177
+ this.stopping = false;
22178
+ this.onStart = () => {
22179
+ this.timer = setTimeout(() => {
22180
+ this.stopping = true;
22181
+ try {
22182
+ this.recorder.stop();
22183
+ } catch (error) {
22184
+ this.cancel(error);
22185
+ }
22186
+ }, this.duration * 1e3);
22187
+ };
22188
+ this.onData = (event) => {
22189
+ if (event.data.size) this.chunks.push(event.data);
22190
+ };
22191
+ this.onStop = () => {
22192
+ if (!this.stopping) return this.cancel(new Error("Video recording stopped before its duration elapsed"));
22193
+ if (!this.chunks.length) return this.cancel(new Error("The browser produced no video data"));
22194
+ const mimeType = this.chunks.find((chunk) => chunk.type)?.type || this.recorder.mimeType;
22195
+ const blob = new Blob(this.chunks, { type: mimeType });
22196
+ this.settled = true;
22197
+ this.cleanup();
22198
+ this.resolve(blob);
22199
+ };
22200
+ this.onError = (event) => {
22201
+ this.cancel(event.error ?? new Error("Video recording failed"));
22202
+ };
22203
+ this.onAbort = () => {
22204
+ this.cancel(this.signal.reason);
22205
+ };
22206
+ this.onTrackEnded = () => {
22207
+ this.cancel(new Error("The video capture stream stopped producing frames"));
22208
+ };
22209
+ const fps = options.fps ?? 30;
22210
+ if (!Number.isFinite(options.duration) || options.duration <= 0 || options.duration * 1e3 > 2147483647) {
22211
+ throw new RangeError("Video duration must be greater than zero and at most 2147483.647 seconds");
22212
+ }
22213
+ if (!Number.isFinite(fps) || fps <= 0) throw new RangeError("Video fps must be a finite positive number");
22214
+ const bitrate = options.videoBitsPerSecond;
22215
+ if (bitrate !== void 0 && (!Number.isInteger(bitrate) || bitrate <= 0 || bitrate > 4294967295)) {
22216
+ throw new RangeError("Video bitrate must be a positive unsigned 32-bit integer");
22217
+ }
22218
+ if (!source.width || !source.height) throw new Error("Cannot export video from an empty canvas");
22219
+ options.signal?.throwIfAborted();
22220
+ if (typeof document === "undefined" || typeof MediaRecorder === "undefined" || typeof MediaRecorder.isTypeSupported !== "function") {
22221
+ throw new Error("Video export requires a browser with MediaRecorder support");
22222
+ }
22223
+ const mimeTypes = options.mimeType === void 0 ? ["video/webm;codecs=vp9", "video/webm;codecs=vp8", "video/webm", "video/mp4"] : [options.mimeType];
22224
+ const mimeType = mimeTypes.find((type) => type.startsWith("video/") && MediaRecorder.isTypeSupported(type));
22225
+ if (!mimeType) throw new Error("The browser does not support the requested video format");
22226
+ this.canvas = document.createElement("canvas");
22227
+ this.canvas.width = source.width;
22228
+ this.canvas.height = source.height;
22229
+ if (typeof this.canvas.captureStream !== "function") throw new Error("Video export requires canvas captureStream support");
22230
+ const context = this.canvas.getContext("2d", { alpha: false });
22231
+ if (!context) throw new Error("Could not create a canvas for video export");
22232
+ this.context = context;
22233
+ this.signal = options.signal;
22234
+ this.duration = options.duration;
22235
+ this.result = new Promise((resolve, reject) => {
22236
+ this.resolve = resolve;
22237
+ this.reject = reject;
22238
+ });
22239
+ try {
22240
+ this.stream = this.canvas.captureStream(fps);
22241
+ this.recorder = new MediaRecorder(this.stream, { mimeType, videoBitsPerSecond: bitrate });
22242
+ this.recorder.addEventListener("start", this.onStart);
22243
+ this.recorder.addEventListener("dataavailable", this.onData);
22244
+ this.recorder.addEventListener("stop", this.onStop);
22245
+ this.recorder.addEventListener("error", this.onError);
22246
+ for (const track of this.stream.getTracks()) {
22247
+ track.addEventListener("ended", this.onTrackEnded);
22248
+ }
22249
+ this.signal?.addEventListener("abort", this.onAbort, { once: true });
22250
+ this.recorder.start();
22251
+ } catch (error) {
22252
+ this.cancel(error);
22253
+ }
22254
+ }
22255
+ /** Copies a freshly drawn source frame before WebGL can clear its drawing buffer. */
22256
+ capture() {
22257
+ if (this.settled || this.stopping) return;
22258
+ try {
22259
+ this.context.fillStyle = "#000000";
22260
+ this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
22261
+ this.context.drawImage(this.source, 0, 0, this.canvas.width, this.canvas.height);
22262
+ } catch (error) {
22263
+ this.cancel(error);
22264
+ }
22265
+ }
22266
+ /** Cancels recording, releases its capture stream, and rejects {@link result}. */
22267
+ cancel(reason = new DOMException("Video export cancelled", "AbortError")) {
22268
+ if (this.settled) return;
22269
+ this.settled = true;
22270
+ this.cleanup();
22271
+ this.reject(reason);
22272
+ }
22273
+ cleanup() {
22274
+ if (this.timer !== void 0) clearTimeout(this.timer);
22275
+ this.signal?.removeEventListener("abort", this.onAbort);
22276
+ if (this.recorder) {
22277
+ this.recorder.removeEventListener("start", this.onStart);
22278
+ this.recorder.removeEventListener("dataavailable", this.onData);
22279
+ this.recorder.removeEventListener("stop", this.onStop);
22280
+ this.recorder.removeEventListener("error", this.onError);
22281
+ if (this.recorder.state !== "inactive") {
22282
+ try {
22283
+ this.recorder.stop();
22284
+ } catch {
22285
+ }
22286
+ }
22287
+ }
22288
+ for (const track of this.stream?.getTracks() ?? []) {
22289
+ track.removeEventListener("ended", this.onTrackEnded);
22290
+ track.stop();
22291
+ }
22292
+ this.chunks.length = 0;
22293
+ }
22294
+ };
22295
+
22168
22296
  // src/gui/GuiHelper.ts
22169
22297
  init_AssetKey();
22170
22298
  init_ModelTextures();
@@ -27474,6 +27602,9 @@ var _Renderer = class _Renderer {
27474
27602
  this._inAnimationLoop = true;
27475
27603
  try {
27476
27604
  this.animate(time);
27605
+ } catch (error) {
27606
+ this._videoExporter?.cancel(error);
27607
+ throw error;
27477
27608
  } finally {
27478
27609
  this._inAnimationLoop = false;
27479
27610
  }
@@ -27668,16 +27799,18 @@ var _Renderer = class _Renderer {
27668
27799
  if (this._frameCallbacks.get(callback) === subscription) this._frameCallbacks.delete(callback);
27669
27800
  };
27670
27801
  }
27671
- /** Starts or resumes the animation loop and registered frame callbacks. */
27802
+ /** Starts or resumes the animation loop and registered frame callbacks. Has no effect while running. */
27672
27803
  start() {
27673
- if (this._disposed) return;
27804
+ if (this._disposed || this._running) return;
27674
27805
  this.stop();
27675
27806
  this._running = true;
27676
27807
  if (!this._inAnimationLoop) this.renderer.setAnimationLoop(this._animationLoop);
27677
27808
  }
27678
- /** Pauses rendering and frame callbacks. Call {@link start} to resume. */
27809
+ /** Pauses rendering and frame callbacks, and cancels video export. Call {@link start} to resume. */
27679
27810
  stop() {
27680
27811
  if (this._disposed) return;
27812
+ this._videoExporter?.cancel();
27813
+ this._videoExporter = void 0;
27681
27814
  this._running = false;
27682
27815
  if (this._inAnimationLoop) {
27683
27816
  queueMicrotask(() => {
@@ -27739,7 +27872,7 @@ var _Renderer = class _Renderer {
27739
27872
  this._controls.update();
27740
27873
  }
27741
27874
  if (!this._running) return;
27742
- if (!this.dirty && !this.options.render.renderAlways && !this._frameCallbacks.size) return;
27875
+ if (!this.dirty && !this.options.render.renderAlways && !this._frameCallbacks.size && !this._videoExporter) return;
27743
27876
  const interval = this._frameInterval;
27744
27877
  if (interval) {
27745
27878
  const next = this._nextFrameTime;
@@ -27764,6 +27897,7 @@ var _Renderer = class _Renderer {
27764
27897
  } else {
27765
27898
  this.renderer.render(this.scene, this.camera);
27766
27899
  }
27900
+ this._videoExporter?.capture();
27767
27901
  this.dirty = false;
27768
27902
  if (this._stats) {
27769
27903
  this._stats.end();
@@ -27784,6 +27918,35 @@ var _Renderer = class _Renderer {
27784
27918
  const canvas = trim ? trimCanvas(this.renderer.domElement) : this.renderer.domElement;
27785
27919
  return canvas.toDataURL(mime, quality);
27786
27920
  }
27921
+ /**
27922
+ * Records a silent browser video in real time, including frame callbacks and animated textures.
27923
+ * Keep the tab visible; browser scheduling and `render.fpsLimit` limit the capture rate.
27924
+ * The video keeps the initial drawing-buffer size and fills transparent pixels with black.
27925
+ * Temporarily starts a stopped renderer. Stop, disposal, or `options.signal` cancels the export.
27926
+ * Only one export can run at a time. Use the returned Blob's type to select a file extension.
27927
+ */
27928
+ async toVideo(options) {
27929
+ if (this._disposed) throw new Error("Cannot export a video from a disposed renderer");
27930
+ if (this._videoExporter) throw new Error("A video export is already running");
27931
+ const exporter = new VideoExporter(this.renderer.domElement, options);
27932
+ const wasRunning = this._running;
27933
+ try {
27934
+ try {
27935
+ if (!wasRunning) this.start();
27936
+ this._videoExporter = exporter;
27937
+ if (this._controls?.enabled) this._controls.update();
27938
+ this.drawFrame();
27939
+ } catch (error) {
27940
+ exporter.cancel(error);
27941
+ }
27942
+ return await exporter.result;
27943
+ } finally {
27944
+ if (this._videoExporter === exporter) {
27945
+ this._videoExporter = void 0;
27946
+ if (!wasRunning) this.stop();
27947
+ }
27948
+ }
27949
+ }
27787
27950
  /** Exports visible scene meshes as OBJ text, without texture images. */
27788
27951
  toObj() {
27789
27952
  return SceneExporter.toObj(this.scene);
@@ -28735,6 +28898,134 @@ var AnvilParser = class {
28735
28898
  }
28736
28899
  };
28737
28900
 
28901
+ // src/world/AnvilWorldSource.ts
28902
+ init_MineRenderError();
28903
+ async function abortable2(promise, signal) {
28904
+ if (!signal) return promise;
28905
+ let abort;
28906
+ const cancelled = new Promise((_, reject) => {
28907
+ abort = () => reject(signal.reason);
28908
+ signal.addEventListener("abort", abort, { once: true });
28909
+ if (signal.aborted) abort();
28910
+ });
28911
+ try {
28912
+ return await Promise.race([promise, cancelled]);
28913
+ } finally {
28914
+ signal.removeEventListener("abort", abort);
28915
+ }
28916
+ }
28917
+ var AnvilWorldSource = class {
28918
+ constructor(readRegion, options = {}) {
28919
+ this.readRegion = readRegion;
28920
+ this.regions = /* @__PURE__ */ new Map();
28921
+ this.pending = /* @__PURE__ */ new Map();
28922
+ this.cachedBytes = 0;
28923
+ this.maxCachedRegions = options.maxCachedRegions ?? 4;
28924
+ this.maxCachedBytes = options.maxCachedBytes ?? 64 * 1024 * 1024;
28925
+ for (const [name, value] of Object.entries({ maxCachedRegions: this.maxCachedRegions, maxCachedBytes: this.maxCachedBytes })) {
28926
+ if (!Number.isSafeInteger(value) || value < 0) throw new RangeError(`${name} must be a nonnegative safe integer`);
28927
+ }
28928
+ }
28929
+ /** Decodes one column at absolute chunk coordinates. Aborting a caller leaves other shared readers active. */
28930
+ async getChunk(x, z, signal) {
28931
+ if (!Number.isSafeInteger(x) || !Number.isSafeInteger(z)) {
28932
+ throw new RangeError("Chunk column coordinates must be safe integers");
28933
+ }
28934
+ const regionX = Math.floor(x / 32), regionZ = Math.floor(z / 32);
28935
+ const data = await this.getRegion(regionX, regionZ, signal);
28936
+ signal?.throwIfAborted();
28937
+ if (!data) return void 0;
28938
+ const chunk = await abortable2(AnvilParser.parseChunk(data, x - regionX * 32, z - regionZ * 32), signal);
28939
+ signal?.throwIfAborted();
28940
+ if (chunk && (chunk.x !== x || chunk.z !== z)) {
28941
+ throw new MineRenderError(`Anvil chunk coordinates ${chunk.x},${chunk.z} do not match requested column ${x},${z}`);
28942
+ }
28943
+ return chunk;
28944
+ }
28945
+ /** Releases cached regions. Reads already in progress finish without repopulating this cache. */
28946
+ clearCache() {
28947
+ this.regions.clear();
28948
+ this.pending.clear();
28949
+ this.cachedBytes = 0;
28950
+ }
28951
+ getRegion(x, z, signal) {
28952
+ signal?.throwIfAborted();
28953
+ const key = `${x},${z}`;
28954
+ const cached2 = this.regions.get(key);
28955
+ if (cached2) {
28956
+ this.regions.delete(key);
28957
+ this.regions.set(key, cached2);
28958
+ return Promise.resolve(cached2.data);
28959
+ }
28960
+ let loading = this.pending.get(key);
28961
+ if (!loading) {
28962
+ const controller = new AbortController();
28963
+ loading = {
28964
+ controller,
28965
+ waiters: 0,
28966
+ settled: false,
28967
+ promise: Promise.resolve().then(() => {
28968
+ controller.signal.throwIfAborted();
28969
+ return this.readRegion(x, z, controller.signal);
28970
+ }).then((data) => {
28971
+ controller.signal.throwIfAborted();
28972
+ let bytes = data instanceof Uint8Array ? data : data === void 0 ? void 0 : new Uint8Array(data);
28973
+ if (bytes && bytes.byteLength !== bytes.buffer.byteLength) bytes = new Uint8Array(bytes);
28974
+ if (bytes) AnvilParser.getChunkList(bytes);
28975
+ loading.settled = true;
28976
+ if (this.pending.get(key) === loading) this.cacheRegion(key, bytes);
28977
+ return bytes;
28978
+ }).finally(() => {
28979
+ loading.settled = true;
28980
+ if (this.pending.get(key) === loading) this.pending.delete(key);
28981
+ })
28982
+ };
28983
+ this.pending.set(key, loading);
28984
+ }
28985
+ const read = loading;
28986
+ read.waiters++;
28987
+ return new Promise((resolve, reject) => {
28988
+ let finished = false;
28989
+ const release = () => {
28990
+ if (finished) return false;
28991
+ finished = true;
28992
+ signal?.removeEventListener("abort", abort);
28993
+ read.waiters--;
28994
+ return true;
28995
+ };
28996
+ const abort = () => {
28997
+ if (!release()) return;
28998
+ if (!read.waiters && !read.settled) {
28999
+ if (this.pending.get(key) === read) this.pending.delete(key);
29000
+ read.controller.abort(signal.reason);
29001
+ }
29002
+ reject(signal.reason);
29003
+ };
29004
+ signal?.addEventListener("abort", abort, { once: true });
29005
+ read.promise.then(
29006
+ (data) => {
29007
+ if (release()) resolve(data);
29008
+ },
29009
+ (error) => {
29010
+ if (release()) reject(error);
29011
+ }
29012
+ );
29013
+ if (signal?.aborted) abort();
29014
+ });
29015
+ }
29016
+ cacheRegion(key, data) {
29017
+ const size = data?.byteLength ?? 0;
29018
+ if (!this.maxCachedRegions || size > this.maxCachedBytes) return;
29019
+ this.regions.set(key, { data, size });
29020
+ this.cachedBytes += size;
29021
+ while (this.regions.size > this.maxCachedRegions || this.cachedBytes > this.maxCachedBytes) {
29022
+ const oldest = this.regions.keys().next().value;
29023
+ this.cachedBytes -= this.regions.get(oldest).size;
29024
+ this.regions.delete(oldest);
29025
+ }
29026
+ }
29027
+ };
29028
+
28738
29029
  // src/world/Chunk.ts
28739
29030
  var import_three42 = require("three");
28740
29031
  init_BlockStates();
@@ -29409,4 +29700,178 @@ var Chunk = class _Chunk {
29409
29700
  );
29410
29701
  }
29411
29702
  };
29703
+
29704
+ // src/world/WorldStreamer.ts
29705
+ var WorldStreamer = class {
29706
+ constructor(world, source, options = {}) {
29707
+ this.world = world;
29708
+ this.source = source;
29709
+ this.loaded = /* @__PURE__ */ new Map();
29710
+ this.owned = /* @__PURE__ */ new Map();
29711
+ this.missing = /* @__PURE__ */ new Map();
29712
+ this.failed = /* @__PURE__ */ new Map();
29713
+ this.desired = [];
29714
+ this.disposed = false;
29715
+ this.loadRadius = options.loadRadius ?? 2;
29716
+ this.unloadRadius = options.unloadRadius ?? Math.min(32, this.loadRadius + 1);
29717
+ if (![this.loadRadius, this.unloadRadius].every((value) => Number.isInteger(value) && value >= 0 && value <= 32) || this.unloadRadius < this.loadRadius) {
29718
+ throw new RangeError("Chunk radii must be integers from 0 to 32, with unloadRadius at least loadRadius");
29719
+ }
29720
+ }
29721
+ /** Returns absolute coordinates of successfully placed columns, including retained columns. */
29722
+ get loadedChunks() {
29723
+ return [...this.loaded.values()].map((position) => ({ ...position }));
29724
+ }
29725
+ /** Returns source failures retained within the unload radius. Failed columns do not block other loads. */
29726
+ get failedChunks() {
29727
+ return [...this.failed.values()].map((failure) => ({ ...failure }));
29728
+ }
29729
+ /** Counts desired columns still waiting for a source read or placement, including the active column. */
29730
+ get pendingChunks() {
29731
+ return this.desired.filter((position) => this.needsLoad(position)).length;
29732
+ }
29733
+ /**
29734
+ * Updates the center in absolute chunk coordinates. Repeated calls share the active drain.
29735
+ * Source failures are recorded in failedChunks; other columns continue loading. Missing and failed
29736
+ * columns are remembered within the retention radius. Call retryFailedChunks to retry source failures.
29737
+ * Placement and unload failures reject the drain; call update again to retry those operations.
29738
+ * Reads outside the latest load radius receive an abort signal; placement finishes before unloading.
29739
+ * Sources should represent an unchanged world during streaming.
29740
+ */
29741
+ async update(x, z) {
29742
+ if (this.disposed) throw new Error("WorldStreamer is disposed");
29743
+ if (![x, z].every((value) => Number.isSafeInteger(value) && Number.isSafeInteger(value - this.unloadRadius) && Number.isSafeInteger(value + this.unloadRadius))) {
29744
+ throw new RangeError("Chunk coordinates must be safe integers within the streaming radius");
29745
+ }
29746
+ if (!this.center || this.center.x !== x || this.center.z !== z) {
29747
+ this.center = { x, z };
29748
+ this.desired = [];
29749
+ for (let dz = -this.loadRadius; dz <= this.loadRadius; dz++) {
29750
+ for (let dx = -this.loadRadius; dx <= this.loadRadius; dx++) {
29751
+ this.desired.push({ x: x + dx, z: z + dz });
29752
+ }
29753
+ }
29754
+ this.desired.sort((a, b) => (a.x - x) ** 2 + (a.z - z) ** 2 - (b.x - x) ** 2 - (b.z - z) ** 2);
29755
+ if (this.loading && !this.within(this.loading.position, this.loadRadius)) {
29756
+ this.loading.controller.abort();
29757
+ }
29758
+ }
29759
+ return this.running ?? (this.running = Promise.resolve().then(() => this.drain()));
29760
+ }
29761
+ /** Updates from a camera or view-center position in scene units. One chunk spans 256 scene units. */
29762
+ async updatePosition(position) {
29763
+ if (![position.x, position.z].every(Number.isFinite)) throw new RangeError("Scene coordinates must be finite");
29764
+ return this.update(Math.floor(position.x / 256), Math.floor(position.z / 256));
29765
+ }
29766
+ /** Forgets recorded source failures and retries desired columns at the latest center. */
29767
+ async retryFailedChunks() {
29768
+ if (this.disposed) throw new Error("WorldStreamer is disposed");
29769
+ this.failed.clear();
29770
+ if (this.center) await this.update(this.center.x, this.center.z);
29771
+ }
29772
+ /** Aborts source reads, waits for active work, then unloads owned columns. The world and source remain caller-owned. */
29773
+ dispose() {
29774
+ this.disposed = true;
29775
+ this.desired = [];
29776
+ this.center = void 0;
29777
+ this.loading?.controller.abort();
29778
+ return this.disposal ?? (this.disposal = Promise.resolve().then(async () => {
29779
+ await this.running?.catch(() => void 0);
29780
+ let failure;
29781
+ let failed = false;
29782
+ for (const position of this.owned.values()) {
29783
+ try {
29784
+ await this.unload(position);
29785
+ } catch (error) {
29786
+ failed = true;
29787
+ failure ?? (failure = error);
29788
+ }
29789
+ }
29790
+ this.missing.clear();
29791
+ this.failed.clear();
29792
+ if (failed) throw failure;
29793
+ }).catch((error) => {
29794
+ this.disposal = void 0;
29795
+ throw error;
29796
+ }));
29797
+ }
29798
+ async drain() {
29799
+ try {
29800
+ while (!this.disposed) {
29801
+ for (const [key, position2] of this.missing) {
29802
+ if (!this.within(position2, this.unloadRadius)) this.missing.delete(key);
29803
+ }
29804
+ for (const [key, position2] of this.failed) {
29805
+ if (!this.within(position2, this.unloadRadius)) this.failed.delete(key);
29806
+ }
29807
+ const obsolete = [...this.owned.values()].find((position2) => !this.loaded.has(this.key(position2)) || !this.within(position2, this.unloadRadius));
29808
+ if (obsolete) {
29809
+ await this.unload(obsolete);
29810
+ continue;
29811
+ }
29812
+ const position = this.desired.find((position2) => this.needsLoad(position2));
29813
+ if (!position) return;
29814
+ let chunk;
29815
+ const controller = new AbortController();
29816
+ this.loading = { position, controller };
29817
+ try {
29818
+ chunk = await this.source.getChunk(position.x, position.z, controller.signal);
29819
+ if (chunk) this.validateChunk(chunk, position);
29820
+ } catch (error) {
29821
+ if (!controller.signal.aborted && !this.disposed && this.within(position, this.loadRadius)) {
29822
+ this.failed.set(this.key(position), { ...position, error });
29823
+ }
29824
+ continue;
29825
+ } finally {
29826
+ this.loading = void 0;
29827
+ }
29828
+ if (controller.signal.aborted || this.disposed || !this.within(position, this.loadRadius)) continue;
29829
+ if (!chunk) {
29830
+ this.missing.set(this.key(position), position);
29831
+ continue;
29832
+ }
29833
+ this.owned.set(this.key(position), position);
29834
+ try {
29835
+ await this.world.placeChunk(chunk);
29836
+ this.loaded.set(this.key(position), position);
29837
+ } catch (error) {
29838
+ try {
29839
+ await this.unload(position);
29840
+ } catch (cleanupError) {
29841
+ const failure = error instanceof Error ? error : new Error(String(error));
29842
+ try {
29843
+ Object.defineProperty(failure, "cause", { value: cleanupError, configurable: true });
29844
+ } catch {
29845
+ throw Object.assign(new Error(failure.message), { cause: cleanupError });
29846
+ }
29847
+ throw failure;
29848
+ }
29849
+ throw error;
29850
+ }
29851
+ }
29852
+ } finally {
29853
+ this.running = void 0;
29854
+ }
29855
+ }
29856
+ needsLoad(position) {
29857
+ const key = this.key(position);
29858
+ return !this.loaded.has(key) && !this.missing.has(key) && !this.failed.has(key);
29859
+ }
29860
+ async unload(position) {
29861
+ await this.world.unloadChunkColumn(position.x, position.z);
29862
+ this.owned.delete(this.key(position));
29863
+ this.loaded.delete(this.key(position));
29864
+ }
29865
+ validateChunk(chunk, position) {
29866
+ if (chunk.x !== position.x || chunk.z !== position.z) {
29867
+ throw new Error(`World source returned chunk ${chunk.x},${chunk.z} for ${position.x},${position.z}`);
29868
+ }
29869
+ }
29870
+ within(position, radius) {
29871
+ return !!this.center && Math.abs(position.x - this.center.x) <= radius && Math.abs(position.z - this.center.z) <= radius;
29872
+ }
29873
+ key(position) {
29874
+ return `${position.x},${position.z}`;
29875
+ }
29876
+ };
29412
29877
  //# sourceMappingURL=index.js.map