minerender 2.1.0-alpha.16 → 2.1.0-alpha.17

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.
@@ -4264,6 +4264,16 @@ function isModelObject(obj) {
4264
4264
  var import_ts_deepmerge6 = __toESM(require_dist());
4265
4265
  import { Euler as Euler3, Matrix4 as Matrix46, Vector3 as Vector36 } from "three";
4266
4266
  var p10 = prefix("BlockObject");
4267
+ function matchesCondition(condition, state) {
4268
+ return Object.entries(condition).every(([key, value]) => {
4269
+ if (Array.isArray(value)) {
4270
+ if (key === "OR") return value.some((child) => matchesCondition(child, state));
4271
+ if (key === "AND") return value.every((child) => matchesCondition(child, state));
4272
+ return false;
4273
+ }
4274
+ return typeof value === "string" && state[key] !== void 0 && value.split("|").includes(`${state[key]}`);
4275
+ });
4276
+ }
4267
4277
  var BlockObject = class _BlockObject extends SceneObject {
4268
4278
  constructor(blockState, options) {
4269
4279
  super(options);
@@ -4297,15 +4307,9 @@ var BlockObject = class _BlockObject extends SceneObject {
4297
4307
  if (this.blockState.variants) {
4298
4308
  await this.setState(Object.keys(this.blockState.variants)[0]);
4299
4309
  } else if (this.blockState.multipart) {
4300
- for (let part of this.blockState.multipart) {
4301
- if (part.when && !("OR" in part.when)) {
4302
- const k = Object.keys(part.when)[0];
4303
- if (k) {
4304
- await this.setState(k, part.when[k].split("|")[0]);
4305
- break;
4306
- }
4307
- }
4308
- }
4310
+ const condition = this.blockState.multipart.map((part) => part.when).find((when) => when !== void 0 && Object.values(when).every((value) => typeof value === "string"));
4311
+ const state = Object.fromEntries(Object.entries(condition ?? {}).map(([key, value]) => [key, value.split("|")[0]]));
4312
+ await this.setState(state);
4309
4313
  }
4310
4314
  }
4311
4315
  } else {
@@ -4365,52 +4369,8 @@ var BlockObject = class _BlockObject extends SceneObject {
4365
4369
  console.debug(p10, "Missing apply for blockState part", part);
4366
4370
  continue;
4367
4371
  }
4368
- if (!part.when) {
4372
+ if (!part.when || matchesCondition(part.when, state)) {
4369
4373
  out.push(this.getSingleVariant(part.apply));
4370
- } else {
4371
- let matches = true;
4372
- if ("OR" in part.when) {
4373
- const or = part.when.OR;
4374
- let anyMatch = false;
4375
- for (let o of or) {
4376
- for (let k in o) {
4377
- const split = o[k].split("|");
4378
- let m = false;
4379
- for (let s of split) {
4380
- if (`${state[k]}` === `${s}`) {
4381
- m = true;
4382
- break;
4383
- }
4384
- }
4385
- if (m) {
4386
- anyMatch = true;
4387
- break;
4388
- }
4389
- }
4390
- }
4391
- if (!anyMatch) {
4392
- matches = false;
4393
- }
4394
- } else {
4395
- for (let k in part.when) {
4396
- const split = part.when[k].split("|");
4397
- let m = false;
4398
- for (let s of split) {
4399
- if (`${state[k]}` === `${s}`) {
4400
- m = true;
4401
- break;
4402
- }
4403
- }
4404
- if (!m) {
4405
- matches = false;
4406
- break;
4407
- }
4408
- }
4409
- }
4410
- if (matches) {
4411
- const variants = part.apply;
4412
- out.push(this.getSingleVariant(variants));
4413
- }
4414
4374
  }
4415
4375
  }
4416
4376
  }
@@ -4440,11 +4400,21 @@ var BlockObject = class _BlockObject extends SceneObject {
4440
4400
  }
4441
4401
  }
4442
4402
  getSingleVariant(variants) {
4443
- if (Array.isArray(variants)) {
4444
- return variants[0];
4445
- } else {
4446
- return variants;
4447
- }
4403
+ if (!Array.isArray(variants)) return variants;
4404
+ if (!variants.length) throw new MineRenderError("Blockstate variant arrays must not be empty");
4405
+ const total = variants.reduce((sum, variant) => {
4406
+ const weight = variant.weight ?? 1;
4407
+ if (!Number.isInteger(weight) || weight < 1) {
4408
+ throw new MineRenderError(`Invalid blockstate variant weight: ${weight}`);
4409
+ }
4410
+ return sum + weight;
4411
+ }, 0);
4412
+ let choice = Math.random() * total;
4413
+ for (const variant of variants) {
4414
+ choice -= variant.weight ?? 1;
4415
+ if (choice < 0) return variant;
4416
+ }
4417
+ return variants[variants.length - 1];
4448
4418
  }
4449
4419
  // @deprecated
4450
4420
  async createAVariant(variants, instanceInfo) {