1- const version = "1.16.5" ;
21const opn = require ( "open" ) ;
32const express = require ( "express" ) ;
43const app = express ( ) ;
5- const server = require ( "http" ) . createServer ( app ) ;
6- const mineflayer = require ( "mineflayer" ) ;
7- const Chunk = require ( "prismarine-chunk" ) ( version ) ;
8- const vec3 = require ( "vec3" ) ;
9- const Convert = require ( "ansi-to-html" ) ;
10- const convert = new Convert ( ) ;
114const helmet = require ( "helmet" ) ;
125const compression = require ( "compression" ) ;
13- const WebSocket = require ( "ws" ) ;
14- const { encode, decode } = require ( "@msgpack/msgpack" ) ;
156const port = process . env . PORT || 8080 ;
16-
7+ var netApi = require ( "net-browserify" ) ;
178app . use (
189 helmet ( {
1910 contentSecurityPolicy : false ,
2011 } )
2112) ;
13+ app . use ( netApi ( { allowOrigin : "*" } ) ) ;
2214app . use ( compression ( ) ) ;
2315
24- const wss = new WebSocket . Server ( {
25- server,
26- clientTracking : false ,
27- } ) ;
28-
2916const mode = process . argv [ 2 ] ;
3017if ( mode === "production" ) {
3118 app . use ( express . static ( `${ __dirname } /src/dist` ) ) ;
@@ -38,206 +25,7 @@ if (mode === "production") {
3825} else {
3926 console . log ( "Incorrect mode!" ) ;
4027}
41- server . listen ( port , function ( ) {
28+ app . listen ( port , function ( ) {
4229 opn ( `http://localhost:${ port } ` ) ;
4330 return console . log ( `Server is running on \x1b[34m*:${ port } \x1b[0m` ) ;
4431} ) ;
45-
46- const botByNick = new Map ( ) ;
47-
48- wss . on ( "connection" , ( socket , req ) => {
49- const query = new URLSearchParams ( req . url . substr ( 2 , req . url . length ) ) ;
50- const emit = ( type , ...data ) => {
51- socket . send ( encode ( [ type , ...data ] ) ) ;
52- } ;
53-
54- if ( botByNick . get ( query . get ( "nick" ) ) !== undefined ) {
55- emit ( "alreadyPlaying" ) ;
56- return ;
57- }
58- console . log ( `[\x1b[32m+\x1b[0m] ${ query . get ( "nick" ) } ` ) ;
59- let heldItem = null ;
60- const bot = mineflayer . createBot ( {
61- host : query . get ( "server" ) ,
62- port : query . get ( "port" ) !== "null" ? query . get ( "port" ) : null ,
63- username : query . get ( "nick" ) ,
64- version : version ,
65- password :
66- query . get ( "premium" ) === "true" ? query . get ( "password" ) : undefined ,
67- } ) ;
68- botByNick . set ( query . get ( "nick" ) , bot ) ;
69- bot . _client . on ( "map_chunk" , function ( packet ) {
70- const cell = new Chunk ( ) ;
71- cell . load ( packet . chunkData , packet . bitMap , true , true ) ;
72- emit ( "mapChunk" , cell . sections , packet . x , packet . z ) ;
73- } ) ;
74- bot . _client . on ( "respawn" , function ( packet ) {
75- emit ( "dimension" , packet . dimension . value . effects . value ) ;
76- } ) ;
77- bot . on ( "heldItemChanged" , function ( item ) {
78- heldItem = item ;
79- } ) ;
80- bot . on ( "login" , function ( ) {
81- emit ( "dimension" , bot . game . dimension ) ;
82- } ) ;
83- bot . on ( "move" , function ( ) {
84- emit ( "move" , bot . entity . position ) ;
85- } ) ;
86- bot . on ( "health" , function ( ) {
87- emit ( "hp" , bot . health ) ;
88- emit ( "food" , bot . food ) ;
89- } ) ;
90- bot . on ( "spawn" , function ( ) {
91- emit ( "spawn" , bot . entity . yaw , bot . entity . pitch ) ;
92- } ) ;
93- bot . on ( "kicked" , function ( reason ) {
94- emit ( "kicked" , reason ) ;
95- } ) ;
96- bot . on ( "message" , function ( msg ) {
97- let message = msg . toAnsi ( ) ;
98-
99- const replacements = [
100- [ / & / g, "&" ] ,
101- [ / < / g, "<" ] ,
102- [ / > / g, ">" ] ,
103- [ / " / g, """ ] ,
104- ] ;
105- for ( const replacement of replacements )
106- message = message . replace ( replacement [ 0 ] , replacement [ 1 ] ) ;
107-
108- emit ( "msg" , convert . toHtml ( message ) ) ;
109- } ) ;
110- bot . on ( "experience" , function ( ) {
111- emit ( "xp" , bot . experience ) ;
112- } ) ;
113- bot . on ( "blockUpdate" , function ( oldb , newb ) {
114- emit ( "blockUpdate" , [
115- newb . position . x ,
116- newb . position . y ,
117- newb . position . z ,
118- newb . stateId ,
119- ] ) ;
120- } ) ;
121- bot . on ( "diggingCompleted" , function ( block ) {
122- emit ( "diggingCompleted" , block ) ;
123- } ) ;
124- bot . on ( "diggingAborted" , function ( block ) {
125- emit ( "diggingAborted" , block ) ;
126- } ) ;
127- bot . on ( "game" , function ( ) {
128- emit ( "game" , bot . game ) ;
129- } ) ;
130- let inv = "" ;
131- const interval = setInterval ( function ( ) {
132- const inv_new = JSON . stringify ( bot . inventory . slots ) ;
133- if ( inv !== inv_new ) {
134- inv = inv_new ;
135- emit ( "inventory" , bot . inventory . slots ) ;
136- }
137- let entities = {
138- mobs : [ ] ,
139- players : [ ] ,
140- } ;
141- for ( let k in bot . entities ) {
142- const v = bot . entities [ k ] ;
143- if ( v . type === "mob" ) {
144- entities . mobs . push ( [ v . position . x , v . position . y , v . position . z ] ) ;
145- }
146- if ( v . type === "player" ) {
147- entities . players . push ( [
148- v . username ,
149- v . position . x ,
150- v . position . y ,
151- v . position . z ,
152- ] ) ;
153- }
154- }
155- emit ( "entities" , entities ) ;
156- } , 100 ) ;
157-
158- const handlers = new Map ( ) ;
159-
160- bot . once ( "spawn" , function ( ) {
161- handlers . set ( "fly" , function ( toggle ) {
162- if ( toggle ) {
163- bot . creative . startFlying ( ) ;
164- } else {
165- bot . creative . stopFlying ( ) ;
166- }
167- } ) ;
168- handlers . set ( "blockPlace" , function ( pos , vec ) {
169- const block = bot . blockAt ( new vec3 ( ...pos ) ) ;
170- if ( heldItem !== void 0 && heldItem !== null ) {
171- console . log ( heldItem ) ;
172- bot . placeBlock ( block , new vec3 ( ...vec ) , function ( r ) {
173- console . log ( r ) ;
174- } ) ;
175- }
176- } ) ;
177- handlers . set ( "invc" , function ( num ) {
178- const item = bot . inventory . slots [ num + 36 ] ;
179- if ( item !== null && item !== void 0 ) {
180- bot . equip ( item , "hand" ) ;
181- } else if ( heldItem !== void 0 ) {
182- bot . unequip ( "hand" ) ;
183- }
184- } ) ;
185- handlers . set ( "move" , function ( state , toggle ) {
186- if ( state === "right" ) {
187- state = "left" ;
188- } else if ( state === "left" ) {
189- state = "right" ;
190- }
191- bot . setControlState ( state , toggle ) ;
192- } ) ;
193- handlers . set ( "command" , function ( com ) {
194- bot . chat ( com ) ;
195- } ) ;
196- handlers . set ( "rotate" , function ( data ) {
197- bot . look ( ...data ) ;
198- } ) ;
199- handlers . set ( "dig" , function ( pos ) {
200- const block = bot . blockAt ( vec3 ( pos [ 0 ] , pos [ 1 ] - 16 , pos [ 2 ] ) ) ;
201- if ( block !== null ) {
202- const digTime = bot . digTime ( block ) ;
203- if ( bot . targetDigBlock !== null ) {
204- console . log ( "Already digging..." ) ;
205- bot . stopDigging ( ) ;
206- }
207- emit ( "digTime" , digTime , block ) ;
208- console . log ( "Start" ) ;
209- bot . dig ( block , false , function ( xd ) {
210- if ( xd === void 0 ) {
211- return console . log ( "SUCCESS" ) ;
212- } else {
213- return console . log ( "FAIL" ) ;
214- }
215- } ) ;
216- }
217- } ) ;
218- handlers . set ( "stopDigging" , function ( ) {
219- bot . stopDigging ( ) ;
220- } ) ;
221-
222- socket . on ( "close" , ( ) => {
223- try {
224- clearInterval ( interval ) ;
225- console . log ( `[\x1b[31m-\x1b[0m] ${ query . get ( "nick" ) } ` ) ;
226- botByNick . delete ( query . get ( "nick" ) ) ;
227- bot . end ( ) ;
228- } catch ( error ) { }
229- } ) ;
230-
231- socket . on ( "message" , ( message ) => {
232- try {
233- const [ type , ...data ] = decode ( message ) ;
234-
235- const handler = handlers . get ( type ) ;
236-
237- handler ( ...data ) ;
238- } catch ( err ) {
239- console . log ( err ) ;
240- }
241- } ) ;
242- } ) ;
243- } ) ;
0 commit comments