Skip to content

Commit

Permalink
Merge pull request #4 from Con-JS-Development/development
Browse files Browse the repository at this point in the history
Dual instance security!
  • Loading branch information
conmaster2112 authored Aug 6, 2023
2 parents a27050e + b5b2ee5 commit 2f5d7a0
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 24 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ Each of these database types supports all possible [Map](https://developer.mozil
- entries(): generator of all values [key, value]
- forEach(callBack: (value, key, this)=>void): for each all elements and call provided function for that
- get(key: string): returns value for specific key
- set(key: string, value: any): sets new value for provided key
- set(key: string: value: any): sets new value for provided key
- has(key: string): returns true when database has a value for that key
- keys(): returns iterbale of keys
- values(): returns iterable of values
### Additional Methods
- load(): will load database from provided scoreboard
- loadAsync(): will load database asynchonously from profived scoreboard
- loadAsync(): will load database asynchonously from provided scoreboard
- rebuild(): when database is deleted by user in the world you can call rebuild to save loaded data without lost
- rebuildAsync(): same as rebuild() but asyncronouse
### Additional Properties
Expand All @@ -32,12 +33,20 @@ Each of these database types supports all possible [Map](https://developer.mozil
- NBTDatabase, is saving data in NBT form. (Fast/HardToRead)
- Custom, is saving data in format of provided parser (undefined/undefined)

### Dual instance security!
```js
const myDB1 = new JsonDatabase("sameId");
const myDB2 = new NBTDatabase("sameId"); //returns JsonDatabase because database with same id "sameId" was already created.

console.log(myDB1 === myDB2); //true the very same instance!
```

### Example
```js
// INITIALIZATION OF DATABASE
const myDB = new JsonDatabase("MyIdentifier").load();
const myDB = new NBTDatabase("MyIdentifier").load();
const myDB = new CustomDatabase(JSON /* JSON is parser */,"MyIdentifier").load();
const myDB = new NBTDatabase("MyIdentifier2").load();
const myDB = new CustomDatabase(JSON /* JSON is parser */,"MyIdentifier3").load();

//using (get/set) to (read/write) data (from/to) database
const worldOpenedCount = myDB.get("openCount")??0;
Expand Down
14 changes: 9 additions & 5 deletions database.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,18 @@ export enum ChangeAction {
/**@extends {Map<string,any>}*/
declare class ScoreboardDatabaseManager extends Map<string,any>{
private _saveMode_: DatabaseSavingModes;
private _loadingPromise_?: Promise<this>;
private hasChanges: boolean;
readonly maxLength: number;
private readonly _scoreboard_: ScoreboardObjective;
protected readonly _source_: Map<string,string|ScoreboardIdentity|Entity>;
protected readonly _parser_: {stringify:(data: any)=>string,parse:(data: string)=>any};
readonly savingMode: DatabaseSavingModes;
constructor(objective, saveMode: DatabaseSavingModes);
constructor(objective, saveMode: DatabaseSavingModes.EndTickSave, interval?: number);
constructor(objective: string | ScoreboardObjective, saveMode?: DatabaseSavingModes);
constructor(objective: string | ScoreboardObjective, saveMode: DatabaseSavingModes.EndTickSave, interval?: number);
/**@inheritdoc */
set(key: string, value: any): this
/**@inheritdoc */
delete(key: string): boolean
/**@inheritdoc */
clear(): void
load(): this
loadAsync(): Promise<this>
Expand All @@ -169,10 +169,14 @@ declare class ScoreboardDatabaseManager extends Map<string,any>{
readonly objective: ScoreboardObjective
readonly id: string;
readonly loaded: boolean;
readonly maxLength: number;
readonly savingMode: DatabaseSavingModes;
readonly type: "DefualtJsonType"| "JsonType"| "NBTType" | "CustomType";
readonly loadingAwaiter: Promise<this>;
}
export class JsonDatabase extends ScoreboardDatabaseManager{}
export class NBTDatabase extends ScoreboardDatabaseManager{}
export class CustomDatabase extends ScoreboardDatabaseManager{
constructor(parser: {parse:(data:string)=>any,stringify:(data: any)=>string}, objective: string | ScoreboardObjective, saveMode: DatabaseSavingModes);
constructor(parser: {parse:(data:string)=>any,stringify:(data: any)=>string}, objective: string | ScoreboardObjective, saveMode?: DatabaseSavingModes);
constructor(parser: {parse:(data:string)=>any,stringify:(data: any)=>string}, objective: string | ScoreboardObjective, saveMode: DatabaseSavingModes.EndTickSave, interval?: number);
}
44 changes: 31 additions & 13 deletions database.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@ NBTReaderOptions.prototype.readers = defualtReaders;
// DATABASE.JS
///////////////////////////////////////////////////
const {scoreboard} = world, {FakePlayer} = ScoreboardIdentityType;

const databases = new Map();

const split = "\n_`Split`_\n";
function endTickCall(callback){
system.run(()=>system.run(()=>system.run(callback)));
Expand Down Expand Up @@ -481,13 +484,14 @@ class ScoreboardDatabaseManager extends Map{
_saveMode_;
/**@private */
hasChanges = false;
/**@private */
_loadingPromise_;
/**@readonly */
get maxLength(){return 30e3;}
/**@private @type {ScoreboardObjective}*/
_scoreboard_;
/**@protected @type {Map<string,string|ScoreboardIdentity|Entity>} */
_source_;
_onHandleLost_;
/**@protected @readonly @type {{stringify:(data: any)=>string,parse:(data: string): any}} */
get _parser_(){return JSON;}
get savingMode(){return this._saveMode_;}
Expand All @@ -500,6 +504,7 @@ class ScoreboardDatabaseManager extends Map{
if(!objective) throw new RangeError("Firt parameter si not valid: " + objective);
if(typeof objective !== "string" && !objective instanceof ScoreboardObjective) throw new RangeError("Firt parameter si not valid: " + objective);
this._scoreboard_ = typeof objective === "string"?(scoreboard.getObjective(objective)??scoreboard.addObjective(objective,objective)):objective;
if(databases.has(this.id)) return databases.get(this.id);
this._nameId_ = this.id;
this._source_ = new Map();
this._changes_ = new Map();
Expand All @@ -514,6 +519,7 @@ class ScoreboardDatabaseManager extends Map{
}
},this.interval);
}
databases.set(this.id,this);
}
load(){
if(this._loaded_) return this;
Expand All @@ -527,17 +533,21 @@ class ScoreboardDatabaseManager extends Map{
this._loaded_=true;
return this;
}
async loadAsync(){
if(this._loaded_) return this;
for (const participant of this._scoreboard_.getParticipants()) {
const {displayName,type} = participant;
if(type !== FakePlayer) continue;
const [name,data] = displayName.split(split);
this._source_.set(name,participant);
super.set(name,this._parser_.parse(data));
}
this._loaded_=true;
return this;
loadAsync(){
if(this._loaded_) return this._loadingPromise_??Promise.resolve(this);
const promise = (async ()=>{
for (const participant of this._scoreboard_.getParticipants()) {
const {displayName,type} = participant;
if(type !== FakePlayer) continue;
const [name,data] = displayName.split(split);
this._source_.set(name,participant);
super.set(name,this._parser_.parse(data));
}
this._loaded_=true;
return this;
})();
this._loadingPromise_ = promise;
return promise;
}
/**@inheritdoc */
set(key, value){
Expand All @@ -554,6 +564,7 @@ class ScoreboardDatabaseManager extends Map{
this._onChange_(key,null,ChangeAction.Remove);
return super.delete(key);
}
/**@inheritdoc */
clear(){
if(!this._loaded_) throw new ReferenceError("Database is not loaded");
for (const [key,value] of this.entries()) this.delete(key,value);
Expand All @@ -569,6 +580,9 @@ class ScoreboardDatabaseManager extends Map{
get id(){return this._scoreboard_.id;}
/**@readonly @returns {boolean} */
get loaded(){return this._loaded_;}
/**@readonly @returns {DefualtJsonType} */
get type(){return "DefualtJsonType";}
get loadingAwaiter(){return this._loadingPromise_??this.loadAsync();}
rebuild(){
if(this.objective?.isValid()) return;
const newScores = scoreboard.addObjective(this._nameId_,this._nameId_);
Expand All @@ -595,15 +609,19 @@ class ScoreboardDatabaseManager extends Map{
return this;
}
}
export class JsonDatabase extends ScoreboardDatabaseManager{}
export class JsonDatabase extends ScoreboardDatabaseManager{
get type(){return "JsonType";}
}
export class NBTDatabase extends ScoreboardDatabaseManager{
get _parser_() {return NBT;};
get type(){return "NBTType";}
}
export class CustomDatabase extends ScoreboardDatabaseManager{
constructor(parser,...params){
super(params);
this._parser_ = parser;
}
get type(){return "CustomType";}
}
function generateRandomString(length) {
let result = '';
Expand Down
2 changes: 1 addition & 1 deletion packs/BP/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"dependencies": [
{"module_name": "@minecraft/server","version": "1.5.0-beta"},
{"module_name": "@minecraft/server","version": "1.6.0-beta"},
{"module_name": "@minecraft/server-ui","version": "1.2.0-beta"}
],
"capabilities": ["script_eval"]
Expand Down
4 changes: 3 additions & 1 deletion packs/BP/scripts/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { DatabaseSavingModes, NBTDatabase } from "db";
import { DatabaseSavingModes, NBTDatabase, JsonDatabase } from "database";


const a = new NBTDatabase("sus2",DatabaseSavingModes.OneTimeSave,50).load();
const b = new JsonDatabase("sus2");
console.warn(a === b);
a.set("sussy",{jerremy:(a.get("sussy")?.jerremy??0) + 1});

console.warn("Current sussy count is " + a.get("sussy").jerremy);

0 comments on commit 2f5d7a0

Please sign in to comment.