1
- /* global ll mc system Format PermType logger */
1
+ /* global ll mc system Format PermType ParamType logger BinaryStream */
2
2
// LiteLoaderScript Dev Helper
3
3
/// <reference path="E:\Coding\bds\.vscode\LLSEDevHelper/Library/JS/Api.js" />
4
4
@@ -59,7 +59,7 @@ const playTasks = new Map();
59
59
function convertNbs ( name , callback ) {
60
60
const nbsPath = `${ pluginDataPath } ${ name } ` ;
61
61
const nbsCachePath = `${ pluginCachePath } ${ name } .json` ;
62
- return system . newProcess (
62
+ const ret = system . newProcess (
63
63
`${ nbsConvertorPath } -f "${ nbsPath } " -o "${ nbsCachePath } "` ,
64
64
( code , returns ) => {
65
65
if ( ! code === 0 ) {
@@ -79,16 +79,20 @@ function convertNbs(name, callback) {
79
79
let j ;
80
80
try {
81
81
j = JSON . parse ( t ) ;
82
- } catch {
82
+ } catch ( e ) {
83
+ logger . error ( `转换nbs文件出错\n${ e . stack } ` ) ;
83
84
callback ( false , '解析转换后Json失败' ) ;
84
85
return ;
85
86
}
86
87
87
88
File . delete ( nbsCachePath ) ;
88
89
callback ( true , j ) ;
89
- } ) ;
90
- }
90
+ } )
91
+ . catch ( ( e ) => logger . error ( `未知错误\n${ e . stack } ` ) ) ;
92
+ } ,
93
+ 10 * 1000
91
94
) ;
95
+ if ( ! ret ) callback ( false , '运行NbsConvertor失败' ) ;
92
96
}
93
97
94
98
/**
@@ -124,12 +128,36 @@ function formatMsTime(msTime) {
124
128
return `${ min } :${ sec } .${ ms } ` ;
125
129
}
126
130
131
+ /**
132
+ * @param {BinaryStream } bs
133
+ * @param {String } sound
134
+ * @param {FloatPos } position
135
+ * @param {Number } volume
136
+ * @param {Number } pitch
137
+ * @returns
138
+ */
139
+ function getPlaySoundDataPack ( bs , sound , position , volume , pitch ) {
140
+ bs . reset ( ) ;
141
+
142
+ bs . writeString ( sound ) ;
143
+
144
+ // bs.writeVec3(position);
145
+ bs . writeVarInt ( position . x * 8 ) ;
146
+ bs . writeUnsignedVarInt ( position . y * 8 ) ;
147
+ bs . writeVarInt ( position . z * 8 ) ;
148
+
149
+ bs . writeFloat ( volume ) ;
150
+ bs . writeFloat ( pitch ) ;
151
+
152
+ return bs . createPacket ( 86 ) ;
153
+ }
154
+
127
155
/**
128
156
* @param {Player } player
129
157
* @param {String } nbsName
130
158
*/
131
159
function startPlay ( player , nbsName ) {
132
- const { xuid, realName } = player ;
160
+ const { xuid } = player ;
133
161
const playingTask = playTasks . get ( xuid ) ;
134
162
if ( playingTask ) stopPlay ( xuid ) ;
135
163
@@ -186,6 +214,9 @@ function startPlay(player, nbsName) {
186
214
note : v ,
187
215
} ) ) ;
188
216
const totalNotes = noteAndTime . length ;
217
+
218
+ const bs = new BinaryStream ( ) ;
219
+
189
220
const startTime = Date . now ( ) ;
190
221
191
222
const task = ( ) => {
@@ -223,12 +254,24 @@ function startPlay(player, nbsName) {
223
254
const layerVol = layerVolMap . get ( layer ) || 100 ;
224
255
const finalVol = ( velocity * ( layerVol / 100 ) ) / 100 ;
225
256
226
- const cmd =
227
- `execute "${ realName } " ~~~ ` +
228
- `playsound ${ instrumentMapEx . get ( instrument ) } @s ~ ~1.65 ~ ` +
229
- `${ finalVol } ${ keyMap . get ( finalPitch ) } ` ;
257
+ const { pos } = pl ;
258
+ pos . y += 0.37 ;
259
+
260
+ pl . sendPacket (
261
+ getPlaySoundDataPack (
262
+ bs ,
263
+ instrumentMapEx . get ( instrument ) || '' ,
264
+ pos ,
265
+ finalVol || 0 ,
266
+ keyMap . get ( finalPitch ) || 1
267
+ )
268
+ ) ;
269
+ // const cmd =
270
+ // `execute "${realName}" ~~~ ` +
271
+ // `playsound ${instrumentMapEx.get(instrument)} @s ~ ~1.65 ~ ` +
272
+ // `${finalVol} ${keyMap.get(finalPitch)}`;
230
273
// log(cmd);
231
- mc . runcmdEx ( cmd ) ;
274
+ // mc.runcmdEx(cmd);
232
275
} ) ;
233
276
234
277
const timeSpentStr = formatMsTime ( timeSpent ) ;
@@ -256,6 +299,17 @@ function nbsForm(player) {
256
299
if ( v . toLowerCase ( ) . endsWith ( '.nbs' ) ) musics . push ( v ) ;
257
300
} ) ;
258
301
302
+ if ( musics . length === 0 ) {
303
+ player . sendModalForm (
304
+ `${ Aqua } ${ pluginName } ` ,
305
+ `${ Green } 插件数据目录内还没有歌曲文件哦!赶快去寻找nbs音乐来播放吧!` ,
306
+ `知道了` ,
307
+ `知道了` ,
308
+ ( ) => { }
309
+ ) ;
310
+ return ;
311
+ }
312
+
259
313
const search = ( param ) => {
260
314
const paramL = param . toLowerCase ( ) . replace ( ' ' , '' ) ;
261
315
const result = [ ] ;
@@ -328,6 +382,17 @@ function nbsForm(player) {
328
382
}
329
383
330
384
if ( i === 1 ) {
385
+ if ( maxPage < 2 ) {
386
+ player . sendModalForm (
387
+ `${ Aqua } ${ pluginName } ` ,
388
+ `${ Red } 页面总数小于2,无法跳转` ,
389
+ `知道了` ,
390
+ `知道了` ,
391
+ ( ) => sendForm ( page )
392
+ ) ;
393
+ return ;
394
+ }
395
+
331
396
const toPageForm = mc
332
397
. newCustomForm ( )
333
398
. setTitle ( `${ Aqua } ${ pluginName } ` )
@@ -364,21 +429,79 @@ function nbsForm(player) {
364
429
sendForm ( 1 ) ;
365
430
}
366
431
432
+ /**
433
+ * 去两侧引号
434
+ * @param {String } str
435
+ */
436
+ function trimQuote ( str ) {
437
+ if ( str && str . startsWith ( '"' ) && str . endsWith ( '"' ) )
438
+ return str . slice ( 1 , str . length - 1 ) ;
439
+ return str ;
440
+ }
441
+
367
442
( ( ) => {
368
443
const cmd = mc . newCommand ( 'nbsplayer' , '来首音乐嘛?' , PermType . Any ) ;
369
444
cmd . setAlias ( 'nbs' ) ;
445
+ cmd . optional ( 'filename' , ParamType . RawText ) ;
446
+ cmd . overload ( [ 'filename' ] ) ;
370
447
371
- cmd . setCallback ( ( _ , origin , out ) => {
448
+ cmd . setCallback ( ( _ , origin , out , result ) => {
372
449
const { player } = origin ;
373
450
if ( ! player ) {
374
451
out . error ( '该命令只能由玩家执行' ) ;
375
452
return false ;
376
453
}
454
+
455
+ const { filename } = result ;
456
+ if ( filename ) {
457
+ const filePath = `${ pluginDataPath } ${ trimQuote ( filename ) } ` ;
458
+ if ( ! File . exists ( filePath ) ) {
459
+ out . error ( '文件不存在!' ) ;
460
+ return false ;
461
+ }
462
+
463
+ startPlay ( player , trimQuote ( filename ) ) ;
464
+ return true ;
465
+ }
466
+
377
467
nbsForm ( player ) ;
378
468
return true ;
379
469
} ) ;
380
470
381
- cmd . overload ( ) ;
471
+ cmd . setup ( ) ;
472
+ } ) ( ) ;
473
+
474
+ ( ( ) => {
475
+ const cmd = mc . newCommand ( 'nbsplay' , '管理员播放指令' ) ;
476
+ cmd . mandatory ( 'player' , ParamType . Player ) ;
477
+ cmd . mandatory ( 'filename' , ParamType . RawText ) ;
478
+ cmd . optional ( 'forcePlay' , ParamType . Bool ) ;
479
+ cmd . overload ( [ 'player' , 'filename' , 'forcePlay' ] ) ;
480
+
481
+ cmd . setCallback ( ( _ , __ , out , result ) => {
482
+ const { player, filename, forcePlay } = result ;
483
+ const filePath = `${ pluginDataPath } ${ trimQuote ( filename ) } ` ;
484
+ if ( player . length === 0 ) {
485
+ out . error ( '玩家不在线' ) ;
486
+ return false ;
487
+ }
488
+
489
+ if ( ! File . exists ( filePath ) ) {
490
+ out . error ( '文件不存在!' ) ;
491
+ return false ;
492
+ }
493
+
494
+ player . forEach ( ( p ) => {
495
+ if ( forcePlay || ! playTasks . get ( p . xuid ) ) {
496
+ startPlay ( p , filename ) ;
497
+ out . success ( `成功为 ${ p . name } 播放 ${ filename } ` ) ;
498
+ return ;
499
+ }
500
+ out . error ( `玩家 ${ p . name } 正在播放中,操作失败` ) ;
501
+ } ) ;
502
+ return true ;
503
+ } ) ;
504
+
382
505
cmd . setup ( ) ;
383
506
} ) ( ) ;
384
507
@@ -402,9 +525,29 @@ function nbsForm(player) {
402
525
cmd . setup ( ) ;
403
526
} ) ( ) ;
404
527
528
+ ( ( ) => {
529
+ const cmd = mc . newCommand ( 'nbsisplaying' , '玩家是否正在播放' , PermType . Any ) ;
530
+
531
+ cmd . setCallback ( ( _ , origin , out ) => {
532
+ const { player } = origin ;
533
+ if ( ! player ) {
534
+ out . error ( '该命令只能由玩家执行' ) ;
535
+ return false ;
536
+ }
537
+
538
+ if ( playTasks . get ( player . xuid ) ) return out . success ( 'true' ) ;
539
+
540
+ out . error ( 'false' ) ;
541
+ return false ;
542
+ } ) ;
543
+
544
+ cmd . overload ( ) ;
545
+ cmd . setup ( ) ;
546
+ } ) ( ) ;
547
+
405
548
mc . listen ( 'onLeft' , ( pl ) => stopPlay ( pl . xuid ) ) ;
406
549
407
- ll . registerPlugin ( pluginName , '在服务器播放NBS音乐!' , [ 0 , 1 , 1 ] , {
550
+ ll . registerPlugin ( pluginName , '在服务器播放NBS音乐!' , [ 0 , 2 , 0 ] , {
408
551
Author : 'student_2333' ,
409
552
License : 'Apache-2.0' ,
410
553
} ) ;
0 commit comments