sandstone 1.0.3 → 1.0.4

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.
@@ -4391,6 +4391,7 @@ WorldBorderCommand = class WorldBorderCommand extends CommandArguments {
4391
4391
  };
4392
4392
  };
4393
4393
  ConditionNode = class ConditionNode extends Node {
4394
+ preNodes;
4394
4395
  };
4395
4396
  SingleConditionNode = class SingleConditionNode extends ConditionNode {
4396
4397
  getValue = (negated = false) => {
@@ -4547,9 +4548,13 @@ DataPointEqualsConditionNode = class DataPointEqualsConditionNode extends Single
4547
4548
  this.value = value;
4548
4549
  const { DataVariable, Variable, commands: commands2 } = sandstoneCore2.pack;
4549
4550
  const { execute: execute2 } = commands2;
4551
+ const mcFunction = sandstoneCore2.getCurrentMCFunctionOrThrow();
4552
+ const before = mcFunction.body.length;
4550
4553
  const anon = DataVariable(this.dataPoint);
4551
4554
  this.conditional = Variable();
4552
4555
  execute2.store.result(this.conditional).run(() => anon.set(this.value));
4556
+ const after = mcFunction.body.length;
4557
+ this.preNodes = mcFunction.body.splice(before, after - before);
4553
4558
  }
4554
4559
  getValue = (negated) => (negated ? ["if", ...this.getCondition()] : ["unless", ...this.getCondition()]).join(" ");
4555
4560
  getCondition() {
@@ -4614,7 +4619,11 @@ CommandConditionNode = class CommandConditionNode extends SingleConditionNode {
4614
4619
  this.result = result;
4615
4620
  const store = sandstoneCore2.pack.commands.execute.store[type];
4616
4621
  this.variable = sandstoneCore2.pack.Variable(undefined, "condition");
4622
+ const mcFunction = sandstoneCore2.getCurrentMCFunctionOrThrow();
4623
+ const before = mcFunction.body.length;
4617
4624
  command(store(this.variable));
4625
+ const after = mcFunction.body.length;
4626
+ this.preNodes = mcFunction.body.splice(before, after - before);
4618
4627
  }
4619
4628
  getCondition() {
4620
4629
  if (this.type === "success") {
@@ -5111,54 +5120,186 @@ LoopTransformationVisitor = class LoopTransformationVisitor extends GenericSands
5111
5120
  visitForINode = this.visitLoopNode;
5112
5121
  visitForOfNode = this.visitLoopNode;
5113
5122
  };
5123
+ FunctionReturnConditionNode = class FunctionReturnConditionNode extends ConditionNode {
5124
+ sandstoneCore;
5125
+ functionName;
5126
+ constructor(sandstoneCore2, functionName) {
5127
+ super(sandstoneCore2);
5128
+ this.sandstoneCore = sandstoneCore2;
5129
+ this.functionName = functionName;
5130
+ }
5131
+ getValue = () => `if function ${this.functionName}`;
5132
+ };
5114
5133
  OrTransformationVisitor = class OrTransformationVisitor extends GenericSandstoneVisitor {
5115
5134
  visitIfNode = (node_) => {
5116
- const { preNodes, conditionNode } = this.parseConditionNode(node_.condition);
5135
+ const { preNodes, conditionNode } = this.parseConditionNode(node_.condition, node_.parentMCFunction);
5117
5136
  node_.condition = conditionNode;
5118
5137
  return [...preNodes, this.genericVisit(node_)];
5119
5138
  };
5120
- parseConditionNode = (node) => {
5139
+ parseConditionNode = (node, parentMCFunction) => {
5121
5140
  if (node instanceof OrNode) {
5122
- return this.parseOrNode(node);
5141
+ return this.parseOrNode(node, parentMCFunction);
5123
5142
  }
5124
5143
  if (node instanceof AndNode) {
5125
- return this.parseAndNode(node);
5144
+ return this.parseAndNode(node, parentMCFunction);
5126
5145
  }
5127
5146
  if (node instanceof NotNode) {
5128
- return this.parseNotNode(node);
5147
+ return this.parseNotNode(node, parentMCFunction);
5129
5148
  }
5130
- return { preNodes: [], conditionNode: node };
5149
+ return { preNodes: [...node.preNodes ?? []], conditionNode: node };
5131
5150
  };
5132
- parseOrNode = (node) => {
5133
- const variable = this.core.pack.Variable(undefined, "condition");
5134
- const conditionNode = variable.equalTo(1)._toMinecraftCondition();
5135
- const realPreNodes = [new ScoreboardCommandNode(this.pack, "players", "reset", variable)];
5136
- const orExecuteNodes = node.conditions.map((condition2) => {
5137
- const { preNodes, conditionNode: parsedConditionNode } = this.parseConditionNode(condition2);
5138
- realPreNodes.push(...preNodes);
5139
- const ifNode = new IfNode(this.core, parsedConditionNode);
5140
- ifNode.body = [new ScoreboardCommandNode(this.pack, "players", "set", variable, 1)];
5141
- return ifNode;
5151
+ parseOrNode = (node, parentMCFunction) => {
5152
+ if (node.conditions.length === 1) {
5153
+ return this.parseConditionNode(node.conditions[0], parentMCFunction);
5154
+ }
5155
+ const parentName = parentMCFunction?.resource.name ?? "__sandstone";
5156
+ const orMCFunction = new MCFunctionClass(this.core, `${parentName}/or_check`, {
5157
+ addToSandstoneCore: true,
5158
+ creator: "sandstone",
5159
+ onConflict: "rename"
5142
5160
  });
5161
+ this.core.enterMCFunction(orMCFunction);
5162
+ try {
5163
+ for (const condition2 of node.conditions) {
5164
+ this.pushCheckBranch(orMCFunction.node, this.parseConditionNode(condition2, orMCFunction.node), false, new ReturnCommandNode(this.pack, [1]));
5165
+ }
5166
+ orMCFunction.node.body.push(new ReturnCommandNode(this.pack, [0]));
5167
+ } finally {
5168
+ this.core.exitMCFunction();
5169
+ }
5143
5170
  return {
5144
- preNodes: [...realPreNodes, ...orExecuteNodes],
5145
- conditionNode
5171
+ preNodes: [],
5172
+ conditionNode: new FunctionReturnConditionNode(this.core, orMCFunction.name)
5146
5173
  };
5147
5174
  };
5148
- parseAndNode = (node) => {
5149
- const finalPreNodes = [];
5150
- const conditionNode = new AndNode(node.sandstoneCore, node.conditions.map((condition2) => {
5151
- const { preNodes, conditionNode: conditionNode2 } = this.parseConditionNode(condition2);
5152
- finalPreNodes.push(...preNodes);
5153
- return conditionNode2;
5154
- }));
5175
+ parseAndNode = (node, parentMCFunction) => {
5176
+ if (node.conditions.length === 1) {
5177
+ return this.parseConditionNode(node.conditions[0], parentMCFunction);
5178
+ }
5179
+ const parsed = node.conditions.map((condition2) => this.parseConditionNode(condition2, parentMCFunction));
5180
+ const anyHasCommands = parsed.some((p) => (p.preNodes ?? []).length > 0);
5181
+ if (!anyHasCommands) {
5182
+ const finalPreNodes = [];
5183
+ const conditionNode = new AndNode(node.sandstoneCore, parsed.map((p) => {
5184
+ finalPreNodes.push(...p.preNodes);
5185
+ return p.conditionNode;
5186
+ }));
5187
+ return { preNodes: finalPreNodes, conditionNode };
5188
+ }
5189
+ const parentName = parentMCFunction?.resource.name ?? "__sandstone";
5190
+ const andMCFunction = new MCFunctionClass(this.core, `${parentName}/and_check`, {
5191
+ addToSandstoneCore: true,
5192
+ creator: "sandstone",
5193
+ onConflict: "rename"
5194
+ });
5195
+ this.core.enterMCFunction(andMCFunction);
5196
+ try {
5197
+ let batch = { preNodes: [], conditionNodes: [] };
5198
+ const flushBatch = () => {
5199
+ if (batch.conditionNodes.length === 0 && batch.preNodes.every((pn) => pn.length === 0)) {
5200
+ batch = { preNodes: [], conditionNodes: [] };
5201
+ return;
5202
+ }
5203
+ const tracker = this.core.pack.Variable(undefined, "and_batch");
5204
+ this.pushBatchedCheckBranch(andMCFunction.node, batch.preNodes.flat(), batch.conditionNodes, tracker);
5205
+ batch = { preNodes: [], conditionNodes: [] };
5206
+ };
5207
+ for (const p of parsed) {
5208
+ if (p.preNodes.length === 0) {
5209
+ batch.preNodes.push(p.preNodes);
5210
+ batch.conditionNodes.push(p.conditionNode);
5211
+ } else {
5212
+ flushBatch();
5213
+ this.pushFailFastBranch(andMCFunction.node, p, new ReturnCommandNode(this.pack, [0]));
5214
+ }
5215
+ }
5216
+ flushBatch();
5217
+ andMCFunction.node.body.push(new ReturnCommandNode(this.pack, [1]));
5218
+ } finally {
5219
+ this.core.exitMCFunction();
5220
+ }
5155
5221
  return {
5156
- preNodes: finalPreNodes,
5157
- conditionNode
5222
+ preNodes: [],
5223
+ conditionNode: new FunctionReturnConditionNode(this.core, andMCFunction.name)
5158
5224
  };
5159
5225
  };
5160
- parseNotNode = (node) => {
5161
- const { preNodes, conditionNode } = this.parseConditionNode(node.condition);
5226
+ pushBatchedCheckBranch = (targetMCFunction, preNodes, conditionNodes, tracker) => {
5227
+ for (const n of preNodes) {
5228
+ targetMCFunction.body.push(n);
5229
+ }
5230
+ targetMCFunction.body.push(new ScoreboardCommandNode(this.pack, "players", "reset", tracker));
5231
+ const args = [];
5232
+ for (const c of conditionNodes) {
5233
+ const condStr = c.getValue(false);
5234
+ const match = condStr.match(/^(if|unless)\s+([\s\S]+)$/);
5235
+ if (!match) {
5236
+ throw new Error(`OrTransformationVisitor: unexpected condition getValue() shape: ${condStr}`);
5237
+ }
5238
+ const [, keyword, condBody] = match;
5239
+ args.push([keyword, condBody]);
5240
+ }
5241
+ if (args.length === 0) {
5242
+ targetMCFunction.body.push(new ScoreboardCommandNode(this.pack, "players", "set", tracker, 1));
5243
+ } else {
5244
+ const executeNode = new ExecuteCommandNode(this.pack, args, {
5245
+ isSingleExecute: true,
5246
+ body: []
5247
+ });
5248
+ targetMCFunction.enterContext(executeNode, false);
5249
+ executeNode.body = [new ScoreboardCommandNode(this.pack, "players", "set", tracker, 1)];
5250
+ targetMCFunction.body.push(executeNode);
5251
+ targetMCFunction.exitContext();
5252
+ }
5253
+ const failCheckNode = new ExecuteCommandNode(this.pack, [["unless", `score ${tracker} matches 1`]], {
5254
+ isSingleExecute: true,
5255
+ body: []
5256
+ });
5257
+ targetMCFunction.enterContext(failCheckNode, false);
5258
+ failCheckNode.body = [new ReturnCommandNode(this.pack, [0])];
5259
+ targetMCFunction.body.push(failCheckNode);
5260
+ targetMCFunction.exitContext();
5261
+ };
5262
+ pushCheckBranch = (targetMCFunction, parsed, negated, bodyNode) => {
5263
+ for (const n of parsed.preNodes) {
5264
+ targetMCFunction.body.push(n);
5265
+ }
5266
+ const condStr = parsed.conditionNode.getValue(negated);
5267
+ const match = condStr.match(/^(if|unless)\s+([\s\S]+)$/);
5268
+ if (!match) {
5269
+ throw new Error(`OrTransformationVisitor: unexpected condition getValue() shape: ${condStr}`);
5270
+ }
5271
+ const [, keyword, condBody] = match;
5272
+ const executeNode = new ExecuteCommandNode(this.pack, [[keyword, condBody]], {
5273
+ isSingleExecute: true,
5274
+ body: []
5275
+ });
5276
+ targetMCFunction.enterContext(executeNode, false);
5277
+ executeNode.body = [bodyNode];
5278
+ targetMCFunction.body.push(executeNode);
5279
+ targetMCFunction.exitContext();
5280
+ };
5281
+ pushFailFastBranch = (targetMCFunction, parsed, bodyNode) => {
5282
+ for (const n of parsed.preNodes) {
5283
+ targetMCFunction.body.push(n);
5284
+ }
5285
+ const condStr = parsed.conditionNode.getValue(true);
5286
+ const match = condStr.match(/^(if|unless)\s+([\s\S]+)$/);
5287
+ if (!match) {
5288
+ throw new Error(`OrTransformationVisitor: unexpected condition getValue() shape: ${condStr}`);
5289
+ }
5290
+ const flippedKeyword = match[1] === "if" ? "unless" : "if";
5291
+ const condBody = match[2];
5292
+ const executeNode = new ExecuteCommandNode(this.pack, [[flippedKeyword, condBody]], {
5293
+ isSingleExecute: true,
5294
+ body: []
5295
+ });
5296
+ targetMCFunction.enterContext(executeNode, false);
5297
+ executeNode.body = [bodyNode];
5298
+ targetMCFunction.body.push(executeNode);
5299
+ targetMCFunction.exitContext();
5300
+ };
5301
+ parseNotNode = (node, parentMCFunction) => {
5302
+ const { preNodes, conditionNode } = this.parseConditionNode(node.condition, parentMCFunction);
5162
5303
  return {
5163
5304
  preNodes,
5164
5305
  conditionNode: new NotNode(node.sandstoneCore, conditionNode)
@@ -6412,6 +6553,7 @@ SleepClass = class SleepClass extends AwaitNode {
6412
6553
  // src/variables/abstractClasses.ts
6413
6554
 
6414
6555
  class ConditionClass {
6556
+ preNodes;
6415
6557
  _toMinecraftCondition() {
6416
6558
  throw new Error("Not implemented");
6417
6559
  }
@@ -10489,12 +10631,13 @@ var init_loopTransformationVisitor = __esm(() => {
10489
10631
  });
10490
10632
 
10491
10633
  // src/pack/visitors/orTransformationVisitor.ts
10492
- var OrTransformationVisitor;
10634
+ var FunctionReturnConditionNode, OrTransformationVisitor;
10493
10635
  var init_orTransformationVisitor = __esm(() => {
10494
10636
  init_commands2();
10637
+ init_core();
10495
10638
  init_flow();
10496
10639
  init_visitor();
10497
- });
10640
+ });
10498
10641
 
10499
10642
  // src/pack/visitors/optimizeMacroTemporaries.ts
10500
10643
  var flattenArgs = (args) => args.flatMap((arg) => Array.isArray(arg) ? flattenArgs(arg) : [arg]), referencesStoragePoint = (node, point) => {
@@ -10736,10 +10879,11 @@ class SandstonePack {
10736
10879
  return pack;
10737
10880
  }
10738
10881
  setupLantern = () => {
10739
- const loadStatus = this.Objective.create("load.status", "dummy", undefined, { useDefaultNamespace: false });
10882
+ const loadStatus = this.Objective.create("load.status", "dummy", undefined, { useDefaultNamespace: false, alreadyExists: true });
10740
10883
  const privateInit = this.Tag("function", "load:_private/init", [
10741
10884
  this.MCFunction("load:_private/init", () => {
10742
10885
  this.commands.comment("Reset scoreboards so packs can set values accurate for current load.");
10886
+ this.commands.scoreboard.objectives.add(loadStatus.name, "dummy");
10743
10887
  loadStatus.reset();
10744
10888
  })
10745
10889
  ]);
@@ -52814,5 +52958,5 @@ export {
52814
52958
  ACTIVITIES_SET
52815
52959
  };
52816
52960
 
52817
- //# debugId=ADCADF930687878B64756E2164756E21
52961
+ //# debugId=663E6B28D264CF0964756E2164756E21
52818
52962
  //# sourceMappingURL=index.js.map