@@ -34,9 +34,10 @@ server.listen(port, function () {
3434 opn ( `http://localhost:${ port } ` ) ;
3535 return console . log ( `Server is running on \x1b[34m*:${ port } \x1b[0m` ) ;
3636} ) ;
37-
37+ var botByNick = { } ;
3838io . sockets . on ( "connection" , function ( socket ) {
3939 var query = socket . handshake . query ;
40+ console . log ( query ) ;
4041 console . log ( `[\x1b[32m+\x1b[0m] ${ query . nick } ` ) ;
4142 var heldItem = null ;
4243 var bot = mineflayer . createBot ( {
@@ -45,145 +46,152 @@ io.sockets.on("connection", function (socket) {
4546 username : query . nick ,
4647 version : config . version ,
4748 } ) ;
48- bot . _client . on ( "map_chunk" , function ( packet ) {
49- var cell = new Chunk ( ) ;
50- cell . load ( packet . chunkData , packet . bitMap , true , true ) ;
51- socket . emit ( "mapChunk" , cell . sections , packet . x , packet . z ) ;
52- } ) ;
53- bot . _client . on ( "respawn" , function ( packet ) {
54- socket . emit ( "dimension" , packet . dimension . value . effects . value ) ;
55- } ) ;
56- bot . on ( "heldItemChanged" , function ( item ) {
57- heldItem = item ;
58- } ) ;
59- bot . on ( "login" , function ( ) {
60- socket . emit ( "dimension" , bot . game . dimension ) ;
61- } ) ;
62- bot . on ( "move" , function ( ) {
63- socket . emit ( "move" , bot . entity . position ) ;
64- } ) ;
65- bot . on ( "health" , function ( ) {
66- socket . emit ( "hp" , bot . health ) ;
67- socket . emit ( "food" , bot . food ) ;
68- } ) ;
69- bot . on ( "spawn" , function ( ) {
70- socket . emit ( "spawn" , bot . entity . yaw , bot . entity . pitch ) ;
71- } ) ;
72- bot . on ( "kicked" , function ( reason ) {
73- socket . emit ( "kicked" , reason ) ;
74- } ) ;
75- bot . on ( "message" , function ( msg ) {
76- socket . emit ( "msg" , convert . toHtml ( msg . toAnsi ( ) ) ) ;
77- } ) ;
78- bot . on ( "experience" , function ( ) {
79- socket . emit ( "xp" , bot . experience ) ;
80- } ) ;
81- bot . on ( "blockUpdate" , function ( oldb , newb ) {
82- socket . emit ( "blockUpdate" , [
83- newb . position . x ,
84- newb . position . y ,
85- newb . position . z ,
86- newb . stateId ,
87- ] ) ;
88- } ) ;
89- bot . on ( "diggingCompleted" , function ( block ) {
90- socket . emit ( "diggingCompleted" , block ) ;
91- } ) ;
92- bot . on ( "diggingAborted" , function ( block ) {
93- socket . emit ( "diggingAborted" , block ) ;
94- } ) ;
95- var inv = "" ;
96- var interval = setInterval ( function ( ) {
97- var inv_new = JSON . stringify ( bot . inventory . slots ) ;
98- if ( inv !== inv_new ) {
99- inv = inv_new ;
100- socket . emit ( "inventory" , bot . inventory . slots ) ;
101- }
102- var entities = {
103- mobs : [ ] ,
104- players : [ ] ,
105- } ;
106- for ( var k in bot . entities ) {
107- var v = bot . entities [ k ] ;
108- if ( v . type === "mob" ) {
109- entities . mobs . push ( [ v . position . x , v . position . y , v . position . z ] ) ;
49+ botByNick [ query . nick ] = bot ;
50+ bot . once ( "spawn" , function ( ) {
51+ bot . _client . on ( "map_chunk" , function ( packet ) {
52+ var cell = new Chunk ( ) ;
53+ cell . load ( packet . chunkData , packet . bitMap , true , true ) ;
54+ socket . emit ( "mapChunk" , cell . sections , packet . x , packet . z ) ;
55+ } ) ;
56+ bot . _client . on ( "respawn" , function ( packet ) {
57+ socket . emit ( "dimension" , packet . dimension . value . effects . value ) ;
58+ } ) ;
59+ bot . on ( "heldItemChanged" , function ( item ) {
60+ heldItem = item ;
61+ } ) ;
62+ bot . on ( "login" , function ( ) {
63+ socket . emit ( "dimension" , bot . game . dimension ) ;
64+ } ) ;
65+ bot . on ( "move" , function ( ) {
66+ socket . emit ( "move" , bot . entity . position ) ;
67+ } ) ;
68+ bot . on ( "health" , function ( ) {
69+ socket . emit ( "hp" , bot . health ) ;
70+ socket . emit ( "food" , bot . food ) ;
71+ } ) ;
72+ bot . on ( "spawn" , function ( ) {
73+ socket . emit ( "spawn" , bot . entity . yaw , bot . entity . pitch ) ;
74+ } ) ;
75+ bot . on ( "kicked" , function ( reason ) {
76+ socket . emit ( "kicked" , reason ) ;
77+ } ) ;
78+ bot . on ( "message" , function ( msg ) {
79+ socket . emit ( "msg" , convert . toHtml ( msg . toAnsi ( ) ) ) ;
80+ } ) ;
81+ bot . on ( "experience" , function ( ) {
82+ socket . emit ( "xp" , bot . experience ) ;
83+ } ) ;
84+ bot . on ( "blockUpdate" , function ( oldb , newb ) {
85+ socket . emit ( "blockUpdate" , [
86+ newb . position . x ,
87+ newb . position . y ,
88+ newb . position . z ,
89+ newb . stateId ,
90+ ] ) ;
91+ } ) ;
92+ bot . on ( "diggingCompleted" , function ( block ) {
93+ socket . emit ( "diggingCompleted" , block ) ;
94+ } ) ;
95+ bot . on ( "diggingAborted" , function ( block ) {
96+ socket . emit ( "diggingAborted" , block ) ;
97+ } ) ;
98+ var inv = "" ;
99+ var interval = setInterval ( function ( ) {
100+ var inv_new = JSON . stringify ( bot . inventory . slots ) ;
101+ if ( inv !== inv_new ) {
102+ inv = inv_new ;
103+ socket . emit ( "inventory" , bot . inventory . slots ) ;
110104 }
111- if ( v . type === "player" ) {
112- entities . players . push ( [
113- v . username ,
114- v . position . x ,
115- v . position . y ,
116- v . position . z ,
117- ] ) ;
105+ var entities = {
106+ mobs : [ ] ,
107+ players : [ ] ,
108+ } ;
109+ for ( var k in bot . entities ) {
110+ var v = bot . entities [ k ] ;
111+ if ( v . type === "mob" ) {
112+ entities . mobs . push ( [
113+ v . position . x ,
114+ v . position . y ,
115+ v . position . z ,
116+ ] ) ;
117+ }
118+ if ( v . type === "player" ) {
119+ entities . players . push ( [
120+ v . username ,
121+ v . position . x ,
122+ v . position . y ,
123+ v . position . z ,
124+ ] ) ;
125+ }
118126 }
119- }
120- socket . emit ( "entities" , entities ) ;
121- } , 10 ) ;
122- socket . on ( "fly" , function ( toggle ) {
123- if ( toggle ) {
124- bot . creative . startFlying ( ) ;
125- } else {
126- bot . creative . stopFlying ( ) ;
127- }
128- } ) ;
129- socket . on ( "blockPlace" , function ( pos , vec ) {
130- var block = bot . blockAt ( new vec3 ( ...pos ) ) ;
131- if ( heldItem !== void 0 && heldItem !== null ) {
132- console . log ( heldItem ) ;
133- bot . placeBlock ( block , new vec3 ( ...vec ) , function ( r ) {
134- console . log ( r ) ;
135- } ) ;
136- }
137- } ) ;
138- socket . on ( "invc" , function ( num ) {
139- var item = bot . inventory . slots [ num + 36 ] ;
140- if ( item !== null && item !== void 0 ) {
141- bot . equip ( item , "hand" ) ;
142- } else if ( heldItem !== void 0 ) {
143- bot . unequip ( "hand" ) ;
144- }
145- } ) ;
146- socket . on ( "move" , function ( state , toggle ) {
147- if ( state === "right" ) {
148- state = "left" ;
149- } else if ( state === "left" ) {
150- state = "right" ;
151- }
152- bot . setControlState ( state , toggle ) ;
153- } ) ;
154- socket . on ( "command" , function ( com ) {
155- bot . chat ( com ) ;
156- } ) ;
157- socket . on ( "rotate" , function ( data ) {
158- bot . look ( ...data ) ;
159- } ) ;
160- socket . on ( "disconnect" , function ( ) {
161- try {
162- clearInterval ( interval ) ;
163- console . log ( `[\x1b[31m-\x1b[0m] ${ query . nick } ` ) ;
164- bot . end ( ) ;
165- } catch ( error ) { }
166- } ) ;
167- socket . on ( "dig" , function ( pos ) {
168- var block = bot . blockAt ( vec3 ( pos [ 0 ] , pos [ 1 ] - 16 , pos [ 2 ] ) ) ;
169- if ( block !== null ) {
170- var digTime = bot . digTime ( block ) ;
171- if ( bot . targetDigBlock !== null ) {
172- console . log ( "Already digging..." ) ;
173- bot . stopDigging ( ) ;
127+ socket . emit ( "entities" , entities ) ;
128+ } , 10 ) ;
129+ socket . on ( "fly" , function ( toggle ) {
130+ if ( toggle ) {
131+ bot . creative . startFlying ( ) ;
132+ } else {
133+ bot . creative . stopFlying ( ) ;
134+ }
135+ } ) ;
136+ socket . on ( "blockPlace" , function ( pos , vec ) {
137+ var block = bot . blockAt ( new vec3 ( ...pos ) ) ;
138+ if ( heldItem !== void 0 && heldItem !== null ) {
139+ console . log ( heldItem ) ;
140+ bot . placeBlock ( block , new vec3 ( ...vec ) , function ( r ) {
141+ console . log ( r ) ;
142+ } ) ;
143+ }
144+ } ) ;
145+ socket . on ( "invc" , function ( num ) {
146+ var item = bot . inventory . slots [ num + 36 ] ;
147+ if ( item !== null && item !== void 0 ) {
148+ bot . equip ( item , "hand" ) ;
149+ } else if ( heldItem !== void 0 ) {
150+ bot . unequip ( "hand" ) ;
174151 }
175- socket . emit ( "digTime" , digTime , block ) ;
176- console . log ( "Start" ) ;
177- bot . dig ( block , false , function ( xd ) {
178- if ( xd === void 0 ) {
179- return console . log ( "SUCCESS" ) ;
180- } else {
181- return console . log ( "FAIL" ) ;
152+ } ) ;
153+ socket . on ( "move" , function ( state , toggle ) {
154+ if ( state === "right" ) {
155+ state = "left" ;
156+ } else if ( state === "left" ) {
157+ state = "right" ;
158+ }
159+ bot . setControlState ( state , toggle ) ;
160+ } ) ;
161+ socket . on ( "command" , function ( com ) {
162+ bot . chat ( com ) ;
163+ } ) ;
164+ socket . on ( "rotate" , function ( data ) {
165+ bot . look ( ...data ) ;
166+ } ) ;
167+ socket . on ( "disconnect" , function ( ) {
168+ try {
169+ clearInterval ( interval ) ;
170+ console . log ( `[\x1b[31m-\x1b[0m] ${ query . nick } ` ) ;
171+ bot . end ( ) ;
172+ } catch ( error ) { }
173+ } ) ;
174+ socket . on ( "dig" , function ( pos ) {
175+ var block = bot . blockAt ( vec3 ( pos [ 0 ] , pos [ 1 ] - 16 , pos [ 2 ] ) ) ;
176+ if ( block !== null ) {
177+ var digTime = bot . digTime ( block ) ;
178+ if ( bot . targetDigBlock !== null ) {
179+ console . log ( "Already digging..." ) ;
180+ bot . stopDigging ( ) ;
182181 }
183- } ) ;
184- }
185- } ) ;
186- socket . emit ( "stopDigging" , function ( ) {
187- bot . stopDigging ( ) ;
182+ socket . emit ( "digTime" , digTime , block ) ;
183+ console . log ( "Start" ) ;
184+ bot . dig ( block , false , function ( xd ) {
185+ if ( xd === void 0 ) {
186+ return console . log ( "SUCCESS" ) ;
187+ } else {
188+ return console . log ( "FAIL" ) ;
189+ }
190+ } ) ;
191+ }
192+ } ) ;
193+ socket . emit ( "stopDigging" , function ( ) {
194+ bot . stopDigging ( ) ;
195+ } ) ;
188196 } ) ;
189197} ) ;
0 commit comments