import type { MultiplePlayersArgumentOf, RootNBT } from 'sandstone/arguments' import type { Macroable, MacroArgument } from 'sandstone/core' import { CommandNode, isMacroArgument } from 'sandstone/core' import { nbtResolver, targetParser } from 'sandstone/variables' import type { FinalCommandOutput } from '../../helpers' import { CommandArguments } from '../../helpers' import type { Registry } from 'sandstone/arguments/generated/registry' import type { MemberModifiers } from 'sandstone/utils' import type { JsonSymbolDataComponent } from 'sandstone/arguments/generated/_json/dispatcher' // Give command export class GiveCommandNode extends CommandNode { command = 'give' as const } export class GiveCommand extends CommandArguments { protected NodeType = GiveCommandNode /** * Give items to players. * * Adds the specified items directly to player inventories. If the inventory * is full, excess items are dropped at the player's location. * * @param targets Players to give items to (e.g., `'@p'`, `'@a'`, `'PlayerName'`). * * @param item The item type to give (e.g., `'minecraft:diamond'`). * * @param count Optional quantity to give (defaults to 1). * * @example * ```ts * give('@p', 'minecraft:apple', 5) * give('@a', 'minecraft:diamond', 10) * give('PlayerName', 'minecraft:emerald', 20) * ``` */ give( targets: Macroable, MACRO>, item: Macroable, count?: number, ): FinalCommandOutput /** * Give items with component modifications to players. * * Adds items with custom data components to player inventories. Components * allow customizing enchantments, display names, lore, attributes, and more. * * @param targets Players to give items to (e.g., `'@p'`, `'@a'`, `'PlayerName'`). * * @param item The item type to give (e.g., `'minecraft:diamond_sword'`). * * @param components Data component modifications for the item. Prefix a * component key with `!` and use `{}` as the value to * remove a default component. * * @param count Optional quantity to give (defaults to 1). * * @example * ```ts * // Enchanted sword with custom name * give('@p', 'minecraft:diamond_sword', { * 'minecraft:enchantments': { * 'minecraft:sharpness': 5, * 'minecraft:unbreaking': 3, * }, * 'minecraft:custom_name': 'Excalibur', * 'minecraft:lore': ['A legendary blade'], * }) * * // Unbreakable chestplate with bonus health * give('@p', 'minecraft:diamond_chestplate', { * 'minecraft:unbreakable': {}, * 'minecraft:attribute_modifiers': [{ * type: 'minecraft:max_health', * amount: 4, * operation: 'add_value', * slot: 'chest', * id: 'my_pack:health_boost', * }], * }) * * // Remove default attributes from a tool * give('@p', 'minecraft:diamond_pickaxe', { * '!minecraft:attribute_modifiers': {}, * }) * * // Written book * give('@a', 'minecraft:written_book', { * 'minecraft:written_book_content': { * title: 'Server Rules', * author: 'Admin', * pages: ['Welcome to the server!'], * }, * }) * ``` */ give( targets: Macroable, MACRO>, item: Macroable, components: Macroable, MACRO>, count?: Macroable, ): FinalCommandOutput give( targets: Macroable, MACRO>, item: Macroable, countOrComponents?: Macroable, count?: Macroable, ) { const select = isMacroArgument(this.sandstoneCore, targets) ? targets : targetParser(targets) if (typeof countOrComponents === 'object' || typeof countOrComponents === 'function') { const components = countOrComponents! const componentsIsMacro = isMacroArgument(this.sandstoneCore, components) if (isMacroArgument(this.sandstoneCore, item) || componentsIsMacro) { return this.finalCommand([ select, this.sandstonePack.Macro`${item}${componentsIsMacro ? components as MacroArgument : componentPatchStringifier(components as any)}`, count, ]) } if (components.constructor.name === 'Object' && Object.keys(components).length === 0) { return this.finalCommand([select, item, count]) } return this.finalCommand([select, `${item}${componentPatchStringifier(components as any)}`, count]) } return this.finalCommand([select, item, countOrComponents]) } } export function componentPatchStringifier(components: Record>) { if (Object.hasOwn(components, 'toMacro')) { // @ts-ignore return components.toMacro() as string } const resultPairs: string[] = [] for (const [key, value] of Object.entries(components)) { if (key.startsWith('!')) { if (typeof value === 'object' && Object.keys(value).length === 0) { resultPairs.push(key) } else { throw new Error(`Attempted to insert a negation component patch of ${key} with a value, use {} instead.`) } } else { resultPairs.push(`${key}=${nbtResolver(value).toString()}`) } } return `[${resultPairs.join(',')}]` }