Skip to content

Commit

Permalink
Refactor for Macro move, finish Flow
Browse files Browse the repository at this point in the history
  • Loading branch information
MulverineX committed Nov 28, 2023
1 parent 7a6b430 commit 870d4fc
Show file tree
Hide file tree
Showing 79 changed files with 504 additions and 1,049 deletions.
3 changes: 2 additions & 1 deletion src/arguments/coords.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { MacroString, VectorClass } from 'sandstone/variables'
import type { MacroString } from 'sandstone/core'
import type { VectorClass } from 'sandstone/variables'

type AbsoluteFloat<MACRO extends boolean> = MacroString<`${number}`, MACRO>;
type RelativeFloat<MACRO extends boolean> = `~${MacroString<(number | ''), MACRO>}`;
Expand Down
2 changes: 1 addition & 1 deletion src/arguments/range.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { MacroString } from 'sandstone/variables'
import type { MacroString } from 'sandstone/core'

export type Range<MACRO extends boolean> =
| MacroString<number, MACRO>
Expand Down
2 changes: 1 addition & 1 deletion src/commands/implementations/_fake/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CommandNode } from 'sandstone/core/nodes'

import { CommandArguments } from '../../helpers.js'

import type { Macroable } from 'sandstone/variables'
import type { Macroable } from 'sandstone/core'

export class CommentCommandNode extends CommandNode<[unknown[]]> {
command = '#' as const
Expand Down
2 changes: 1 addition & 1 deletion src/commands/implementations/_fake/raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CommandNode } from 'sandstone/core/nodes'

import { CommandArguments } from '../../helpers.js'

import type { Macroable } from 'sandstone/variables'
import type { Macroable } from 'sandstone/core'

