forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.coffee
More file actions
182 lines (174 loc) · 4.42 KB
/
index.coffee
File metadata and controls
182 lines (174 loc) · 4.42 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
module.exports=(mode)->
opn=require "opn"
fs=require "fs"
config=JSON.parse fs.readFileSync "#{__dirname}/server.json"
express=require("express")
app=express()
server=require("http").createServer(app)
io=require("socket.io")(server)
mineflayer = require "mineflayer"
Chunk = require("prismarine-chunk")(config.version)
vec3=require "vec3"
Convert = require "ansi-to-html"
convert = new Convert()
helmet = require "helmet"
port=process.env.PORT or 8080
app.use helmet {
contentSecurityPolicy: false
}
if mode is "production"
app.use express.static "#{__dirname}/client/dist"
else
webpack = require "webpack"
middleware = require "webpack-dev-middleware"
devconfig=require "#{__dirname}/client/webpack.dev.coffee"
compiler = webpack devconfig
app.use middleware compiler
server.listen port,()->
opn "http://localhost:#{port}"
console.log "Server is running on \x1b[34m*:#{port}\x1b[0m"
io.sockets.on "connection", (socket)->
query=socket.handshake.query
console.log "[\x1b[32m+\x1b[0m] #{query.nick}"
heldItem=null
bot=mineflayer.createBot {
host: config.ip
port: config.port
username: query.nick
version: config.version
}
emit=(array)->
io.to(socket.id).emit array...
bot._client.on "map_chunk",(packet)->
cell=new Chunk()
cell.load packet.chunkData,packet.bitMap,true,true
emit ["mapChunk", cell.sections,packet.x,packet.z,packet.biomes]
return
bot._client.on "respawn",(packet)->
emit ["dimension",packet.dimension.value.effects.value]
return
botEventMap=
"heldItemChanged":(item)->
heldItem=item
return
"login":()->
emit ["dimension",bot.game.dimension]
return
"move":()->
emit ["move",bot.entity.position]
return
"health":()->
emit ["hp",bot.health]
emit ["food",bot.food]
return
"spawn":()->
emit ["spawn",bot.entity.yaw,bot.entity.pitch]
return
"kicked":(reason,loggedIn)->
emit ["kicked",reason]
return
"message":(msg)->
emit ["msg",convert.toHtml(msg.toAnsi())]
return
"experience":()->
emit ["xp",bot.experience]
return
"blockUpdate":(oldb,newb)->
emit ["blockUpdate",[newb.position.x,newb.position.y,newb.position.z,newb.stateId]]
return
"diggingCompleted":(block)->
emit ["diggingCompleted",block]
return
"diggingAborted":(block)->
emit ["diggingAborted",block]
return
for i of botEventMap
((i)->
bot.on i, ()->
if bot isnt null
botEventMap[i] arguments...
return
)(i)
inv=""
interval=setInterval ()->
inv_new=JSON.stringify(bot.inventory.slots)
if inv isnt inv_new
inv=inv_new
emit ["inventory",bot.inventory.slots]
entities={
mobs:[]
players:[]
}
for k,v of bot.entities
if v.type is "mob"
entities.mobs.push [v.position.x,v.position.y,v.position.z]
if v.type is "player"
entities.players.push [v.username,v.position.x,v.position.y,v.position.z]
emit ["entities",entities]
return
,10
socketEventMap=
"fly":(toggle)->
if toggle
bot.creative.startFlying()
else
bot.creative.stopFlying()
return
"blockPlace":(pos,vec)->
block=bot.blockAt(new vec3(pos...))
vecx=[
[1,0,0]
[-1,0,0]
[0,1,0]
[0,-1,0]
]
if heldItem isnt undefined and heldItem isnt null
console.log heldItem
bot.placeBlock block,new vec3(vec...),(r)->
console.log r
return
return
"invc":(num)->
item=bot.inventory.slots[num+36]
if item isnt null and item isnt undefined
bot.equip item,"hand"
else if heldItem isnt undefined
bot.unequip "hand"
return
"move":(state,toggle)->
bot.setControlState(state,toggle)
return
"command":(com)->
bot.chat(com)
return
"rotate":(data)->
bot.look data...
return
"disconnect":()->
try
clearInterval interval
console.log "[\x1b[31m-\x1b[0m] #{query.nick}"
bot.end()
return
"dig":(pos)->
block=bot.blockAt(vec3(pos[0],pos[1]-16,pos[2]))
if block isnt null
digTime=bot.digTime(block)
if bot.targetDigBlock isnt null
console.log "Already digging..."
bot.stopDigging()
emit ["digTime",digTime,block]
console.log "Start"
bot.dig block,false,(xd)->
if xd is undefined
console.log "SUCCESS"
else
console.log "FAIL"
return
"stopDigging":(callback)->
bot.stopDigging()
return
for i of socketEventMap
socket.on i,socketEventMap[i]
return
return