2
2
// Author: Nperma <https://github.com/nperma>
3
3
// Project: https://github.com/JaylyDev/ScriptAPI
4
4
5
- import { world , World } from '@minecraft/server' ;
5
+ import { world , system , World } from '@minecraft/server' ;
6
6
7
7
const DATABASE_PREFIX = '\u0235\u0235' ;
8
8
@@ -27,38 +27,11 @@ class QuickDB {
27
27
28
28
for ( const keyFull of this . getIds ( ) ) {
29
29
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 ) ;
32
32
}
33
33
}
34
34
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
-
62
35
/** @returns {number } */
63
36
get size ( ) {
64
37
return this . keys ( ) . length ;
@@ -83,14 +56,12 @@ class QuickDB {
83
56
* Stores a key-value pair.
84
57
* @param {string } key
85
58
* @param {any } value
86
- * @returns {boolean }
59
+ * @returns {void }
87
60
*/
88
61
set ( key , value ) {
89
62
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 ) ) ) ;
92
64
this . __cache [ key ] = value ;
93
- return true ;
94
65
}
95
66
96
67
/**
@@ -100,7 +71,7 @@ class QuickDB {
100
71
*/
101
72
delete ( key ) {
102
73
if ( ! this . has ( key ) ) return false ;
103
- SET . call ( world , this . #identifier + key , undefined ) ;
74
+ system . run ( ( ) => SET . call ( world , this . #identifier + key ) ) ;
104
75
delete this . __cache [ key ] ;
105
76
return true ;
106
77
}
@@ -126,16 +97,21 @@ class QuickDB {
126
97
127
98
/** @returns {string[] } */
128
99
static get ids ( ) {
129
- return [ ...new Set (
130
- IDS . call ( world )
100
+ let keys ;
101
+ system . run ( ( ) => {
102
+ keys = IDS . call ( world )
131
103
. 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
133
108
) ] ;
134
109
}
135
110
136
111
/** @returns {string[] } */
137
112
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 ;
139
115
}
140
116
141
117
/** Clears the database. */
@@ -148,8 +124,9 @@ class QuickDB {
148
124
149
125
/** Clears all databases globally. */
150
126
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 ) ) ;
153
130
}
154
131
}
155
132
}
0 commit comments