/* eslint-disable max-len */ import type { NBTSerializable } from 'sandstone/arguments' import type { MakeInstanceCallable, NamespacedString, NonEmptyString } from 'sandstone/utils' import { makeClassCallable } from 'sandstone/utils' import type { ConditionClass, SelectorPickClass } from './abstractClasses' import type { SelectorEntityType, SelectorProperties } from './Selector' import { SelectorClass } from './Selector' import type { SandstonePack } from 'sandstone' /** * Label tag (/tag) handler */ export class _RawLabelClass implements NBTSerializable { /** * Label name */ public name: NonEmptyString /** * Label Tag name with namespace */ public fullName: `${string}.${string}` /** * Label Description (optional) */ public description?: NonEmptyString constructor( private pack: SandstonePack, name: NonEmptyString | NamespacedString, description?: NonEmptyString, ) { if (name.includes(':')) { this.name = name.split(':')[1] as NonEmptyString this.fullName = name.replace(':', '.') as `${string}.${string}` } else { this.name = name this.fullName = `${pack.defaultNamespace}.${name}` } if (description) this.description = description } public LabelHolder(entity: SelectorClass | SelectorPickClass): EntityLabel public LabelHolder(target: '@e'): EntityLabel public LabelHolder(target: '@a'): EntityLabel public LabelHolder(target: '@s' | '@n'): EntityLabel public LabelHolder(target: '@p' | '@r', selectorArguments?: Omit, 'type'>, 'limit'>): EntityLabel public LabelHolder, 'limit'>>(target: '@s' | '@n', selectorArguments: PROPERTIES): EntityLabel public LabelHolder, 'type'>>(target: '@a', selectorArguments: PROPERTIES): EntityLabel public LabelHolder>(target: '@e', selectorArguments: PROPERTIES): EntityLabel public LabelHolder(entity: '@s' | '@p' | '@a' | '@e' | '@n' | '@r' | SelectorPickClass, selectorArguments?: SelectorProperties): EntityLabel { if (typeof entity === 'string') { return new EntityLabel(this.pack, new SelectorClass(this.pack, entity, selectorArguments), this) } return new EntityLabel(this.pack, entity, this) } /** * Contains the name and description of the Label (eg. 'wasd:is_walking; Whether the player is not mounted') */ toString = () => `${this.fullName}${this.description ? `; ${this.description}` : ''}` /** * @internal */ toNBT = () => `'${this.fullName}'` __call__(entity: SelectorClass | SelectorPickClass): EntityLabel __call__(target: '@e'): EntityLabel __call__(target: '@a'): EntityLabel __call__(target: '@s' | '@n'): EntityLabel __call__(target: '@p' | '@r', selectorArguments?: Omit, 'type'>, 'limit'>): EntityLabel __call__, 'limit'>>(target: '@s' | '@n', selectorArguments: PROPERTIES): EntityLabel __call__, 'type'>>(target: '@a', selectorArguments: PROPERTIES): EntityLabel __call__>(target: '@e', selectorArguments: PROPERTIES): EntityLabel __call__(entity: '@s' | '@p' | '@a' | '@e' | '@n' | '@r' | SelectorPickClass, selectorArguments?: SelectorProperties): EntityLabel { if (typeof entity === 'string') { return new EntityLabel(this.pack, new SelectorClass(this.pack, entity, selectorArguments), this) } return new EntityLabel(this.pack, entity, this) } } export const LabelClass = makeClassCallable(_RawLabelClass) export type LabelClass = MakeInstanceCallable<_RawLabelClass> export class EntityLabel implements ConditionClass, SelectorPickClass { declare readonly __textComponentClassBrand: true declare readonly __conditionClassBrand: true /** * Phantom brand property for TypeScript type discrimination. */ declare readonly __selectorPickBrand: { single: IS_SINGLE; player: IS_PLAYER } /** Label */ public label /** * Selects entity with the label */ public selector: SelectorClass /** * Selects entity */ public originalSelector: string | SelectorClass /** Test for label on entity */ public test = this as ConditionClass constructor( private pack: SandstonePack, entity: SelectorClass | SelectorPickClass, label: _RawLabelClass, ) { this.originalSelector = entity._toSelector() this.label = label // Haha brrrrrrr const selector = ( typeof this.originalSelector === 'string' ? new SelectorClass(this.pack, '@s') : new SelectorClass(this.pack, this.originalSelector.target, { ...this.originalSelector.arguments }) ) as SelectorClass if (selector.arguments) { if (selector.arguments.tag) { if (typeof selector.arguments.tag === 'string' || !Array.isArray(selector.arguments.tag)) { selector.arguments.tag = [selector.arguments.tag, label.fullName] } else { selector.arguments.tag = [...selector.arguments.tag, label.fullName] } } else selector.arguments.tag = [label.fullName] } else selector.arguments = { tag: [label.fullName] } as SelectorProperties this.selector = selector } /** * Add label to entity */ public add = () => this.pack.commands.tag(this.originalSelector).add(this.label.fullName) /** * Remove label from entity */ public remove = () => this.pack.commands.tag(this.originalSelector).remove(this.label.fullName) /** * Set label on/off for entity */ public set(set: boolean | ConditionClass) { if (typeof set === 'boolean') { if (set) this.add() else this.remove() } else { this.pack._.if(set, () => this.add()).else(() => this.remove()) } } /** * Toggle label on/off for entity */ public toggle() { this.pack._.if(this.test, () => this.remove()).else(() => this.add()) } /** * Contains the selector, and the name/description of the Label (eg. 'Whether @s has the label wasd:is_walking; Whether the player is not mounted') */ public toString = () => `Whether ${this.originalSelector.toString()} has the label ${this.label.toString()}` /** * @internal */ _toMinecraftCondition = () => new this.pack.conditions.Label(this.pack.core, this) /** * @internal */ _toSelector = () => this.selector._toSelector() /** * @internal */ toNBT = () => this.selector._toSelector().toNBT() /** * @internal */ _toChatComponent = () => this.selector._toSelector()._toChatComponent() }