Skip to content

Commit fc5f516

Browse files
committed
Update jaylydb and fix link
Fix JaylyDev#250 Fix JaylyDev#249
1 parent 8e5d3f8 commit fc5f516

3 files changed

Lines changed: 56 additions & 13 deletions

File tree

scripts/editor-extensions/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Description
44

5-
Extensions for Minecraft: Bedrock Editor using editor API. Mirror of the [DarkGamerYT/Bedrock-Editor-Extension](https://github.com/DarkGamerYT/Bedrock-Editor-Extension), here is used for testing against [Editor Typings](https://github.com/JaylyDev/ScriptAPI/tree/main/lib).
5+
Extensions for Minecraft: Bedrock Editor using editor API. Mirror of the [DarkGamerYT/Bedrock-Editor-Extension](https://github.com/DarkGamerYT/Bedrock-Editor-Extension), here is used for testing against [Editor Typings](https://www.npmjs.com/package/@jayly/minecraft-editor).
66

77
## Credits
88

scripts/jaylydb/index.js

Lines changed: 29 additions & 6 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.0.7";
27+
const version = "1.0.8";
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,
@@ -87,7 +87,7 @@ const overworld = world.getDimension("overworld");
8787
*/
8888
class JaylyDB {
8989
/** @internal */
90-
updateParticipants() {
90+
updateParticipants(fetchCache = false) {
9191
const id = this.objective.id.substring(this.objective.id.indexOf(":") + 1);
9292
if (this.tempCache.size <= 0 && this.warningSent === true) {
9393
console.warn(`[JaylyDB] Database '${id}' has written data to world. It is now safe to exit the world.`);
@@ -111,10 +111,22 @@ class JaylyDB {
111111
}
112112
;
113113
this.participants.clear();
114+
if (fetchCache)
115+
this.localCache.clear();
114116
for (const participant of this.objective.getParticipants()) {
115117
if (participant.type !== ScoreboardIdentityType.fakePlayer)
116118
continue;
117-
this.participants.set(Object.keys(DisplayName.parse(participant.displayName, this.salt))[0], participant);
119+
const data = DisplayName.parse(participant.displayName, this.salt);
120+
const key = Object.keys(data)[0];
121+
const value = data[key];
122+
this.participants.set(key, participant);
123+
if (fetchCache)
124+
this.localCache.set(key, value);
125+
}
126+
;
127+
if (this.SYNC_OK === false) {
128+
console.warn(`[JaylyDB] Database '${id}' is now sync with disk.`);
129+
this.SYNC_OK = true;
118130
}
119131
;
120132
}
@@ -129,6 +141,8 @@ class JaylyDB {
129141
this.localCache = new Map();
130142
/** @internal */
131143
this.warningSent = false;
144+
/** @internal */
145+
this.SYNC_OK = true;
132146
/**
133147
* Internal cache object to allow data to write from memory to scoreboard every tick interval.
134148
* This is done to prevent multiple values written to a same key to scoreboard each operation.
@@ -140,9 +154,18 @@ class JaylyDB {
140154
this.encrypted = encrypted;
141155
this.salt = this.encrypted ? this.objective.displayName : undefined;
142156
// Fetch all data when database initialize
143-
this.updateParticipants();
144-
// This function queues the data to be written to the scoreboard every second interval.
145-
system.runInterval(() => this.updateParticipants());
157+
this.updateParticipants(true);
158+
system.runInterval(() => {
159+
if (!!world.scoreboard.getObjective("jaylydb:" + id))
160+
return this.updateParticipants();
161+
else if (this.SYNC_OK === true)
162+
console.error(`[JaylyDB] There is a sync issue with database '${id}'.`);
163+
this.localCache.forEach((value, key) => {
164+
const encoded = DisplayName.stringify({ [key]: value }, this.salt);
165+
this.tempCache.set(key, encoded);
166+
});
167+
this.SYNC_OK = false;
168+
});
146169
}
147170
/**
148171
* @returns the number of elements in the database.

scripts/jaylydb/index.ts

Lines changed: 26 additions & 6 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.0.7";
29+
const version = "1.0.8";
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,
@@ -111,14 +111,16 @@ class JaylyDB implements Map<string, string | number | boolean> {
111111
private readonly salt: string | undefined;
112112
/** @internal */
113113
private warningSent = false;
114+
/** @internal */
115+
private SYNC_OK: boolean = true;
114116
/**
115117
* Internal cache object to allow data to write from memory to scoreboard every tick interval.
116118
* This is done to prevent multiple values written to a same key to scoreboard each operation.
117119
* @internal
118120
*/
119121
private readonly tempCache = new Map<string, string | number | boolean>();
120122
/** @internal */
121-
private updateParticipants() {
123+
private updateParticipants(fetchCache: boolean = false) {
122124
const id = this.objective.id.substring(this.objective.id.indexOf(":") + 1);
123125
if (this.tempCache.size <= 0 && this.warningSent === true) {
124126
console.warn(`[JaylyDB] Database '${id}' has written data to world. It is now safe to exit the world.`);
@@ -139,10 +141,19 @@ class JaylyDB implements Map<string, string | number | boolean> {
139141
};
140142

141143
this.participants.clear();
144+
if (fetchCache) this.localCache.clear();
142145

143146
for (const participant of this.objective.getParticipants()) {
144147
if (participant.type !== ScoreboardIdentityType.fakePlayer) continue;
145-
this.participants.set(Object.keys(DisplayName.parse(participant.displayName, this.salt))[0], participant);
148+
const data = DisplayName.parse(participant.displayName, this.salt);
149+
const key = Object.keys(data)[0];
150+
const value = data[key];
151+
this.participants.set(key, participant);
152+
if (fetchCache) this.localCache.set(key, value);
153+
};
154+
if (this.SYNC_OK === false) {
155+
console.warn(`[JaylyDB] Database '${id}' is now sync with disk.`);
156+
this.SYNC_OK = true;
146157
};
147158
}
148159
/**
@@ -155,9 +166,18 @@ class JaylyDB implements Map<string, string | number | boolean> {
155166
this.salt = this.encrypted ? this.objective.displayName : undefined;
156167

157168
// Fetch all data when database initialize
158-
this.updateParticipants();
159-
// This function queues the data to be written to the scoreboard every second interval.
160-
system.runInterval(() => this.updateParticipants());
169+
this.updateParticipants(true);
170+
171+
system.runInterval(() => {
172+
if (!!world.scoreboard.getObjective("jaylydb:" + id)) return this.updateParticipants();
173+
else if (this.SYNC_OK === true) console.error(`[JaylyDB] There is a sync issue with database '${id}'.`);
174+
175+
this.localCache.forEach((value, key) => {
176+
const encoded = DisplayName.stringify({ [key]: value }, this.salt);
177+
this.tempCache.set(key, encoded);
178+
});
179+
this.SYNC_OK = false;
180+
});
161181
}
162182
/**
163183
* @returns the number of elements in the database.

0 commit comments

Comments
 (0)