@@ -68,28 +68,25 @@ function convertNbs(name, callback) {
6868 return ;
6969 }
7070
71- let file ;
72- try {
73- file = new File ( nbsCachePath , File . ReadMode ) ;
74- } catch {
75- callback ( false , '打开文件失败' ) ;
76- return ;
77- }
78- const ok = file . readAll ( ( ret ) => {
79- let j ;
80- try {
81- j = JSON . parse ( ret ) ;
82- } catch {
83- callback ( false , '解析转换后Json失败' ) ;
84- return ;
85- }
71+ Promise . resolve ( )
72+ . then ( ( ) => File . readFrom ( nbsCachePath ) )
73+ . then ( ( t ) => {
74+ if ( ! t ) {
75+ callback ( false , '读取文件失败' ) ;
76+ return ;
77+ }
8678
87- file . close ( ) ;
88- File . delete ( nbsCachePath ) ;
89- callback ( true , j ) ;
90- } ) ;
79+ let j ;
80+ try {
81+ j = JSON . parse ( t ) ;
82+ } catch {
83+ callback ( false , '解析转换后Json失败' ) ;
84+ return ;
85+ }
9186
92- if ( ! ok ) callback ( false , '读取文件失败' ) ;
87+ File . delete ( nbsCachePath ) ;
88+ callback ( true , j ) ;
89+ } ) ;
9390 }
9491 ) ;
9592}
@@ -188,18 +185,24 @@ function startPlay(player, nbsName) {
188185 time : tickToMs ( v . tick , tempo ) ,
189186 note : v ,
190187 } ) ) ;
188+ const totalNotes = noteAndTime . length ;
191189 const startTime = Date . now ( ) ;
192190
193191 const task = ( ) => {
194- const willPlay = [ ] ;
192+ let notesRemain = noteAndTime . length ;
195193 const pl = mc . getPlayer ( xuid ) ;
194+ if ( notesRemain === 0 || ! pl ) {
195+ stopPlay ( xuid ) ;
196+ return ;
197+ }
198+
196199 const timeSpent = Date . now ( ) - startTime ;
200+ const willPlay = [ ] ;
197201
198202 for ( ; ; ) {
199- if ( noteAndTime . length === 0 || ! pl ) {
200- stopPlay ( xuid ) ;
201- return ;
202- }
203+ notesRemain = noteAndTime . length ;
204+ if ( notesRemain === 0 ) break ;
205+
203206 const { time, note } = noteAndTime [ 0 ] ;
204207 if ( time <= timeSpent ) {
205208 willPlay . push ( note ) ;
@@ -227,11 +230,14 @@ function startPlay(player, nbsName) {
227230 // log(cmd);
228231 mc . runcmdEx ( cmd ) ;
229232 } ) ;
233+
234+ const timeSpentStr = formatMsTime ( timeSpent ) ;
230235 pl . tell (
231236 `${ Green } ▶ ${ LightPurple } NbsPlayer\n` +
232237 `${ songDisplayName } \n` +
233- `${ Yellow } ${ formatMsTime ( timeSpent ) } ${ Gray } | ` +
234- `${ Gold } ${ totalLengthStr } ` ,
238+ `${ Yellow } ${ timeSpentStr } ${ White } / ${ Gold } ${ totalLengthStr } ` +
239+ `${ Gray } | ` +
240+ `${ Yellow } ${ totalNotes - notesRemain } ${ White } / ${ Gold } ${ totalNotes } ` ,
235241 4
236242 ) ;
237243 } ;
@@ -398,7 +404,7 @@ function nbsForm(player) {
398404
399405mc . listen ( 'onLeft' , ( pl ) => stopPlay ( pl . xuid ) ) ;
400406
401- ll . registerPlugin ( pluginName , '在服务器播放NBS音乐!' , [ 0 , 1 , 0 ] , {
407+ ll . registerPlugin ( pluginName , '在服务器播放NBS音乐!' , [ 0 , 1 , 1 ] , {
402408 Author : 'student_2333' ,
403409 License : 'Apache-2.0' ,
404410} ) ;
0 commit comments