-
Notifications
You must be signed in to change notification settings - Fork 68
/
mineflayer.js
91 lines (73 loc) · 2.48 KB
/
mineflayer.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
const EventEmitter = require('events')
const { WorldView } = require('../viewer')
module.exports = (bot, { viewDistance = 6, firstPerson = false, port = 3000, prefix = '' }) => {
const express = require('express')
const app = express()
const http = require('http').createServer(app)
const io = require('socket.io')(http, { path: prefix + '/socket.io' })
const { setupRoutes } = require('./common')
setupRoutes(app, prefix)
const sockets = []
const primitives = {}
bot.viewer = new EventEmitter()
bot.viewer.erase = (id) => {
delete primitives[id]
for (const socket of sockets) {
socket.emit('primitive', { id })
}
}
bot.viewer.drawBoxGrid = (id, start, end, color = 'aqua') => {
primitives[id] = { type: 'boxgrid', id, start, end, color }
for (const socket of sockets) {
socket.emit('primitive', primitives[id])
}
}
bot.viewer.drawLine = (id, points, color = 0xff0000) => {
primitives[id] = { type: 'line', id, points, color }
for (const socket of sockets) {
socket.emit('primitive', primitives[id])
}
}
bot.viewer.drawPoints = (id, points, color = 0xff0000, size = 5) => {
primitives[id] = { type: 'points', id, points, color, size }
for (const socket of sockets) {
socket.emit('primitive', primitives[id])
}
}
io.on('connection', (socket) => {
socket.emit('version', bot.version)
sockets.push(socket)
const worldView = new WorldView(bot.world, viewDistance, bot.entity.position, socket)
worldView.init(bot.entity.position)
worldView.on('blockClicked', (block, face, button) => {
bot.viewer.emit('blockClicked', block, face, button)
})
for (const id in primitives) {
socket.emit('primitive', primitives[id])
}
function botPosition () {
const packet = { pos: bot.entity.position, yaw: bot.entity.yaw, addMesh: true }
if (firstPerson) {
packet.pitch = bot.entity.pitch
}
socket.emit('position', packet)
worldView.updatePosition(bot.entity.position)
}
bot.on('move', botPosition)
worldView.listenToBot(bot)
socket.on('disconnect', () => {
bot.removeListener('move', botPosition)
worldView.removeListenersFromBot(bot)
sockets.splice(sockets.indexOf(socket), 1)
})
})
http.listen(port, () => {
console.log(`Prismarine viewer web server running on *:${port}`)
})
bot.viewer.close = () => {
http.close()
for (const socket of sockets) {
socket.disconnect()
}
}
}