|
| 1 | +import { world } from '@minecraft/server'; |
| 2 | + |
| 3 | +export class ArrayDatabase<T = any> { |
| 4 | + public static readonly PROPERTY_MAX_SIZE = 12000; |
| 5 | + public static readonly PREFIX = 'array'; |
| 6 | + |
| 7 | + private readonly cache: T[][] = []; |
| 8 | + private cacheLoaded = false; |
| 9 | + private currentKeyIndex = 0; |
| 10 | + |
| 11 | + constructor(public readonly id: string) {} |
| 12 | + |
| 13 | + public getAll(): T[] { |
| 14 | + if (!this.cacheLoaded) this.load(); |
| 15 | + const result: T[] = []; |
| 16 | + for (const value of this.cache) result.push(...value); |
| 17 | + return result; |
| 18 | + } |
| 19 | + |
| 20 | + public add(value: T): this { |
| 21 | + if (!this.cacheLoaded) this.load(); |
| 22 | + const baseData = this.cache[this.currentKeyIndex] ?? []; |
| 23 | + baseData.push(value); |
| 24 | + this.trySave(baseData); |
| 25 | + return this; |
| 26 | + } |
| 27 | + |
| 28 | + public has(value: T) { |
| 29 | + const locations = this.getAll(); |
| 30 | + return locations.includes(value); |
| 31 | + } |
| 32 | + |
| 33 | + public clear() { |
| 34 | + const keys = world.getDynamicPropertyIds(); |
| 35 | + const prefix = this.keyPrefix; |
| 36 | + for (const key of keys) { |
| 37 | + if (key.startsWith(prefix)) world.setDynamicProperty(key); |
| 38 | + } |
| 39 | + this.currentKeyIndex = 0; |
| 40 | + world.setDynamicProperty(this.indexKey); |
| 41 | + this.cache.length = 0; |
| 42 | + } |
| 43 | + |
| 44 | + public *values(): IterableIterator<T> { |
| 45 | + if (!this.cacheLoaded) this.load(); |
| 46 | + for (const values of this.cache) { |
| 47 | + for (const value of values) { |
| 48 | + yield value; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public find(callbackfn: (value: T) => boolean): T | undefined { |
| 54 | + for (const value of this.values()) { |
| 55 | + if (callbackfn(value)) return value; |
| 56 | + } |
| 57 | + return undefined; |
| 58 | + } |
| 59 | + |
| 60 | + public filter(callbackfn: (value: T) => boolean): T[] { |
| 61 | + const result: T[] = []; |
| 62 | + for (const value of this.values()) { |
| 63 | + if (callbackfn(value)) result.push(value); |
| 64 | + } |
| 65 | + return result; |
| 66 | + } |
| 67 | + |
| 68 | + public map<U>(callbackfn: (value: T) => U): U[] { |
| 69 | + const result: U[] = []; |
| 70 | + for (const value of this.values()) { |
| 71 | + result.push(callbackfn(value)); |
| 72 | + } |
| 73 | + return result; |
| 74 | + } |
| 75 | + |
| 76 | + public forEach(callbackfn: (value: T) => void): void { |
| 77 | + for (const value of this.values()) { |
| 78 | + callbackfn(value); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public some(callbackfn: (value: T) => boolean): boolean { |
| 83 | + for (const value of this.values()) { |
| 84 | + if (callbackfn(value)) return true; |
| 85 | + } |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + public every(callbackfn: (value: T) => boolean): boolean { |
| 90 | + for (const value of this.values()) { |
| 91 | + if (!callbackfn(value)) return false; |
| 92 | + } |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + public [Symbol.iterator](): IterableIterator<T> { |
| 97 | + return this.values(); |
| 98 | + } |
| 99 | + |
| 100 | + private trySave(values: T[], swap: T[] = []) { |
| 101 | + let sizeOK = true; |
| 102 | + const stringified = JSON.stringify(values); |
| 103 | + if (stringified.length > ArrayDatabase.PROPERTY_MAX_SIZE) { |
| 104 | + sizeOK = false; |
| 105 | + } else { |
| 106 | + try { |
| 107 | + world.setDynamicProperty(this.currentKey, stringified); |
| 108 | + this.cache[this.currentKeyIndex] = values; |
| 109 | + } catch (e) { |
| 110 | + sizeOK = false; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (!sizeOK) { |
| 115 | + if (values.length > 0) swap.push(values.shift()!); |
| 116 | + this.trySave(values, swap); |
| 117 | + this.currentKeyIndex++; |
| 118 | + world.setDynamicProperty(this.indexKey, this.currentKeyIndex); |
| 119 | + this.trySave(swap); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public get size(): number { |
| 124 | + return this.cache.reduce((acc, arr) => acc + arr.length, 0); |
| 125 | + } |
| 126 | + |
| 127 | + private load() { |
| 128 | + this.currentKeyIndex = world.getDynamicProperty(this.indexKey) as number ?? 0; |
| 129 | + const keys = world.getDynamicPropertyIds(); |
| 130 | + const prefix = this.keyPrefix; |
| 131 | + for (const key of keys) { |
| 132 | + if (key.includes('_index')) console.warn(key) |
| 133 | + if (!key.startsWith(prefix)) continue; |
| 134 | + const index = Number(key.slice(prefix.length)); |
| 135 | + if (Number.isNaN(index)) { |
| 136 | + console.error(`Found invalid key: ${key}`); |
| 137 | + continue; |
| 138 | + } |
| 139 | + this.cache[index] = JSON.parse( |
| 140 | + world.getDynamicProperty(key) as string ?? '[]' |
| 141 | + ); |
| 142 | + } |
| 143 | + this.cacheLoaded = true; |
| 144 | + } |
| 145 | + |
| 146 | + private unload(): void { |
| 147 | + this.cache.length = 0; |
| 148 | + this.cacheLoaded = false; |
| 149 | + this.currentKeyIndex = 0; |
| 150 | + } |
| 151 | + |
| 152 | + private get keyPrefix(): string { |
| 153 | + return `${ArrayDatabase.PREFIX}:${this.id}` as string; |
| 154 | + } |
| 155 | + |
| 156 | + private get currentKey(): string { |
| 157 | + return `${this.keyPrefix}${this.currentKeyIndex}`; |
| 158 | + } |
| 159 | + |
| 160 | + private get indexKey(): string { |
| 161 | + return `${ArrayDatabase.PREFIX}:index_${this.id}` as string; |
| 162 | + } |
| 163 | + |
| 164 | + public get [Symbol.toStringTag](): string { |
| 165 | + return this.id; |
| 166 | + } |
| 167 | +} |
0 commit comments