minerender 2.1.0-alpha.16 → 2.1.0-alpha.18

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.
@@ -174,6 +174,7 @@ declare class AssetKey extends BasicAssetKey {
174
174
  rootType: "assets" | "data" | string;
175
175
  extension: ".json" | ".png" | string;
176
176
  root?: string | undefined;
177
+ readonly isAssetKey: true;
177
178
  constructor(namespace: string, path: string, assetType?: AssetType | undefined, type?: string | undefined, rootType?: "assets" | "data" | string, extension?: ".json" | ".png" | string, root?: string | undefined);
178
179
  static parse(assetType: AssetType, str: string, origin?: AssetKey): AssetKey;
179
180
  toString(): string;
@@ -690,16 +691,17 @@ interface BlockStateVariant {
690
691
  y?: number;
691
692
  x?: number;
692
693
  uvlock?: boolean;
694
+ weight?: number;
693
695
  }
694
696
  interface BlockStateMultipart {
695
- when?: MultipartCondition | {
696
- OR: MultipartCondition[];
697
- };
698
- apply?: BlockStateVariant;
699
- }
700
- interface MultipartCondition {
701
- [key: string]: string;
697
+ when?: MultipartCondition;
698
+ apply?: BlockStateVariant | BlockStateVariant[];
702
699
  }
700
+ type MultipartCondition = Record<string, string> | {
701
+ OR: MultipartCondition[];
702
+ } | {
703
+ AND: MultipartCondition[];
704
+ };
703
705
 
704
706
  declare const DEFAULT_ROOT = "https://assets.mcasset.cloud/1.21.11";
705
707
  declare const DEFAULT_NAMESPACE = "minecraft";
@@ -174,6 +174,7 @@ declare class AssetKey extends BasicAssetKey {
174
174
  rootType: "assets" | "data" | string;
175
175
  extension: ".json" | ".png" | string;
176
176
  root?: string | undefined;
177
+ readonly isAssetKey: true;
177
178
  constructor(namespace: string, path: string, assetType?: AssetType | undefined, type?: string | undefined, rootType?: "assets" | "data" | string, extension?: ".json" | ".png" | string, root?: string | undefined);
178
179
  static parse(assetType: AssetType, str: string, origin?: AssetKey): AssetKey;
179
180
  toString(): string;
@@ -690,16 +691,17 @@ interface BlockStateVariant {
690
691
  y?: number;
691
692
  x?: number;
692
693
  uvlock?: boolean;
694
+ weight?: number;
693
695
  }
694
696
  interface BlockStateMultipart {
695
- when?: MultipartCondition | {
696
- OR: MultipartCondition[];
697
- };
698
- apply?: BlockStateVariant;
699
- }
700
- interface MultipartCondition {
701
- [key: string]: string;
697
+ when?: MultipartCondition;
698
+ apply?: BlockStateVariant | BlockStateVariant[];
702
699
  }
700
+ type MultipartCondition = Record<string, string> | {
701
+ OR: MultipartCondition[];
702
+ } | {
703
+ AND: MultipartCondition[];
704
+ };
703
705
 
704
706
  declare const DEFAULT_ROOT = "https://assets.mcasset.cloud/1.21.11";
705
707
  declare const DEFAULT_NAMESPACE = "minecraft";
@@ -1281,6 +1281,7 @@ var AssetKey = class _AssetKey extends BasicAssetKey {
1281
1281
  this.rootType = rootType;
1282
1282
  this.extension = extension;
1283
1283
  this.root = root;
1284
+ this.isAssetKey = true;
1284
1285
  }
1285
1286
  static parse(assetType, str, origin) {
1286
1287
  let namespace = origin?.namespace || DEFAULT_NAMESPACE;
@@ -1296,7 +1297,10 @@ var AssetKey = class _AssetKey extends BasicAssetKey {
1296
1297
  split.shift();
1297
1298
  }
1298
1299
  let path = split.join("/");
1299
- let extension = assetType === "textures" ? ".png" : origin?.assetType || ".json";
1300
+ const extension = assetType === "textures" ? ".png" : origin?.extension ?? ".json";
1301
+ if (extension && path.endsWith(extension)) {
1302
+ path = path.slice(0, -extension.length);
1303
+ }
1300
1304
  return new _AssetKey(namespace, path, assetType, type, "assets", extension, origin?.root);
1301
1305
  }
1302
1306
  toString() {
@@ -1326,10 +1330,10 @@ var AssetKey = class _AssetKey extends BasicAssetKey {
1326
1330
  }
1327
1331
  };
1328
1332
  function isBasicAssetKey(obj) {
1329
- return "namespace" in obj && "path" in obj;
1333
+ return obj !== null && typeof obj === "object" && typeof obj.namespace === "string" && typeof obj.path === "string";
1330
1334
  }
1331
1335
  function isAssetKey(obj) {
1332
- return isBasicAssetKey(obj) && "parse" in obj;
1336
+ return isBasicAssetKey(obj) && obj.isAssetKey === true;
1333
1337
  }
1334
1338
 
1335
1339
  // src/model/ModelMerger.ts
@@ -4409,6 +4413,16 @@ function isModelObject(obj) {
4409
4413
  var import_ts_deepmerge6 = __toESM(require_dist());
4410
4414
  var import_three16 = require("three");
4411
4415
  var p10 = prefix("BlockObject");
4416
+ function matchesCondition(condition, state) {
4417
+ return Object.entries(condition).every(([key, value]) => {
4418
+ if (Array.isArray(value)) {
4419
+ if (key === "OR") return value.some((child) => matchesCondition(child, state));
4420
+ if (key === "AND") return value.every((child) => matchesCondition(child, state));
4421
+ return false;
4422
+ }
4423
+ return typeof value === "string" && state[key] !== void 0 && value.split("|").includes(`${state[key]}`);
4424
+ });
4425
+ }
4412
4426
  var BlockObject = class _BlockObject extends SceneObject {
4413
4427
  constructor(blockState, options) {
4414
4428
  super(options);
@@ -4442,15 +4456,9 @@ var BlockObject = class _BlockObject extends SceneObject {
4442
4456
  if (this.blockState.variants) {
4443
4457
  await this.setState(Object.keys(this.blockState.variants)[0]);
4444
4458
  } else if (this.blockState.multipart) {
4445
- for (let part of this.blockState.multipart) {
4446
- if (part.when && !("OR" in part.when)) {
4447
- const k = Object.keys(part.when)[0];
4448
- if (k) {
4449
- await this.setState(k, part.when[k].split("|")[0]);
4450
- break;
4451
- }
4452
- }
4453
- }
4459
+ const condition = this.blockState.multipart.map((part) => part.when).find((when) => when !== void 0 && Object.values(when).every((value) => typeof value === "string"));
4460
+ const state = Object.fromEntries(Object.entries(condition ?? {}).map(([key, value]) => [key, value.split("|")[0]]));
4461
+ await this.setState(state);
4454
4462
  }
4455
4463
  }
4456
4464
  } else {
@@ -4510,52 +4518,8 @@ var BlockObject = class _BlockObject extends SceneObject {
4510
4518
  console.debug(p10, "Missing apply for blockState part", part);
4511
4519
  continue;
4512
4520
  }
4513
- if (!part.when) {
4521
+ if (!part.when || matchesCondition(part.when, state)) {
4514
4522
  out.push(this.getSingleVariant(part.apply));
4515
- } else {
4516
- let matches = true;
4517
- if ("OR" in part.when) {
4518
- const or = part.when.OR;
4519
- let anyMatch = false;
4520
- for (let o of or) {
4521
- for (let k in o) {
4522
- const split = o[k].split("|");
4523
- let m = false;
4524
- for (let s of split) {
4525
- if (`${state[k]}` === `${s}`) {
4526
- m = true;
4527
- break;
4528
- }
4529
- }
4530
- if (m) {
4531
- anyMatch = true;
4532
- break;
4533
- }
4534
- }
4535
- }
4536
- if (!anyMatch) {
4537
- matches = false;
4538
- }
4539
- } else {
4540
- for (let k in part.when) {
4541
- const split = part.when[k].split("|");
4542
- let m = false;
4543
- for (let s of split) {
4544
- if (`${state[k]}` === `${s}`) {
4545
- m = true;
4546
- break;
4547
- }
4548
- }
4549
- if (!m) {
4550
- matches = false;
4551
- break;
4552
- }
4553
- }
4554
- }
4555
- if (matches) {
4556
- const variants = part.apply;
4557
- out.push(this.getSingleVariant(variants));
4558
- }
4559
4523
  }
4560
4524
  }
4561
4525
  }
@@ -4585,11 +4549,21 @@ var BlockObject = class _BlockObject extends SceneObject {
4585
4549
  }
4586
4550
  }
4587
4551
  getSingleVariant(variants) {
4588
- if (Array.isArray(variants)) {
4589
- return variants[0];
4590
- } else {
4591
- return variants;
4592
- }
4552
+ if (!Array.isArray(variants)) return variants;
4553
+ if (!variants.length) throw new MineRenderError("Blockstate variant arrays must not be empty");
4554
+ const total = variants.reduce((sum, variant) => {
4555
+ const weight = variant.weight ?? 1;
4556
+ if (!Number.isInteger(weight) || weight < 1) {
4557
+ throw new MineRenderError(`Invalid blockstate variant weight: ${weight}`);
4558
+ }
4559
+ return sum + weight;
4560
+ }, 0);
4561
+ let choice = Math.random() * total;
4562
+ for (const variant of variants) {
4563
+ choice -= variant.weight ?? 1;
4564
+ if (choice < 0) return variant;
4565
+ }
4566
+ return variants[variants.length - 1];
4593
4567
  }
4594
4568
  // @deprecated
4595
4569
  async createAVariant(variants, instanceInfo) {