|
1 | | -import { world, Scoreboard, ScoreboardObjective, ScoreboardIdentity } from "@minecraft/server"; |
2 | | - |
| 1 | +import { world } from "@minecraft/server"; |
3 | 2 | const KEY_4NO = "⧉⧉"; |
4 | | - |
5 | | - |
6 | | - |
7 | | -interface DBAdapter<T> { |
8 | | - |
9 | | - set(key: string, value: T): void |
10 | | - |
11 | | - get(key: string): T | undefined |
12 | | - |
13 | | - has(key: string): boolean |
14 | | - |
15 | | - delete(key: string): boolean |
16 | | - |
17 | | - keys(): string[] |
18 | | - |
19 | | - values(): T[] |
20 | | - |
21 | | - entries(): [string, T][] |
22 | | -} |
23 | | - |
24 | | -export class ScoreboardDB<T = unknown> implements DBAdapter<T> { |
25 | | - #identifier: string; |
26 | | - readonly #keyacc: string = '⟡⟡'; |
27 | | - readonly #obj; |
28 | | - #participantNames: Record<string, string>; |
29 | | - #participants: ScoreboardIdentity[]; |
30 | | - #cache: Record<string, T> = {} |
31 | | - |
32 | | - constructor(name: string) { |
33 | | - this.#identifier = `${KEY_4NO |
34 | | - }${name}${KEY_4NO}` |
35 | | - this.#obj = world.scoreboard.getObjective(this.#identifier) ?? |
36 | | - world.scoreboard.addObjective(this.#identifier) |
37 | | - this.#participantNames = {} |
38 | | - this.#participants = (this.#obj as ScoreboardObjective).getParticipants() |
39 | | - let iteration = this.#participants.length; |
40 | | - while (iteration--) { |
41 | | - const participantName = this.#participants[iteration]?.displayName |
42 | | - const [key, rawValue] = participantName.split(this.#keyacc) |
43 | | - const value = rawValue.startsWith('num:') ? Number(rawValue.slice('num:'.length)) : rawValue.startsWith('bool:') ? Boolean(rawValue.slice('bool:'.length)) : JSON.parse(rawValue) |
44 | | - this.#participantNames[key] = participantName |
45 | | - this.#cache[key] = value |
46 | | - } |
47 | | - } |
48 | | - |
49 | | - set(key: string, value?: T) { |
50 | | - if (key in this.#cache && value == null) this.delete(key) |
51 | | - const valueFormatting = typeof value == 'number' ? `num:${value}` : typeof value == 'boolean' ? `bool:${value}` : value; |
52 | | - const participantName = `${key}${this.#keyacc}${JSON.stringify(valueFormatting)}`; |
53 | | - (this.#obj as ScoreboardObjective).setScore(participantName, 1) |
54 | | - this.#participantNames[key] = participantName; |
55 | | - this.#cache[key] = value |
56 | | - } |
57 | | - |
58 | | - get(key: string) { |
59 | | - return this.#cache[key] |
60 | | - } |
61 | | - |
62 | | - has(key: string) { |
63 | | - return !!this.#cache |
64 | | - } |
65 | | - |
66 | | - delete(key: string) { |
67 | | - if (!(key in this.#cache)) return false; |
68 | | - (this.#obj as ScoreboardObjective).removeParticipant(this.#participantNames[key]) |
69 | | - delete this.#participantNames[key] |
70 | | - delete this.#cache[key] |
71 | | - return true |
72 | | - } |
73 | | - |
74 | | - keys() { |
75 | | - return Object.keys(this.#cache) |
76 | | - } |
77 | | - |
78 | | - values() { |
79 | | - return Object.values(this.#cache) |
80 | | - } |
81 | | - |
82 | | - entries() { |
83 | | - return Object.entries(this.#cache) |
84 | | - } |
85 | | - |
| 3 | +export class ScoreboardDB { |
| 4 | + constructor(name) { |
| 5 | + this.keyacc = '⟡⟡'; |
| 6 | + this.cache = {}; |
| 7 | + this.identifier = `${KEY_4NO}${name}${KEY_4NO}`; |
| 8 | + this.obj = world.scoreboard.getObjective(this.identifier) ?? |
| 9 | + world.scoreboard.addObjective(this.identifier); |
| 10 | + this.participantNames = {}; |
| 11 | + this.participants = this.obj.getParticipants(); |
| 12 | + let iteration = this.participants.length; |
| 13 | + while (iteration--) { |
| 14 | + const participantName = this.participants[iteration]?.displayName; |
| 15 | + const [key, rawValue] = participantName.split(this.keyacc); |
| 16 | + const value = rawValue.startsWith('num:') ? Number(rawValue.slice('num:'.length)) : rawValue.startsWith('bool:') ? Boolean(rawValue.slice('bool:'.length)) : JSON.parse(rawValue); |
| 17 | + this.participantNames[key] = participantName; |
| 18 | + this.cache[key] = value; |
| 19 | + } |
| 20 | + } |
| 21 | + set(key, value) { |
| 22 | + if (key in this.cache && value == null) |
| 23 | + this.delete(key); |
| 24 | + const valueFormatting = typeof value == 'number' ? `num:${value}` : typeof value == 'boolean' ? `bool:${value}` : value; |
| 25 | + const participantName = `${key}${this.keyacc}${JSON.stringify(valueFormatting)}`; |
| 26 | + this.obj.setScore(participantName, 1); |
| 27 | + this.participantNames[key] = participantName; |
| 28 | + this.cache[key] = value; |
| 29 | + } |
| 30 | + get(key) { |
| 31 | + return this.cache[key]; |
| 32 | + } |
| 33 | + has(key) { |
| 34 | + return !!this.cache; |
| 35 | + } |
| 36 | + delete(key) { |
| 37 | + if (!(key in this.cache)) |
| 38 | + return false; |
| 39 | + this.obj.removeParticipant(this.participantNames[key]); |
| 40 | + delete this.participantNames[key]; |
| 41 | + delete this.cache[key]; |
| 42 | + return true; |
| 43 | + } |
| 44 | + keys() { |
| 45 | + return Object.keys(this.cache); |
| 46 | + } |
| 47 | + values() { |
| 48 | + return Object.values(this.cache); |
| 49 | + } |
| 50 | + entries() { |
| 51 | + return Object.entries(this.cache); |
| 52 | + } |
86 | 53 | } |
87 | | - |
88 | | -export class DynamicDB<T = unknown> implements DBAdapter<T> { |
89 | | - #identifier: string; |
90 | | - #cache: Record<string, T> = {}; |
91 | | - static size: number = world.getDynamicPropertyTotalByteCount() |
92 | | - |
93 | | - |
94 | | - constructor(name: string) { |
95 | | - this.#identifier = `${KEY_4NO |
96 | | - }${name}${KEY_4NO}` |
97 | | - |
98 | | - const ids = world.getDynamicPropertyIds() |
99 | | - let i = ids.length |
100 | | - |
101 | | - while (i--) { |
102 | | - const id = ids[i] |
103 | | - if (!id.startsWith(this.#identifier)) continue |
104 | | - |
105 | | - const raw = world.getDynamicProperty(id) |
106 | | - const key = id.slice(this.#identifier.length) |
107 | | - |
108 | | - const value = |
109 | | - typeof raw === "string" |
110 | | - ? JSON.parse(raw) |
111 | | - : (raw as T) |
112 | | - |
113 | | - this.#cache[key] = value |
114 | | - } |
115 | | - } |
116 | | - |
117 | | - set(key: string, value?: T): void { |
118 | | - const id = this.#identifier + key |
119 | | - if (key in this.#cache && value == null) { this.delete(key); return; } |
120 | | - |
121 | | - const data = |
122 | | - typeof value === "string" || |
123 | | - typeof value === "number" || |
124 | | - typeof value === "boolean" |
125 | | - ? value |
126 | | - : JSON.stringify(value) |
127 | | - |
128 | | - world.setDynamicProperty(id, data) |
129 | | - this.#cache[key] = value |
130 | | - } |
131 | | - |
132 | | - get(key: string): T | undefined { |
133 | | - return this.#cache[key] |
134 | | - } |
135 | | - |
136 | | - has(key: string): boolean { |
137 | | - return key in this.#cache |
138 | | - } |
139 | | - |
140 | | - delete(key: string): boolean { |
141 | | - if (!(key in this.#cache)) return false; |
142 | | - |
143 | | - world.setDynamicProperty(this.#identifier + key, undefined) |
144 | | - delete this.#cache[key] |
145 | | - return true |
146 | | - } |
147 | | - |
148 | | - keys(): string[] { |
149 | | - return Object.keys(this.#cache) |
150 | | - } |
151 | | - |
152 | | - values(): T[] { |
153 | | - return Object.values(this.#cache) |
154 | | - } |
155 | | - |
156 | | - entries(): [string, T][] { |
157 | | - return Object.entries(this.#cache) as [string, T][] |
158 | | - } |
| 54 | +export class DynamicDB { |
| 55 | + constructor(name) { |
| 56 | + this.cache = {}; |
| 57 | + this.identifier = `${KEY_4NO}${name}${KEY_4NO}`; |
| 58 | + const ids = world.getDynamicPropertyIds(); |
| 59 | + let i = ids.length; |
| 60 | + while (i--) { |
| 61 | + const id = ids[i]; |
| 62 | + if (!id.startsWith(this.identifier)) |
| 63 | + continue; |
| 64 | + const raw = world.getDynamicProperty(id); |
| 65 | + const key = id.slice(this.identifier.length); |
| 66 | + const value = typeof raw === "string" |
| 67 | + ? JSON.parse(raw) |
| 68 | + : raw; |
| 69 | + this.cache[key] = value; |
| 70 | + } |
| 71 | + } |
| 72 | + set(key, value) { |
| 73 | + const id = this.identifier + key; |
| 74 | + if (key in this.cache && value == null) { |
| 75 | + this.delete(key); |
| 76 | + return; |
| 77 | + } |
| 78 | + const data = typeof value === "string" || |
| 79 | + typeof value === "number" || |
| 80 | + typeof value === "boolean" |
| 81 | + ? value |
| 82 | + : JSON.stringify(value); |
| 83 | + world.setDynamicProperty(id, data); |
| 84 | + this.cache[key] = value; |
| 85 | + } |
| 86 | + get(key) { |
| 87 | + return this.cache[key]; |
| 88 | + } |
| 89 | + has(key) { |
| 90 | + return key in this.cache; |
| 91 | + } |
| 92 | + delete(key) { |
| 93 | + if (!(key in this.cache)) |
| 94 | + return false; |
| 95 | + world.setDynamicProperty(this.identifier + key, undefined); |
| 96 | + delete this.cache[key]; |
| 97 | + return true; |
| 98 | + } |
| 99 | + keys() { |
| 100 | + return Object.keys(this.cache); |
| 101 | + } |
| 102 | + values() { |
| 103 | + return Object.values(this.cache); |
| 104 | + } |
| 105 | + entries() { |
| 106 | + return Object.entries(this.cache); |
| 107 | + } |
159 | 108 | } |
160 | | - |
161 | | -export default class QuickDB<T = unknown> implements DBAdapter<T> { |
162 | | - #db; |
163 | | - constructor( |
164 | | - name: string, |
165 | | - storage: "local" | "dynamic" | "global" | "scoreboard" = "local" |
166 | | - ) { |
167 | | - this.#db = |
168 | | - storage === "scoreboard" || storage === "global" |
169 | | - ? new ScoreboardDB<T>(name) |
170 | | - : new DynamicDB<T>(name) |
171 | | - } |
172 | | - |
173 | | - set(key: string, value?: T): void { |
174 | | - this.#db.set(key, value) |
175 | | - } |
176 | | - |
177 | | - get(key: string): T | undefined { |
178 | | - return this.#db.get(key) |
179 | | - } |
180 | | - |
181 | | - has(key: string): boolean { |
182 | | - return this.#db.has(key) |
183 | | - } |
184 | | - |
185 | | - delete(key: string): boolean { |
186 | | - return this.#db.delete(key) |
187 | | - } |
188 | | - |
189 | | - keys(): string[] { |
190 | | - return this.#db.keys() |
191 | | - } |
192 | | - |
193 | | - values(): T[] { |
194 | | - return this.#db.values() |
195 | | - } |
196 | | - |
197 | | - entries(): [string, T][] { |
198 | | - return this.#db.entries() |
199 | | - } |
| 109 | +DynamicDB.size = world.getDynamicPropertyTotalByteCount(); |
| 110 | +export default class QuickDB { |
| 111 | + constructor(name, storage = "local") { |
| 112 | + this.db = |
| 113 | + storage === "scoreboard" || storage === "global" |
| 114 | + ? new ScoreboardDB(name) |
| 115 | + : new DynamicDB(name); |
| 116 | + } |
| 117 | + set(key, value) { |
| 118 | + this.db.set(key, value); |
| 119 | + } |
| 120 | + get(key) { |
| 121 | + return this.db.get(key); |
| 122 | + } |
| 123 | + has(key) { |
| 124 | + return this.db.has(key); |
| 125 | + } |
| 126 | + delete(key) { |
| 127 | + return this.db.delete(key); |
| 128 | + } |
| 129 | + keys() { |
| 130 | + return this.db.keys(); |
| 131 | + } |
| 132 | + values() { |
| 133 | + return this.db.values(); |
| 134 | + } |
| 135 | + entries() { |
| 136 | + return this.db.entries(); |
| 137 | + } |
200 | 138 | } |
0 commit comments