|
| 1 | +# Starting with Con-Databases |
| 2 | +If you don't have experience with the Map build-in class, go get acquainted. Here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map |
| 3 | + |
| 4 | +## Steps |
| 5 | + |
| 6 | +### Downloading specific version |
| 7 | +Pick your version what you want to use [here](https://github.com/Con-JS-Development/Con-Database/tags) |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +Then download your required files |
| 12 | +- *(try downloading types for better experience)* |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +### Import and use! |
| 17 | +- Place database files into your coding folder |
| 18 | +- Import your required classes from database file |
| 19 | + ```js |
| 20 | + import {JsonDatabase} from "./database"; //import your downloaded file |
| 21 | + ``` |
| 22 | +Now you can just use, save and load your data |
| 23 | +```js |
| 24 | +
|
| 25 | +const database = new JsonDatabase("my_database_id").load(); |
| 26 | +
|
| 27 | +database.set("some key", "string data"); |
| 28 | +database.set("some key2", 645654); |
| 29 | +database.set("some key3", ["array","array",{name:"object"}]); |
| 30 | +
|
| 31 | +database.get("some key2") - 645654; |
| 32 | +database.get("some key") + " is useful"; |
| 33 | +database.remove("some key3"); |
| 34 | +``` |
| 35 | +## Example saving deaths for each player |
| 36 | +Each key is unique to its player because key include players id, so database keys looks like |
| 37 | +``` |
| 38 | +5648463deaths |
| 39 | +6843684deaths |
| 40 | +6545463deaths |
| 41 | +``` |
| 42 | +Code |
| 43 | +```js |
| 44 | +import {world} from "@minecraft/server"; |
| 45 | +
|
| 46 | +const stats = new JsonDatabase("playerStats").load(); |
| 47 | +
|
| 48 | +world.afterEvents.entityDie.subscribe(({deadEntity})=> |
| 49 | + setDeaths(deadEntity,getDeaths(deadEntity) + 1); |
| 50 | +,{entityTypes:["mineraft:player"]}); |
| 51 | +
|
| 52 | +
|
| 53 | +function getDeaths(player){ |
| 54 | + return stats.get(player.id + "deaths")??0;} |
| 55 | +
|
| 56 | +function setDeaths(player,deaths){ |
| 57 | + stats.set(player.id + "deaths",deaths);} |
| 58 | +``` |
0 commit comments