/* eslint-disable @typescript-eslint/no-non-null-assertion */ /* eslint-disable max-len */ import type { NBTObject, RootNBT } from 'sandstone/arguments' import type { DataPointPickClass } from 'sandstone/core' import type { ConditionNode } from 'sandstone/flow' import type { LiteralUnion, NonEmptyString } from 'sandstone/utils' import type { Macroable } from '../core/Macro' import { ConditionalDataPointPickClass } from '../core/Macro' import type { SandstonePack } from '../pack' import { DataPointClass, StringDataPointClass } from './Data' import { Score } from './Score' export abstract class IterableDataClass extends ConditionalDataPointPickClass { constructor(sandstoneCore: SandstonePack['core'], public iteratorType: TYPE) { super(sandstoneCore) } iterator( _callback: (dataPoints: TYPE extends 'list' ? [DataPointClass] : [StringDataPointClass, DataPointClass]) => void, _direction: 'normal' | 'reverse' = 'normal' ): () => void { throw new Error('Not implemented') } size(): DataPointClass<'storage'> { throw new Error('Not implemented') } last(): readonly [StringDataPointClass, DataPointClass] | DataPointClass { throw new Error('Not implemented') } continue(direction: 'normal' | 'reverse' = 'normal'): ConditionNode { throw new Error('Not implemented') } } type GetKeys = T extends unknown[] ? T extends [] // special case empty tuple => no keys ? never : '0' extends keyof T // any tuple with at least one element ? Exclude : number // other array : keyof T // not an array export type DataIndexMapInitial = RootNBT | Record export class DataIndexMapClass extends IterableDataClass<'map'> { entries: Record = {} dataPoint: NonNullable> constructor( private pack: SandstonePack, initialize: INITIAL, dataPoint?: DataPointClass<'storage'>, ) { super(pack.core, 'map') if (dataPoint) { this.dataPoint = dataPoint } else { this.dataPoint = this.pack.DataVariable(undefined, 'IndexMap') } // We currently are in a MCFunction => that's where the initialization should take place if (pack.core.currentMCFunction) { this.init(initialize) } else { // Else, we should run it in the init MCFunction pack.initMCFunction.push(() => this.init(initialize)) } } private init(initialize: INITIAL) { for (const [i, [key, value]] of Object.entries(initialize).entries()) { this.dataPoint.select('Index').select(key).set(i) this.dataPoint.select('Entries').append([]) const entry = this.dataPoint.select(`Entries[${i}]`) entry.append(key) entry.append(value) this.entries[key] = i } } __entryIterator?: DataPointClass<'storage'> entryIterator(direction: 'normal' | 'reverse') { if (!this.__entryIterator) { this.__entryIterator = this.pack.DataVariable(this.dataPoint.select('Entries')).select(`[${direction === 'normal' ? 0 : -1}]`) } return this.__entryIterator } iterator( callback: (data: [key: StringDataPointClass, value: DataPointClass]) => void, direction: 'normal' | 'reverse' = 'normal' ) { this.__entryIterator = undefined const entryIterator = this.entryIterator(direction) return () => { callback([entryIterator.select('[0]').slice(0), entryIterator.select('[1]')]) entryIterator.remove() } } continue(direction: 'normal' | 'reverse' = 'normal') { return this.entryIterator(direction)._toMinecraftCondition() } /** * Use in conjunction with a Score to get the count of entries. * * @example * Variable(map.size(), 'map_size') * * @returns The entries of the map. */ size = () => this.dataPoint.select('Entries') last = () => [this.dataPoint.select('Entries[-1][0]').slice(0), this.dataPoint.select('Entries[-1][1]')] as readonly [ key: StringDataPointClass, value: DataPointClass, ] /** * @returns The index of the entry. */ set(key: Macroable>, true>, value: NBTObject | DataPointClass | DataPointPickClass) { if (typeof key === 'string') { if (this.entries[key]) { const index = this.entries[key] this.dataPoint.select(`Entries[${index}]`).set(value as NBTObject) return index } this.dataPoint.select('Keys').append(key) this.dataPoint.select('Values').append(value as NBTObject) const index = this.pack.Variable(this.size()) this.dataPoint.select('Index').select(key).set(index) this.dataPoint.select('Entries').append([]) const entry = this.dataPoint.select('Entries[-1]') entry.append(key) entry.append(value) return index } const { Macro, MCFunction, Variable, DataVariable } = this.pack let _key: DataPointClass if (key instanceof DataPointClass) { _key = key } else if (Object.hasOwn(key, '_toDataPoint')) { _key = (key as DataPointPickClass)._toDataPoint() } else if (key instanceof Score) { _key = DataVariable().set(key) } const index = Variable(this.size()) this.dataPoint.select('Keys').append(_key!) this.dataPoint.select('Values').append(value as NBTObject) MCFunction('./__data_index_map/set', [_key!], () => { const indexData = DataVariable().set(index) Macro.data.modify .storage(this.dataPoint.currentTarget, Macro`${this.dataPoint.path}.Index[${_key}]`) .set.from.storage(indexData.currentTarget, indexData.path) }, { creator: 'sandstone', onConflict: 'rename', })() this.dataPoint.select('Entries').append([]) const entry = this.dataPoint.select('Entries[-1]') entry.append(_key!) entry.append(value) return index } // If the map was declared with a DataPointClass, return that class type to retain child type safety, otherwise return a normal DataPointClass get( key: GetKeys, ): INITIAL[typeof key] extends DataPointClass ? INITIAL[typeof key] : DataPointClass<'storage'> get(key: Macroable): DataPointClass<'storage'> get(key: Macroable>, true>) { const { MCFunction, Macro, DataVariable } = this.pack if (typeof key === 'string') { if (this.entries[key]) { return this.dataPoint.select(`Entries[${this.entries[key]}]`) } const index = this.dataPoint.select('Index').select(key) const value = DataVariable() MCFunction('./__data_index_map/get', [index], () => { Macro.data.modify .storage(value.currentTarget, value.path) .set.from.storage(this.dataPoint.currentTarget, Macro`Entries[${index}]`) }, { creator: 'sandstone', onConflict: 'rename', })() return value } let _key: DataPointClass if (key instanceof DataPointClass) { _key = key } else if (Object.hasOwn(key, '_toDataPoint')) { _key = (key as DataPointPickClass)._toDataPoint() } else if (key instanceof Score) { _key = DataVariable().set(key) } const output = DataVariable() MCFunction('./__data_index_map/get', [_key!], () => { const index = DataVariable() Macro.data.modify .storage(index.currentTarget, index.path) .set.from.storage(this.dataPoint.currentTarget, Macro`${this.dataPoint.path}.Index[${_key}]`) MCFunction('./__data_index_map/get/inner', [index], () => { Macro.data.modify .storage(output.currentTarget, output.path) .set.from.storage(this.dataPoint.currentTarget, Macro`${this.dataPoint.path}.Entries[${index}]`) }, { creator: 'sandstone', onConflict: 'rename', })() }, { creator: 'sandstone', onConflict: 'rename', })() return output } remove(key: Macroable>, true>) { const { MCFunction, Macro, DataVariable } = this.pack if (typeof key === 'string') { const index = this.dataPoint.select('Index').select(key) MCFunction('./__data_index_map/remove', [index], () => { Macro.data.modify.storage(this.dataPoint.currentTarget, Macro`Entries[${index}]`).set.value(0) index.remove() }, { creator: 'sandstone', onConflict: 'rename', })() return this } let _key: DataPointClass | Score if (key instanceof DataPointClass || key instanceof Score) { _key = key } else { _key = (key as DataPointPickClass)._toDataPoint() } const index = DataVariable() MCFunction('./__data_index_map/remove', [_key], () => { Macro.data.modify .storage(index.currentTarget, index.path) .set.from.storage(this.dataPoint.currentTarget, Macro`${this.dataPoint.path}.Index[${_key}]`) Macro.data.remove.storage(this.dataPoint.currentTarget, Macro`${this.dataPoint.path}.Index[${_key}]`) MCFunction('./__data_index_map/remove/inner', [index], () => { Macro.data.modify .storage(this.dataPoint.currentTarget, Macro`${this.dataPoint.path}.Entries[${index}]`) .set.value(0) }, { creator: 'sandstone', onConflict: 'rename', })() }, { creator: 'sandstone', onConflict: 'rename', })() return this } _toDataPoint = () => this.dataPoint _toMinecraftCondition() { return this.dataPoint._toMinecraftCondition() } } export type DataArrayInitial = readonly NBTObject[] | readonly [string, DataPointClass | DataPointPickClass] export class DataArrayClass extends IterableDataClass<'list'> { dataPoint: NonNullable> constructor( private pack: SandstonePack, initialize: INITIAL, dataPoint?: DataPointClass<'storage'>, ) { super(pack.core, 'list') if (dataPoint) { this.dataPoint = dataPoint } else { this.dataPoint = this.pack.DataVariable([], 'Array') } // We currently are in a MCFunction => that's where the initialization should take place if (pack.core.currentMCFunction) { this.init(initialize) } else { // Else, we should run it in the init MCFunction pack.initMCFunction.push(() => this.init(initialize)) } } private init(initialize: INITIAL) { for (const entry of initialize) { this.dataPoint.append(entry) } } __entryIterator?: DataPointClass<'storage'> entryIterator(direction: 'normal' | 'reverse') { if (!this.__entryIterator) { this.__entryIterator = this.pack.DataVariable(this.dataPoint).select(`[${direction === 'normal' ? 0 : -1}]`) } return this.__entryIterator } iterator(callback: (data: [entry: DataPointClass]) => void, direction: 'normal' | 'reverse' = 'normal') { this.__entryIterator = undefined const entryIterator = this.entryIterator(direction) return () => { callback([entryIterator]) entryIterator.remove() } } continue(direction: 'normal' | 'reverse' = 'normal') { return this.entryIterator(direction)._toMinecraftCondition() } /** * Use in conjunction with a Score to get the count of entries. * * @example * Variable(array.size(), 'array_size') * * @returns The entries of the array. */ size = () => this.dataPoint last = () => this.dataPoint.select('[-1]') set(index: Score | number, value: NBTObject | DataPointClass | DataPointPickClass) { if (typeof index === 'number') { this.dataPoint.select(`[${index}]`).set(value as NBTObject) return this } const { Macro, MCFunction } = this.pack MCFunction('./__data_array/set', [index], () => { if (value instanceof StringDataPointClass) { const [start, end] = value.sliceBounds Macro.data.modify .storage(this.dataPoint.currentTarget, Macro`${this.dataPoint.path}[${index}]`) .set.string.storage(value.currentTarget, value.path, start, end) } else if (value instanceof DataPointClass) { Macro.data.modify .storage(this.dataPoint.currentTarget, Macro`${this.dataPoint.path}[${index}]`) .set.from.storage(value.currentTarget, value.path) } else if (typeof value === 'object' && Object.hasOwn(value, '_toDataPoint')) { const point = (value as DataPointPickClass)._toDataPoint() Macro.data.modify .storage(this.dataPoint.currentTarget, Macro`${this.dataPoint.path}[${index}]`) .set.from.storage(point.currentTarget, point.path) } else { Macro.data.modify .storage(this.dataPoint.currentTarget, Macro`${this.dataPoint.path}[${index}]`) .set.value(value) } }, { creator: 'sandstone', onConflict: 'rename', })() return this } push(value: NBTObject | DataPointClass | DataPointPickClass) { this.dataPoint.append(value as NBTObject) return this } unshift(value: NBTObject | DataPointClass | DataPointPickClass) { this.dataPoint.prepend(value as NBTObject) return this } get(index: Score | number) { if (typeof index === 'number') { return this.dataPoint.select(`[${index}]`) } const { Macro, MCFunction, DataVariable } = this.pack const output = DataVariable() MCFunction('./__data_array/get', [index], () => { Macro.data.modify .storage(output.currentTarget, output.path) .set.from.storage(this.dataPoint.currentTarget, Macro`${this.dataPoint.path}[${index}]`) }, { creator: 'sandstone', onConflict: 'rename', })() return output } remove(index: Score | number) { if (typeof index === 'number') { this.dataPoint.select(`[${index}]`).remove() return this } const { MCFunction, Macro } = this.pack MCFunction('./__data_array/remove', [index], () => { Macro.data.remove.storage(this.dataPoint.currentTarget, Macro`${this.dataPoint.path}[${index}]`) }, { creator: 'sandstone', onConflict: 'rename', })() return this } _toDataPoint = () => this.dataPoint _toMinecraftCondition() { return this.dataPoint._toMinecraftCondition() } } export function DataIndexMapInternal( pack: SandstonePack, initialize: INITIAL, dataPoint?: DataPointClass<'storage'>, ) { const indexMap = new DataIndexMapClass(pack, initialize, dataPoint) return new Proxy(indexMap, { get(target, p, receiver) { if (p in target) { return Reflect.get(target, p, receiver) } return target.get(p as any) }, set(target, p, value, receiver) { if (p in target) { return Reflect.set(target, p, value, receiver) } target.set(p as any, value) return true }, }) as DataIndexMapClass & { [K in keyof INITIAL]: DataIndexMapInitial[NonEmptyString] } & { [K in string]: DataIndexMapInitial[NonEmptyString] } } export type DataIndexMapType = DataIndexMapClass & { [K in keyof INITIAL]: DataIndexMapInitial[NonEmptyString] } & { [K in string]: DataIndexMapInitial[NonEmptyString] } export function DataArrayInternal( pack: SandstonePack, initialize: INITIAL, dataPoint?: DataPointClass<'storage'>, ) { const array = new DataArrayClass(pack, initialize, dataPoint) return new Proxy(array, { get(target, p, receiver) { if (typeof p === 'string' && p in target) { return Reflect.get(target, p, receiver) } return target.get(Number(p)) }, set(target, p, value, receiver) { if (typeof p === 'string' && p in target) { return Reflect.set(target, p, value, receiver) } target.set(Number(p), value) return true }, }) as DataArrayClass & { [K in keyof INITIAL]: DataArrayInitial[number] } & { [K in number]: DataArrayInitial[number] } } export type DataArrayType = DataArrayClass & { [K in keyof INITIAL]: DataArrayInitial[number] } & { [K in number]: DataArrayInitial[number] }