@@ -56,14 +56,14 @@ function writeFile(file, data) {
5656 */
5757function paginateArray ( array , itemsPerPage ) {
5858 let pages = [ ] ;
59- try {
59+ try {
6060 for ( let i = 0 ; i < array . length ; i += itemsPerPage ) {
6161 pages . push ( array . slice ( i , i + itemsPerPage ) ) ;
6262 }
63- } catch ( _ ) {
63+ } catch ( _ ) {
6464 pages . push ( "白名单解析出现异常,请管理员检查白名单" )
6565 }
66-
66+
6767 return pages ;
6868}
6969
@@ -200,6 +200,7 @@ class FWebsocketClient {
200200 */
201201 _Close ( ) {
202202 this . isShakeHand = false ;
203+ this . tryConnect = false ;
203204 if ( this . WSC . readyState == this . WSC . OPEN ) {
204205 return this . close ( false ) ;
205206 }
@@ -211,9 +212,10 @@ class FWebsocketClient {
211212 */
212213 _InitMsgProcess ( ) {
213214 let wsc = this . WSC ;
214-
215+
215216 // 替换原来的 listen 方法调用
216217 wsc . on ( 'message' , ( msg ) => {
218+ //logger.info(`收到消息: ${msg}`)
217219 try {
218220 let json = JSON . parse ( msg ) ;
219221 this . _processMessage ( json . header , json . body ) ;
@@ -341,8 +343,8 @@ class FWebsocketClient {
341343 case "queryOnline" : this . onQueryOnline ( header . id , body ) ; break ;
342344 case "shutdown" : this . onShutDown ( header . id , body ) ; break ;
343345 case "sendConfig" : this . onSendConfig ( header . id , body ) ; break ;
344- case "run" : this . onRun ( header . id , body , header . type , false ) ; break ;
345- case "runAdmin" : this . onRun ( header . id , body , header . type , true ) ; break ;
346+ case "run" : this . onRun ( header . id , body , header . type , false ) ; break ;
347+ case "runAdmin" : this . onRun ( header . id , body , header . type , true ) ; break ;
346348 case "bindRequest" : this . onBindRequest ( header . id , body , header . type ) ; break ;
347349 }
348350 } catch ( e ) {
@@ -373,26 +375,26 @@ class FWebsocketClient {
373375 * @param {string } type
374376 * @param {boolean } isAdmin
375377 */
376- onRun ( id , body , type , isAdmin ) {
378+ onRun ( id , body , type , isAdmin ) {
377379 let keyWord = body . key ;
378380 let params = body . runParams ;
379381
380382 //配置文件自定义命令
381383 let config = readFile ( CONFIGPATH ) ;
382384 let customCommand = config . customCommand ;
383- for ( let i = 0 ; i < customCommand . length ; i ++ ) {
385+ for ( let i = 0 ; i < customCommand . length ; i ++ ) {
384386 let command = customCommand [ i ]
385- if ( command . key == keyWord ) {
387+ if ( command . key == keyWord ) {
386388 //判断是否是管理员
387- if ( command . permission > 0 && ! isAdmin ) {
389+ if ( command . permission > 0 && ! isAdmin ) {
388390 this . _Respone ( `权限不足,若您是管理员,请使用/管理员执行` , body . groupId , "error" , id )
389391 return ;
390392 }
391393 //格式化参数
392394 let cmd = command . command ;
393- for ( let j = 0 ; j < params . length ; j ++ ) {
395+ for ( let j = 0 ; j < params . length ; j ++ ) {
394396 let param = params [ j ]
395- cmd = cmd . replace ( `&${ j + 1 } ` , param )
397+ cmd = cmd . replace ( `&${ j + 1 } ` , param )
396398 }
397399 //执行
398400 let outputCmd = mc . runcmdEx ( cmd ) ;
@@ -407,19 +409,19 @@ class FWebsocketClient {
407409
408410 //插件自定义命令
409411 let data = JSON . stringify ( body )
410- if ( Object . keys ( callbackEvent [ type ] ) . indexOf ( keyWord ) != - 1 ) {
412+ if ( Object . keys ( callbackEvent [ type ] ) . indexOf ( keyWord ) != - 1 ) {
411413 let ret = callbackEvent [ type ] [ keyWord ] ( data )
412414 if ( typeof ret === "string" ) {
413415 this . _Respone ( ret , body . groupId , "success" , id )
414- } else {
416+ } else {
415417 throw new Error ( `自定义命令返回值必须为字符串!` )
416418 }
417419 return ;
418- } else {
420+ } else {
419421 let ret = ( `未找到自定义命令:${ keyWord } ` )
420422 this . _Respone ( ret , body . groupId , "error" , id )
421423 }
422-
424+
423425
424426
425427 }
@@ -526,12 +528,12 @@ class FWebsocketClient {
526528 if ( ! config . chatFormat . post_chat ) return ; // 总开关关闭时不处理
527529
528530 let chatMsg = "群:<{nick}> {msg}"
529- if ( config . chatFormat ) {
531+ if ( config . chatFormat ) {
530532 chatMsg = config . chatFormat . group
531- . replace ( "{nick}" , body . nick )
532- . replace ( "{msg}" , body . msg ) ;
533+ . replace ( "{nick}" , body . nick )
534+ . replace ( "{msg}" , body . msg ) ;
533535 }
534-
536+
535537 sendGroupMsg2Game ( chatMsg )
536538 }
537539
@@ -585,15 +587,15 @@ class FWebsocketClient {
585587 */
586588 onQueryAllowList ( id , body ) {
587589 let BDSAllowlist = { }
588- try {
590+ try {
589591 BDSAllowlist = readFile ( BDSALLOWLISTPATH )
590- } catch ( err ) {
592+ } catch ( err ) {
591593 logger . error ( "读取白名单文件失败,请检查白名单文件是否正确!" )
592594 logger . error ( err )
593595 this . _sendMsg ( "queryWl" , { "list" : "读取白名单文件失败,请检查白名单文件是否正确!" } , id )
594596 return ;
595597 }
596-
598+
597599 let nameList = [ ]
598600 for ( let i = 0 ; i < BDSAllowlist . length ; i ++ ) {
599601 nameList . push ( BDSAllowlist [ i ] [ "name" ] ) ;
@@ -666,7 +668,7 @@ class FWebsocketClient {
666668 //拼接在线列表
667669 let onlineNameString = ""
668670 let online = mc . getOnlinePlayers ( ) ;
669- if ( output_online_list ) {
671+ if ( output_online_list ) {
670672 for ( let i = 0 ; i < online . length ; i ++ ) {
671673 let simulated = ""
672674 if ( online [ i ] . isSimulatedPlayer ( ) && config . addSimulatedPlayerTip ) {
@@ -680,13 +682,13 @@ class FWebsocketClient {
680682 onlineNameString += text
681683
682684 this . _sendMsg ( "queryOnline" , {
683- "list" : {
684- "msg" : onlineNameString ,
685+ "list" : {
686+ "msg" : onlineNameString ,
685687 "url" : `${ server_ip } :${ server_port } ` ,
686688 "imgUrl" : api . replace ( "{server_ip}" , server_ip ) . replace ( "{server_port}" , server_port ) ,
687689 "post_img" : post_img ,
688690 "serverType" : "bedrock" ,
689- }
691+ }
690692 } , id )
691693 }
692694
@@ -727,8 +729,8 @@ class FWebsocketClient {
727729 * @returns
728730 */
729731 _sendMsg ( type , body , uuid = system . randomGuid ( ) ) {
730- if ( this . WSC . readyState != this . WSC . OPEN && this . isShakeHand ) {
731- //cb(null);
732+ if ( ( this . WSC . readyState != this . WSC . OPEN ) ) {
733+ logger . error ( "Websocket未连接,请连接后再试." )
732734 return ;
733735 }
734736 let response = {
@@ -763,7 +765,7 @@ class FWebsocketClient {
763765 * 回复消息
764766 * @param {string } msg
765767 */
766- _postChat ( msg ) {
768+ _postChat ( msg ) {
767769 let serverId = readFile ( CONFIGPATH ) . serverId
768770 this . _sendMsg (
769771 "chat" ,
@@ -789,6 +791,9 @@ class FWebsocketClient {
789791 if ( ! bool ) {
790792 return this . WSC . close ( 1000 ) ;
791793 }
794+ if ( this . autoReconnect ) {
795+ clearTimeout ( this . autoReconnect ) ;
796+ }
792797 return true ;
793798 }
794799}
@@ -819,7 +824,7 @@ function initWebsocketServer() {
819824 */
820825function regCommand ( ws ) {
821826 const cmd = mc . newCommand ( "huhobot" , `${ PLUGINNAME } 管理` , PermType . Any ) ;
822- cmd . setEnum ( "Gui" , [ "gui" , "reconnect" , "close" , "help" ] ) ;
827+ cmd . setEnum ( "Gui" , [ "gui" , "reconnect" , "close" , "help" ] ) ;
823828 cmd . setEnum ( "Bind" , [ "bind" ] )
824829 cmd . mandatory ( "gui" , ParamType . Enum , "Gui" , 1 ) ;
825830 cmd . mandatory ( "bind" , ParamType . Enum , "Bind" , 1 ) ;
@@ -842,7 +847,7 @@ function regCommand(ws) {
842847 if ( _ori . player == null || _ori . player . permLevel > 0 ) {
843848 if ( ws . WSC . status == ws . WSC . Open ) {
844849 ws . _Close ( )
845- }
850+ }
846851 ws . _Connect ( ) ;
847852 out . error ( `[${ PLUGINNAME } ]Websocket 正在重连` )
848853 } else {
@@ -876,9 +881,9 @@ function regCommand(ws) {
876881
877882 break
878883 case "update" :
879- if ( _ori . player == null ) {
884+ if ( _ori . player == null ) {
880885 updateVersion ( ) ;
881- } else {
886+ } else {
882887 out . error ( "此命令无法在玩家终端执行!" ) ;
883888 }
884889 break ;
@@ -897,7 +902,7 @@ function regCommand(ws) {
897902}
898903
899904function convertConfig ( ) {
900- const oldConfigVersion = CONFIG_VERSION - 1 ;
905+ const oldConfigVersion = CONFIG_VERSION - 1 ;
901906 try {
902907 // 备份当前配置
903908 const oldConfig = readFile ( CONFIGPATH ) ;
@@ -918,28 +923,28 @@ function convertConfig() {
918923 // 写入新配置
919924 writeFile ( CONFIGPATH , newConfig ) ;
920925 logger . info ( `配置文件已由 v${ oldConfigVersion } 升级为 v${ CONFIG_VERSION } ` ) ;
921-
926+
922927 } catch ( error ) {
923928 logger . error ( `配置文件v${ oldConfigVersion } 转至v${ CONFIG_VERSION } 失败:` , error . message ) ;
924929 }
925930}
926931
927932//自动更新
928- function updateVersion ( ) {
929- network . httpGet ( LATESTURL , ( statusCode , result ) => {
930- if ( statusCode == 200 ) {
933+ function updateVersion ( ) {
934+ network . httpGet ( LATESTURL , ( statusCode , result ) => {
935+ if ( statusCode == 200 ) {
931936 let latestVersion = JSON . parse ( result ) . latest
932- if ( latestVersion != "v" + VERSION ) {
933- network . httpGet ( UPDATEURL . replace ( "{VERSION}" , latestVersion ) , ( statusCode , result ) => {
934- if ( statusCode == 200 ) {
937+ if ( latestVersion != "v" + VERSION ) {
938+ network . httpGet ( UPDATEURL . replace ( "{VERSION}" , latestVersion ) , ( statusCode , result ) => {
939+ if ( statusCode == 200 ) {
935940 const normalizedResult = result . replace ( / \r \n / g, '\n' ) ;
936- File . writeTo ( PATH + "huhobot.js" , normalizedResult )
941+ File . writeTo ( PATH + "huhobot.js" , normalizedResult )
937942 //尝试重载
938943 logger . info ( `HuHoBot已更新至${ latestVersion } ,已尝试重载插件,若未生效,请重启服务器.` )
939944 mc . runcmd ( `ll reload ${ PLUGINNAME } ` )
940945 }
941946 } )
942- } else {
947+ } else {
943948 logger . info ( `当前版本为最新版本v${ VERSION } ,无需更新.` )
944949 }
945950 }
@@ -956,12 +961,12 @@ function initPlugin() {
956961 //检测是否需要更新配置文件
957962 let config = readFile ( CONFIGPATH )
958963 logger . info ( "配置文件版本为:" + config . version )
959- if ( config . version == null || config . version < CONFIG_VERSION - 1 ) {
964+ if ( config . version == null || config . version < CONFIG_VERSION - 1 ) {
960965 logger . error ( "配置文件版本过低,请手动升级。" )
961966 logger . error ( "HuHoBot将不会加载." )
962967 return ;
963968 }
964- else if ( config . version == CONFIG_VERSION - 1 ) {
969+ else if ( config . version == CONFIG_VERSION - 1 ) {
965970 logger . info ( "配置文件版本过低,正在升级..." )
966971 convertConfig ( )
967972 }
@@ -982,24 +987,24 @@ function initPlugin() {
982987
983988 mc . listen ( "onChat" , ( pl , msg ) => {
984989 const config = readFile ( CONFIGPATH ) ;
985-
990+
986991 // 读取配置参数
987992 const { post_chat, post_prefix, max_length } = config . chatFormat ;
988993 if ( ! post_chat ) return ; // 总开关关闭时不处理
989-
994+
990995 let processedMsg = msg ;
991-
996+
992997 // 处理前缀逻辑
993998 if ( post_prefix ) {
994999 // 当设置前缀时,仅转发带前缀的消息
9951000 if ( ! msg . startsWith ( post_prefix ) ) return ;
996-
1001+
9971002 // 去除前缀并修剪空白(可选)
9981003 processedMsg = msg . slice ( post_prefix . length ) . trim ( ) ;
9991004 }
10001005
10011006 //检查是否超过最大值
1002- if ( processedMsg . length > max_length ) {
1007+ if ( processedMsg . length > max_length ) {
10031008 pl . tell ( `消息过长,请勿发送超过${ max_length } 个字符的消息。` )
10041009 return ;
10051010 }
@@ -1008,14 +1013,14 @@ function initPlugin() {
10081013 const formatString = config . chatFormat . game
10091014 . replace ( "{name}" , pl . realName )
10101015 . replace ( "{msg}" , processedMsg ) ;
1011-
1016+
10121017 // 发送到WebSocket
10131018 if ( WebsocketObject ) {
10141019 WebsocketObject . _postChat ( formatString ) ;
10151020 }
10161021 } ) ;
10171022
1018-
1023+
10191024}
10201025
10211026initPlugin ( )
0 commit comments