import type { TimeArgument } from 'sandstone/arguments' import type { Macroable, Node } from 'sandstone/core' import { isMacroArgument } from 'sandstone/core/Macro' import { ContainerCommandNode } from 'sandstone/core/nodes' import type { MCFunctionClass, MCFunctionNode } from 'sandstone/core/resources/datapack' import type { TagClass } from 'sandstone/core/resources/datapack/tag' import { CommandArguments } from '../../helpers' type ScheduledFunction = string | TagClass<'function'> | MCFunctionClass | (() => any | Promise) export class ScheduleCommandNode extends ContainerCommandNode { command = 'schedule' as const override createMCFunction: (currentMCFunction: MCFunctionNode | null) => { node: Node | Node[] mcFunction?: MCFunctionNode | undefined } = (currentMCFunction) => { if (this.body.length === 0 || this.args[0] === 'clear' || !currentMCFunction) { return { node: this } } const [_, func, time, append] = this.args if (typeof func !== 'string' && func !== undefined) { throw new Error('Cannot have something else than a string as the argument for a scheduled function with callback') } // Create a new MCFunctionNode with the body of the ExecuteNode. const mcFunction = this.sandstonePack.MCFunction( `${currentMCFunction.resource.name}/${func ?? 'schedule'}`, { addToSandstoneCore: false, creator: 'sandstone', onConflict: 'rename', }, ) const mcFunctionNode = mcFunction.node mcFunctionNode.body = this.body this.body = [] // Return an execute calling this MCFunction. this.args = ['function', mcFunction.name, time, append] return { node: this, mcFunction: mcFunctionNode } } } export type ScheduleType = 'append' | 'replace' export class ScheduleCommand extends CommandArguments { protected NodeType = ScheduleCommandNode /** * Cancel a scheduled function. * * @param func Function name, MCFunction, or function tag to unschedule. * Examples: 'minecraft:my_timer', myFunction, '#mypack:timers' * * @example * ```ts * schedule.clear('minecraft:my_timer') // Cancel scheduled function * schedule.clear(myScheduledFunction) // Cancel Sandstone function * ``` */ clear = (func: Macroable | string | TagClass<'function'>, MACRO>) => { const funcArg = isMacroArgument(this.sandstoneCore, func) ? func : `${func}` const result = this.finalCommand(['clear', funcArg]) return result } /** * Schedule function execution after a delay. * * @param func Function to schedule. Can be name, MCFunction, tag, or callback. * Examples: 'minecraft:timer', myFunction, '#mypack:events', () => {...} * * @param delay Time delay before execution. * Examples: '5s' (5 seconds), '100t' (100 ticks), '1d' (1 day) * * @param type Optional scheduling mode: 'replace' or 'append'. * 'replace' cancels existing schedules, 'append' allows multiple. * Defaults to 'replace'. * * @example * ```ts * schedule.function('minecraft:timer', '30s') // Schedule in 30 seconds * schedule.function(myFunction, '100t', 'append') // Schedule in 100 ticks * schedule.function(() => { say('Hello!') }, '5s') // Schedule callback * ``` */ function = ( func: Macroable, delay: Macroable, type?: Macroable, ) => { const node = this.getNode() if (typeof func === 'function' && Object.hasOwn(func, 'addToTag')) { return this.finalCommand(['function', (func as unknown as MCFunctionClass).name, delay, type], node) } // A callback has been given if (typeof func === 'function') { console.log('hello!') this.sandstoneCore.insideContext(node, func as () => void | Promise, false) return this.finalCommand(['function', undefined, delay, type], node) } const funcArg = isMacroArgument(this.sandstoneCore, func) ? func : `${func}` return this.finalCommand(['function', funcArg, delay, type], node) } }