Skip to content

Commit fda8df9

Browse files
authored
QuickDB (Fixed V2) (#384)
1 parent aa6ba8c commit fda8df9

File tree

1 file changed

+18
-41
lines changed

1 file changed

+18
-41
lines changed

scripts/quick-db/index.js

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Author: Nperma <https://github.com/nperma>
33
// Project: https://github.com/JaylyDev/ScriptAPI
44

5-
import { world, World } from '@minecraft/server';
5+
import { world,system, World } from '@minecraft/server';
66

77
const DATABASE_PREFIX = '\u0235\u0235';
88

@@ -27,38 +27,11 @@ class QuickDB {
2727

2828
for (const keyFull of this.getIds()) {
2929
const key = keyFull.replace(this.#identifier, '');
30-
const rawValue = GET.call(world, keyFull);
31-
this.__cache[key] = this.#parseValue(rawValue);
30+
let value;system.run(()=>{value=GET.call(world, keyFull);})
31+
this.__cache[key] = JSON.parse(value);
3232
}
3333
}
3434

35-
/**
36-
* Parses stored string values into their appropriate types.
37-
* @param {any} value
38-
* @returns {any}
39-
*/
40-
#parseValue(value) {
41-
if (typeof value === 'string') {
42-
if (value.startsWith('obj')) return JSON.parse(value.slice(3));
43-
if (value === 'null') return null;
44-
if (value === 'true' || value === 'false') return value === 'true';
45-
const num = Number(value);
46-
if (!isNaN(num)) return num;
47-
}
48-
return value;
49-
}
50-
51-
/**
52-
* Converts values into a storable format.
53-
* @param {any} value
54-
* @returns {string}
55-
*/
56-
#stringifyValue(value) {
57-
if (typeof value === 'object' && value !== null) return 'obj' + JSON.stringify(value);
58-
if (typeof value === 'boolean' || value === null) return String(value);
59-
return String(value);
60-
}
61-
6235
/** @returns {number} */
6336
get size() {
6437
return this.keys().length;
@@ -83,14 +56,12 @@ class QuickDB {
8356
* Stores a key-value pair.
8457
* @param {string} key
8558
* @param {any} value
86-
* @returns {boolean}
59+
* @returns {void}
8760
*/
8861
set(key, value) {
8962
if (typeof key !== 'string' || !key.trim()) throw new Error('Key must be a non-empty string');
90-
const finalValue = this.#stringifyValue(value);
91-
SET.call(world, this.#identifier + key, finalValue);
63+
system.run(()=>SET.call(world, this.#identifier + key, JSON.stringify(value)));
9264
this.__cache[key] = value;
93-
return true;
9465
}
9566

9667
/**
@@ -100,7 +71,7 @@ class QuickDB {
10071
*/
10172
delete(key) {
10273
if (!this.has(key)) return false;
103-
SET.call(world, this.#identifier + key, undefined);
74+
system.run(()=>SET.call(world, this.#identifier + key));
10475
delete this.__cache[key];
10576
return true;
10677
}
@@ -126,16 +97,21 @@ class QuickDB {
12697

12798
/** @returns {string[]} */
12899
static get ids() {
129-
return [...new Set(
130-
IDS.call(world)
100+
let keys;
101+
system.run(() =>{
102+
keys=IDS.call(world)
131103
.filter((id) => id.startsWith(DATABASE_PREFIX))
132-
.map((k) => k.slice(DATABASE_PREFIX.length).split(DATABASE_PREFIX)[0])
104+
.map((k) => k.slice(DATABASE_PREFIX.length).split(DATABASE_PREFIX)[0]);
105+
});
106+
return [...new Set(
107+
keys
133108
)];
134109
}
135110

136111
/** @returns {string[]} */
137112
getIds() {
138-
return IDS.call(world).filter((id) => id.startsWith(this.#identifier));
113+
let result;system.run(()=>{result=IDS.call(world).filter((id) => id.startsWith(this.#identifier));});
114+
return result;
139115
}
140116

141117
/** Clears the database. */
@@ -148,8 +124,9 @@ class QuickDB {
148124

149125
/** Clears all databases globally. */
150126
static clearAll() {
151-
for (const real_id of IDS.call(world).filter((id) => id.startsWith(DATABASE_PREFIX))) {
152-
SET.call(world, real_id, undefined);
127+
let keys;system.run(()=>{keys=IDS.call(world).filter((id) => id.startsWith(DATABASE_PREFIX))});
128+
for (const real_id of keys) {
129+
system.run(()=>SET.call(world, real_id));
153130
}
154131
}
155132
}

0 commit comments

Comments
 (0)