-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmineflayer-stream.js
105 lines (97 loc) · 3.46 KB
/
mineflayer-stream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict';
var mc = require('./minecraft-protocol-stream')
, hex = require('browser-hex')
, EventEmitter = require('events').EventEmitter
, util = require('util')
, path = require('path')
, plugins = {
bed: require('mineflayer/lib/plugins/bed'),
block_actions: require('mineflayer/lib/plugins/block_actions'),
blocks: require('mineflayer/lib/plugins/blocks'),
chat: require('mineflayer/lib/plugins/chat'),
chest: require('mineflayer/lib/plugins/chest'),
craft: require('mineflayer/lib/plugins/craft'),
creative: require('mineflayer/lib/plugins/creative'),
digging: require('mineflayer/lib/plugins/digging'),
dispenser: require('mineflayer/lib/plugins/dispenser'),
enchantment_table: require('mineflayer/lib/plugins/enchantment_table'),
entities: require('mineflayer/lib/plugins/entities'),
experience: require('mineflayer/lib/plugins/experience'),
game: require('mineflayer/lib/plugins/game'),
health: require('mineflayer/lib/plugins/health'),
inventory: require('mineflayer/lib/plugins/inventory'),
kick: require('mineflayer/lib/plugins/kick'),
physics: require('mineflayer/lib/plugins/physics'),
rain: require('mineflayer/lib/plugins/rain'),
settings: require('mineflayer/lib/plugins/settings'),
simple_inventory: require('mineflayer/lib/plugins/simple_inventory'),
sound: require('mineflayer/lib/plugins/sound'),
spawn_point: require('mineflayer/lib/plugins/spawn_point'),
time: require('mineflayer/lib/plugins/time')
}
, mcData = require('minecraft-data');
var PACKET_DEBUG = false;
if (PACKET_DEBUG) global.hex = hex;
module.exports = {
//vec3: require('vec3'), // not really needed
createBot: createBot,
Block: require('mineflayer').Block,
Location: require('mineflayer').Location,
Biome: require('mineflayer').Biome,
Entity: require('mineflayer').Entity,
Painting: require('mineflayer').Painting,
Item: require('mineflayer').Item,
Recipe: require('mineflayer').Recipe,
windows: require('mineflayer').windows,
Chest: require('mineflayer').Chest,
Furnace: require('mineflayer').Furnace,
Dispenser: require('mineflayer').Dispenser,
EnchantmentTable: require('mineflayer').EnchantmentTable,
blocks: mcData.blocks,
biomes: mcData.biomes,
items: mcData.items,
recipes: mcData.recipes,
instruments: mcData.instruments,
materials: mcData.materials,
};
function createBot(options) {
options.username = options.username || 'Player';
if (!options.stream) throw new Error('createBot requires options.stream');
var bot = new Bot();
bot.connect(options);
return bot;
}
function Bot() {
EventEmitter.call(this);
this._client = null;
}
util.inherits(Bot, EventEmitter);
Bot.prototype.connect = function(options) {
var self = this;
self._client = mc.createClient(options);
self.username = self._client.username;
self._client.on('raw', function(raw) {
if (PACKET_DEBUG) {
console.log('received ',raw.length+' raw bytes');
console.log(hex(raw));
}
});
self._client.on('session', function() {
self.username = self._client.username;
});
self._client.on('connect', function() {
self.emit('connect');
});
self._client.on('error', function(err) {
self.emit('error', err);
});
self._client.on('end', function() {
self.emit('end');
});
for (var pluginName in plugins) {
plugins[pluginName](self, options);
}
};
Bot.prototype.end = function() {
this.client.end();
};