Skip to content

Commit cd4233e

Browse files
authored
Simple Database for ScriptAPI
1 parent 43baa56 commit cd4233e

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

scripts/quick-db/index.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import { world } from "@minecraft/server";
1+
import { world, World } from "@minecraft/server";
22

33
const DATABASE_PREFIX = "\u0235\u0235";
44

5+
const {
6+
getDynamicProperty: GET,
7+
setDynamicProperty: SET,
8+
getDynamicPropertyIds: IDS
9+
} = World.prototype;
10+
511
//adapt code to JalyDev/scriptAPI
612
class QuickDB {
713
#identifier;
@@ -10,33 +16,29 @@ class QuickDB {
1016
}
1117

1218
get size() {
13-
return world
14-
.getDynamicPropertyIds()
15-
.filter((id) => id.startsWith(this.#identifier)).length;
19+
return IDS.call(world).filter((id) => id.startsWith(this.#identifier))
20+
.length;
1621
}
1722

1823
has(key) {
19-
const dynamicKey = `${this.#identifier}${key}`;
20-
return !!world.getDynamicProperty(dynamicKey);
24+
return !!GET.call(world, `${this.#identifier}${key}`);
2125
}
2226

2327
get(key) {
24-
const dynamicKey = `${this.#identifier}${key}`;
25-
const value = world.getDynamicProperty(dynamicKey);
26-
return this.has(key) ? JSON.parse(value) : undefined;
28+
return this.has(key)
29+
? JSON.parse(GET.call(world, `${this.#identifier}${key}`))
30+
: undefined;
2731
}
2832

2933
set(key, value) {
3034
if (typeof key !== "string") return false;
31-
const dynamicKey = `${this.#identifier}${key}`;
32-
world.setDynamicProperty(dynamicKey, JSON.stringify(value));
35+
SET.call(world, `${this.#identifier}${key}`, JSON.stringify(value));
3336
return true;
3437
}
3538

3639
delete(key) {
37-
const dynamicKey = `${this.#identifier}${key}`;
3840
if (!this.has(key)) return false;
39-
world.setDynamicProperty(dynamicKey, undefined);
41+
SET.call(world, `${this.#identifier}${key}`, undefined);
4042
return true;
4143
}
4244

@@ -53,7 +55,7 @@ class QuickDB {
5355
}
5456

5557
#UIDX(type) {
56-
const ids = world.getDynamicPropertyIds();
58+
const ids = IDS.call(world);
5759
const result = [];
5860

5961
for (const id of ids) {
@@ -63,12 +65,10 @@ class QuickDB {
6365
if (type === "keys") {
6466
result.push(key);
6567
} else if (type === "values") {
66-
const val = world.getDynamicProperty(id);
67-
const value = JSON.parse(val);
68+
const value = JSON.parse(this.get(key));
6869
result.push(value);
6970
} else if (type === "entries") {
70-
const val = world.getDynamicProperty(id);
71-
const value = JSON.parse(val);
71+
const value = JSON.parse(this.get(key));
7272
result.push([key, value]);
7373
}
7474
}

0 commit comments

Comments
 (0)