import type { Coordinates, NBTObject, SingleEntityArgumentOf } from 'sandstone/arguments' import { type MacroArgument, type Macroable, DataPointPickClass } from 'sandstone/core' import { CommandNode } from 'sandstone/core/nodes' import { coordinatesParser, targetParser } from 'sandstone/variables/parsers' import { DataPointClass, type VectorClass } from 'sandstone/variables' import { nbtResolver } from 'sandstone/variables/nbt/NBTs' import { makeCallable } from 'sandstone/utils' import { CommandArguments, type FinalCommandOutput } from '../../helpers' import { type FloatNumberProviderArgument, type IntegerNumberProviderArgument, floatNumberProviderArgument, integerNumberProviderArgument, } from '../server/compute' export class DataCommandNode extends CommandNode { command = 'data' as const } export class DataCommand extends CommandArguments { protected NodeType = DataCommandNode /** * Read NBT data from blocks, entities, or storage. * * @example * ```ts * data.get(dataPoint, 1) // Get data point NBT with scale * data.get.block(abs(100, 70, 200), 'Items[0]', 1) // Get block NBT * data.get.entity('@p', 'Health') // Get player health * data.get.storage('minecraft:temp', 'value') // Get storage data * ``` */ get get() { const cmd = this.subCommand(['get'], DataGetCommand, false) return makeCallable(cmd, (dataPoint: DataPointClass<'entity'> | DataPointClass<'block'> | DataPointClass<'storage'>, scale?: Macroable): FinalCommandOutput => { if (dataPoint.type === 'block') { return cmd.block(coordinatesParser(dataPoint.currentTarget) as Macroable, MACRO>, dataPoint.path, scale) } if (dataPoint.type === 'entity') { return cmd.entity(dataPoint.currentTarget as Macroable, dataPoint.path, scale) } return cmd.storage(dataPoint.currentTarget as Macroable, dataPoint.path, scale) }) } /** * Merge NBT data into blocks, entities, or storage. * * @example * ```ts * data.merge.block(abs(100, 70, 200), {Items: []}) // Merge block NBT * data.merge.entity('@p', {Health: 20}) // Merge entity NBT * data.merge.storage('minecraft:temp', {flag: true}) // Merge storage data * ``` */ get merge() { return this.subCommand(['merge'], DataMergeCommand, false) } /** * Modify NBT data in blocks, entities, or storage. * * @example * ```ts * data.modify(dataPoint).set.value(20) // Modify data point * data.modify.entity('@p', 'Health').set.value(20) // Set health * data.modify.block(abs(100, 70, 200), 'Items').append.value({id: 'stone'}) // Add item * data.modify.storage('minecraft:temp', 'list').prepend.from.entity('@p', 'Inventory[0]') * ``` */ get modify() { const cmd = this.subCommand(['modify'], DataModifyCommand, false) return makeCallable(cmd, (dataPoint: DataPointClass<'entity'> | DataPointClass<'block'> | DataPointClass<'storage'>): DataModifyTypeCommand => { if (dataPoint.type === 'block') { return cmd.block(coordinatesParser(dataPoint.currentTarget) as Macroable, MACRO>, dataPoint.path) } if (dataPoint.type === 'entity') { return cmd.entity(dataPoint.currentTarget as Macroable, dataPoint.path) } return cmd.storage(dataPoint.currentTarget as Macroable, dataPoint.path) }) } /** * Remove NBT data from blocks, entities, or storage. * * @example * ```ts * data.remove(dataPoint) // Remove data point NBT * data.remove.block(abs(100, 70, 200), 'Items[0]') // Remove block NBT * data.remove.entity('@e[type=item]', 'Motion') // Remove entity motion * data.remove.storage('minecraft:temp', 'old_data') // Remove storage data * ``` */ get remove() { const cmd = this.subCommand(['remove'], DataRemoveCommand, false) return makeCallable(cmd, (dataPoint: DataPointClass<'entity'> | DataPointClass<'block'> | DataPointClass<'storage'>): FinalCommandOutput => { if (dataPoint.type === 'block') { return cmd.block(coordinatesParser(dataPoint.currentTarget) as Macroable, MACRO>, dataPoint.path) } if (dataPoint.type === 'entity') { return cmd.entity(dataPoint.currentTarget as Macroable, dataPoint.path) } return cmd.storage(dataPoint.currentTarget as Macroable, dataPoint.path) }) } } export class DataGetCommand extends CommandArguments { /** * Get the NBT of a block at the given position. * * @param targetPos The coordinates of the block to get the NBT from. * @param path The path of the NBT to get. * @param scale The scale to multiply the NBT value by. */ block = ( targetPos: Macroable, MACRO>, path?: Macroable, scale?: Macroable, ): FinalCommandOutput => { return this.finalCommand(['block', coordinatesParser(targetPos), path, scale]) } /** * Get the NBT of a given entity. * * @param target The entity to get the NBT from. * @param path The path of the NBT to get. * @param scale The scale to multiply the NBT value by. */ entity = ( target: Macroable, MACRO>, path?: Macroable, scale?: Macroable, ): FinalCommandOutput => { return this.finalCommand(['entity', targetParser(target), path, scale]) } /** * Get the NBT from a given storage path. * * @param target The storage to get the NBT from. * @param path The path of the NBT to get. * @param scale The scale to multiply the NBT value by. */ storage = ( target: Macroable, path?: Macroable, scale?: Macroable, ): FinalCommandOutput => { return this.finalCommand(['storage', target, path, scale]) } } export class DataMergeCommand extends CommandArguments { /** * Merge the NBT of a block at the given position, with the given NBT. * * @param targetPos The coordinates of the block to merge the NBT with. * @param nbt The NBT to merge with. */ block = (targetPos: Macroable, MACRO>, nbt: Macroable) => this.finalCommand(['block', coordinatesParser(targetPos), nbtResolver(nbt)]) /** * Merge the NBT of the given entity, with the given NBT. * * @param target The entity to merge the NBT with. * @param nbt The NBT to merge with. */ entity = (target: Macroable, MACRO>, nbt: Macroable) => this.finalCommand(['entity', targetParser(target), nbtResolver(nbt)]) /** * Merge the NBT of the given storage path, with the given NBT. * * @param target The storage to merge the NBT with. * @param nbt The NBT to merge with. */ storage = (target: Macroable, nbt: Macroable) => this.finalCommand(['storage', target, nbtResolver(nbt)]) } export class DataModifyFromCommand extends CommandArguments { /** * Modify with the NBT of a block at the given position. * * @param sourcePosition The coordinates of the block to modify the NBT with. * @param sourcePath The path of the NBT to modify with. */ block = (sourcePosition: Macroable, MACRO>, sourcePath: Macroable) => this.finalCommand(['block', coordinatesParser(sourcePosition), sourcePath]) /** * Modify with the NBT of a given entity. * * @param source The entity to modify the NBT with. * @param sourcePath The path of the NBT to modify with. */ entity = (source: Macroable, MACRO>, sourcePath: Macroable) => this.finalCommand(['entity', targetParser(source), sourcePath]) /** * Modify with the NBT of a given storage path. * * @param source The storage target to modify the NBT with. * @param sourcePath The path of the NBT to modify with. */ storage = (source: Macroable, sourcePath: Macroable): FinalCommandOutput => { return this.finalCommand(['storage', source, sourcePath]) } } export class DataModifyValuesCommand extends CommandArguments { get from() { const cmd = this.subCommand(['from'], DataModifyFromCommand, false) return makeCallable(cmd, (dataPoint: DataPointClass<'entity' | 'block' | 'storage'> | DataPointPickClass) => { let targetDataPoint: DataPointClass<'entity' | 'block' | 'storage'> if (dataPoint instanceof DataPointPickClass) { targetDataPoint = dataPoint._toDataPoint() as DataPointClass<'entity' | 'block' | 'storage'> } else { targetDataPoint = dataPoint } if (targetDataPoint.type === 'block') { return cmd.block(coordinatesParser(targetDataPoint.currentTarget) as Macroable, MACRO>, targetDataPoint.path) } if (targetDataPoint.type === 'entity') { return cmd.entity(targetDataPoint.currentTarget as Macroable, targetDataPoint.path) } return cmd.storage(targetDataPoint.currentTarget as Macroable, targetDataPoint.path) }) } /** * Modify with the value of a `minecraft:context_int_provider` or * `minecraft:context_float_provider`. Mirrors `/compute`'s three target * contexts (`default` / `block ` / `entity `). * * Integer and float providers each get a separate chained entry, since the * underlying MC syntax is now `compute integer|float `. */ get compute() { return this.subCommand(['compute'], DataModifyComputeCommand, false) } string = { /** * Modify with the NBT String of a block at the given position. * * @param sourcePosition The coordinates of the block to modify the NBT with. * @param sourcePath The path of the NBT to modify with. * @param start Optional. Index of first character to include at the start of the string. * @param end Optional. Index of the first character to exclude at the end of the string */ block: ( sourcePosition: Macroable, MACRO>, sourcePath: Macroable, start?: Macroable, end?: Macroable, ) => { const command: (string | VectorClass<[string, string, string]> | number | MacroArgument)[] = [ 'string', 'block', coordinatesParser(sourcePosition), sourcePath, ] if (start !== undefined && end !== undefined) { command.push(start, end) } else if (start !== undefined && start !== 0) { command.push(start) } this.finalCommand(command) }, /** * Modify with the NBT String of a given entity. * * @param source The entity to modify the NBT with. * @param sourcePath The path of the NBT to modify with. * @param start Optional. Index of first character to include at the start of the string. * @param end Optional. Index of the first character to exclude at the end of the string */ entity: ( source: Macroable, MACRO>, sourcePath: Macroable, start?: Macroable, end?: Macroable, ) => { const command: Macroable | number, MACRO>[] = [ 'string', 'entity', targetParser(source), sourcePath, ] if (start !== undefined && end !== undefined) { command.push(start, end) } else if (start !== undefined && start !== 0) { command.push(start) } this.finalCommand(command) }, /** * Modify with the NBT String of a given storage path. * * @param source The storage path to modify the NBT with. * @param sourcePath The path of the NBT to modify with. * @param start Optional. Index of first character to include at the start of the string. * @param end Optional. Index of the first character to exclude at the end of the string */ storage: ( source: Macroable, sourcePath: Macroable, start?: Macroable, end?: Macroable, ) => { const command: (string | number | MacroArgument)[] = ['string', 'storage', source, sourcePath] if (start !== undefined && end !== undefined) { command.push(start, end) } else if (start !== undefined && start !== 0) { command.push(start) } this.finalCommand(command) }, } /** * Modify the NBT with the given value. */ value = (value: Macroable) => this.finalCommand(['value', nbtResolver(value)]) } /** * `data modify compute` — selects the target context (`default` / * `block ` / `entity `), then picks integer / float on the * returned chain. */ export class DataModifyComputeCommand extends CommandArguments { /** * `compute default` — only common arguments (position, `this` entity) in scope. */ default = (): DataModifyComputeContext => this.subCommand(['default'], DataModifyComputeContext, false) /** * `compute block ` — binds the `command_compute_position` context. * * @param pos Block position whose context is bound to the provider. */ block = ( pos: Macroable, MACRO>, ): DataModifyComputeContext => this.subCommand(['block', coordinatesParser(pos)], DataModifyComputeContext, false) /** * `compute entity ` — binds the `command_compute_entity` context. * * @param entity Selector for the entity whose context is bound to the provider. */ entity = ( entity: Macroable, MACRO>, ): DataModifyComputeContext => this.subCommand(['entity', targetParser(entity)], DataModifyComputeContext, false) } /** * `data modify compute ` — picks integer or float provider * kind for the final command. */ export class DataModifyComputeContext extends CommandArguments { /** * Modify with the value of an `minecraft:context_int_provider`. * * @example * ```ts * data.modify(target).compute.default.integer({ type: 'minecraft:constant', value: 1 }) * data.modify(entity('Health')).compute.entity('@p').integer('minecraft:my_pack:kill_count') * ``` */ integer = >( provider: PROVIDER, ): FinalCommandOutput => this.finalCommand(['integer', integerNumberProviderArgument(this.sandstoneCore, provider)]) /** * Modify with the value of an `minecraft:context_float_provider`. No scale * modifier — `/data compute` is provider-only. * * @example * ```ts * data.modify(target).compute.default.float({ type: 'minecraft:uniform', min: 0, max: 1 }) * data.modify(storage('foo')).compute.block([0, 64, 0]).float('minecraft:my_pack:damage') * ``` */ float = >( provider: PROVIDER, ): FinalCommandOutput => this.finalCommand(['float', floatNumberProviderArgument(this.sandstoneCore, provider)]) } export class DataModifyTypeCommand extends CommandArguments { /** Append the source data onto the end of the pointed-to list. */ get append() { return this.subCommand(['append'], DataModifyValuesCommand, false) } /** * Insert the source data into the pointed-to list as element `index`, then shift higher elements one position upwards. * * @param index The index to insert the NBT to. */ insert = (index: Macroable) => this.subCommand(['insert', index], DataModifyValuesCommand, false) /** Merge the source data into the pointed-to object. */ get merge() { return this.subCommand(['merge'], DataModifyValuesCommand, false) } /** Prepend the source data onto the beginning of the pointed-to list. */ get prepend() { return this.subCommand(['prepend'], DataModifyValuesCommand, false) } /** Set the tag specified by `targetPath` to the source data. */ get set() { return this.subCommand(['set'], DataModifyValuesCommand, false) } } export class DataModifyCommand extends CommandArguments { /** * Modify the NBT of a block at the given position. * * @param targetPos The coordinates of the block to modify the NBT from. * @param path The path of the NBT to modify. */ block = (targetPos: Macroable, MACRO>, targetPath: Macroable): DataModifyTypeCommand => { return this.subCommand(['block', coordinatesParser(targetPos), targetPath], DataModifyTypeCommand, false) } /** * Modify the NBT of a given entity. * * @param target The entity to modify the NBT from. * @param path The path of the NBT to modify. */ entity = (target: Macroable, MACRO>, targetPath: Macroable): DataModifyTypeCommand => { return this.subCommand(['entity', targetParser(target), targetPath], DataModifyTypeCommand, false) } /** * Modify the NBT from a given storage path. * * @param target The storage to modify the NBT from. * @param path The path of the NBT to modify. */ storage = (target: Macroable, targetPath: Macroable): DataModifyTypeCommand => { return this.subCommand(['storage', target, targetPath], DataModifyTypeCommand, false) } } export class DataRemoveCommand extends CommandArguments { /** * Remove the NBT of a block at the given position. * * @param targetPos The coordinates of the block to remove the NBT from. * @param path The path of the NBT to remove. */ block = ( targetPos: Macroable, MACRO>, targetPath: Macroable, ): FinalCommandOutput => { return this.finalCommand(['block', coordinatesParser(targetPos), targetPath]) } /** * Remove the NBT of a given entity. * * @param target The entity to remove the NBT from. * @param path The path of the NBT to remove. */ entity = ( target: Macroable, MACRO>, targetPath: Macroable, ): FinalCommandOutput => { return this.finalCommand(['entity', targetParser(target), targetPath]) } /** * Remove the NBT from a given storage path. * * @param target The storage to remove the NBT from. * @param path The path of the NBT to remove. */ storage = ( target: Macroable, targetPath: Macroable, ): FinalCommandOutput => { return this.finalCommand(['storage', target, targetPath]) } }