export class RawCommandNode extends CommandNode {
command = '' as const
Expand Down
4 changes: 2 additions & 2 deletions src/commands/implementations/block/clone.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { CommandNode } from 'sandstone/core/nodes'
import { coordinatesParser } from 'sandstone/variables'
import { coordinatesParser } from 'sandstone/variables/parsers'

import { CommandArguments } from '../../helpers.js'

import type { BLOCKS, Coordinates } from 'sandstone/arguments'
import type { Macroable } from 'sandstone/core'
import type { LiteralUnion } from 'sandstone/utils'
import type { Macroable } from 'sandstone/variables'

export class CloneCommandNode extends CommandNode {
command = 'clone' as const
Expand Down
4 changes: 2 additions & 2 deletions src/commands/implementations/block/fill.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { CommandNode } from 'sandstone/core/nodes'
import { coordinatesParser } from 'sandstone/variables'
import { coordinatesParser } from 'sandstone/variables/parsers'

import { CommandArguments } from '../../helpers.js'

import type { BLOCKS, Coordinates } from 'sandstone/arguments'
import type { Macroable } from 'sandstone/core'
import type { LiteralUnion } from 'sandstone/utils'
import type { Macroable } from 'sandstone/variables'

export class FillCommandNode extends CommandNode {
command = 'fill' as const
Expand Down
6 changes: 3 additions & 3 deletions src/commands/implementations/block/place.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { CommandNode } from 'sandstone/core/nodes'
import {
coordinatesParser, structureMirrorParser, structureRotationParser,
} from 'sandstone/variables'
} from 'sandstone/variables/parsers'

import { CommandArguments } from '../../helpers.js'
import { validateIntegerRange } from '../../validators.js'

import type {
Coordinates, STRUCTURES, WORLDGEN_CONFIGURED_FEATURES, WORLDGEN_STRUCTURES, WORLDGEN_TEMPLATE_POOLS,
} from 'sandstone/arguments'
import type { StructureClass } from 'sandstone/core'
import type { Macroable, StructureClass } from 'sandstone/core'
import type { LiteralUnion } from 'sandstone/utils'
import type { Macroable, StructureMirror, StructureRotation } from 'sandstone/variables'
import type { StructureMirror, StructureRotation } from 'sandstone/variables'

export class PlaceCommandNode extends CommandNode {
command = 'place' as const
Expand Down
46 changes: 41 additions & 5 deletions src/commands/implementations/block/setblock.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { CommandNode } from 'sandstone/core/nodes'
import { coordinatesParser } from 'sandstone/variables'
import { coordinatesParser, nbtStringifier } from 'sandstone/variables'

import { CommandArguments } from '../../helpers.js'

import type { BLOCKS, Coordinates } from 'sandstone/arguments'
import type { BLOCKS, Coordinates, RootNBT } from 'sandstone/arguments'
import type { Macroable } from 'sandstone/core'
import type { LiteralUnion } from 'sandstone/utils'
import type { Macroable } from 'sandstone/variables'
import type { FinalCommandOutput } from '../../helpers.js'

export class SetBlockCommandNode extends CommandNode {
command = 'setblock' as const
Expand All @@ -28,9 +29,44 @@ export class SetBlockCommand<MACRO extends boolean> extends CommandArguments {
*
* If not specified, defaults to `replace`.
*/
setblock = (
setblock(
pos: Macroable<Coordinates<MACRO>, MACRO>,
block: Macroable<LiteralUnion<BLOCKS>, MACRO>,
type?: Macroable<'destroy' | 'keep' | 'replace', MACRO>,
) => this.finalCommand([coordinatesParser(pos), block, type])
): FinalCommandOutput

/**
* Changes a block to another block.
*
* @param pos Specifies the position of the block to be changed.
*
* @param block Specifies the new block.
*
* @param nbt Specifies the nbt of the block to be changed.
*
* @param type Specifies how to handle the block change. Must be one of:
* - `destroy`: The old block drops both itself and its contents (as if destroyed by a player). Plays the appropriate block breaking noise.
* - `keep`: Only air blocks are changed (non-air blocks are unchanged).
* - `replace`: The old block drops neither itself nor any contents. Plays no sound.
*
* If not specified, defaults to `replace`.
*/
setblock(
pos: Macroable<Coordinates<MACRO>, MACRO>,
block: Macroable<LiteralUnion<BLOCKS>, MACRO>,
nbt?: Macroable<RootNBT, MACRO>,
type?: Macroable<'destroy' | 'keep' | 'replace', MACRO>,
): FinalCommandOutput

setblock(
pos: Macroable<Coordinates<MACRO>, MACRO>,
block: Macroable<LiteralUnion<BLOCKS>, MACRO>,
nbtOrType?: Macroable<RootNBT | 'destroy' | 'keep' | 'replace', MACRO>,
type?: Macroable<'destroy' | 'keep' | 'replace', MACRO>,
) {
if (typeof nbtOrType === 'object') {
return this.finalCommand([coordinatesParser(pos), `${block}${nbtStringifier(nbtOrType)}`, type])
}
return this.finalCommand([coordinatesParser(pos), block, nbtOrType])
}
}
4 changes: 2 additions & 2 deletions src/commands/implementations/entity/attribute.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CommandNode } from 'sandstone/core'
import { CommandNode } from 'sandstone/core/nodes'
import { targetParser } from 'sandstone/variables/parsers'

import { CommandArguments } from '../../helpers.js'

import type { SingleEntityArgument } from 'sandstone/arguments'
import type { Macroable } from 'sandstone/variables'
import type { Macroable } from 'sandstone/core'

// Attribute command

Expand Down
2 changes: 1 addition & 1 deletion src/commands/implementations/entity/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CommandArguments } from '../../helpers.js'
import type { ITEMS, MultiplePlayersArgument } from 'sandstone/arguments'
import type { TagClass } from 'sandstone/core'
import type { LiteralUnion } from 'sandstone/utils'
import type { Macroable } from 'sandstone/variables'
import type { Macroable } from 'sandstone/core'

export class ClearCommandNode extends CommandNode {
command = 'clear' as const
Expand Down
2 changes: 1 addition & 1 deletion src/commands/implementations/entity/damage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CommandArguments } from '../../helpers.js'
import type { Coordinates, DAMAGE_TYPES, SingleEntityArgument } from 'sandstone/arguments'
import type { DamageTypeClass } from 'sandstone/core'
import type { LiteralUnion } from 'sandstone/utils'
import type { Macroable } from 'sandstone/variables'
import type { Macroable } from 'sandstone/core'

export class DamageCommandNode extends CommandNode {
command = 'damage' as const
Expand Down
2 changes: 1 addition & 1 deletion src/commands/implementations/entity/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CommandArguments } from '../../helpers.js'

import type { MOB_EFFECTS, MultipleEntitiesArgument } from 'sandstone/arguments'
import type { LiteralUnion } from 'sandstone/utils'
import type { Macroable } from 'sandstone/variables'
import type { Macroable } from 'sandstone/core'

export class EffectCommandNode extends CommandNode {
command = 'effect' as const
Expand Down
2 changes: 1 addition & 1 deletion src/commands/implementations/entity/enchant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CommandArguments } from '../../helpers.js'

import type { ENCHANTMENTS, MultipleEntitiesArgument } from 'sandstone/arguments'
import type { LiteralUnion } from 'sandstone/utils'
import type { Macroable } from 'sandstone/variables'
import type { Macroable } from 'sandstone/core'

export class EnchantCommandNode extends CommandNode {
command = 'enchant' as const
Expand Down
74 changes: 52 additions & 22 deletions src/commands/implementations/entity/execute.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import {
_RawMCFunctionClass,
MCFunctionClass,
} from 'sandstone/core'
import { ContainerCommandNode } from 'sandstone/core/nodes'
import { makeCallable, toMinecraftResourceName } from 'sandstone/utils'
import {
coordinatesParser, ObjectiveClass, rangeParser, rotationParser,
Score,
coordinatesParser, rangeParser, rotationParser,
targetParser,
} from 'sandstone/variables'
} from 'sandstone/variables/parsers'

import { CommandArguments, FinalCommandOutput } from '../../helpers.js'
import { FunctionCommandNode } from '../server/function.js'
Expand All @@ -21,16 +16,28 @@ import type {
Coordinates, DIMENSIONS, ENTITY_TYPES, MultipleEntitiesArgument, ObjectiveArgument, Range, Rotation, SingleEntityArgument,
} from 'sandstone/arguments'
import type { SandstoneCommands } from 'sandstone/commands'
import type { MCFunctionNode, PredicateClass } from 'sandstone/core'
import type {
Macroable, MacroArgument, MCFunctionNode, PredicateClass,
} from 'sandstone/core'
import type { Node } from 'sandstone/core/nodes'
import type {
_RawMCFunctionClass,
} from 'sandstone/core/resources/datapack/mcfunction'
import type { SandstonePack } from 'sandstone/pack'
import type { LiteralUnion } from 'sandstone/utils'
import type { Macroable } from 'sandstone/variables'
import type { DataPointClass } from 'sandstone/variables/Data'
import type { ObjectiveClass } from 'sandstone/variables/Objective.js'
import type { Score } from 'sandstone/variables/Score.js'

// Execute command
export type SubCommand = [subcommand: string, ...args: unknown[]]

// Yes these suck

const isObjective = (arg: any): arg is ObjectiveClass => typeof arg === 'object' && Object.hasOwn(arg, 'reset')

const isScore = (arg: any): arg is Score => typeof arg === 'object' && Object.hasOwn(arg, 'unaryOperation')

class ExecuteCommandPart<MACRO extends boolean> extends CommandArguments<typeof ExecuteCommandNode> {
protected nestedExecute = (args: SubCommand, executable = true) => this.subCommand([args], ExecuteCommand<MACRO>, executable)
}
Expand Down Expand Up @@ -93,8 +100,25 @@ export class ExecuteCommandNode extends ContainerCommandNode<SubCommand[]> {

// This will be the execute string without "run"
const flattenedArgs = this.args.flat(1)
const filteredArgs = flattenedArgs.filter((arg) => arg !== undefined)
const executeString = `${this.command} ${filteredArgs.join(' ')}`
const args = []

for (const arg of flattenedArgs) {
if (arg !== undefined && arg !== null) {
// Yes these are cursed, unfortunately, there's not really a better way to do this as visitors only visit the root nodes.
if (typeof arg === 'object') {
if (Object.hasOwn(arg, 'toMacro') && (arg as MacroArgument)['local'].has(this.sandstoneCore.currentNode)) {
this.isMacro = true

args.push((arg as MacroArgument).toMacro())
} else {
args.push(arg)
}
} else {
args.push(arg)
}
}
}
const executeString = `${this.command} ${args.join(' ')}`

if (this.body.length === 0) {
return executeString
Expand All @@ -105,7 +129,14 @@ export class ExecuteCommandNode extends ContainerCommandNode<SubCommand[]> {
return this.body[0].getValue()
}

return `${executeString} run ${this.body[0].getValue()}`
let command = this.body[0].getValue()

if (command.startsWith('/')) {
this.isMacro = true
command = command.slice(1)
}

return `${this.isMacro ? '/' : ''}${executeString} run ${command}`
}

createMCFunction = (currentMCFunction: MCFunctionNode | null) => {
Expand All @@ -116,8 +147,7 @@ export class ExecuteCommandNode extends ContainerCommandNode<SubCommand[]> {
const namespace = currentMCFunction.resource.name.includes(':') ? `${currentMCFunction.resource.name.split(':')[0]}:` : ''

// Create a new MCFunctionNode with the body of the ExecuteNode.
const mcFunction = new MCFunctionClass(this.sandstoneCore, `${namespace}${currentMCFunction.resource.path.slice(2).join('/')}/${this.callbackName}`, {
addToSandstoneCore: false,
const mcFunction = this.sandstonePack.MCFunction(`${namespace}${currentMCFunction.resource.path.slice(2).join('/')}/${this.callbackName}`, {
creator: 'sandstone',
onConflict: 'rename',
})
Expand Down Expand Up @@ -194,7 +224,7 @@ export class ExecuteStoreArgsCommand<MACRO extends boolean> extends ExecuteComma
* @param playerScore The player's score to override.
*/
score(...args: [targets: Macroable<MultipleEntitiesArgument<MACRO>, MACRO>, objective: Macroable<ObjectiveArgument, MACRO>] | [playerScore: Macroable<Score, MACRO>]) {
if (args[0] instanceof Score) {
if (isScore(args[0])) {
return this.nestedExecute(['score', args[0]])
}
return this.nestedExecute(['score', targetParser(args[0]), args[1]])
Expand Down Expand Up @@ -307,17 +337,17 @@ export class ExecuteIfUnlessCommand<MACRO extends boolean> extends ExecuteComman
score(...args: any[]) {
const finalArgs: string[] = []

if (args[0] instanceof Score) {
if (isScore(args[0])) {
finalArgs.push(args[0].target.toString(), args[0].objective.name, args[1])
if (args[2] instanceof Score) {
if (isScore(args[2])) {
finalArgs.push(args[2].target.toString(), args[2].objective.name)
} else {
finalArgs.push(rangeParser(args[2]))
}
} else {
finalArgs.push(targetParser(args[0]), args[1] instanceof ObjectiveClass ? args[1].name : args[1], args[2])
finalArgs.push(targetParser(args[0]), isObjective(args[1]) ? args[1].name : args[1], args[2])
if (args[4]) {
finalArgs.push(targetParser(args[3]), args[4] instanceof ObjectiveClass ? args[4].name : args[4])
finalArgs.push(targetParser(args[3]), isObjective(args[4]) ? args[4].name : args[4])
} else {
finalArgs.push(rangeParser(args[3]))
}
Expand Down Expand Up @@ -350,16 +380,16 @@ export class ExecuteIfUnlessCommand<MACRO extends boolean> extends ExecuteComman
}

function(func: Macroable<_RawMCFunctionClass<[], []> | (() => any) | string, MACRO>) {
if (func instanceof _RawMCFunctionClass) {
return this.nestedExecute(['function', toMinecraftResourceName(func.path)])
if (typeof func === 'object' && Object.hasOwn(func, 'addToTag')) {
return this.nestedExecute(['function', toMinecraftResourceName((func as _RawMCFunctionClass<[], []>).path)])
}
/* @ts-ignore */
if (typeof func === 'string' || Object.hasOwn(func, 'toMacro')) {
return this.nestedExecute(['function', func])
}
const name = `${this.sandstoneCore.getCurrentMCFunctionOrThrow().resource.path.slice(2).join('/')}/execute_if_function`

const _func = new MCFunctionClass(this.sandstoneCore, name, {
const _func = this.sandstonePack.MCFunction(name, {
addToSandstoneCore: false,
creator: 'sandstone',
onConflict: 'rename',
Expand Down
2 changes: 1 addition & 1 deletion src/commands/implementations/entity/kill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { targetParser } from 'sandstone/variables/parsers'
import { CommandArguments } from '../../helpers.js'

import type { MultipleEntitiesArgument } from 'sandstone/arguments'
import type { Macroable } from 'sandstone/variables'
import type { Macroable } from 'sandstone/core'

export class KillCommandNode extends CommandNode {
command = 'kill' as const
Expand Down
4 changes: 2 additions & 2 deletions src/commands/implementations/entity/ride.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CommandNode } from 'sandstone/core'
import { CommandNode } from 'sandstone/core/nodes'
import { targetParser } from 'sandstone/variables/parsers'

import { CommandArguments } from '../../helpers.js'

import type { SingleEntityArgument } from 'sandstone/arguments'
import type { Macroable } from 'sandstone/variables'
import type { Macroable } from 'sandstone/core'

export class RideCommandNode extends CommandNode {
command = 'ride' as const
Expand Down
Loading

0 comments on commit 870d4fc

Please sign in to comment.