sandstone 1.1.4 → 1.1.6

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.
@@ -4438,6 +4438,7 @@ WorldBorderCommand = class WorldBorderCommand extends CommandArguments {
4438
4438
  };
4439
4439
  };
4440
4440
  ConditionNode = class ConditionNode extends Node {
4441
+ preNodes;
4441
4442
  };
4442
4443
  SingleConditionNode = class SingleConditionNode extends ConditionNode {
4443
4444
  getValue = (negated = false) => {
@@ -4594,9 +4595,13 @@ DataPointEqualsConditionNode = class DataPointEqualsConditionNode extends Single
4594
4595
  this.value = value2;
4595
4596
  const { DataVariable, Variable, commands: commands2 } = sandstoneCore2.pack;
4596
4597
  const { execute: execute2 } = commands2;
4598
+ const mcFunction = sandstoneCore2.getCurrentMCFunctionOrThrow();
4599
+ const before = mcFunction.body.length;
4597
4600
  const anon = DataVariable(this.dataPoint);
4598
4601
  this.conditional = Variable();
4599
4602
  execute2.store.result(this.conditional).run(() => anon.set(this.value));
4603
+ const after = mcFunction.body.length;
4604
+ this.preNodes = mcFunction.body.splice(before, after - before);
4600
4605
  }
4601
4606
  getValue = (negated) => (negated ? ["if", ...this.getCondition()] : ["unless", ...this.getCondition()]).join(" ");
4602
4607
  getCondition() {
@@ -4661,7 +4666,11 @@ CommandConditionNode = class CommandConditionNode extends SingleConditionNode {
4661
4666
  this.result = result;
4662
4667
  const store = sandstoneCore2.pack.commands.execute.store[type4];
4663
4668
  this.variable = sandstoneCore2.pack.Variable(undefined, "condition");
4669
+ const mcFunction = sandstoneCore2.getCurrentMCFunctionOrThrow();
4670
+ const before = mcFunction.body.length;
4664
4671
  command(store(this.variable));
4672
+ const after = mcFunction.body.length;
4673
+ this.preNodes = mcFunction.body.splice(before, after - before);
4665
4674
  }
4666
4675
  getCondition() {
4667
4676
  if (this.type === "success") {
@@ -5158,54 +5167,186 @@ LoopTransformationVisitor = class LoopTransformationVisitor extends GenericSands
5158
5167
  visitForINode = this.visitLoopNode;
5159
5168
  visitForOfNode = this.visitLoopNode;
5160
5169
  };
5170
+ FunctionReturnConditionNode = class FunctionReturnConditionNode extends ConditionNode {
5171
+ sandstoneCore;
5172
+ functionName;
5173
+ constructor(sandstoneCore2, functionName) {
5174
+ super(sandstoneCore2);
5175
+ this.sandstoneCore = sandstoneCore2;
5176
+ this.functionName = functionName;
5177
+ }
5178
+ getValue = () => `if function ${this.functionName}`;
5179
+ };
5161
5180
  OrTransformationVisitor = class OrTransformationVisitor extends GenericSandstoneVisitor {
5162
5181
  visitIfNode = (node_) => {
5163
- const { preNodes, conditionNode } = this.parseConditionNode(node_.condition);
5182
+ const { preNodes, conditionNode } = this.parseConditionNode(node_.condition, node_.parentMCFunction);
5164
5183
  node_.condition = conditionNode;
5165
5184
  return [...preNodes, this.genericVisit(node_)];
5166
5185
  };
5167
- parseConditionNode = (node) => {
5186
+ parseConditionNode = (node, parentMCFunction) => {
5168
5187
  if (node instanceof OrNode) {
5169
- return this.parseOrNode(node);
5188
+ return this.parseOrNode(node, parentMCFunction);
5170
5189
  }
5171
5190
  if (node instanceof AndNode) {
5172
- return this.parseAndNode(node);
5191
+ return this.parseAndNode(node, parentMCFunction);
5173
5192
  }
5174
5193
  if (node instanceof NotNode) {
5175
- return this.parseNotNode(node);
5194
+ return this.parseNotNode(node, parentMCFunction);
5176
5195
  }
5177
- return { preNodes: [], conditionNode: node };
5196
+ return { preNodes: [...node.preNodes ?? []], conditionNode: node };
5178
5197
  };
5179
- parseOrNode = (node) => {
5180
- const variable = this.core.pack.Variable(undefined, "condition");
5181
- const conditionNode = variable.equalTo(1)._toMinecraftCondition();
5182
- const realPreNodes = [new ScoreboardCommandNode(this.pack, "players", "reset", variable)];
5183
- const orExecuteNodes = node.conditions.map((condition2) => {
5184
- const { preNodes, conditionNode: parsedConditionNode } = this.parseConditionNode(condition2);
5185
- realPreNodes.push(...preNodes);
5186
- const ifNode = new IfNode(this.core, parsedConditionNode);
5187
- ifNode.body = [new ScoreboardCommandNode(this.pack, "players", "set", variable, 1)];
5188
- return ifNode;
5198
+ parseOrNode = (node, parentMCFunction) => {
5199
+ if (node.conditions.length === 1) {
5200
+ return this.parseConditionNode(node.conditions[0], parentMCFunction);
5201
+ }
5202
+ const parentName = parentMCFunction?.resource.name ?? "__sandstone";
5203
+ const orMCFunction = new MCFunctionClass(this.core, `${parentName}/or_check`, {
5204
+ addToSandstoneCore: true,
5205
+ creator: "sandstone",
5206
+ onConflict: "rename"
5189
5207
  });
5208
+ this.core.enterMCFunction(orMCFunction);
5209
+ try {
5210
+ for (const condition2 of node.conditions) {
5211
+ this.pushCheckBranch(orMCFunction.node, this.parseConditionNode(condition2, orMCFunction.node), false, new ReturnCommandNode(this.pack, [1]));
5212
+ }
5213
+ orMCFunction.node.body.push(new ReturnCommandNode(this.pack, [0]));
5214
+ } finally {
5215
+ this.core.exitMCFunction();
5216
+ }
5190
5217
  return {
5191
- preNodes: [...realPreNodes, ...orExecuteNodes],
5192
- conditionNode
5218
+ preNodes: [],
5219
+ conditionNode: new FunctionReturnConditionNode(this.core, orMCFunction.name)
5193
5220
  };
5194
5221
  };
5195
- parseAndNode = (node) => {
5196
- const finalPreNodes = [];
5197
- const conditionNode = new AndNode(node.sandstoneCore, node.conditions.map((condition2) => {
5198
- const { preNodes, conditionNode: conditionNode2 } = this.parseConditionNode(condition2);
5199
- finalPreNodes.push(...preNodes);
5200
- return conditionNode2;
5201
- }));
5222
+ parseAndNode = (node, parentMCFunction) => {
5223
+ if (node.conditions.length === 1) {
5224
+ return this.parseConditionNode(node.conditions[0], parentMCFunction);
5225
+ }
5226
+ const parsed = node.conditions.map((condition2) => this.parseConditionNode(condition2, parentMCFunction));
5227
+ const anyHasCommands = parsed.some((p) => (p.preNodes ?? []).length > 0);
5228
+ if (!anyHasCommands) {
5229
+ const finalPreNodes = [];
5230
+ const conditionNode = new AndNode(node.sandstoneCore, parsed.map((p) => {
5231
+ finalPreNodes.push(...p.preNodes);
5232
+ return p.conditionNode;
5233
+ }));
5234
+ return { preNodes: finalPreNodes, conditionNode };
5235
+ }
5236
+ const parentName = parentMCFunction?.resource.name ?? "__sandstone";
5237
+ const andMCFunction = new MCFunctionClass(this.core, `${parentName}/and_check`, {
5238
+ addToSandstoneCore: true,
5239
+ creator: "sandstone",
5240
+ onConflict: "rename"
5241
+ });
5242
+ this.core.enterMCFunction(andMCFunction);
5243
+ try {
5244
+ let batch = { preNodes: [], conditionNodes: [] };
5245
+ const flushBatch = () => {
5246
+ if (batch.conditionNodes.length === 0 && batch.preNodes.every((pn) => pn.length === 0)) {
5247
+ batch = { preNodes: [], conditionNodes: [] };
5248
+ return;
5249
+ }
5250
+ const tracker = this.core.pack.Variable(undefined, "and_batch");
5251
+ this.pushBatchedCheckBranch(andMCFunction.node, batch.preNodes.flat(), batch.conditionNodes, tracker);
5252
+ batch = { preNodes: [], conditionNodes: [] };
5253
+ };
5254
+ for (const p of parsed) {
5255
+ if (p.preNodes.length === 0) {
5256
+ batch.preNodes.push(p.preNodes);
5257
+ batch.conditionNodes.push(p.conditionNode);
5258
+ } else {
5259
+ flushBatch();
5260
+ this.pushFailFastBranch(andMCFunction.node, p, new ReturnCommandNode(this.pack, [0]));
5261
+ }
5262
+ }
5263
+ flushBatch();
5264
+ andMCFunction.node.body.push(new ReturnCommandNode(this.pack, [1]));
5265
+ } finally {
5266
+ this.core.exitMCFunction();
5267
+ }
5202
5268
  return {
5203
- preNodes: finalPreNodes,
5204
- conditionNode
5269
+ preNodes: [],
5270
+ conditionNode: new FunctionReturnConditionNode(this.core, andMCFunction.name)
5205
5271
  };
5206
5272
  };
5207
- parseNotNode = (node) => {
5208
- const { preNodes, conditionNode } = this.parseConditionNode(node.condition);
5273
+ pushBatchedCheckBranch = (targetMCFunction, preNodes, conditionNodes, tracker) => {
5274
+ for (const n of preNodes) {
5275
+ targetMCFunction.body.push(n);
5276
+ }
5277
+ targetMCFunction.body.push(new ScoreboardCommandNode(this.pack, "players", "reset", tracker));
5278
+ const args = [];
5279
+ for (const c of conditionNodes) {
5280
+ const condStr = c.getValue(false);
5281
+ const match = condStr.match(/^(if|unless)\s+([\s\S]+)$/);
5282
+ if (!match) {
5283
+ throw new Error(`OrTransformationVisitor: unexpected condition getValue() shape: ${condStr}`);
5284
+ }
5285
+ const [, keyword, condBody] = match;
5286
+ args.push([keyword, condBody]);
5287
+ }
5288
+ if (args.length === 0) {
5289
+ targetMCFunction.body.push(new ScoreboardCommandNode(this.pack, "players", "set", tracker, 1));
5290
+ } else {
5291
+ const executeNode = new ExecuteCommandNode(this.pack, args, {
5292
+ isSingleExecute: true,
5293
+ body: []
5294
+ });
5295
+ targetMCFunction.enterContext(executeNode, false);
5296
+ executeNode.body = [new ScoreboardCommandNode(this.pack, "players", "set", tracker, 1)];
5297
+ targetMCFunction.body.push(executeNode);
5298
+ targetMCFunction.exitContext();
5299
+ }
5300
+ const failCheckNode = new ExecuteCommandNode(this.pack, [["unless", `score ${tracker} matches 1`]], {
5301
+ isSingleExecute: true,
5302
+ body: []
5303
+ });
5304
+ targetMCFunction.enterContext(failCheckNode, false);
5305
+ failCheckNode.body = [new ReturnCommandNode(this.pack, [0])];
5306
+ targetMCFunction.body.push(failCheckNode);
5307
+ targetMCFunction.exitContext();
5308
+ };
5309
+ pushCheckBranch = (targetMCFunction, parsed, negated, bodyNode) => {
5310
+ for (const n of parsed.preNodes) {
5311
+ targetMCFunction.body.push(n);
5312
+ }
5313
+ const condStr = parsed.conditionNode.getValue(negated);
5314
+ const match = condStr.match(/^(if|unless)\s+([\s\S]+)$/);
5315
+ if (!match) {
5316
+ throw new Error(`OrTransformationVisitor: unexpected condition getValue() shape: ${condStr}`);
5317
+ }
5318
+ const [, keyword, condBody] = match;
5319
+ const executeNode = new ExecuteCommandNode(this.pack, [[keyword, condBody]], {
5320
+ isSingleExecute: true,
5321
+ body: []
5322
+ });
5323
+ targetMCFunction.enterContext(executeNode, false);
5324
+ executeNode.body = [bodyNode];
5325
+ targetMCFunction.body.push(executeNode);
5326
+ targetMCFunction.exitContext();
5327
+ };
5328
+ pushFailFastBranch = (targetMCFunction, parsed, bodyNode) => {
5329
+ for (const n of parsed.preNodes) {
5330
+ targetMCFunction.body.push(n);
5331
+ }
5332
+ const condStr = parsed.conditionNode.getValue(true);
5333
+ const match = condStr.match(/^(if|unless)\s+([\s\S]+)$/);
5334
+ if (!match) {
5335
+ throw new Error(`OrTransformationVisitor: unexpected condition getValue() shape: ${condStr}`);
5336
+ }
5337
+ const flippedKeyword = match[1] === "if" ? "unless" : "if";
5338
+ const condBody = match[2];
5339
+ const executeNode = new ExecuteCommandNode(this.pack, [[flippedKeyword, condBody]], {
5340
+ isSingleExecute: true,
5341
+ body: []
5342
+ });
5343
+ targetMCFunction.enterContext(executeNode, false);
5344
+ executeNode.body = [bodyNode];
5345
+ targetMCFunction.body.push(executeNode);
5346
+ targetMCFunction.exitContext();
5347
+ };
5348
+ parseNotNode = (node, parentMCFunction) => {
5349
+ const { preNodes, conditionNode } = this.parseConditionNode(node.condition, parentMCFunction);
5209
5350
  return {
5210
5351
  preNodes,
5211
5352
  conditionNode: new NotNode(node.sandstoneCore, conditionNode)
@@ -6459,6 +6600,7 @@ SleepClass = class SleepClass extends AwaitNode {
6459
6600
  // src/variables/abstractClasses.ts
6460
6601
 
6461
6602
  class ConditionClass {
6603
+ preNodes;
6462
6604
  _toMinecraftCondition() {
6463
6605
  throw new Error("Not implemented");
6464
6606
  }
@@ -19661,8 +19803,14 @@ class Flow {
19661
19803
  this.sandstoneCore = sandstoneCore2;
19662
19804
  }
19663
19805
  if = (condition2, callback) => new IfStatement(this.sandstoneCore, conditionToNode(condition2), callback);
19664
- and = (...conditions) => new AndNode(this.sandstoneCore, conditions.map((condition2) => conditionToNode(condition2)));
19665
- or = (...conditions) => new OrNode(this.sandstoneCore, conditions.map((condition2) => conditionToNode(condition2)));
19806
+ and(...args) {
19807
+ const conditions = args[0] instanceof Array ? args[0] : args;
19808
+ return new AndNode(this.sandstoneCore, conditions.map((condition2) => conditionToNode(condition2)));
19809
+ }
19810
+ or(...args) {
19811
+ const conditions = args[0] instanceof Array ? args[0] : args;
19812
+ return new OrNode(this.sandstoneCore, conditions.map((condition2) => conditionToNode(condition2)));
19813
+ }
19666
19814
  not = (condition2) => new NotNode(this.sandstoneCore, conditionToNode(condition2));
19667
19815
  get return() {
19668
19816
  return this.sandstoneCore.pack.commands.returnCmd;
@@ -19922,12 +20070,13 @@ var init_loopTransformationVisitor = __esm(() => {
19922
20070
  });
19923
20071
 
19924
20072
  // src/pack/visitors/orTransformationVisitor.ts
19925
- var OrTransformationVisitor;
20073
+ var FunctionReturnConditionNode, OrTransformationVisitor;
19926
20074
  var init_orTransformationVisitor = __esm(() => {
19927
20075
  init_commands2();
20076
+ init_core();
19928
20077
  init_flow();
19929
20078
  init_visitor();
19930
- });
20079
+ });
19931
20080
 
19932
20081
  // src/pack/visitors/optimizeMacroTemporaries.ts
19933
20082
  var flattenArgs = (args) => args.flatMap((arg) => Array.isArray(arg) ? flattenArgs(arg) : [arg]), referencesStoragePoint = (node, point) => {
@@ -20169,10 +20318,11 @@ class SandstonePack {
20169
20318
  return pack;
20170
20319
  }
20171
20320
  setupLantern = () => {
20172
- const loadStatus = this.Objective.create("load.status", "dummy", undefined, { useDefaultNamespace: false });
20321
+ const loadStatus = this.Objective.create("load.status", "dummy", undefined, { useDefaultNamespace: false, alreadyExists: true });
20173
20322
  const privateInit = this.Tag("function", "load:_private/init", [
20174
20323
  this.MCFunction("load:_private/init", () => {
20175
20324
  this.commands.comment("Reset scoreboards so packs can set values accurate for current load.");
20325
+ this.commands.scoreboard.objectives.add(loadStatus.name, "dummy");
20176
20326
  loadStatus.reset();
20177
20327
  })
20178
20328
  ]);
@@ -63488,5 +63638,5 @@ export {
63488
63638
  ACTIVITIES_SET
63489
63639
  };
63490
63640
 
63491
- //# debugId=A65E69D8F92C95B464756E2164756E21
63641
+ //# debugId=D77F3F3DCD4BFA5364756E2164756E21
63492
63642
  //# sourceMappingURL=sandstone.esm.js.map