|
1 | | -This documentation describes the `ScoreboardDB`, `AsyncDatabase`, and `ScoreboardDatabase` classes. It also using these classes from [Con-Module](https://github.com/Con-JS-Development/Con-Module): |
2 | | -- The `AsyncSemaphore` from ["Con-Module/Utils/AsyncSemaphore"](https://github.com/Con-JS-Development/Con-Module/blob/main/Docs/con-utils/AsyncSemaphore.md) ([source](https://github.com/Con-JS-Development/Con-Module/blob/main/con-lib/con-utils/Semaphore.js)). |
3 | | - |
4 | | -## Internal ScoreboardDB |
5 | | - |
6 | | -`ScoreboardDB` is a class that extends [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) and provides an interface to interact with a scoreboard objective in Minecraft. It allows you to store key-value pairs in a scoreboard objective and retrieve them later. |
7 | | - |
8 | | -### Create |
9 | | - |
10 | | -```js |
11 | | -ScoreboardDB.create(objective: string | ScoreboardObjective, createNew?: boolean); |
12 | | -``` |
13 | | - |
14 | | -Creates a new `ScoreboardDB` instance. |
15 | | - |
16 | | -- `objective`: The scoreboard objective to open. Can be a string or a `ScoreboardObjective` instance. |
17 | | -- `createNew`: Optional. Whether to create a new scoreboard objective if it doesn't exist. Defaults to `true`. |
18 | | - |
19 | | -### Properties |
20 | | - |
21 | | -#### participants |
22 | | - |
23 | | -```js |
24 | | -scoreboardDB.participants: Map<string, ScoreboardIdentity>; |
25 | | -``` |
26 | | - |
27 | | -A read-only property that returns the participants of the scoreboard objective. |
28 | | - |
29 | | -#### id |
30 | | - |
31 | | -```js |
32 | | -scoreboardDB.id: string; |
33 | | -``` |
34 | | - |
35 | | -A read-only property that returns the id of the scoreboard objective. |
36 | | - |
37 | | -#### scoreboardObjective |
38 | | - |
39 | | -```js |
40 | | -scoreboardDB.scoreboardObjective: ScoreboardObjective; |
41 | | -``` |
42 | | - |
43 | | -A read-only property that returns the scoreboard objective. |
44 | | - |
45 | | -### Methods |
46 | | - |
47 | | -#### open |
48 | | - |
49 | | -```js |
50 | | -ScoreboardDB.open(objective: string | ScoreboardObjective): ScoreboardDB; |
51 | | -``` |
52 | | - |
53 | | -Opens a scoreboard objective and returns a `ScoreboardDB` instance. |
54 | | - |
55 | | -- `objective`: The scoreboard objective to open. Can be a string or a `ScoreboardObjective` instance. |
56 | | -- Returns: The `ScoreboardDB` instance. |
57 | | - |
58 | | -#### delete |
59 | | - |
60 | | -```js |
61 | | -scoreboardDB.delete(key: string): boolean; |
62 | | -``` |
63 | | - |
64 | | -Deletes a key from the database. |
65 | | - |
66 | | -- `key`: The key to delete from the database. |
67 | | -- Returns: Whether the key was deleted. |
68 | | - |
69 | | -#### clear |
70 | | - |
71 | | -```js |
72 | | -scoreboardDB.clear(): void; |
73 | | -``` |
74 | | - |
75 | | -Clears the database. |
76 | | - |
77 | | -## Public AsyncDatabase |
78 | | - |
79 | | -`AsyncDatabase` is a class that extends `ScoreboardDB` and provides an asynchronous interface to interact with a scoreboard objective in Minecraft. It allows you to store key-value pairs in a scoreboard objective and retrieve them later asynchronously. It is more performant than the synchronous version (`ScoreboardDatabase`) but can be slower when setting values for the same key in one tick. |
80 | | - |
81 | | -### Methods |
82 | | - |
83 | | -#### set |
84 | | - |
85 | | -```js |
86 | | -async asyncDatabase.set(key: string, value: any): Promise<void>; |
87 | | -``` |
88 | | - |
89 | | -Sets a value for a key in the database asynchronously. |
90 | | - |
91 | | -- `key`: The key to set. |
92 | | -- `value`: The value to set. |
93 | | - |
94 | | -### Properties |
95 | | - |
96 | | -#### semaphores |
97 | | - |
98 | | -```js |
99 | | -asyncDatabase.semaphores: Map<string, AsyncSemaphore>; |
100 | | -``` |
101 | | - |
102 | | -A read-only property that returns the semaphores of the database. |
103 | | - |
104 | | -## Public ScoreboardDatabase |
105 | | - |
106 | | -`ScoreboardDatabase` is a class that extends `ScoreboardDB` and provides an interface to interact with a scoreboard objective in Minecraft. It allows you to store key-value pairs in a scoreboard objective and retrieve them later synchronously. |
107 | | - |
108 | | -### Methods |
109 | | - |
110 | | -#### set |
111 | | - |
112 | | -```js |
113 | | -scoreboardDatabase.set(key: string, value: any): this; |
114 | | -``` |
115 | | - |
116 | | -Sets a value for a key in the database synchronously. |
117 | | - |
118 | | -- `key`: The key to set. |
119 | | -- `value`: The value to set. |
120 | | -- Returns: The `ScoreboardDatabase` instance. |
121 | | - |
122 | | -## Requirements |
123 | | - - Server Module: |
124 | | - - `1.2.0-beta` |
125 | | - - `1.3.0-beta` |
126 | | - |
127 | | -### [MIT License](./LICENSE) |
| 1 | +# Con-Databases |
| 2 | +This package contains three types of databases, JsonDatabase, NBTDatabase, CustomDatabase. |
| 3 | +Each of these database types supports all possible Map class operations from ECMAScript, which means. |
| 4 | +### Inherited from Map |
| 5 | + - size: number of properties |
| 6 | + - [Symbol.iterator]: is iterable [key, value] |
| 7 | + - clear(): clear all values |
| 8 | + - delete(key: string): delete specific value |
| 9 | + - entries(): generator of all values [key, value] |
| 10 | + - forEach(callBack: (value, key, this)=>void): for each all elements and call provided function for that |
| 11 | + - get(key: string): returns value for specific key |
| 12 | + - set(key: string, value: any): sets new value for provided key |
| 13 | + - keys(): returns iterbale of keys |
| 14 | + - values(): returns iterable of values |
| 15 | +### Additional Methods |
| 16 | + - load(): will load database from provided scoreboard |
| 17 | + - loadAsync(): will load database asynchonously from profived scoreboard |
| 18 | + - rebuild(): when database is deleted by user in the world you can call rebuild to save loaded data without lost |
| 19 | + - rebuildAsync(): same as rebuild() but asyncronouse |
| 20 | +### Additional Properties |
| 21 | + - objective: returns scoreboard objective what is database binded to |
| 22 | + - id: returns id of the objective |
| 23 | + - loaded: returns boolean, true when database is loaded |
| 24 | + - maxLength: max size of key and value after parsed via parse |
| 25 | + - savingMode: mode when your database is saving your data after change |
| 26 | + - OneTimeSave: Saving after value was changed |
| 27 | + - EndTickSave: Saving at the end of the tick, ater any changes occurs |
| 28 | + - IntervalTick: Saving Every interval if changes occurs |
| 29 | + |
| 30 | +### Database Types |
| 31 | + - JsonDatabase, is saving data in JSON form. (SuperFast/EasyToRead) |
| 32 | + - NBTDatabase, is saving data in NBT form. (Fast/HardToRead) |
| 33 | + - Custom, is saving data in format of provided parser (undefined/undefined) |
| 34 | + |
| 35 | +### Example |
| 36 | +```js |
| 37 | +// INITIALIZATION OF DATABASE |
| 38 | +const myDB = new JsonDatabase("MyIdentifier").load(); |
| 39 | +const myDB = new NBTDatabase("MyIdentifier").load(); |
| 40 | +const myDB = new CustomDatabase(JSON /* JSON is parser */,"MyIdentifier").load(); |
| 41 | + |
| 42 | +//using (get/set) to (read/write) data (from/to) database |
| 43 | +const worldOpenedCount = myDB.get("openCount")??0; |
| 44 | +myDB.set("openCount", ++worldOpenedCount); |
| 45 | + |
| 46 | +//getting all data saved in database |
| 47 | +for(const [key, value] of myDB.entries()){ |
| 48 | + console.warn(key, value); |
| 49 | +} |
| 50 | + |
| 51 | +//clearing database |
| 52 | +myDB.clear(); |
| 53 | + |
| 54 | +//removing specific value |
| 55 | +myDB.delete("Josh"); |
| 56 | + |
| 57 | +//forEaching every element in scoreboard |
| 58 | +myDB.forEach((value,key)=>{ |
| 59 | + console.warn(key, value); |
| 60 | +}); |
| 61 | +``` |
0 commit comments