sandstone 1.1.4 → 1.1.5

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.
@@ -4408,6 +4408,7 @@ WorldBorderCommand = class WorldBorderCommand extends CommandArguments {
4408
4408
  };
4409
4409
  };
4410
4410
  ConditionNode = class ConditionNode extends Node {
4411
+ preNodes;
4411
4412
  };
4412
4413
  SingleConditionNode = class SingleConditionNode extends ConditionNode {
4413
4414
  getValue = (negated = false) => {
@@ -4564,9 +4565,13 @@ DataPointEqualsConditionNode = class DataPointEqualsConditionNode extends Single
4564
4565
  this.value = value;
4565
4566
  const { DataVariable, Variable, commands: commands2 } = sandstoneCore2.pack;
4566
4567
  const { execute: execute2 } = commands2;
4568
+ const mcFunction = sandstoneCore2.getCurrentMCFunctionOrThrow();
4569
+ const before = mcFunction.body.length;
4567
4570
  const anon = DataVariable(this.dataPoint);
4568
4571
  this.conditional = Variable();
4569
4572
  execute2.store.result(this.conditional).run(() => anon.set(this.value));
4573
+ const after = mcFunction.body.length;
4574
+ this.preNodes = mcFunction.body.splice(before, after - before);
4570
4575
  }
4571
4576
  getValue = (negated) => (negated ? ["if", ...this.getCondition()] : ["unless", ...this.getCondition()]).join(" ");
4572
4577
  getCondition() {
@@ -4631,7 +4636,11 @@ CommandConditionNode = class CommandConditionNode extends SingleConditionNode {
4631
4636
  this.result = result;
4632
4637
  const store = sandstoneCore2.pack.commands.execute.store[type];
4633
4638
  this.variable = sandstoneCore2.pack.Variable(undefined, "condition");
4639
+ const mcFunction = sandstoneCore2.getCurrentMCFunctionOrThrow();
4640
+ const before = mcFunction.body.length;
4634
4641
  command(store(this.variable));
4642
+ const after = mcFunction.body.length;
4643
+ this.preNodes = mcFunction.body.splice(before, after - before);
4635
4644
  }
4636
4645
  getCondition() {
4637
4646
  if (this.type === "success") {
@@ -5128,54 +5137,186 @@ LoopTransformationVisitor = class LoopTransformationVisitor extends GenericSands
5128
5137
  visitForINode = this.visitLoopNode;
5129
5138
  visitForOfNode = this.visitLoopNode;
5130
5139
  };
5140
+ FunctionReturnConditionNode = class FunctionReturnConditionNode extends ConditionNode {
5141
+ sandstoneCore;
5142
+ functionName;
5143
+ constructor(sandstoneCore2, functionName) {
5144
+ super(sandstoneCore2);
5145
+ this.sandstoneCore = sandstoneCore2;
5146
+ this.functionName = functionName;
5147
+ }
5148
+ getValue = () => `if function ${this.functionName}`;
5149
+ };
5131
5150
  OrTransformationVisitor = class OrTransformationVisitor extends GenericSandstoneVisitor {
5132
5151
  visitIfNode = (node_) => {
5133
- const { preNodes, conditionNode } = this.parseConditionNode(node_.condition);
5152
+ const { preNodes, conditionNode } = this.parseConditionNode(node_.condition, node_.parentMCFunction);
5134
5153
  node_.condition = conditionNode;
5135
5154
  return [...preNodes, this.genericVisit(node_)];
5136
5155
  };
5137
- parseConditionNode = (node) => {
5156
+ parseConditionNode = (node, parentMCFunction) => {
5138
5157
  if (node instanceof OrNode) {
5139
- return this.parseOrNode(node);
5158
+ return this.parseOrNode(node, parentMCFunction);
5140
5159
  }
5141
5160
  if (node instanceof AndNode) {
5142
- return this.parseAndNode(node);
5161
+ return this.parseAndNode(node, parentMCFunction);
5143
5162
  }
5144
5163
  if (node instanceof NotNode) {
5145
- return this.parseNotNode(node);
5164
+ return this.parseNotNode(node, parentMCFunction);
5146
5165
  }
5147
- return { preNodes: [], conditionNode: node };
5166
+ return { preNodes: [...node.preNodes ?? []], conditionNode: node };
5148
5167
  };
5149
- parseOrNode = (node) => {
5150
- const variable = this.core.pack.Variable(undefined, "condition");
5151
- const conditionNode = variable.equalTo(1)._toMinecraftCondition();
5152
- const realPreNodes = [new ScoreboardCommandNode(this.pack, "players", "reset", variable)];
5153
- const orExecuteNodes = node.conditions.map((condition2) => {
5154
- const { preNodes, conditionNode: parsedConditionNode } = this.parseConditionNode(condition2);
5155
- realPreNodes.push(...preNodes);
5156
- const ifNode = new IfNode(this.core, parsedConditionNode);
5157
- ifNode.body = [new ScoreboardCommandNode(this.pack, "players", "set", variable, 1)];
5158
- return ifNode;
5168
+ parseOrNode = (node, parentMCFunction) => {
5169
+ if (node.conditions.length === 1) {
5170
+ return this.parseConditionNode(node.conditions[0], parentMCFunction);
5171
+ }
5172
+ const parentName = parentMCFunction?.resource.name ?? "__sandstone";
5173
+ const orMCFunction = new MCFunctionClass(this.core, `${parentName}/or_check`, {
5174
+ addToSandstoneCore: true,
5175
+ creator: "sandstone",
5176
+ onConflict: "rename"
5159
5177
  });
5178
+ this.core.enterMCFunction(orMCFunction);
5179
+ try {
5180
+ for (const condition2 of node.conditions) {
5181
+ this.pushCheckBranch(orMCFunction.node, this.parseConditionNode(condition2, orMCFunction.node), false, new ReturnCommandNode(this.pack, [1]));
5182
+ }
5183
+ orMCFunction.node.body.push(new ReturnCommandNode(this.pack, [0]));
5184
+ } finally {
5185
+ this.core.exitMCFunction();
5186
+ }
5160
5187
  return {
5161
- preNodes: [...realPreNodes, ...orExecuteNodes],
5162
- conditionNode
5188
+ preNodes: [],
5189
+ conditionNode: new FunctionReturnConditionNode(this.core, orMCFunction.name)
5163
5190
  };
5164
5191
  };
5165
- parseAndNode = (node) => {
5166
- const finalPreNodes = [];
5167
- const conditionNode = new AndNode(node.sandstoneCore, node.conditions.map((condition2) => {
5168
- const { preNodes, conditionNode: conditionNode2 } = this.parseConditionNode(condition2);
5169
- finalPreNodes.push(...preNodes);
5170
- return conditionNode2;
5171
- }));
5192
+ parseAndNode = (node, parentMCFunction) => {
5193
+ if (node.conditions.length === 1) {
5194
+ return this.parseConditionNode(node.conditions[0], parentMCFunction);
5195
+ }
5196
+ const parsed = node.conditions.map((condition2) => this.parseConditionNode(condition2, parentMCFunction));
5197
+ const anyHasCommands = parsed.some((p) => (p.preNodes ?? []).length > 0);
5198
+ if (!anyHasCommands) {
5199
+ const finalPreNodes = [];
5200
+ const conditionNode = new AndNode(node.sandstoneCore, parsed.map((p) => {
5201
+ finalPreNodes.push(...p.preNodes);
5202
+ return p.conditionNode;
5203
+ }));
5204
+ return { preNodes: finalPreNodes, conditionNode };
5205
+ }
5206
+ const parentName = parentMCFunction?.resource.name ?? "__sandstone";
5207
+ const andMCFunction = new MCFunctionClass(this.core, `${parentName}/and_check`, {
5208
+ addToSandstoneCore: true,
5209
+ creator: "sandstone",
5210
+ onConflict: "rename"
5211
+ });
5212
+ this.core.enterMCFunction(andMCFunction);
5213
+ try {
5214
+ let batch = { preNodes: [], conditionNodes: [] };
5215
+ const flushBatch = () => {
5216
+ if (batch.conditionNodes.length === 0 && batch.preNodes.every((pn) => pn.length === 0)) {
5217
+ batch = { preNodes: [], conditionNodes: [] };
5218
+ return;
5219
+ }
5220
+ const tracker = this.core.pack.Variable(undefined, "and_batch");
5221
+ this.pushBatchedCheckBranch(andMCFunction.node, batch.preNodes.flat(), batch.conditionNodes, tracker);
5222
+ batch = { preNodes: [], conditionNodes: [] };
5223
+ };
5224
+ for (const p of parsed) {
5225
+ if (p.preNodes.length === 0) {
5226
+ batch.preNodes.push(p.preNodes);
5227
+ batch.conditionNodes.push(p.conditionNode);
5228
+ } else {
5229
+ flushBatch();
5230
+ this.pushFailFastBranch(andMCFunction.node, p, new ReturnCommandNode(this.pack, [0]));
5231
+ }
5232
+ }
5233
+ flushBatch();
5234
+ andMCFunction.node.body.push(new ReturnCommandNode(this.pack, [1]));
5235
+ } finally {
5236
+ this.core.exitMCFunction();
5237
+ }
5172
5238
  return {
5173
- preNodes: finalPreNodes,
5174
- conditionNode
5239
+ preNodes: [],
5240
+ conditionNode: new FunctionReturnConditionNode(this.core, andMCFunction.name)
5175
5241
  };
5176
5242
  };
5177
- parseNotNode = (node) => {
5178
- const { preNodes, conditionNode } = this.parseConditionNode(node.condition);
5243
+ pushBatchedCheckBranch = (targetMCFunction, preNodes, conditionNodes, tracker) => {
5244
+ for (const n of preNodes) {
5245
+ targetMCFunction.body.push(n);
5246
+ }
5247
+ targetMCFunction.body.push(new ScoreboardCommandNode(this.pack, "players", "reset", tracker));
5248
+ const args = [];
5249
+ for (const c of conditionNodes) {
5250
+ const condStr = c.getValue(false);
5251
+ const match = condStr.match(/^(if|unless)\s+([\s\S]+)$/);
5252
+ if (!match) {
5253
+ throw new Error(`OrTransformationVisitor: unexpected condition getValue() shape: ${condStr}`);
5254
+ }
5255
+ const [, keyword, condBody] = match;
5256
+ args.push([keyword, condBody]);
5257
+ }
5258
+ if (args.length === 0) {
5259
+ targetMCFunction.body.push(new ScoreboardCommandNode(this.pack, "players", "set", tracker, 1));
5260
+ } else {
5261
+ const executeNode = new ExecuteCommandNode(this.pack, args, {
5262
+ isSingleExecute: true,
5263
+ body: []
5264
+ });
5265
+ targetMCFunction.enterContext(executeNode, false);
5266
+ executeNode.body = [new ScoreboardCommandNode(this.pack, "players", "set", tracker, 1)];
5267
+ targetMCFunction.body.push(executeNode);
5268
+ targetMCFunction.exitContext();
5269
+ }
5270
+ const failCheckNode = new ExecuteCommandNode(this.pack, [["unless", `score ${tracker} matches 1`]], {
5271
+ isSingleExecute: true,
5272
+ body: []
5273
+ });
5274
+ targetMCFunction.enterContext(failCheckNode, false);
5275
+ failCheckNode.body = [new ReturnCommandNode(this.pack, [0])];
5276
+ targetMCFunction.body.push(failCheckNode);
5277
+ targetMCFunction.exitContext();
5278
+ };
5279
+ pushCheckBranch = (targetMCFunction, parsed, negated, bodyNode) => {
5280
+ for (const n of parsed.preNodes) {
5281
+ targetMCFunction.body.push(n);
5282
+ }
5283
+ const condStr = parsed.conditionNode.getValue(negated);
5284
+ const match = condStr.match(/^(if|unless)\s+([\s\S]+)$/);
5285
+ if (!match) {
5286
+ throw new Error(`OrTransformationVisitor: unexpected condition getValue() shape: ${condStr}`);
5287
+ }
5288
+ const [, keyword, condBody] = match;
5289
+ const executeNode = new ExecuteCommandNode(this.pack, [[keyword, condBody]], {
5290
+ isSingleExecute: true,
5291
+ body: []
5292
+ });
5293
+ targetMCFunction.enterContext(executeNode, false);
5294
+ executeNode.body = [bodyNode];
5295
+ targetMCFunction.body.push(executeNode);
5296
+ targetMCFunction.exitContext();
5297
+ };
5298
+ pushFailFastBranch = (targetMCFunction, parsed, bodyNode) => {
5299
+ for (const n of parsed.preNodes) {
5300
+ targetMCFunction.body.push(n);
5301
+ }
5302
+ const condStr = parsed.conditionNode.getValue(true);
5303
+ const match = condStr.match(/^(if|unless)\s+([\s\S]+)$/);
5304
+ if (!match) {
5305
+ throw new Error(`OrTransformationVisitor: unexpected condition getValue() shape: ${condStr}`);
5306
+ }
5307
+ const flippedKeyword = match[1] === "if" ? "unless" : "if";
5308
+ const condBody = match[2];
5309
+ const executeNode = new ExecuteCommandNode(this.pack, [[flippedKeyword, condBody]], {
5310
+ isSingleExecute: true,
5311
+ body: []
5312
+ });
5313
+ targetMCFunction.enterContext(executeNode, false);
5314
+ executeNode.body = [bodyNode];
5315
+ targetMCFunction.body.push(executeNode);
5316
+ targetMCFunction.exitContext();
5317
+ };
5318
+ parseNotNode = (node, parentMCFunction) => {
5319
+ const { preNodes, conditionNode } = this.parseConditionNode(node.condition, parentMCFunction);
5179
5320
  return {
5180
5321
  preNodes,
5181
5322
  conditionNode: new NotNode(node.sandstoneCore, conditionNode)
@@ -6429,6 +6570,7 @@ SleepClass = class SleepClass extends AwaitNode {
6429
6570
  // src/variables/abstractClasses.ts
6430
6571
 
6431
6572
  class ConditionClass {
6573
+ preNodes;
6432
6574
  _toMinecraftCondition() {
6433
6575
  throw new Error("Not implemented");
6434
6576
  }
@@ -10255,8 +10397,14 @@ class Flow {
10255
10397
  this.sandstoneCore = sandstoneCore2;
10256
10398
  }
10257
10399
  if = (condition2, callback) => new IfStatement(this.sandstoneCore, conditionToNode(condition2), callback);
10258
- and = (...conditions) => new AndNode(this.sandstoneCore, conditions.map((condition2) => conditionToNode(condition2)));
10259
- or = (...conditions) => new OrNode(this.sandstoneCore, conditions.map((condition2) => conditionToNode(condition2)));
10400
+ and(...args) {
10401
+ const conditions = args[0] instanceof Array ? args[0] : args;
10402
+ return new AndNode(this.sandstoneCore, conditions.map((condition2) => conditionToNode(condition2)));
10403
+ }
10404
+ or(...args) {
10405
+ const conditions = args[0] instanceof Array ? args[0] : args;
10406
+ return new OrNode(this.sandstoneCore, conditions.map((condition2) => conditionToNode(condition2)));
10407
+ }
10260
10408
  not = (condition2) => new NotNode(this.sandstoneCore, conditionToNode(condition2));
10261
10409
  get return() {
10262
10410
  return this.sandstoneCore.pack.commands.returnCmd;
@@ -10516,12 +10664,13 @@ var init_loopTransformationVisitor = __esm(() => {
10516
10664
  });
10517
10665
 
10518
10666
  // src/pack/visitors/orTransformationVisitor.ts
10519
- var OrTransformationVisitor;
10667
+ var FunctionReturnConditionNode, OrTransformationVisitor;
10520
10668
  var init_orTransformationVisitor = __esm(() => {
10521
10669
  init_commands2();
10670
+ init_core();
10522
10671
  init_flow();
10523
10672
  init_visitor();
10524
- });
10673
+ });
10525
10674
 
10526
10675
  // src/pack/visitors/optimizeMacroTemporaries.ts
10527
10676
  var flattenArgs = (args) => args.flatMap((arg) => Array.isArray(arg) ? flattenArgs(arg) : [arg]), referencesStoragePoint = (node, point) => {
@@ -10763,10 +10912,11 @@ class SandstonePack {
10763
10912
  return pack;
10764
10913
  }
10765
10914
  setupLantern = () => {
10766
- const loadStatus = this.Objective.create("load.status", "dummy", undefined, { useDefaultNamespace: false });
10915
+ const loadStatus = this.Objective.create("load.status", "dummy", undefined, { useDefaultNamespace: false, alreadyExists: true });
10767
10916
  const privateInit = this.Tag("function", "load:_private/init", [
10768
10917
  this.MCFunction("load:_private/init", () => {
10769
10918
  this.commands.comment("Reset scoreboards so packs can set values accurate for current load.");
10919
+ this.commands.scoreboard.objectives.add(loadStatus.name, "dummy");
10770
10920
  loadStatus.reset();
10771
10921
  })
10772
10922
  ]);
@@ -54082,5 +54232,5 @@ export {
54082
54232
  ACTIVITIES_SET
54083
54233
  };
54084
54234
 
54085
- //# debugId=C3B58667DCFE12B864756E2164756E21
54235
+ //# debugId=4AE8B281EBDAB42264756E2164756E21
54086
54236
  //# sourceMappingURL=index.js.map