forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxy.worker.js
More file actions
194 lines (189 loc) · 5.1 KB
/
Proxy.worker.js
File metadata and controls
194 lines (189 loc) · 5.1 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* eslint-env worker */
import vec3 from 'vec3'
import Convert from 'ansi-to-html'
const convert = new Convert()
global.window = self
global.importScripts('../../assets/mineflayer.js')
let bot = null
const emit = (type, ...params) => {
postMessage({ type, params })
}
addEventListener('message', function (e) {
const type = e.data.type
let data = e.data.data
let block, state
let inv = ''
switch (type) {
case 'init':
data = data[0]
bot = self.mineflayer(data.hostname, data.port, {
host: data.server,
port: data.serverPort,
username: data.nick
})
bot.heldItem = null
bot.on('chunkColumnLoad', (p) => {
emit(
'mapChunk',
bot.world.getColumn(p.x / 16, p.z / 16).sections,
p.x / 16,
p.z / 16
)
})
bot._client.on('respawn', function (packet) {
emit('dimension', packet.dimension.value.effects.value)
})
bot.on('heldItemChanged', function (item) {
bot.heldItem = item
})
bot.on('login', function () {
emit('login')
emit('dimension', bot.game.dimension)
})
bot.on('move', function () {
emit('move', bot.entity.position)
})
bot.on('health', function () {
emit('hp', bot.health)
emit('food', bot.food)
})
bot.on('spawn', function () {
emit('spawn', bot.entity.yaw, bot.entity.pitch)
})
bot.on('kicked', function (reason) {
emit('kicked', reason)
})
bot.on('message', function (msg) {
let message = msg.toAnsi()
const replacements = [
[/&/g, '&'],
[/</g, '<'],
[/>/g, '>'],
[/"/g, '"']
]
for (const replacement of replacements) { message = message.replace(replacement[0], replacement[1]) }
emit('msg', convert.toHtml(message))
})
bot.on('experience', function () {
emit('xp', bot.experience)
})
bot.on('blockUpdate', function (oldb, newb) {
emit('blockUpdate', [
newb.position.x,
newb.position.y,
newb.position.z,
newb.stateId
])
})
bot.on('diggingCompleted', function (block) {
emit('diggingCompleted', block)
})
bot.on('diggingAborted', function (block) {
emit('diggingAborted', block)
})
bot.on('game', function () {
emit('game', bot.game)
})
setInterval(function () {
if (bot.inventory !== undefined) {
const invNew = JSON.stringify(bot.inventory.slots)
if (inv !== invNew) {
inv = invNew
emit('inventory', bot.inventory.slots)
}
}
const entities = {
mobs: [],
players: [],
objects: []
}
for (const k in bot.entities) {
const v = bot.entities[k]
if (v.type === 'mob') {
entities.mobs.push([
v.position.x,
v.position.y,
v.position.z
])
} else if (v.type === 'player') {
entities.players.push([
v.username,
v.position.x,
v.position.y,
v.position.z
])
} else if (v.type === 'object') {
entities.objects.push([
v.position.x,
v.position.y,
v.position.z
])
}
}
emit('entities', entities)
}, 10)
break
case 'move':
state = data[0]
if (state === 'right') {
state = 'left'
} else if (state === 'left') {
state = 'right'
}
if (bot.setControlState !== undefined) {
bot.setControlState(state, data[1])
}
break
case 'rotate':
bot.look(data[0][0], data[0][1])
break
case 'drop':
if (bot.heldItem !== null) {
bot.tossStack(bot.heldItem)
}
break
case 'dig':
block = bot.blockAt(vec3(data[0][0], data[0][1] - 16, data[0][2]))
if (block !== null) {
const digTime = bot.digTime(block)
if (bot.targetDigBlock !== null) {
// console.log("Already digging...");
bot.stopDigging()
}
emit('digTime', digTime, block)
// console.log("Start");
bot.dig(block, false)
}
break
case 'stopDigging':
bot.stopDigging()
break
case 'fly':
if (data[0]) {
bot.creative.startFlying()
} else {
bot.creative.stopFlying()
}
break
case 'command':
bot.chat(data[0])
break
case 'invc':
if (bot.inventory !== undefined) {
const item = bot.inventory.slots[data[0] + 36]
if (item !== null && item !== undefined) {
bot.equip(item, 'hand')
} else if (bot.heldItem !== undefined) {
bot.unequip('hand')
}
}
break
case 'blockPlace':
block = bot.blockAt(vec3(...data[0]))
if (bot.heldItem !== undefined && bot.heldItem !== null) {
// console.log(heldItem);
bot.placeBlock(block, vec3(...data[1]))
}
break
}
})