import { RESOURCE_PATHS, type CONTAINER_SLOTS, type Coordinates, type ENTITY_SLOTS, type MultipleEntitiesArgument, } from 'sandstone/arguments' import { targetParser } from 'sandstone/variables/parsers' import { ContainerNode } from '../../nodes' import type { SandstoneCore } from '../../sandstoneCore' import type { JsonResource, ListResource, ResourceClassArguments, ResourceNode } from '../resource' import { ResourceClass, jsonStringify } from '../resource' import type { JsonSymbolResource } from 'sandstone/arguments/generated/_json/dispatcher' /** * A node representing a Minecraft item modifier. */ export class ItemModifierNode extends ContainerNode implements ResourceNode { constructor( sandstoneCore: SandstoneCore, public resource: ItemModifierClass, ) { super(sandstoneCore) } getValue = () => jsonStringify(this.resource.json, this.resource._resourceType as keyof typeof RESOURCE_PATHS) } type ItemModifierJSON = JsonSymbolResource[(typeof ItemModifierClass)['resourceType']] export type ItemModifierClassArguments = { /** * The item modifier's JSON. */ json: ItemModifierJSON | ItemModifierJSON[] } & ResourceClassArguments<'list'> type Modifier = ItemModifierJSON | ItemModifierClass export class ItemModifierClass extends ResourceClass implements JsonResource, ListResource { static readonly resourceType = 'item_modifier' as const public json: ItemModifierClassArguments['json'] constructor(sandstoneCore: SandstoneCore, name: string, args: ItemModifierClassArguments) { super( sandstoneCore, { packType: sandstoneCore.pack.dataPack(), extension: 'json' }, ItemModifierNode, ItemModifierClass.resourceType, sandstoneCore.pack.resourceToPath(name, RESOURCE_PATHS[ItemModifierClass.resourceType].path), args, ) this.json = args.json this.handleConflicts() } public push(...modifiers: Modifier[]) { if (!Array.isArray(this.json)) { this.json = [this.json] } for (const modifier of modifiers) { let modifierJSON: any if (modifier instanceof ItemModifierClass) { modifierJSON = modifier.json } else { modifierJSON = modifier } if (Array.isArray(modifierJSON)) { this.json.push(...modifierJSON) } else { this.json.push(modifierJSON) } } } public unshift(...modifiers: Modifier[]) { if (!Array.isArray(this.json)) { this.json = [this.json] } for (const modifier of modifiers) { let modifierJSON: any if (modifier instanceof ItemModifierClass) { modifierJSON = modifier.json } else { modifierJSON = modifier } if (Array.isArray(modifierJSON)) { this.json.unshift(...modifierJSON) } else { this.json.unshift(modifierJSON) } } } get modify() { return { /** * @param pos The position of the container containing the slot to apply the modifier to. * @param slot The slot to apply the modifier to. */ block: (pos: Coordinates, slot: CONTAINER_SLOTS) => { this.pack.commands.item.modify.block(pos, slot, this) }, /** * @param targets The entity/entities containing the slot to apply the modifier to. * @param slot The slot to apply the modifier to. * @param modifier The name of the modifier. */ entity: (targets: MultipleEntitiesArgument, slot: ENTITY_SLOTS) => { this.pack.commands.item.modify.entity(targetParser(targets), slot, this) }, } } }