From 811c0446b37f8914c20283c32b49ad4da8fcbb7e Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Tue, 10 Dec 2024 20:35:59 +0700 Subject: [PATCH 01/13] Simple Database for ScriptAPI Database usiny DynamicDatabase --- scripts/quick-db/index.js | 107 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 scripts/quick-db/index.js diff --git a/scripts/quick-db/index.js b/scripts/quick-db/index.js new file mode 100644 index 00000000..a89df8a2 --- /dev/null +++ b/scripts/quick-db/index.js @@ -0,0 +1,107 @@ +/** + * # QUICK DB @DATABASE + * + * @format + * @author @Nperma fomo datang (jaskiding) use like map method: set(), delete(), get(), keys(), values(), entries() + * @example const db = new QuickDB("user"); + * + * + * @param {key: string} @param {value: any} @returns {boolean} //set item + * db.set("FomoKiwor", {money:20000}) + * + * @param {key} @returns {value} // log: {money:20000} + * db.get("FomoKiwor") + * + * @param {key} @returns {boolean} // log: true + * db.has("FomoKiwor") + * // log: false + * db.has("Lope387") + * + * @param {key} @returns {boolean} //delete item + * db.delete("FomoKiwor") + * + * // log: false + * db.has("FomoKiwor") + * + * @returns {keys[]} get all key on db + * db.keys() + * @returns {values[]} get all value on db + * db.values(); + * + * @returns {entries[key,value]} get all key and value on db + * db.entries(); + */ + +import { world,World} from "@minecraft/server"; + +const DATABASE_PREFIX = "\u0235\u0235"; + +const {getDynamicProperty:GET,setDynamicProperty:SET,getDynamicPropertyIds:IDS,getDynamicPropertyTotalByteCount:BYTES}=World.prototype; + +class QuickDB { + #identifier; + constructor(id) { + this.#identifier = `${DATABASE_PREFIX}${id}${DATABASE_PREFIX}`; + } + + get size(){return BYTES.call(world)} + + has(key){return !!GET.call(world, key)} + + get(key){ + return this.has(key)?JSON.parse(GET.call(world,key)):undefined + } + + set(key,value){ + if(!(key instanceof String)||typeof key!=="string") return false; + SET.call(world,key,JSON.stringify(value)) + return true; + } + + delete(key){ + if(!this.has(key)) return false; + SET.call(world,key,undefined); + return true; + } + + keys() { + return this.#UIDX("keys"); + } + + values() { + return this.#UIDX("values"); + } + + entries() { + return this.#UIDX("entries"); + } + + /** + * Helper method to iterate through database properties + * @param {"keys" | "values" | "entries"} type + * @returns {Array} + */ + #UIDX(type) { + const ids = IDS.call(world); + const result = []; + + for (const id of ids) { + if (!id.startsWith(this.#identifier)) continue; + + const key = id.replace(this.#identifier, ""); + if (type === "keys") { + result.push(key); + } else if (type === "values") { + const value = JSON.parse(GET.call(world, id)); + result.push(value); + } else if (type === "entries") { + const value = JSON.parse(GET.call(world, id)); + result.push([key, value]); + } + } + + return result; + } +} + +export default QuickDB From 623979a69ea8fee8b59d08e05563c87172783d3e Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Tue, 10 Dec 2024 20:49:35 +0700 Subject: [PATCH 02/13] testing --- scripts/quick-db/test.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 scripts/quick-db/test.js diff --git a/scripts/quick-db/test.js b/scripts/quick-db/test.js new file mode 100644 index 00000000..a046603b --- /dev/null +++ b/scripts/quick-db/test.js @@ -0,0 +1,31 @@ +import QuickDB from "./index.js"; +import { world } from "@minecraft/server"; + +const db = new QuickDB("twst"); + +db.set("anjay", ["alok","ruok"]); + + +console.log(db.get("anjay")); //log: ["alok","ruok"] + +db.set("anjay", db.get("anjay").filter(value=>value!=="alok")); //value: ["alok"] + +db.set("anjay", "nandalopadit") //value: nandalopadit + +db.delete("anjay"); + +world.afterEvents.playerSpawn.subscribe((event)=>{ + if(event.initialSpawn) { + if(!db.has(player.id){ + + db.set(player.id,{ + name: player.name, + joinDate: Date.now() + }); + world.sendMessage("§aregister new player with name " + player.name); + return; + } + + world.sendMessage(`§a${player.name} join date: ${new Date(db.get(player.id)).toLocaleDateString()?.replace(new RegExp("/","g"),"-")}`); + } +}) From bcac82f01a09cdc9a5e4c41853737588e86fccd7 Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Tue, 10 Dec 2024 20:55:05 +0700 Subject: [PATCH 03/13] Readme --- scripts/quick-db/readme.md | 165 +++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 scripts/quick-db/readme.md diff --git a/scripts/quick-db/readme.md b/scripts/quick-db/readme.md new file mode 100644 index 00000000..7fdd5c59 --- /dev/null +++ b/scripts/quick-db/readme.md @@ -0,0 +1,165 @@ +# QuickDB - A Lightweight Database for Minecraft Bedrock ScriptAPI + +**QuickDB** is a simple and efficient database system designed for Minecraft Bedrock Edition ScriptAPI. It utilizes dynamic properties from the `@minecraft/server` module, allowing developers to store and manage key-value pairs in a way similar to JavaScript's `Map` object. + +--- + +## Features + +- **CRUD Operations**: + - `set(key, value)` - Save a value to the database. + - `get(key)` - Retrieve a value by its key. + - `delete(key)` - Remove a key-value pair from the database. + - `has(key)` - Check if a key exists in the database. + +- **Iteration**: + - `keys()` - Get all keys in the database. + - `values()` - Retrieve all values stored in the database. + - `entries()` - Retrieve all key-value pairs as an array of entries. + +- **Database Size**: + - `size` - Get the total byte count used by the dynamic properties. + +--- + +## Installation + +Import `QuickDB` into your ScriptAPI project. Ensure `QuickDB` is included in your `index.js` file for easy integration. + +```javascript +import QuickDB from "./index.js"; +``` + +--- + +## Documentation + +### **Constructor** + +```javascript +const db = new QuickDB("user"); +``` + +- **Parameters**: + - `id` *(string)*: A unique identifier for your database instance. + +--- + +### **Methods** + +#### `set(key, value)` +- **Description**: Stores a value associated with a key. +- **Parameters**: + - `key` *(string)*: The key to store the value. + - `value` *(any)*: The value to be stored. +- **Returns**: `boolean` - `true` if successful. + +```javascript +db.set("FomoKiwor", { money: 20000 }); +``` + +--- + +#### `get(key)` +- **Description**: Retrieves the value associated with a key. +- **Parameters**: + - `key` *(string)*: The key to retrieve the value. +- **Returns**: `any` - The value associated with the key. + +```javascript +const userData = db.get("FomoKiwor"); // { money: 20000 } +``` + +--- + +#### `has(key)` +- **Description**: Checks if a key exists in the database. +- **Parameters**: + - `key` *(string)*: The key to check. +- **Returns**: `boolean` - `true` if the key exists. + +```javascript +const exists = db.has("FomoKiwor"); // true +``` + +--- + +#### `delete(key)` +- **Description**: Removes a key-value pair from the database. +- **Parameters**: + - `key` *(string)*: The key to remove. +- **Returns**: `boolean` - `true` if successful. + +```javascript +db.delete("FomoKiwor"); +``` + +--- + +#### `keys()` +- **Description**: Retrieves all keys in the database. +- **Returns**: `string[]` - An array of all keys. + +```javascript +const allKeys = db.keys(); // ["key1", "key2"] +``` + +--- + +#### `values()` +- **Description**: Retrieves all values in the database. +- **Returns**: `any[]` - An array of all values. + +```javascript +const allValues = db.values(); // [{ money: 20000 }, { items: [] }] +``` + +--- + +#### `entries()` +- **Description**: Retrieves all key-value pairs in the database. +- **Returns**: `Array<[string, any]>` - An array of key-value entries. + +```javascript +const allEntries = db.entries(); // [["key1", { money: 20000 }], ["key2", { items: [] }]] +``` + +--- + +### **Property** + +#### `size` +- **Description**: Gets the total byte count used by dynamic properties. +- **Returns**: `number` - The total byte count. + +```javascript +console.log(db.size); // e.g., 512 +``` + +--- + +## Example Usage + +```javascript +import QuickDB from "./index.js"; + +const db = new QuickDB("user"); + +db.set("FomoKiwor", { money: 20000 }); +console.log(db.get("FomoKiwor")); // { money: 20000 } + +console.log(db.has("FomoKiwor")); // true +db.delete("FomoKiwor"); +console.log(db.has("FomoKiwor")); // false + +db.set("User1", { score: 100 }); +db.set("User2", { score: 150 }); + +console.log(db.keys()); // ["User1", "User2"] +console.log(db.values()); // [{ score: 100 }, { score: 150 }] +console.log(db.entries()); // [["User1", { score: 100 }], ["User2", { score: 150 }]] +``` + +--- + +Developed by [Nperma](https://github.com/nperma) From a1af1e987726785ced84a252e86899cad0698db0 Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Tue, 10 Dec 2024 20:59:19 +0700 Subject: [PATCH 04/13] Update test.js --- scripts/quick-db/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/quick-db/test.js b/scripts/quick-db/test.js index a046603b..7bdbd583 100644 --- a/scripts/quick-db/test.js +++ b/scripts/quick-db/test.js @@ -16,7 +16,7 @@ db.delete("anjay"); world.afterEvents.playerSpawn.subscribe((event)=>{ if(event.initialSpawn) { - if(!db.has(player.id){ + if(!db.has(player.id)){ db.set(player.id,{ name: player.name, From 192065a541573fc594dc894232f115c725f2c8bf Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Tue, 10 Dec 2024 21:37:13 +0700 Subject: [PATCH 05/13] Main Database Extension --- scripts/quick-db/index.js | 150 +++++++++++++++----------------------- 1 file changed, 60 insertions(+), 90 deletions(-) diff --git a/scripts/quick-db/index.js b/scripts/quick-db/index.js index a89df8a2..f4b0e988 100644 --- a/scripts/quick-db/index.js +++ b/scripts/quick-db/index.js @@ -1,107 +1,77 @@ -/** - * # QUICK DB @DATABASE - * - * @format - * @author @Nperma fomo datang (jaskiding) use like map method: set(), delete(), get(), keys(), values(), entries() - * @example const db = new QuickDB("user"); - * - * - * @param {key: string} @param {value: any} @returns {boolean} //set item - * db.set("FomoKiwor", {money:20000}) - * - * @param {key} @returns {value} // log: {money:20000} - * db.get("FomoKiwor") - * - * @param {key} @returns {boolean} // log: true - * db.has("FomoKiwor") - * // log: false - * db.has("Lope387") - * - * @param {key} @returns {boolean} //delete item - * db.delete("FomoKiwor") - * - * // log: false - * db.has("FomoKiwor") - * - * @returns {keys[]} get all key on db - * db.keys() - * @returns {values[]} get all value on db - * db.values(); - * - * @returns {entries[key,value]} get all key and value on db - * db.entries(); - */ - -import { world,World} from "@minecraft/server"; +import { world, World } from "@minecraft/server"; const DATABASE_PREFIX = "\u0235\u0235"; -const {getDynamicProperty:GET,setDynamicProperty:SET,getDynamicPropertyIds:IDS,getDynamicPropertyTotalByteCount:BYTES}=World.prototype; +const { + getDynamicProperty: GET, + setDynamicProperty: SET, + getDynamicPropertyIds: IDS, + getDynamicPropertyTotalByteCount: BYTES +} = World.prototype; class QuickDB { #identifier; constructor(id) { this.#identifier = `${DATABASE_PREFIX}${id}${DATABASE_PREFIX}`; } - - get size(){return BYTES.call(world)} - - has(key){return !!GET.call(world, key)} - - get(key){ - return this.has(key)?JSON.parse(GET.call(world,key)):undefined + + get size() { + return BYTES.call(world); + } + + has(key) { + return !!GET.call(world, key); + } + + get(key) { + return this.has(key) ? JSON.parse(GET.call(world, key)) : undefined; } - - set(key,value){ - if(!(key instanceof String)||typeof key!=="string") return false; - SET.call(world,key,JSON.stringify(value)) - return true; + + set(key, value) { + if (!(key instanceof String) || typeof key !== "string") return false; + SET.call(world, key, JSON.stringify(value)); + return true; } - - delete(key){ - if(!this.has(key)) return false; - SET.call(world,key,undefined); - return true; + + delete(key) { + if (!this.has(key)) return false; + SET.call(world, key, undefined); + return true; } - + keys() { - return this.#UIDX("keys"); - } - - values() { - return this.#UIDX("values"); - } - - entries() { - return this.#UIDX("entries"); - } - - /** - * Helper method to iterate through database properties - * @param {"keys" | "values" | "entries"} type - * @returns {Array} - */ - #UIDX(type) { - const ids = IDS.call(world); - const result = []; - - for (const id of ids) { - if (!id.startsWith(this.#identifier)) continue; - - const key = id.replace(this.#identifier, ""); - if (type === "keys") { - result.push(key); - } else if (type === "values") { - const value = JSON.parse(GET.call(world, id)); - result.push(value); - } else if (type === "entries") { - const value = JSON.parse(GET.call(world, id)); - result.push([key, value]); - } + return this.#UIDX("keys"); } - return result; - } + values() { + return this.#UIDX("values"); + } + + entries() { + return this.#UIDX("entries"); + } + + #UIDX(type) { + const ids = IDS.call(world); + const result = []; + + for (const id of ids) { + if (!id.startsWith(this.#identifier)) continue; + + const key = id.replace(this.#identifier, ""); + if (type === "keys") { + result.push(key); + } else if (type === "values") { + const value = JSON.parse(GET.call(world, id)); + result.push(value); + } else if (type === "entries") { + const value = JSON.parse(GET.call(world, id)); + result.push([key, value]); + } + } + + return result; + } } -export default QuickDB +export default QuickDB; From 365f132066b6807dc2a18fe0567d28e8fd78010d Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Tue, 10 Dec 2024 21:39:50 +0700 Subject: [PATCH 06/13] Delete scripts/quick-db/test.js --- scripts/quick-db/test.js | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 scripts/quick-db/test.js diff --git a/scripts/quick-db/test.js b/scripts/quick-db/test.js deleted file mode 100644 index 7bdbd583..00000000 --- a/scripts/quick-db/test.js +++ /dev/null @@ -1,31 +0,0 @@ -import QuickDB from "./index.js"; -import { world } from "@minecraft/server"; - -const db = new QuickDB("twst"); - -db.set("anjay", ["alok","ruok"]); - - -console.log(db.get("anjay")); //log: ["alok","ruok"] - -db.set("anjay", db.get("anjay").filter(value=>value!=="alok")); //value: ["alok"] - -db.set("anjay", "nandalopadit") //value: nandalopadit - -db.delete("anjay"); - -world.afterEvents.playerSpawn.subscribe((event)=>{ - if(event.initialSpawn) { - if(!db.has(player.id)){ - - db.set(player.id,{ - name: player.name, - joinDate: Date.now() - }); - world.sendMessage("§aregister new player with name " + player.name); - return; - } - - world.sendMessage(`§a${player.name} join date: ${new Date(db.get(player.id)).toLocaleDateString()?.replace(new RegExp("/","g"),"-")}`); - } -}) From 027eb6924f32f68d3f9246f6004561ccecd4289d Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Tue, 10 Dec 2024 21:52:48 +0700 Subject: [PATCH 07/13] QuickDB --- scripts/quick-db/index.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/scripts/quick-db/index.js b/scripts/quick-db/index.js index f4b0e988..da63ed34 100644 --- a/scripts/quick-db/index.js +++ b/scripts/quick-db/index.js @@ -1,14 +1,7 @@ -import { world, World } from "@minecraft/server"; +import { world } from "@minecraft/server"; const DATABASE_PREFIX = "\u0235\u0235"; -const { - getDynamicProperty: GET, - setDynamicProperty: SET, - getDynamicPropertyIds: IDS, - getDynamicPropertyTotalByteCount: BYTES -} = World.prototype; - class QuickDB { #identifier; constructor(id) { @@ -20,22 +13,27 @@ class QuickDB { } has(key) { - return !!GET.call(world, key); + return !!world.getDynamicProperty(`${this.#identifier}${key}`); } get(key) { - return this.has(key) ? JSON.parse(GET.call(world, key)) : undefined; + return this.has(key) + ? JSON.parse(world.getDynamicProperty(`${this.#identifier}${key}`)) + : undefined; } set(key, value) { if (!(key instanceof String) || typeof key !== "string") return false; - SET.call(world, key, JSON.stringify(value)); + world.setDynamicProperty( + `${this.#identifier}${key}`, + JSON.stringify(value) + ); return true; } delete(key) { if (!this.has(key)) return false; - SET.call(world, key, undefined); + world.setDynamicProperty(`${this.#identifier}${key}`, undefined); return true; } @@ -62,10 +60,14 @@ class QuickDB { if (type === "keys") { result.push(key); } else if (type === "values") { - const value = JSON.parse(GET.call(world, id)); + const value = JSON.parse( + world.getDynamicProperty(`${this.#identifier}${key}`) + ); result.push(value); } else if (type === "entries") { - const value = JSON.parse(GET.call(world, id)); + const value = JSON.parse( + world.getDynamicProperty(`${this.#identifier}${key}`) + ); result.push([key, value]); } } From 6d07353904051a5831de0ba9a641404aa2b16415 Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Tue, 10 Dec 2024 21:59:04 +0700 Subject: [PATCH 08/13] Simple Database for ScriptAPI --- scripts/quick-db/index.js | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/scripts/quick-db/index.js b/scripts/quick-db/index.js index da63ed34..4c0a18ee 100644 --- a/scripts/quick-db/index.js +++ b/scripts/quick-db/index.js @@ -9,31 +9,33 @@ class QuickDB { } get size() { - return BYTES.call(world); + return world.getDynamicPropertyIds() + .filter((id) => id.startsWith(this.#identifier)).length; } has(key) { - return !!world.getDynamicProperty(`${this.#identifier}${key}`); + const dynamicKey = `${this.#identifier}${key}`; + return !!world.getDynamicProperty(dynamicKey); } get(key) { - return this.has(key) - ? JSON.parse(world.getDynamicProperty(`${this.#identifier}${key}`)) + const dynamicKey = `${this.#identifier}${key}`; + return this.has(key) + ? JSON.parse(world.getDynamicProperty(dynamicKey)) : undefined; } set(key, value) { - if (!(key instanceof String) || typeof key !== "string") return false; - world.setDynamicProperty( - `${this.#identifier}${key}`, - JSON.stringify(value) - ); + if (typeof key !== "string") return false; + const dynamicKey = `${this.#identifier}${key}`; + world.setDynamicProperty(dynamicKey, JSON.stringify(value)); return true; } delete(key) { + const dynamicKey = `${this.#identifier}${key}`; if (!this.has(key)) return false; - world.setDynamicProperty(`${this.#identifier}${key}`, undefined); + world.setDynamicProperty(dynamicKey, undefined); return true; } @@ -50,7 +52,7 @@ class QuickDB { } #UIDX(type) { - const ids = IDS.call(world); + const ids = world.getDynamicPropertyIds(); const result = []; for (const id of ids) { @@ -60,14 +62,10 @@ class QuickDB { if (type === "keys") { result.push(key); } else if (type === "values") { - const value = JSON.parse( - world.getDynamicProperty(`${this.#identifier}${key}`) - ); + const value = JSON.parse(world.getDynamicProperty(id)); result.push(value); } else if (type === "entries") { - const value = JSON.parse( - world.getDynamicProperty(`${this.#identifier}${key}`) - ); + const value = JSON.parse(world.getDynamicProperty(id)); result.push([key, value]); } } From f9626b5e1ca989bf8c954eb70d4256e2f190d15b Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Tue, 10 Dec 2024 22:07:21 +0700 Subject: [PATCH 09/13] Simple Database for ScriptAPI --- scripts/quick-db/index.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/scripts/quick-db/index.js b/scripts/quick-db/index.js index 4c0a18ee..41f0e1a4 100644 --- a/scripts/quick-db/index.js +++ b/scripts/quick-db/index.js @@ -20,16 +20,21 @@ class QuickDB { get(key) { const dynamicKey = `${this.#identifier}${key}`; - return this.has(key) - ? JSON.parse(world.getDynamicProperty(dynamicKey)) - : undefined; + const value = world.getDynamicProperty(dynamicKey); + + return value ? JSON.parse(value) : undefined; } set(key, value) { if (typeof key !== "string") return false; const dynamicKey = `${this.#identifier}${key}`; - world.setDynamicProperty(dynamicKey, JSON.stringify(value)); - return true; + + + if (value !== null && value !== undefined) { + world.setDynamicProperty(dynamicKey, JSON.stringify(value)); + return true; + } + return false; } delete(key) { @@ -62,11 +67,11 @@ class QuickDB { if (type === "keys") { result.push(key); } else if (type === "values") { - const value = JSON.parse(world.getDynamicProperty(id)); - result.push(value); + const value = world.getDynamicProperty(id); + result.push(value ? JSON.parse(value) : undefined); } else if (type === "entries") { - const value = JSON.parse(world.getDynamicProperty(id)); - result.push([key, value]); + const value = world.getDynamicProperty(id); + result.push([key, value ? JSON.parse(value) : undefined]); } } From 43baa5621e75548bd102984517597ee56db21acf Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Tue, 10 Dec 2024 22:15:22 +0700 Subject: [PATCH 10/13] Simple Database for ScriptAPI --- scripts/quick-db/index.js | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/scripts/quick-db/index.js b/scripts/quick-db/index.js index 41f0e1a4..d07eb0a7 100644 --- a/scripts/quick-db/index.js +++ b/scripts/quick-db/index.js @@ -2,6 +2,7 @@ import { world } from "@minecraft/server"; const DATABASE_PREFIX = "\u0235\u0235"; +//adapt code to JalyDev/scriptAPI class QuickDB { #identifier; constructor(id) { @@ -9,7 +10,8 @@ class QuickDB { } get size() { - return world.getDynamicPropertyIds() + return world + .getDynamicPropertyIds() .filter((id) => id.startsWith(this.#identifier)).length; } @@ -21,20 +23,14 @@ class QuickDB { get(key) { const dynamicKey = `${this.#identifier}${key}`; const value = world.getDynamicProperty(dynamicKey); - - return value ? JSON.parse(value) : undefined; + return this.has(key) ? JSON.parse(value) : undefined; } set(key, value) { if (typeof key !== "string") return false; const dynamicKey = `${this.#identifier}${key}`; - - - if (value !== null && value !== undefined) { - world.setDynamicProperty(dynamicKey, JSON.stringify(value)); - return true; - } - return false; + world.setDynamicProperty(dynamicKey, JSON.stringify(value)); + return true; } delete(key) { @@ -67,11 +63,13 @@ class QuickDB { if (type === "keys") { result.push(key); } else if (type === "values") { - const value = world.getDynamicProperty(id); - result.push(value ? JSON.parse(value) : undefined); + const val = world.getDynamicProperty(id); + const value = JSON.parse(val); + result.push(value); } else if (type === "entries") { - const value = world.getDynamicProperty(id); - result.push([key, value ? JSON.parse(value) : undefined]); + const val = world.getDynamicProperty(id); + const value = JSON.parse(val); + result.push([key, value]); } } From cd4233e526be38ef772d5bf479df4ffca17abb4d Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Tue, 10 Dec 2024 22:35:28 +0700 Subject: [PATCH 11/13] Simple Database for ScriptAPI --- scripts/quick-db/index.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/scripts/quick-db/index.js b/scripts/quick-db/index.js index d07eb0a7..cd126211 100644 --- a/scripts/quick-db/index.js +++ b/scripts/quick-db/index.js @@ -1,7 +1,13 @@ -import { world } from "@minecraft/server"; +import { world, World } from "@minecraft/server"; const DATABASE_PREFIX = "\u0235\u0235"; +const { + getDynamicProperty: GET, + setDynamicProperty: SET, + getDynamicPropertyIds: IDS +} = World.prototype; + //adapt code to JalyDev/scriptAPI class QuickDB { #identifier; @@ -10,33 +16,29 @@ class QuickDB { } get size() { - return world - .getDynamicPropertyIds() - .filter((id) => id.startsWith(this.#identifier)).length; + return IDS.call(world).filter((id) => id.startsWith(this.#identifier)) + .length; } has(key) { - const dynamicKey = `${this.#identifier}${key}`; - return !!world.getDynamicProperty(dynamicKey); + return !!GET.call(world, `${this.#identifier}${key}`); } get(key) { - const dynamicKey = `${this.#identifier}${key}`; - const value = world.getDynamicProperty(dynamicKey); - return this.has(key) ? JSON.parse(value) : undefined; + return this.has(key) + ? JSON.parse(GET.call(world, `${this.#identifier}${key}`)) + : undefined; } set(key, value) { if (typeof key !== "string") return false; - const dynamicKey = `${this.#identifier}${key}`; - world.setDynamicProperty(dynamicKey, JSON.stringify(value)); + SET.call(world, `${this.#identifier}${key}`, JSON.stringify(value)); return true; } delete(key) { - const dynamicKey = `${this.#identifier}${key}`; if (!this.has(key)) return false; - world.setDynamicProperty(dynamicKey, undefined); + SET.call(world, `${this.#identifier}${key}`, undefined); return true; } @@ -53,7 +55,7 @@ class QuickDB { } #UIDX(type) { - const ids = world.getDynamicPropertyIds(); + const ids = IDS.call(world); const result = []; for (const id of ids) { @@ -63,12 +65,10 @@ class QuickDB { if (type === "keys") { result.push(key); } else if (type === "values") { - const val = world.getDynamicProperty(id); - const value = JSON.parse(val); + const value = JSON.parse(this.get(key)); result.push(value); } else if (type === "entries") { - const val = world.getDynamicProperty(id); - const value = JSON.parse(val); + const value = JSON.parse(this.get(key)); result.push([key, value]); } } From d724392fd117441ef6b7851b7ef26977acafb055 Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Tue, 10 Dec 2024 22:41:18 +0700 Subject: [PATCH 12/13] Simple Database for ScriptAPI --- scripts/quick-db/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/quick-db/index.js b/scripts/quick-db/index.js index cd126211..5e9d93e5 100644 --- a/scripts/quick-db/index.js +++ b/scripts/quick-db/index.js @@ -1,3 +1,6 @@ +// Script example for ScriptAPI +// Author: Nperma +// Project: https://github.com/JaylyDev/ScriptAPI import { world, World } from "@minecraft/server"; const DATABASE_PREFIX = "\u0235\u0235"; From 1cdd4e691efefa59e8d7e561d8812f4295124dc6 Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Tue, 10 Dec 2024 22:46:44 +0700 Subject: [PATCH 13/13] Simple Database for ScriptAPI --- scripts/quick-db/{readme.md => README.md} | 6 ++++++ 1 file changed, 6 insertions(+) rename scripts/quick-db/{readme.md => README.md} (99%) diff --git a/scripts/quick-db/readme.md b/scripts/quick-db/README.md similarity index 99% rename from scripts/quick-db/readme.md rename to scripts/quick-db/README.md index 7fdd5c59..c7c2e1a0 100644 --- a/scripts/quick-db/readme.md +++ b/scripts/quick-db/README.md @@ -162,4 +162,10 @@ console.log(db.entries()); // [["User1", { score: 100 }], ["User2", { score: 150 --- +## License + +MIT License + +--- + Developed by [Nperma](https://github.com/nperma)