import { RESOURCE_PATHS, type MultiplePlayersArgumentOf } from 'sandstone/arguments' import type { ConditionClass } from 'sandstone/variables' import { ContainerNode } from '../../nodes' import type { SandstoneCore } from '../../sandstoneCore' import type { ResourceClassArguments, ResourceNode } from '../resource' import { JsonResource, ResourceClass, jsonStringify } from '../resource' import type { NonEmptyString } from 'sandstone/utils' import type { JsonSymbolResource } from 'sandstone/arguments/generated/_json/dispatcher' /** * A node representing a Minecraft advancement. */ export class AdvancementNode extends ContainerNode implements ResourceNode { constructor( sandstoneCore: SandstoneCore, public resource: AdvancementClass, ) { super(sandstoneCore) } getValue = () => jsonStringify(this.resource.json, this.resource._resourceType as keyof typeof RESOURCE_PATHS) } export type AdvancementClassArguments = { /** * The advancement's JSON. NBT primitive classes (`NBTInt`, `NBTFloat`, ...) * are deep-replaced with plain `number` (and `NBTIntArray` etc. with * `number[]`) via `MCDocToJSON`, so you can write raw numeric literals * instead of `NBT.int(5)`. */ json: AdvancementJSON } & ResourceClassArguments<'default'> export class AdvancementClass extends ResourceClass implements ConditionClass, JsonResource { declare readonly __conditionClassBrand: true static readonly resourceType = 'advancement' public json: AdvancementClassArguments['json'] constructor(sandstoneCore: SandstoneCore, name: string, args: AdvancementClassArguments) { super( sandstoneCore, { packType: sandstoneCore.pack.dataPack(), extension: 'json' }, AdvancementNode, AdvancementClass.resourceType, sandstoneCore.pack.resourceToPath(name, RESOURCE_PATHS[AdvancementClass.resourceType].path), args, ) this.json = args.json this.handleConflicts() } /** * Grant this advancement. * @param players Optional. Specifies the player(s). Defaults to `@s`. */ grant( players: MultiplePlayersArgumentOf = '@s' as MultiplePlayersArgumentOf, criterion?: AdvancementJSON extends undefined ? NonEmptyString : keyof NonNullable['criteria'] ) { this.pack.commands.advancement.grant(players as any).only(this.name, criterion) } /** * Revoke this advancement. * @param players Optional. Specifies the player(s). Defaults to `@s`. */ revoke( players: MultiplePlayersArgumentOf = '@s' as MultiplePlayersArgumentOf, criterion?: AdvancementJSON extends undefined ? NonEmptyString : keyof NonNullable['criteria'] ) { this.pack.commands.advancement.revoke(players as any).only(this.name, criterion) } /** * Grant this advancement and all its parent advancements. * Think of specifying everything from the start *until* that advancement. * * The exact order the operation is carried out in is: `parent > parent's parent > ... > root > this advancement.` * * @param players Optional. Specifies the player(s). Defaults to `@s`. */ grantUntilThis(players: MultiplePlayersArgumentOf = '@s' as MultiplePlayersArgumentOf) { this.pack.commands.advancement.grant(players as any).until(this.name) } /** * Revoke this advancement and all its parent advancements. * Think of specifying everything from the start *until* that advancement. * * The exact order the operation is carried out in is: `parent > parent's parent > ... > root > this advancement.` * * @param players Optional. Specifies the player(s). Defaults to `@s`. */ revokeUntilThis(players: MultiplePlayersArgumentOf = '@s' as MultiplePlayersArgumentOf) { this.pack.commands.advancement.revoke(players as any).until(this.name) } /** * Grant this advancement and all its children advancements. * Think of specifying everything *from* that advancement to the end. * * The exact order the operation is carried out in is specified `advancement > child > child's child > ...`. * When it operates on a child that branches, it iterates through all its children before continuing. * * @param players Optional. Specifies the player(s). Defaults to `@s`. */ grantFromThis(players: MultiplePlayersArgumentOf = '@s' as MultiplePlayersArgumentOf) { this.pack.commands.advancement.grant(players).from(this.name) } /** * Revoke this advancement and all its children advancements. * Think of specifying everything *from* that advancement to the end. * * The exact order the operation is carried out in is specified `advancement > child > child's child > ...`. * When it operates on a child that branches, it iterates through all its children before continuing. * * @param players Optional. Specifies the player(s). Defaults to `@s`. */ revokeFromThis(players: MultiplePlayersArgumentOf = '@s' as MultiplePlayersArgumentOf) { this.pack.commands.advancement.revoke(players).from(this.name) } /** * Grant this advancement, all its parent advancements, and all its children advancements. * Think of specifying everything through the specified advancement, going both backwards and forwards. * * The exact order the operation is as if the command were executed with `until` specified, then with `from` specified: * `parent > parent's parent > ... > root > specified advancement > child > child's child > ...` * * @param players Optional. Specifies the player(s). Defaults to `@s`. */ grantThroughThis(players: MultiplePlayersArgumentOf = '@s' as MultiplePlayersArgumentOf) { this.pack.commands.advancement.grant(players).through(this.name) } /** * Revoke this advancement, all its parent advancements, and all its children advancements. * Think of specifying everything through the specified advancement, going both backwards and forwards. * * The exact order the operation is as if the command were executed with `until` specified, then with `from` specified: * `parent > parent's parent > ... > root > specified advancement > child > child's child > ...` * * @param players Optional. Specifies the player(s). Defaults to `@s`. */ revokeThroughThis(players: MultiplePlayersArgumentOf = '@s' as MultiplePlayersArgumentOf) { this.pack.commands.advancement.revoke(players).through(this.name) } /** * @internal */ _toMinecraftCondition = () => new this.pack.conditions.Advancement(this.core, this.name) /** * Test if the currently executing player has a specific advancement criterion. */ criterion(criterion: string) { return new this.pack.conditions.Advancement(this.core, this.name, criterion) } }