@@ -428,6 +428,9 @@ NBTReaderOptions.prototype.readers = defualtReaders;
428428// DATABASE.JS
429429///////////////////////////////////////////////////
430430const { scoreboard} = world , { FakePlayer} = ScoreboardIdentityType ;
431+
432+ const databases = new Map ( ) ;
433+
431434const split = "\n_`Split`_\n" ;
432435function endTickCall ( callback ) {
433436 system . run ( ( ) => system . run ( ( ) => system . run ( callback ) ) ) ;
@@ -481,13 +484,14 @@ class ScoreboardDatabaseManager extends Map{
481484 _saveMode_ ;
482485 /**@private */
483486 hasChanges = false ;
487+ /**@private */
488+ _loadingPromise_ ;
484489 /**@readonly */
485490 get maxLength ( ) { return 30e3 ; }
486491 /**@private @type {ScoreboardObjective }*/
487492 _scoreboard_ ;
488493 /**@protected @type {Map<string,string|ScoreboardIdentity|Entity> } */
489494 _source_ ;
490- _onHandleLost_ ;
491495 /**@protected @readonly @type {{stringify:(data: any)=>string,parse:(data: string): any} } */
492496 get _parser_ ( ) { return JSON ; }
493497 get savingMode ( ) { return this . _saveMode_ ; }
@@ -500,6 +504,7 @@ class ScoreboardDatabaseManager extends Map{
500504 if ( ! objective ) throw new RangeError ( "Firt parameter si not valid: " + objective ) ;
501505 if ( typeof objective !== "string" && ! objective instanceof ScoreboardObjective ) throw new RangeError ( "Firt parameter si not valid: " + objective ) ;
502506 this . _scoreboard_ = typeof objective === "string" ?( scoreboard . getObjective ( objective ) ?? scoreboard . addObjective ( objective , objective ) ) :objective ;
507+ if ( databases . has ( this . id ) ) return databases . get ( this . id ) ;
503508 this . _nameId_ = this . id ;
504509 this . _source_ = new Map ( ) ;
505510 this . _changes_ = new Map ( ) ;
@@ -514,6 +519,7 @@ class ScoreboardDatabaseManager extends Map{
514519 }
515520 } , this . interval ) ;
516521 }
522+ databases . set ( this . id , this ) ;
517523 }
518524 load ( ) {
519525 if ( this . _loaded_ ) return this ;
@@ -527,17 +533,21 @@ class ScoreboardDatabaseManager extends Map{
527533 this . _loaded_ = true ;
528534 return this ;
529535 }
530- async loadAsync ( ) {
531- if ( this . _loaded_ ) return this ;
532- for ( const participant of this . _scoreboard_ . getParticipants ( ) ) {
533- const { displayName, type} = participant ;
534- if ( type !== FakePlayer ) continue ;
535- const [ name , data ] = displayName . split ( split ) ;
536- this . _source_ . set ( name , participant ) ;
537- super . set ( name , this . _parser_ . parse ( data ) ) ;
538- }
539- this . _loaded_ = true ;
540- return this ;
536+ loadAsync ( ) {
537+ if ( this . _loaded_ ) return this . _loadingPromise_ ?? Promise . resolve ( this ) ;
538+ const promise = ( async ( ) => {
539+ for ( const participant of this . _scoreboard_ . getParticipants ( ) ) {
540+ const { displayName, type} = participant ;
541+ if ( type !== FakePlayer ) continue ;
542+ const [ name , data ] = displayName . split ( split ) ;
543+ this . _source_ . set ( name , participant ) ;
544+ super . set ( name , this . _parser_ . parse ( data ) ) ;
545+ }
546+ this . _loaded_ = true ;
547+ return this ;
548+ } ) ( ) ;
549+ this . _loadingPromise_ = promise ;
550+ return promise ;
541551 }
542552 /**@inheritdoc */
543553 set ( key , value ) {
@@ -554,6 +564,7 @@ class ScoreboardDatabaseManager extends Map{
554564 this . _onChange_ ( key , null , ChangeAction . Remove ) ;
555565 return super . delete ( key ) ;
556566 }
567+ /**@inheritdoc */
557568 clear ( ) {
558569 if ( ! this . _loaded_ ) throw new ReferenceError ( "Database is not loaded" ) ;
559570 for ( const [ key , value ] of this . entries ( ) ) this . delete ( key , value ) ;
@@ -569,6 +580,9 @@ class ScoreboardDatabaseManager extends Map{
569580 get id ( ) { return this . _scoreboard_ . id ; }
570581 /**@readonly @returns {boolean } */
571582 get loaded ( ) { return this . _loaded_ ; }
583+ /**@readonly @returns {DefualtJsonType } */
584+ get type ( ) { return "DefualtJsonType" ; }
585+ get loadingAwaiter ( ) { return this . _loadingPromise_ ?? this . loadAsync ( ) ; }
572586 rebuild ( ) {
573587 if ( this . objective ?. isValid ( ) ) return ;
574588 const newScores = scoreboard . addObjective ( this . _nameId_ , this . _nameId_ ) ;
@@ -595,15 +609,19 @@ class ScoreboardDatabaseManager extends Map{
595609 return this ;
596610 }
597611}
598- export class JsonDatabase extends ScoreboardDatabaseManager { }
612+ export class JsonDatabase extends ScoreboardDatabaseManager {
613+ get type ( ) { return "JsonType" ; }
614+ }
599615export class NBTDatabase extends ScoreboardDatabaseManager {
600616 get _parser_ ( ) { return NBT ; } ;
617+ get type ( ) { return "NBTType" ; }
601618}
602619export class CustomDatabase extends ScoreboardDatabaseManager {
603620 constructor ( parser , ...params ) {
604621 super ( params ) ;
605622 this . _parser_ = parser ;
606623 }
624+ get type ( ) { return "CustomType" ; }
607625}
608626function generateRandomString ( length ) {
609627 let result = '' ;
0 commit comments