|
1 | 1 | // Script example for ScriptAPI |
2 | 2 | // Author: Nperma <https://github.com/nperma> |
3 | 3 | // Project: https://github.com/JaylyDev/ScriptAPI |
4 | | -import { world, World } from "@minecraft/server"; |
| 4 | +import { world, World } from '@minecraft/server'; |
5 | 5 |
|
6 | | -const DATABASE_PREFIX = "\u0235\u0235"; |
| 6 | +const DATABASE_PREFIX = '\u0235\u0235'; |
7 | 7 |
|
8 | 8 | const { |
9 | | - getDynamicProperty: GET, |
10 | | - setDynamicProperty: SET, |
11 | | - getDynamicPropertyIds: IDS |
| 9 | + getDynamicProperty: GET, |
| 10 | + setDynamicProperty: SET, |
| 11 | + getDynamicPropertyIds: IDS |
12 | 12 | } = World.prototype; |
13 | 13 |
|
14 | 14 | class QuickDB { |
15 | | - #identifier; |
16 | | - constructor(id) { |
17 | | - this.#identifier = `${DATABASE_PREFIX}${id}${DATABASE_PREFIX}`; |
18 | | - |
19 | | - const IDDS = this.getIds(); |
20 | | - let length_ = IDDS.length; |
21 | | - this.__cache = {}; |
22 | | - while (length_--) { |
23 | | - const key = IDDS[length_].replace(this.#identifier, ""); |
24 | | - let value = GET.call(world, this.#identifier + key); |
25 | | - if (typeof value == "string" && value.startsWith("obj")) |
26 | | - this.__cache[key] = JSON.parse(value.slice(3)); |
27 | | - this.__cache[key] = value; |
28 | | - } |
29 | | - } |
30 | | - |
31 | | - get size() { |
32 | | - return this.keys().length; |
33 | | - } |
34 | | - |
35 | | - keys() { |
36 | | - return Object.keys(this.__cache); |
37 | | - } |
38 | | - |
39 | | - values() { |
40 | | - return Object.values(this.__cache); |
41 | | - } |
42 | | - |
43 | | - entries() { |
44 | | - return Object.entries(this.__cache); |
45 | | - } |
46 | | - |
47 | | - set(key, value) { |
48 | | - if (!key) throw new Error("pls type the key!!"); |
49 | | - if (value.length > 0 || value !== undefined || value !== undefined) |
50 | | - SET.call(world, this.#identifier + String(key), JSON.stringify(value)); |
51 | | - else SET.call(world, this.#identifier + String(key)); |
52 | | - return true; |
53 | | - } |
54 | | - |
55 | | - delete(key) { |
56 | | - if (!this.has(key)) return false; |
57 | | - SET.call(world, `${this.#identifier}${String(key)}`); |
58 | | - return true; |
59 | | - } |
60 | | - |
61 | | - get(key) { |
62 | | - if (!key) throw new Error("pls type the key!!"); |
63 | | - return this.__cache?.[key]; |
64 | | - } |
65 | | - |
66 | | - has(key) { |
67 | | - return !!this.get(key); |
68 | | - } |
69 | | - |
70 | | - static get ids() { |
71 | | - return [ |
72 | | - ...new Set( |
73 | | - IDS.call(world) |
74 | | - .filter((id) => id.startsWith(DATABASE_PREFIX)) |
75 | | - .map((k) => k.slice(DATABASE_PREFIX.length).split(DATABASE_PREFIX)[0]) |
76 | | - ) |
77 | | - ]; |
78 | | - } |
79 | | - getIds() { |
80 | | - return IDS.call(world).filter((id) => id.startsWith(this.#identifier)); |
81 | | - } |
82 | | - |
83 | | - clear() { |
84 | | - let length_ = this.size; |
85 | | - while (length_--) this.delete(this.keys()[length_]); |
86 | | - this.__cache = {}; |
87 | | - } |
88 | | - |
89 | | - static clearAll() { |
90 | | - for (const real_id of IDS.call(world).filter((id) => |
91 | | - id.startsWith(DATABASE_PREFIX) |
92 | | - )) |
93 | | - SET.call(world, real_id); |
94 | | - } |
| 15 | + #identifier; |
| 16 | + constructor(id) { |
| 17 | + this.#identifier = `${DATABASE_PREFIX}${id}${DATABASE_PREFIX}`; |
| 18 | + |
| 19 | + const IDDS = this.getIds(); |
| 20 | + this.__cache = {}; |
| 21 | + |
| 22 | + for (const keyFull of IDDS) { |
| 23 | + const key = keyFull.replace(this.#identifier, ''); |
| 24 | + const rawValue = GET.call(world, keyFull); |
| 25 | + |
| 26 | + this.__cache[key] = this.#parseValue(rawValue); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + #parseValue(value) { |
| 31 | + if (typeof value === 'string') { |
| 32 | + if (value.startsWith('obj')) return JSON.parse(value.slice(3)); |
| 33 | + if (value === 'null') return null; |
| 34 | + if (value === 'true') return true; |
| 35 | + if (value === 'false') return false; |
| 36 | + if (!isNaN(value)) return Number(value); |
| 37 | + } |
| 38 | + return value; |
| 39 | + } |
| 40 | + |
| 41 | + #stringifyValue(value) { |
| 42 | + if (typeof value === 'object' && value !== null) return 'obj' + JSON.stringify(value); |
| 43 | + if (typeof value === 'boolean' || value === null) return String(value); |
| 44 | + return value; |
| 45 | + } |
| 46 | + |
| 47 | + get size() { |
| 48 | + return this.keys().length; |
| 49 | + } |
| 50 | + |
| 51 | + keys() { |
| 52 | + return Object.keys(this.__cache); |
| 53 | + } |
| 54 | + |
| 55 | + values() { |
| 56 | + return Object.values(this.__cache); |
| 57 | + } |
| 58 | + |
| 59 | + entries() { |
| 60 | + return Object.entries(this.__cache); |
| 61 | + } |
| 62 | + |
| 63 | + set(key, value) { |
| 64 | + if (!key) throw new Error('pls type the key!!'); |
| 65 | + const finalValue = this.#stringifyValue(value); |
| 66 | + SET.call(world, this.#identifier + String(key), finalValue); |
| 67 | + this.__cache[key] = value; |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + delete(key) { |
| 72 | + if (!this.has(key)) return false; |
| 73 | + SET.call(world, this.#identifier + String(key)); |
| 74 | + delete this.__cache[key]; |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + get(key) { |
| 79 | + if (!key) throw new Error('pls type the key!!'); |
| 80 | + return this.__cache?.[key]; |
| 81 | + } |
| 82 | + |
| 83 | + has(key) { |
| 84 | + return key in this.__cache; |
| 85 | + } |
| 86 | + |
| 87 | + static get ids() { |
| 88 | + return [ |
| 89 | + ...new Set( |
| 90 | + IDS.call(world) |
| 91 | + .filter((id) => id.startsWith(DATABASE_PREFIX)) |
| 92 | + .map((k) => k.slice(DATABASE_PREFIX.length).split(DATABASE_PREFIX)[0]) |
| 93 | + ) |
| 94 | + ]; |
| 95 | + } |
| 96 | + |
| 97 | + getIds() { |
| 98 | + return IDS.call(world).filter((id) => id.startsWith(this.#identifier)); |
| 99 | + } |
| 100 | + |
| 101 | + clear() { |
| 102 | + for (const key of this.keys()) { |
| 103 | + this.delete(key); |
| 104 | + } |
| 105 | + this.__cache = {}; |
| 106 | + } |
| 107 | + |
| 108 | + static clearAll() { |
| 109 | + for (const real_id of IDS.call(world).filter((id) => id.startsWith(DATABASE_PREFIX))) { |
| 110 | + SET.call(world, real_id); |
| 111 | + } |
| 112 | + } |
95 | 113 | } |
96 | 114 |
|
97 | 115 | export default QuickDB; |
|
0 commit comments