Skip to content

Commit a5dae6f

Browse files
committed
JaylyDB 1.1.2
Resolves #289
1 parent bc72b15 commit a5dae6f

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

scripts/jaylydb/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ declare class JaylyDB implements Map<string, string | number | boolean> {
2929
* @returns Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.
3030
*/
3131
get(key: string): string | number | boolean | undefined;
32+
/**
33+
* @returns boolean indicating whether an element with the specified key exists or not in jaylydb.
34+
*/
3235
has(key: string): boolean;
3336
/**
3437
* Adds a new element with a specified key and value to the database. If an element with the same key already exists, the element will be updated.
3538
*/
3639
set(key: string, value: string | number | boolean): this;
40+
/**
41+
* Returns an iterable of key, value pairs for every entry in the database.
42+
*/
3743
entries(): IterableIterator<[string, string | number | boolean]>;
3844
/**
3945
* Returns an iterable of keys in the database

scripts/jaylydb/index.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var _a;
2424
* IN THE SOFTWARE.
2525
*/
2626
import { ScoreboardIdentityType, system, world } from "@minecraft/server";
27-
const version = "1.1.1";
27+
const version = "1.1.2";
2828
const str = () => ('00000000000000000' + (Math.random() * 0xffffffffffffffff).toString(16)).slice(-16);
2929
/**
3030
* A rough mechanism for create a random uuid. Not as secure as uuid without as much of a guarantee of uniqueness,
@@ -61,16 +61,27 @@ const CreateCrashReport = (action, data, error, salt) => {
6161
* @beta
6262
*/
6363
const DisplayName = {
64-
parse(text, salt) {
64+
parse(text, objective, salt) {
6565
try {
6666
const a = salt ? decrypt(text, salt) : text;
6767
return JSON.parse(`{${a}}`);
6868
}
6969
catch (error) {
7070
if (!(error instanceof Error))
7171
throw error;
72-
CreateCrashReport("load", text, error, salt);
73-
throw new Error(`Failed to load data. Please check content log file for more info.\n`);
72+
// fallback to 1.0
73+
try {
74+
const a = JSON.parse(`"${salt ? decrypt(text, salt) : text}"`);
75+
const b = JSON.parse(`{${a}}`);
76+
// upgrade format
77+
objective.removeParticipant(text);
78+
objective.setScore(DisplayName.stringify(b, salt), 0);
79+
return b;
80+
}
81+
catch {
82+
CreateCrashReport("load", text, error, salt);
83+
throw new Error(`Failed to load data. Please check content log file for more info.\n`);
84+
}
7485
}
7586
},
7687
stringify(value, salt) {
@@ -100,7 +111,7 @@ class JaylyDB {
100111
for (const participant of this.objective.getParticipants()) {
101112
if (participant.type !== ScoreboardIdentityType.FakePlayer)
102113
continue;
103-
const data = DisplayName.parse(participant.displayName, this.salt);
114+
const data = DisplayName.parse(participant.displayName, this.objective, this.salt);
104115
const key = Object.keys(data)[0];
105116
const value = data[key];
106117
this.localState.set(key, {
@@ -186,6 +197,9 @@ class JaylyDB {
186197
this.updateParticipants();
187198
return this.localState.get(key)?.decoded_value;
188199
}
200+
/**
201+
* @returns boolean indicating whether an element with the specified key exists or not in jaylydb.
202+
*/
189203
has(key) {
190204
return this.localState.has(key);
191205
}
@@ -210,6 +224,9 @@ class JaylyDB {
210224
this.localState.set(key, data);
211225
return this;
212226
}
227+
/**
228+
* Returns an iterable of key, value pairs for every entry in the database.
229+
*/
213230
*entries() {
214231
for (const [key, data] of this.localState.entries())
215232
yield [key, data.decoded_value];

scripts/jaylydb/index.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
import { ScoreboardIdentity, ScoreboardIdentityType, ScoreboardObjective, system, world } from "@minecraft/server";
2828

29-
const version = "1.1.1";
29+
const version = "1.1.2";
3030
const str = () => ('00000000000000000' + (Math.random() * 0xffffffffffffffff).toString(16)).slice(-16);
3131
/**
3232
* A rough mechanism for create a random uuid. Not as secure as uuid without as much of a guarantee of uniqueness,
@@ -74,14 +74,24 @@ const CreateCrashReport = (action: "save" | "load", data: string, error: Error,
7474
* @beta
7575
*/
7676
const DisplayName = {
77-
parse(text: string, salt?: string): Record<string, string | number | boolean> {
77+
parse(text: string, objective: ScoreboardObjective, salt?: string): Record<string, string | number | boolean> {
7878
try {
7979
const a = salt ? decrypt(text, salt) : text;
8080
return JSON.parse(`{${a}}`);
8181
} catch (error) {
8282
if (!(error instanceof Error)) throw error;
83-
CreateCrashReport("load", text, error, salt);
84-
throw new Error(`Failed to load data. Please check content log file for more info.\n`);
83+
// fallback to 1.0
84+
try {
85+
const a = JSON.parse(`"${salt ? decrypt(text, salt) : text}"`);
86+
const b = JSON.parse(`{${a}}`);
87+
// upgrade format
88+
objective.removeParticipant(text);
89+
objective.setScore(DisplayName.stringify(b, salt), 0);
90+
return b;
91+
} catch {
92+
CreateCrashReport("load", text, error, salt);
93+
throw new Error(`Failed to load data. Please check content log file for more info.\n`);
94+
}
8595
}
8696
},
8797
stringify(value: Record<string, string | number | boolean>, salt?: string): string {
@@ -124,7 +134,7 @@ class JaylyDB implements Map<string, string | number | boolean> {
124134
this.localState.clear();
125135
for (const participant of this.objective.getParticipants()) {
126136
if (participant.type !== ScoreboardIdentityType.FakePlayer) continue;
127-
const data = DisplayName.parse(participant.displayName, this.salt);
137+
const data = DisplayName.parse(participant.displayName, this.objective, this.salt);
128138
const key = Object.keys(data)[0];
129139
const value = data[key];
130140
this.localState.set(key, {
@@ -204,6 +214,9 @@ class JaylyDB implements Map<string, string | number | boolean> {
204214
if (!this.localState.has(key)) this.updateParticipants();
205215
return this.localState.get(key)?.decoded_value
206216
}
217+
/**
218+
* @returns boolean indicating whether an element with the specified key exists or not in jaylydb.
219+
*/
207220
has(key: string): boolean {
208221
return this.localState.has(key);
209222
}
@@ -228,6 +241,9 @@ class JaylyDB implements Map<string, string | number | boolean> {
228241

229242
return this;
230243
}
244+
/**
245+
* Returns an iterable of key, value pairs for every entry in the database.
246+
*/
231247
*entries(): IterableIterator<[string, string | number | boolean]> {
232248
for (const [key, data] of this.localState.entries()) yield [key, data.decoded_value];
233249
}

scripts/jaylydb/tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
"target":"es6",
44
"moduleResolution":"node",
55
"module":"es2020",
6-
"declaration":false,
6+
"declaration":true,
77
"noLib":false,
88
"emitDecoratorMetadata":true,
99
"experimentalDecorators":true,
10-
"sourceMap":true,
10+
"sourceMap":false,
1111
"pretty":true,
1212
"forceConsistentCasingInFileNames": true,
1313
"strict": true,
@@ -18,6 +18,7 @@
1818
"noImplicitUseStrict":false,
1919
"rootDir": ".",
2020
"listFiles":false,
21+
"stripInternal": true,
2122
"noEmitHelpers":true
2223
},
2324
"include":[

0 commit comments

Comments
 (0)