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
135 lines (116 loc) · 2.87 KB
/
index.js
File metadata and controls
135 lines (116 loc) · 2.87 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
// Script example for ScriptAPI
// Author: Nperma <https://github.com/nperma>
// Project: https://github.com/JaylyDev/ScriptAPI
import { world,system, 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, '');
let value;system.run(()=>{value=GET.call(world, keyFull);})
this.__cache[key] = JSON.parse(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 {void}
*/
set(key, value) {
if (typeof key !== 'string' || !key.trim()) throw new Error('Key must be a non-empty string');
system.run(()=>SET.call(world, this.#identifier + key, JSON.stringify(value)));
this.__cache[key] = value;
}
/**
* Deletes a key.
* @param {string} key
* @returns {boolean}
*/
delete(key) {
if (!this.has(key)) return false;
system.run(()=>SET.call(world, this.#identifier + key));
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() {
let keys;
system.run(() =>{
keys=IDS.call(world)
.filter((id) => id.startsWith(DATABASE_PREFIX))
.map((k) => k.slice(DATABASE_PREFIX.length).split(DATABASE_PREFIX)[0]);
});
return [...new Set(
keys
)];
}
/** @returns {string[]} */
getIds() {
let result;system.run(()=>{result=IDS.call(world).filter((id) => id.startsWith(this.#identifier));});
return result;
}
/** Clears the database. */
clear() {
for (const key of this.keys()) {
this.delete(key);
}
this.__cache = {};
}
/** Clears all databases globally. */
static clearAll() {
let keys;system.run(()=>{keys=IDS.call(world).filter((id) => id.startsWith(DATABASE_PREFIX))});
for (const real_id of keys) {
system.run(()=>SET.call(world, real_id));
}
}
}
export default QuickDB;
export { QuickDB };