forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
158 lines (137 loc) · 3.47 KB
/
index.js
File metadata and controls
158 lines (137 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Script example for ScriptAPI
// Author: Nperma <https://github.com/nperma>
// Project: https://github.com/JaylyDev/ScriptAPI
import { world, World } from '@minecraft/server';
const DATABASE_PREFIX = '\u0235\u0235';
const {
getDynamicProperty: GET,
setDynamicProperty: SET,
getDynamicPropertyIds: IDS
} = World.prototype;
class QuickDB {
#identifier;
__cache = {};
/**
* @param {string} id - Unique database identifier.
*/
constructor(id) {
if (typeof id !== 'string' || !id.trim()) {
throw new Error('Invalid database ID');
}
this.#identifier = `${DATABASE_PREFIX}${id}${DATABASE_PREFIX}`;
for (const keyFull of this.getIds()) {
const key = keyFull.replace(this.#identifier, '');
const rawValue = GET.call(world, keyFull);
this.__cache[key] = this.#parseValue(rawValue);
}
}
/**
* Parses stored string values into their appropriate types.
* @param {any} value
* @returns {any}
*/
#parseValue(value) {
if (typeof value === 'string') {
if (value.startsWith('obj')) return JSON.parse(value.slice(3));
if (value === 'null') return null;
if (value === 'true' || value === 'false') return value === 'true';
const num = Number(value);
if (!isNaN(num)) return num;
}
return value;
}
/**
* Converts values into a storable format.
* @param {any} value
* @returns {string}
*/
#stringifyValue(value) {
if (typeof value === 'object' && value !== null) return 'obj' + JSON.stringify(value);
if (typeof value === 'boolean' || value === null) return String(value);
return String(value);
}
/** @returns {number} */
get size() {
return this.keys().length;
}
/** @returns {string[]} */
keys() {
return Object.keys(this.__cache);
}
/** @returns {any[]} */
values() {
return Object.values(this.__cache);
}
/** @returns {[string, any][]} */
entries() {
return Object.entries(this.__cache);
}
/**
* Stores a key-value pair.
* @param {string} key
* @param {any} value
* @returns {boolean}
*/
set(key, value) {
if (typeof key !== 'string' || !key.trim()) throw new Error('Key must be a non-empty string');
const finalValue = this.#stringifyValue(value);
SET.call(world, this.#identifier + key, finalValue);
this.__cache[key] = value;
return true;
}
/**
* Deletes a key.
* @param {string} key
* @returns {boolean}
*/
delete(key) {
if (!this.has(key)) return false;
SET.call(world, this.#identifier + key, undefined);
delete this.__cache[key];
return true;
}
/**
* Retrieves a value.
* @param {string} key
* @returns {any}
*/
get(key) {
if (typeof key !== 'string' || !key.trim()) throw new Error('Key must be a non-empty string');
return this.__cache[key];
}
/**
* Checks if a key exists.
* @param {string} key
* @returns {boolean}
*/
has(key) {
return key in this.__cache;
}
/** @returns {string[]} */
static get ids() {
return [...new Set(
IDS.call(world)
.filter((id) => id.startsWith(DATABASE_PREFIX))
.map((k) => k.slice(DATABASE_PREFIX.length).split(DATABASE_PREFIX)[0])
)];
}
/** @returns {string[]} */
getIds() {
return IDS.call(world).filter((id) => id.startsWith(this.#identifier));
}
/** Clears the database. */
clear() {
for (const key of this.keys()) {
this.delete(key);
}
this.__cache = {};
}
/** Clears all databases globally. */
static clearAll() {
for (const real_id of IDS.call(world).filter((id) => id.startsWith(DATABASE_PREFIX))) {
SET.call(world, real_id, undefined);
}
}
}
export default QuickDB;
export { QuickDB };