sandstone 1.2.1 → 1.2.2

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.
@@ -4471,6 +4471,7 @@ WorldBorderCommand = class WorldBorderCommand extends CommandArguments {
4471
4471
  };
4472
4472
  };
4473
4473
  ConditionNode = class ConditionNode extends Node {
4474
+ preNodes;
4474
4475
  };
4475
4476
  SingleConditionNode = class SingleConditionNode extends ConditionNode {
4476
4477
  getValue = (negated = false) => {
@@ -4637,9 +4638,13 @@ DataPointEqualsConditionNode = class DataPointEqualsConditionNode extends Single
4637
4638
  this.value = value;
4638
4639
  const { DataVariable, Variable, commands: commands2 } = sandstoneCore2.pack;
4639
4640
  const { execute: execute2 } = commands2;
4641
+ const mcFunction = sandstoneCore2.getCurrentMCFunctionOrThrow();
4642
+ const before = mcFunction.body.length;
4640
4643
  const anon = DataVariable(this.dataPoint);
4641
4644
  this.conditional = Variable();
4642
4645
  execute2.store.result(this.conditional).run(() => anon.set(this.value));
4646
+ const after = mcFunction.body.length;
4647
+ this.preNodes = mcFunction.body.splice(before, after - before);
4643
4648
  }
4644
4649
  getValue = (negated) => (negated ? ["if", ...this.getCondition()] : ["unless", ...this.getCondition()]).join(" ");
4645
4650
  getCondition() {
@@ -4728,7 +4733,11 @@ CommandConditionNode = class CommandConditionNode extends SingleConditionNode {
4728
4733
  this.result = result;
4729
4734
  const store = sandstoneCore2.pack.commands.execute.store[type];
4730
4735
  this.variable = sandstoneCore2.pack.Variable(undefined, "condition");
4736
+ const mcFunction = sandstoneCore2.getCurrentMCFunctionOrThrow();
4737
+ const before = mcFunction.body.length;
4731
4738
  command(store(this.variable));
4739
+ const after = mcFunction.body.length;
4740
+ this.preNodes = mcFunction.body.splice(before, after - before);
4732
4741
  }
4733
4742
  getCondition() {
4734
4743
  if (this.type === "success") {
@@ -5225,54 +5234,186 @@ LoopTransformationVisitor = class LoopTransformationVisitor extends GenericSands
5225
5234
  visitForINode = this.visitLoopNode;
5226
5235
  visitForOfNode = this.visitLoopNode;
5227
5236
  };
5237
+ FunctionReturnConditionNode = class FunctionReturnConditionNode extends ConditionNode {
5238
+ sandstoneCore;
5239
+ functionName;
5240
+ constructor(sandstoneCore2, functionName) {
5241
+ super(sandstoneCore2);
5242
+ this.sandstoneCore = sandstoneCore2;
5243
+ this.functionName = functionName;
5244
+ }
5245
+ getValue = () => `if function ${this.functionName}`;
5246
+ };
5228
5247
  OrTransformationVisitor = class OrTransformationVisitor extends GenericSandstoneVisitor {
5229
5248
  visitIfNode = (node_) => {
5230
- const { preNodes, conditionNode } = this.parseConditionNode(node_.condition);
5249
+ const { preNodes, conditionNode } = this.parseConditionNode(node_.condition, node_.parentMCFunction);
5231
5250
  node_.condition = conditionNode;
5232
5251
  return [...preNodes, this.genericVisit(node_)];
5233
5252
  };
5234
- parseConditionNode = (node) => {
5253
+ parseConditionNode = (node, parentMCFunction) => {
5235
5254
  if (node instanceof OrNode) {
5236
- return this.parseOrNode(node);
5255
+ return this.parseOrNode(node, parentMCFunction);
5237
5256
  }
5238
5257
  if (node instanceof AndNode) {
5239
- return this.parseAndNode(node);
5258
+ return this.parseAndNode(node, parentMCFunction);
5240
5259
  }
5241
5260
  if (node instanceof NotNode) {
5242
- return this.parseNotNode(node);
5261
+ return this.parseNotNode(node, parentMCFunction);
5243
5262
  }
5244
- return { preNodes: [], conditionNode: node };
5263
+ return { preNodes: [...node.preNodes ?? []], conditionNode: node };
5245
5264
  };
5246
- parseOrNode = (node) => {
5247
- const variable = this.core.pack.Variable(undefined, "condition");
5248
- const conditionNode = variable.equalTo(1)._toMinecraftCondition();
5249
- const realPreNodes = [new ScoreboardCommandNode(this.pack, "players", "reset", variable)];
5250
- const orExecuteNodes = node.conditions.map((condition2) => {
5251
- const { preNodes, conditionNode: parsedConditionNode } = this.parseConditionNode(condition2);
5252
- realPreNodes.push(...preNodes);
5253
- const ifNode = new IfNode(this.core, parsedConditionNode);
5254
- ifNode.body = [new ScoreboardCommandNode(this.pack, "players", "set", variable, 1)];
5255
- return ifNode;
5265
+ parseOrNode = (node, parentMCFunction) => {
5266
+ if (node.conditions.length === 1) {
5267
+ return this.parseConditionNode(node.conditions[0], parentMCFunction);
5268
+ }
5269
+ const parentName = parentMCFunction?.resource.name ?? "__sandstone";
5270
+ const orMCFunction = new MCFunctionClass(this.core, `${parentName}/or_check`, {
5271
+ addToSandstoneCore: true,
5272
+ creator: "sandstone",
5273
+ onConflict: "rename"
5256
5274
  });
5275
+ this.core.enterMCFunction(orMCFunction);
5276
+ try {
5277
+ for (const condition2 of node.conditions) {
5278
+ this.pushCheckBranch(orMCFunction.node, this.parseConditionNode(condition2, orMCFunction.node), false, new ReturnCommandNode(this.pack, [1]));
5279
+ }
5280
+ orMCFunction.node.body.push(new ReturnCommandNode(this.pack, [0]));
5281
+ } finally {
5282
+ this.core.exitMCFunction();
5283
+ }
5257
5284
  return {
5258
- preNodes: [...realPreNodes, ...orExecuteNodes],
5259
- conditionNode
5285
+ preNodes: [],
5286
+ conditionNode: new FunctionReturnConditionNode(this.core, orMCFunction.name)
5260
5287
  };
5261
5288
  };
5262
- parseAndNode = (node) => {
5263
- const finalPreNodes = [];
5264
- const conditionNode = new AndNode(node.sandstoneCore, node.conditions.map((condition2) => {
5265
- const { preNodes, conditionNode: conditionNode2 } = this.parseConditionNode(condition2);
5266
- finalPreNodes.push(...preNodes);
5267
- return conditionNode2;
5268
- }));
5289
+ parseAndNode = (node, parentMCFunction) => {
5290
+ if (node.conditions.length === 1) {
5291
+ return this.parseConditionNode(node.conditions[0], parentMCFunction);
5292
+ }
5293
+ const parsed = node.conditions.map((condition2) => this.parseConditionNode(condition2, parentMCFunction));
5294
+ const anyHasCommands = parsed.some((p) => (p.preNodes ?? []).length > 0);
5295
+ if (!anyHasCommands) {
5296
+ const finalPreNodes = [];
5297
+ const conditionNode = new AndNode(node.sandstoneCore, parsed.map((p) => {
5298
+ finalPreNodes.push(...p.preNodes);
5299
+ return p.conditionNode;
5300
+ }));
5301
+ return { preNodes: finalPreNodes, conditionNode };
5302
+ }
5303
+ const parentName = parentMCFunction?.resource.name ?? "__sandstone";
5304
+ const andMCFunction = new MCFunctionClass(this.core, `${parentName}/and_check`, {
5305
+ addToSandstoneCore: true,
5306
+ creator: "sandstone",
5307
+ onConflict: "rename"
5308
+ });
5309
+ this.core.enterMCFunction(andMCFunction);
5310
+ try {
5311
+ let batch = { preNodes: [], conditionNodes: [] };
5312
+ const flushBatch = () => {
5313
+ if (batch.conditionNodes.length === 0 && batch.preNodes.every((pn) => pn.length === 0)) {
5314
+ batch = { preNodes: [], conditionNodes: [] };
5315
+ return;
5316
+ }
5317
+ const tracker = this.core.pack.Variable(undefined, "and_batch");
5318
+ this.pushBatchedCheckBranch(andMCFunction.node, batch.preNodes.flat(), batch.conditionNodes, tracker);
5319
+ batch = { preNodes: [], conditionNodes: [] };
5320
+ };
5321
+ for (const p of parsed) {
5322
+ if (p.preNodes.length === 0) {
5323
+ batch.preNodes.push(p.preNodes);
5324
+ batch.conditionNodes.push(p.conditionNode);
5325
+ } else {
5326
+ flushBatch();
5327
+ this.pushFailFastBranch(andMCFunction.node, p, new ReturnCommandNode(this.pack, [0]));
5328
+ }
5329
+ }
5330
+ flushBatch();
5331
+ andMCFunction.node.body.push(new ReturnCommandNode(this.pack, [1]));
5332
+ } finally {
5333
+ this.core.exitMCFunction();
5334
+ }
5269
5335
  return {
5270
- preNodes: finalPreNodes,
5271
- conditionNode
5336
+ preNodes: [],
5337
+ conditionNode: new FunctionReturnConditionNode(this.core, andMCFunction.name)
5272
5338
  };
5273
5339
  };
5274
- parseNotNode = (node) => {
5275
- const { preNodes, conditionNode } = this.parseConditionNode(node.condition);
5340
+ pushBatchedCheckBranch = (targetMCFunction, preNodes, conditionNodes, tracker) => {
5341
+ for (const n of preNodes) {
5342
+ targetMCFunction.body.push(n);
5343
+ }
5344
+ targetMCFunction.body.push(new ScoreboardCommandNode(this.pack, "players", "reset", tracker));
5345
+ const args = [];
5346
+ for (const c of conditionNodes) {
5347
+ const condStr = c.getValue(false);
5348
+ const match = condStr.match(/^(if|unless)\s+([\s\S]+)$/);
5349
+ if (!match) {
5350
+ throw new Error(`OrTransformationVisitor: unexpected condition getValue() shape: ${condStr}`);
5351
+ }
5352
+ const [, keyword, condBody] = match;
5353
+ args.push([keyword, condBody]);
5354
+ }
5355
+ if (args.length === 0) {
5356
+ targetMCFunction.body.push(new ScoreboardCommandNode(this.pack, "players", "set", tracker, 1));
5357
+ } else {
5358
+ const executeNode = new ExecuteCommandNode(this.pack, args, {
5359
+ isSingleExecute: true,
5360
+ body: []
5361
+ });
5362
+ targetMCFunction.enterContext(executeNode, false);
5363
+ executeNode.body = [new ScoreboardCommandNode(this.pack, "players", "set", tracker, 1)];
5364
+ targetMCFunction.body.push(executeNode);
5365
+ targetMCFunction.exitContext();
5366
+ }
5367
+ const failCheckNode = new ExecuteCommandNode(this.pack, [["unless", `score ${tracker} matches 1`]], {
5368
+ isSingleExecute: true,
5369
+ body: []
5370
+ });
5371
+ targetMCFunction.enterContext(failCheckNode, false);
5372
+ failCheckNode.body = [new ReturnCommandNode(this.pack, [0])];
5373
+ targetMCFunction.body.push(failCheckNode);
5374
+ targetMCFunction.exitContext();
5375
+ };
5376
+ pushCheckBranch = (targetMCFunction, parsed, negated, bodyNode) => {
5377
+ for (const n of parsed.preNodes) {
5378
+ targetMCFunction.body.push(n);
5379
+ }
5380
+ const condStr = parsed.conditionNode.getValue(negated);
5381
+ const match = condStr.match(/^(if|unless)\s+([\s\S]+)$/);
5382
+ if (!match) {
5383
+ throw new Error(`OrTransformationVisitor: unexpected condition getValue() shape: ${condStr}`);
5384
+ }
5385
+ const [, keyword, condBody] = match;
5386
+ const executeNode = new ExecuteCommandNode(this.pack, [[keyword, condBody]], {
5387
+ isSingleExecute: true,
5388
+ body: []
5389
+ });
5390
+ targetMCFunction.enterContext(executeNode, false);
5391
+ executeNode.body = [bodyNode];
5392
+ targetMCFunction.body.push(executeNode);
5393
+ targetMCFunction.exitContext();
5394
+ };
5395
+ pushFailFastBranch = (targetMCFunction, parsed, bodyNode) => {
5396
+ for (const n of parsed.preNodes) {
5397
+ targetMCFunction.body.push(n);
5398
+ }
5399
+ const condStr = parsed.conditionNode.getValue(true);
5400
+ const match = condStr.match(/^(if|unless)\s+([\s\S]+)$/);
5401
+ if (!match) {
5402
+ throw new Error(`OrTransformationVisitor: unexpected condition getValue() shape: ${condStr}`);
5403
+ }
5404
+ const flippedKeyword = match[1] === "if" ? "unless" : "if";
5405
+ const condBody = match[2];
5406
+ const executeNode = new ExecuteCommandNode(this.pack, [[flippedKeyword, condBody]], {
5407
+ isSingleExecute: true,
5408
+ body: []
5409
+ });
5410
+ targetMCFunction.enterContext(executeNode, false);
5411
+ executeNode.body = [bodyNode];
5412
+ targetMCFunction.body.push(executeNode);
5413
+ targetMCFunction.exitContext();
5414
+ };
5415
+ parseNotNode = (node, parentMCFunction) => {
5416
+ const { preNodes, conditionNode } = this.parseConditionNode(node.condition, parentMCFunction);
5276
5417
  return {
5277
5418
  preNodes,
5278
5419
  conditionNode: new NotNode(node.sandstoneCore, conditionNode)
@@ -6526,6 +6667,7 @@ SleepClass = class SleepClass extends AwaitNode {
6526
6667
  // src/variables/abstractClasses.ts
6527
6668
 
6528
6669
  class ConditionClass {
6670
+ preNodes;
6529
6671
  _toMinecraftCondition() {
6530
6672
  throw new Error("Not implemented");
6531
6673
  }
@@ -10667,12 +10809,13 @@ var init_loopTransformationVisitor = __esm(() => {
10667
10809
  });
10668
10810
 
10669
10811
  // src/pack/visitors/orTransformationVisitor.ts
10670
- var OrTransformationVisitor;
10812
+ var FunctionReturnConditionNode, OrTransformationVisitor;
10671
10813
  var init_orTransformationVisitor = __esm(() => {
10672
10814
  init_commands2();
10815
+ init_core();
10673
10816
  init_flow();
10674
10817
  init_visitor();
10675
- });
10818
+ });
10676
10819
 
10677
10820
  // src/pack/visitors/optimizeMacroTemporaries.ts
10678
10821
  var flattenArgs = (args) => args.flatMap((arg) => Array.isArray(arg) ? flattenArgs(arg) : [arg]), referencesStoragePoint = (node, point) => {
@@ -10914,10 +11057,11 @@ class SandstonePack {
10914
11057
  return pack;
10915
11058
  }
10916
11059
  setupLantern = () => {
10917
- const loadStatus = this.Objective.create("load.status", "dummy", undefined, { useDefaultNamespace: false });
11060
+ const loadStatus = this.Objective.create("load.status", "dummy", undefined, { useDefaultNamespace: false, alreadyExists: true });
10918
11061
  const privateInit = this.Tag("function", "load:_private/init", [
10919
11062
  this.MCFunction("load:_private/init", () => {
10920
11063
  this.commands.comment("Reset scoreboards so packs can set values accurate for current load.");
11064
+ this.commands.scoreboard.objectives.add(loadStatus.name, "dummy");
10921
11065
  loadStatus.reset();
10922
11066
  })
10923
11067
  ]);
@@ -55801,5 +55945,5 @@ export {
55801
55945
  ACTIVITIES_SET
55802
55946
  };
55803
55947
 
55804
- //# debugId=42B8EBF3535D96CF64756E2164756E21
55948
+ //# debugId=9989C4C6C7550F2364756E2164756E21
55805
55949
  //# sourceMappingURL=index.js.map