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.
@@ -1123,6 +1123,7 @@ var AssetKey = class _AssetKey extends BasicAssetKey {
1123
1123
  this.rootType = rootType;
1124
1124
  this.extension = extension;
1125
1125
  this.root = root;
1126
+ this.isAssetKey = true;
1126
1127
  }
1127
1128
  static parse(assetType, str, origin) {
1128
1129
  let namespace = origin?.namespace || DEFAULT_NAMESPACE;
@@ -1138,7 +1139,10 @@ var AssetKey = class _AssetKey extends BasicAssetKey {
1138
1139
  split.shift();
1139
1140
  }
1140
1141
  let path = split.join("/");
1141
- let extension = assetType === "textures" ? ".png" : origin?.assetType || ".json";
1142
+ const extension = assetType === "textures" ? ".png" : origin?.extension ?? ".json";
1143
+ if (extension && path.endsWith(extension)) {
1144
+ path = path.slice(0, -extension.length);
1145
+ }
1142
1146
  return new _AssetKey(namespace, path, assetType, type, "assets", extension, origin?.root);
1143
1147
  }
1144
1148
  toString() {
@@ -1168,10 +1172,10 @@ var AssetKey = class _AssetKey extends BasicAssetKey {
1168
1172
  }
1169
1173
  };
1170
1174
  function isBasicAssetKey(obj) {
1171
- return "namespace" in obj && "path" in obj;
1175
+ return obj !== null && typeof obj === "object" && typeof obj.namespace === "string" && typeof obj.path === "string";
1172
1176
  }
1173
1177
  function isAssetKey(obj) {
1174
- return isBasicAssetKey(obj) && "parse" in obj;
1178
+ return isBasicAssetKey(obj) && obj.isAssetKey === true;
1175
1179
  }
1176
1180
 
1177
1181
  // src/model/ModelMerger.ts
@@ -4222,6 +4226,16 @@ function isModelObject(obj) {
4222
4226
  var import_ts_deepmerge6 = __toESM(require_dist());
4223
4227
  import { Euler as Euler3, Matrix4 as Matrix46, Vector3 as Vector36 } from "three";
4224
4228
  var p10 = prefix("BlockObject");
4229
+ function matchesCondition(condition, state) {
4230
+ return Object.entries(condition).every(([key, value]) => {
4231
+ if (Array.isArray(value)) {
4232
+ if (key === "OR") return value.some((child) => matchesCondition(child, state));
4233
+ if (key === "AND") return value.every((child) => matchesCondition(child, state));
4234
+ return false;
4235
+ }
4236
+ return typeof value === "string" && state[key] !== void 0 && value.split("|").includes(`${state[key]}`);
4237
+ });
4238
+ }
4225
4239
  var _BlockObject = class _BlockObject extends SceneObject {
4226
4240
  constructor(blockState, options) {
4227
4241
  super(options);
@@ -4250,15 +4264,9 @@ var _BlockObject = class _BlockObject extends SceneObject {
4250
4264
  if (this.blockState.variants) {
4251
4265
  await this.setState(Object.keys(this.blockState.variants)[0]);
4252
4266
  } else if (this.blockState.multipart) {
4253
- for (let part of this.blockState.multipart) {
4254
- if (part.when && !("OR" in part.when)) {
4255
- const k = Object.keys(part.when)[0];
4256
- if (k) {
4257
- await this.setState(k, part.when[k].split("|")[0]);
4258
- break;
4259
- }
4260
- }
4261
- }
4267
+ const condition = this.blockState.multipart.map((part) => part.when).find((when) => when !== void 0 && Object.values(when).every((value) => typeof value === "string"));
4268
+ const state = Object.fromEntries(Object.entries(condition ?? {}).map(([key, value]) => [key, value.split("|")[0]]));
4269
+ await this.setState(state);
4262
4270
  }
4263
4271
  }
4264
4272
  } else {
@@ -4318,52 +4326,8 @@ var _BlockObject = class _BlockObject extends SceneObject {
4318
4326
  console.debug(p10, "Missing apply for blockState part", part);
4319
4327
  continue;
4320
4328
  }
4321
- if (!part.when) {
4329
+ if (!part.when || matchesCondition(part.when, state)) {
4322
4330
  out.push(this.getSingleVariant(part.apply));
4323
- } else {
4324
- let matches = true;
4325
- if ("OR" in part.when) {
4326
- const or = part.when.OR;
4327
- let anyMatch = false;
4328
- for (let o of or) {
4329
- for (let k in o) {
4330
- const split = o[k].split("|");
4331
- let m = false;
4332
- for (let s of split) {
4333
- if (`${state[k]}` === `${s}`) {
4334
- m = true;
4335
- break;
4336
- }
4337
- }
4338
- if (m) {
4339
- anyMatch = true;
4340
- break;
4341
- }
4342
- }
4343
- }
4344
- if (!anyMatch) {
4345
- matches = false;
4346
- }
4347
- } else {
4348
- for (let k in part.when) {
4349
- const split = part.when[k].split("|");
4350
- let m = false;
4351
- for (let s of split) {
4352
- if (`${state[k]}` === `${s}`) {
4353
- m = true;
4354
- break;
4355
- }
4356
- }
4357
- if (!m) {
4358
- matches = false;
4359
- break;
4360
- }
4361
- }
4362
- }
4363
- if (matches) {
4364
- const variants = part.apply;
4365
- out.push(this.getSingleVariant(variants));
4366
- }
4367
4331
  }
4368
4332
  }
4369
4333
  }
@@ -4393,11 +4357,21 @@ var _BlockObject = class _BlockObject extends SceneObject {
4393
4357
  }
4394
4358
  }
4395
4359
  getSingleVariant(variants) {
4396
- if (Array.isArray(variants)) {
4397
- return variants[0];
4398
- } else {
4399
- return variants;
4400
- }
4360
+ if (!Array.isArray(variants)) return variants;
4361
+ if (!variants.length) throw new MineRenderError("Blockstate variant arrays must not be empty");
4362
+ const total = variants.reduce((sum, variant) => {
4363
+ const weight = variant.weight ?? 1;
4364
+ if (!Number.isInteger(weight) || weight < 1) {
4365
+ throw new MineRenderError(`Invalid blockstate variant weight: ${weight}`);
4366
+ }
4367
+ return sum + weight;
4368
+ }, 0);
4369
+ let choice = Math.random() * total;
4370
+ for (const variant of variants) {
4371
+ choice -= variant.weight ?? 1;
4372
+ if (choice < 0) return variant;
4373
+ }
4374
+ return variants[variants.length - 1];
4401
4375
  }
4402
4376
  // @deprecated
4403
4377
  async createAVariant(variants, instanceInfo) {