import { MySystem } from "./system" ; interface ICustomComponent { thirsty : number ; broken : number ; burn : number ; infect : number ; } var tick = 0 ; var playerList = [ ] ; const system = server . registerSystem < MySystem > ( 0 , 0 ) ; system . initialize = function ( ) { server . log ( "生存v0.1 Loaded by haojie06" ) ; system . registerComponent ( "survival:player_state" , { thirsty : 100 , broken : 100 , burn : 100 } ) ; system . listenForEvent ( "minecraft:entity_created" , onPlayerCreate ) ; this . registerCommand ( "status" , { description : "查看身体状况" , permission : 0 , overloads : [ { parameters : [ ] , handler ( origin ) { let component ; let info = system . actorInfo ( origin . entity ) ; for ( let player of playerList ) { let playerInfo = system . actorInfo ( player ) ; if ( info . name == playerInfo . name ) { component = system . getComponent < ICustomComponent > ( player , "survival:player_state" ) ; } } this . openModalForm ( origin . entity , JSON . stringify ( { type : "form" , title : "status" , content : "你当前的身体状况" , buttons : [ { text : `口渴值: ${ component . data . thirsty }` , image : { type : "url" , data : "https://s2.ax1x.com/2019/06/13/VfC7dA.png" } } , { text : `感染值: 0` , image : { type : "url" , data : "https://s2.ax1x.com/2019/06/18/VL58r4.png" } } , { text : `饥渴值:我开玩笑的,不要举报我` , image : { type : "url" , data : "https://s2.ax1x.com/2019/06/18/VLIeyD.png" } } ] } ) ) ; } } as CommandOverload < MySystem , [ ] > ] } ) ; this . checkUse ( ( player , info ) => { if ( info . item . name == "potion" ) { server . log ( "正在喝水" ) ; } } ) ; } ; system . update = function ( ) { tick ++ ; if ( tick >= 20 ) { updateStatus ( playerList ) ; tick = 0 ; } } ; function updateStatus ( playerList ) { if ( playerList . length != 0 ) { for ( let player of playerList ) { try { let info = system . actorInfo ( player ) ; let component = system . getComponent < ICustomComponent > ( player , "survival:player_state" ) ; if ( component . data . thirsty > 1 ) { component . data . thirsty -= 1 ; } else if ( component . data . thirsty == 1 ) { server . log ( "提醒玩家" ) ; system . invokeConsoleCommand ( "§eSurvival" , `tell ${ info . name } §c口好渴啊！` ) ; component . data . thirsty = 0 ; } else { } system . applyComponentChanges ( player , component ) ; } catch ( err ) { server . log ( "出现错误" ) ; } } } else { } } function onPlayerCreate ( data ) { var entity = data . entity ; if ( ! entity ) throw "not entity" ; if ( entity . __identifier__ == "minecraft:player" ) { server . log ( "玩家创建,注册组件,添加至列表" ) ; system . createComponent < ICustomComponent > ( entity , "survival:player_state" ) ; playerList . push ( entity ) ; } } function onPlayerLeave ( data ) { var entity = data . entity ; if ( ! entity ) throw "not entity" ; if ( entity . __identifier__ == "minecraft:player" ) { server . log ( "玩家离开，移除列表" ) ; playerList . splice ( playerList . indexOf ( entity ) , 1 ) ; } }