22 * server forms
33 */
44
5- import * as ui from '@minecraft/server-ui' ;
65import { Player } from '@minecraft/server' ;
7- import { safeCall } from './utils.js' ;
6+ import * as ui from '@minecraft/server-ui' ;
7+ import * as util from './utils.js' ;
88
99// custom ui registry
1010const registry : Map < string , BaseFormBuilder > = new Map ( ) ;
@@ -63,7 +63,7 @@ export abstract class BaseFormBuilder
6363 protected _handleCancel ( ctx : UIContext , response : ui . FormResponse ) : boolean
6464 {
6565 if ( ! response . canceled ) return false ;
66- safeCall ( this . _cancel , ctx , response . cancelationReason ) ;
66+ util . safeCall ( this . _cancel , ctx , response . cancelationReason ) ;
6767 return true ;
6868 }
6969
@@ -167,11 +167,9 @@ export class ActionFormBuilder extends BaseFormBuilder
167167 return this . _data . show ( ctx . user ) . then ( r => {
168168 playersOnUI . delete ( ctx . user . id ) ;
169169
170- // the form were canceled
171170 if ( this . _handleCancel ( ctx , r ) ) return r ;
172171
173- // button action
174- safeCall ( this . _actions [ r . selection ] , ctx ) ;
172+ util . safeCall ( this . _actions [ r . selection ] , ctx ) ;
175173
176174 return r ;
177175 } ) ;
@@ -256,12 +254,10 @@ export class MessageFormBuilder extends BaseFormBuilder
256254 return this . _data . show ( ctx . user ) . then ( r => {
257255 playersOnUI . delete ( ctx . user . id ) ;
258256
259- // the form were canceled
260257 if ( this . _handleCancel ( ctx , r ) ) return r ;
261258
262- // button action
263- if ( r . selection == 0 ) safeCall ( this . _btn1 , ctx ) ;
264- else if ( r . selection == 1 ) safeCall ( this . _btn2 , ctx ) ;
259+ if ( r . selection == 0 ) util . safeCall ( this . _btn1 , ctx ) ;
260+ else if ( r . selection == 1 ) util . safeCall ( this . _btn2 , ctx ) ;
265261
266262 return r ;
267263 } ) ;
@@ -394,14 +390,11 @@ export class ModalFormBuilder extends BaseFormBuilder
394390 return this . _data . show ( ctx . user ) . then ( r => {
395391 playersOnUI . delete ( ctx . user . id ) ;
396392
397- // the form were canceled
398393 if ( this . _handleCancel ( ctx , r ) ) return r ;
399394
400- // process result
401395 const result : modalResponse = { } ;
402396 r . formValues . forEach ( ( val , idx ) => result [ this . _inputIds [ idx ] ] = val ) ;
403- // run the submit action
404- safeCall ( this . _submit , ctx , result ) ;
397+ util . safeCall ( this . _submit , ctx , result ) ;
405398
406399 return r ;
407400 } ) ;
@@ -436,9 +429,9 @@ export class UIContext
436429 public readonly user : Player ;
437430
438431 /**
439- * whether we're on the top ui (we cant go back)
432+ * whether we're on the root ui (we cant go back)
440433 */
441- public get topUI ( ) : boolean
434+ public get isOnRoot ( ) : boolean
442435 {
443436 return this . _uiStack . length == 1 ;
444437 }
@@ -458,15 +451,11 @@ export class UIContext
458451 */
459452 public goto ( form : BaseFormBuilder | string ) : boolean
460453 {
461- // player is still on a custom ui
462- if ( displayingUI ( this . user ) ) return false ;
454+ if ( isOnUI ( this . user ) ) return false ;
463455
464- // get form data
465456 const ui = form instanceof BaseFormBuilder ? form : registry . get ( form ) ;
466- // not found
467457 if ( ! ui ) return false ;
468458
469- // show to player
470459 this . _uiStack . push ( ui ) ;
471460 ui . show ( this ) ;
472461 return true ;
@@ -478,14 +467,9 @@ export class UIContext
478467 */
479468 public back ( ) : boolean
480469 {
481- // player is still on a custom ui
482- if ( displayingUI ( this . user ) ) return false ;
483- // we're on the top most ui
484- if ( this . topUI ) return false ;
470+ if ( isOnUI ( this . user ) || this . isOnRoot ) return false ;
485471
486- // pop the current ui
487472 this . _uiStack . pop ( ) ;
488- // show the last ui
489473 this . currentUI . show ( this ) ;
490474
491475 return true ;
@@ -497,11 +481,9 @@ export class UIContext
497481 */
498482 public backto ( id : string ) : boolean
499483 {
500- // player is still on another form
501- if ( displayingUI ( this . user ) ) return false ;
484+ if ( isOnUI ( this . user ) ) return false ;
502485
503- // pop the ui until the given form id
504- while ( ! this . topUI ) {
486+ while ( ! this . isOnRoot ) {
505487 this . _uiStack . pop ( ) ;
506488 if ( this . currentUI . id != id )
507489 continue ;
@@ -533,7 +515,7 @@ export function showForm(
533515 * @param player the player to test
534516 * @returns boolean
535517 */
536- export function displayingUI ( player : Player ) : boolean
518+ export function isOnUI ( player : Player ) : boolean
537519{
538520 return playersOnUI . has ( player . id ) ;
539521}
0 commit comments