@@ -8,12 +8,13 @@ const Writable = require('stream').Writable;
8
8
const through = require ( 'through' ) ;
9
9
const ndarray = require ( 'ndarray' ) ;
10
10
const vec3Object = require ( 'vec3' ) ; // note: object type used by mineflayer, NOT gl-vec3 which is just a typed array :(
11
+ const isBuffer = require ( 'is-buffer' ) ;
11
12
12
13
module . exports = function ( self ) {
13
14
console . log ( 'mf-worker initializing' , self ) ;
14
15
15
16
self . readStream = ParentStream ( ) . pipe ( toBufferStream ) . pipe ( through ( function write ( event ) {
16
- if ( Buffer . isBuffer ( event ) ) {
17
+ if ( isBuffer ( event ) ) {
17
18
// buffer data passes through to readStream -> duplexStream for bot
18
19
this . queue ( event ) ;
19
20
} else {
@@ -39,56 +40,6 @@ module.exports = function(self) {
39
40
40
41
self . duplexStream = duplexer ( self . writeStream , self . readStream ) ;
41
42
42
- self . bot = mineflayer . createBot ( {
43
- username : 'user1' , // TODO
44
- stream : self . duplexStream ,
45
- noPacketFramer : true
46
- } ) ;
47
-
48
- console . log ( 'mf-worker bot' , self . bot ) ;
49
-
50
- self . bot . on ( 'game' , function ( ) {
51
- console . log ( 'mf-worker spawn position: ' + JSON . stringify ( self . bot . spawnPoint ) ) ;
52
- self . postMessage ( { cmd : 'spawn' , spawnPoint : self . bot . spawnPoint } ) ;
53
- } ) ;
54
-
55
- self . bot . on ( 'kicked' , function ( reason ) {
56
- self . postMessage ( { cmd : 'kicked' , reason : reason } ) ;
57
- } ) ;
58
-
59
- self . bot . on ( 'message' , function ( message ) {
60
- //self.console.logNode(tellraw2dom(message.json)); // TODO: send back to parent
61
- console . log ( 'mf-worker chat message' , message ) ;
62
- self . postMessage ( { cmd : 'chat' , message : message } ) ;
63
- } ) ;
64
-
65
- self . bot . on ( 'error' , function ( err ) {
66
- console . log ( 'WebSocket error' , err ) ;
67
- self . postMessage ( { cmd : 'error' , error : err } ) ;
68
- } ) ;
69
-
70
- self . bot . on ( 'close' , function ( ) {
71
- console . log ( 'WebSocket closed' ) ;
72
- self . postMessage ( { cmd : 'close' } ) ;
73
- } ) ;
74
-
75
- self . bot . on ( 'chunkColumnLoad' , function ( point ) {
76
- self . addColumn ( point ) ;
77
- } ) ;
78
-
79
- let pos = [ 0 , 0 , 0 ] ;
80
- self . bot . on ( 'blockUpdate' , function ( oldBlock , newBlock ) {
81
- console . log ( 'blockUpdate' , oldBlock , newBlock ) ;
82
- const position = newBlock . position ;
83
- pos [ 0 ] = position . x ;
84
- pos [ 1 ] = position . y ;
85
- pos [ 2 ] = position . z ;
86
- const val = self . translateBlockID ( ( newBlock . type << 4 ) | newBlock . metadata ) ;
87
- self . postMessage ( { cmd : 'setBlock' , position : pos , value : val } ) ;
88
- //self.game.setBlock(pos, val);
89
- } ) ;
90
- // TODO: also handle mass block update (event? would like to optimize multi_block_change, but..)
91
-
92
43
// Translate packed MC block ID (lower 4 bits metadata, upper 12 block) to 16-bit voxel ID
93
44
self . translateBlockID = function ( mcPackedID ) {
94
45
let ourBlockID ;
@@ -255,46 +206,6 @@ module.exports = function(self) {
255
206
self . postMessage ( { cmd : 'chunks' , chunks : chunkCache } ) ; // TODO: transferrable
256
207
} ;
257
208
258
- self . bot . on ( 'blockBreakProgressObserved' , function ( block , destroyStage ) {
259
- self . postMessage ( { cmd : 'blockBreakProgressObserved' , position :[ block . position . x , block . position . y , block . position . z ] , destroyStage : destroyStage } ) ;
260
- } ) ;
261
- self . bot . on ( 'blockBreakProgressEnd' , function ( block ) {
262
- self . postMessage ( { cmd : 'blockBreakProgressEnd' , position :[ block . position . x , block . position . y , block . position . z ] } ) ;
263
- } ) ;
264
-
265
-
266
- self . bot . on ( 'move' , function ( ) { // TODO: also support entityMoved, other entities, players, mobs
267
- // player move
268
- self . postMessage ( { cmd : 'move' , position :[ self . bot . entity . position . x , self . bot . entity . position . y , self . bot . entity . position . z ] } ) ;
269
- } ) ;
270
-
271
- self . bot . on ( 'soundEffectHeard' , function ( soundName , position , volume , pitch ) {
272
- //console.log('soundEffectHeard',arguments);
273
- // TODO: event.x,y,z(location?), volume, pitch - 3D sound https://github.com/deathcap/voxel-sfx/issues/3 Positional audio? (voxel-audio)
274
- self . postMessage ( { cmd : 'sound' , soundName :soundName } ) ;
275
- } ) ;
276
-
277
- self . bot . _client . on ( 'held_item_slot' , function ( packet ) { // TODO: this really should be emitted in mineflayer
278
- //packet.slot
279
- const slot = self . bot . quickBarSlot ;
280
- console . log ( 'held_item_slot' , slot ) ;
281
- self . postMessage ( { cmd : 'heldItemSlot' , slot :slot } ) ;
282
- } ) ;
283
-
284
- self . bot . on ( 'setSlot:0' , function ( oldItem , newItem ) { // slot 0 is player inventory
285
- console . log ( 'setSlot' , oldItem , newItem ) ;
286
- if ( ! oldItem && ! newItem ) return ; // TODO: why does mineflayer send this?
287
- self . postMessage ( { cmd : 'setSlot' , oldItem :oldItem , newItem :newItem } ) ;
288
- } ) ;
289
- // TODO: window items packet? for setting multiple slots
290
-
291
- self . bot . _client . on ( 'resource_pack_send' , function ( packet ) { // TODO: mineflayer api
292
- self . postMessage ( { cmd : 'resourcePack' , url :packet . url , hash :packet . hash } ) ;
293
- } ) ;
294
-
295
- // if we exist (the webworker), socket is connected
296
- self . bot . _client . emit ( 'connect' ) ;
297
-
298
209
299
210
// handlers called for main thread
300
211
self . chat = function ( event ) {
@@ -305,6 +216,100 @@ module.exports = function(self) {
305
216
for ( let key in event ) {
306
217
self [ key ] = event [ key ] ;
307
218
}
219
+
220
+ // Now that the main thread has gave us the necessary data (username, ...), initialize the bot
221
+
222
+ self . bot = mineflayer . createBot ( {
223
+ username : self . username ,
224
+ stream : self . duplexStream ,
225
+ noPacketFramer : true
226
+ } ) ;
227
+
228
+ console . log ( 'mf-worker bot' , self . bot ) ;
229
+
230
+ self . bot . on ( 'game' , function ( ) {
231
+ console . log ( 'mf-worker spawn position: ' + JSON . stringify ( self . bot . spawnPoint ) ) ;
232
+ self . postMessage ( { cmd : 'spawn' , spawnPoint : self . bot . spawnPoint } ) ;
233
+ } ) ;
234
+
235
+ self . bot . on ( 'kicked' , function ( reason ) {
236
+ self . postMessage ( { cmd : 'kicked' , reason : reason } ) ;
237
+ } ) ;
238
+
239
+ self . bot . on ( 'message' , function ( message ) {
240
+ //self.console.logNode(tellraw2dom(message.json)); // TODO: send back to parent
241
+ console . log ( 'mf-worker chat message' , message ) ;
242
+ self . postMessage ( { cmd : 'chat' , message : message } ) ;
243
+ } ) ;
244
+
245
+ self . bot . on ( 'error' , function ( err ) {
246
+ console . log ( 'WebSocket error' , err ) ;
247
+ self . postMessage ( { cmd : 'error' , error : err } ) ;
248
+ } ) ;
249
+
250
+ self . bot . on ( 'close' , function ( ) {
251
+ console . log ( 'WebSocket closed' ) ;
252
+ self . postMessage ( { cmd : 'close' } ) ;
253
+ } ) ;
254
+
255
+ self . bot . on ( 'chunkColumnLoad' , function ( point ) {
256
+ self . addColumn ( point ) ;
257
+ } ) ;
258
+
259
+ let pos = [ 0 , 0 , 0 ] ;
260
+ self . bot . on ( 'blockUpdate' , function ( oldBlock , newBlock ) {
261
+ console . log ( 'blockUpdate' , oldBlock , newBlock ) ;
262
+ const position = newBlock . position ;
263
+ pos [ 0 ] = position . x ;
264
+ pos [ 1 ] = position . y ;
265
+ pos [ 2 ] = position . z ;
266
+ const val = self . translateBlockID ( ( newBlock . type << 4 ) | newBlock . metadata ) ;
267
+ self . postMessage ( { cmd : 'setBlock' , position : pos , value : val } ) ;
268
+ //self.game.setBlock(pos, val);
269
+ } ) ;
270
+ // TODO: also handle mass block update (event? would like to optimize multi_block_change, but..)
271
+
272
+ self . bot . on ( 'blockBreakProgressObserved' , function ( block , destroyStage ) {
273
+ self . postMessage ( { cmd : 'blockBreakProgressObserved' , position :[ block . position . x , block . position . y , block . position . z ] , destroyStage : destroyStage } ) ;
274
+ } ) ;
275
+ self . bot . on ( 'blockBreakProgressEnd' , function ( block ) {
276
+ self . postMessage ( { cmd : 'blockBreakProgressEnd' , position :[ block . position . x , block . position . y , block . position . z ] } ) ;
277
+ } ) ;
278
+
279
+
280
+ self . bot . on ( 'move' , function ( ) { // TODO: also support entityMoved, other entities, players, mobs
281
+ // player move
282
+ self . postMessage ( { cmd : 'move' , position :[ self . bot . entity . position . x , self . bot . entity . position . y , self . bot . entity . position . z ] } ) ;
283
+ } ) ;
284
+
285
+ self . bot . on ( 'soundEffectHeard' , function ( soundName , position , volume , pitch ) {
286
+ //console.log('soundEffectHeard',arguments);
287
+ // TODO: event.x,y,z(location?), volume, pitch - 3D sound https://github.com/deathcap/voxel-sfx/issues/3 Positional audio? (voxel-audio)
288
+ self . postMessage ( { cmd : 'sound' , soundName :soundName } ) ;
289
+ } ) ;
290
+
291
+ self . bot . _client . on ( 'held_item_slot' , function ( packet ) { // TODO: this really should be emitted in mineflayer
292
+ //packet.slot
293
+ const slot = self . bot . quickBarSlot ;
294
+ console . log ( 'held_item_slot' , slot ) ;
295
+ self . postMessage ( { cmd : 'heldItemSlot' , slot :slot } ) ;
296
+ } ) ;
297
+
298
+ self . bot . on ( 'setSlot:0' , function ( oldItem , newItem ) { // slot 0 is player inventory
299
+ console . log ( 'setSlot' , oldItem , newItem ) ;
300
+ if ( ! oldItem && ! newItem ) return ; // TODO: why does mineflayer send this?
301
+ self . postMessage ( { cmd : 'setSlot' , oldItem :oldItem , newItem :newItem } ) ;
302
+ } ) ;
303
+ // TODO: window items packet? for setting multiple slots
304
+
305
+ self . bot . _client . on ( 'resource_pack_send' , function ( packet ) { // TODO: mineflayer api
306
+ self . postMessage ( { cmd : 'resourcePack' , url :packet . url , hash :packet . hash } ) ;
307
+ } ) ;
308
+
309
+ // if we exist (the webworker), socket is connected
310
+ self . bot . _client . emit ( 'connect' ) ;
311
+
312
+
308
313
} ;
309
314
310
315
// http://wiki.vg/Protocol#Player_Digging
0 commit comments