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
196 lines (187 loc) · 4.99 KB
/
index.coffee
File metadata and controls
196 lines (187 loc) · 4.99 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
195
196
module.exports=(mode)->
#biblioteki
opn=require "opn"
fs=require "fs"
config=JSON.parse fs.readFileSync "#{__dirname}/server.json"
http=require "http"
express=require 'express'
app=express()
server=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()
#początkowe zmienne
sf={}
socketInfo={}
port=process.env.PORT || 8080
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)
#Konfiguracja serwera express
server.listen port,()->
opn "http://localhost:#{port}"
console.log "Server is running on \x1b[34m*:#{port}\x1b[0m"
#websocket
io.sockets.on "connection", (socket)->
socketInfo[socket.id]={}
bot=socketInfo[socket.id]
socket.on "initClient",(data)->
console.log "[\x1b[32m+\x1b[0m] "+data.nick
#Dodawanie informacji o graczu do socketInfo
socketInfo[socket.id]=data
socketInfo[socket.id].bot=mineflayer.createBot {
host: config.ip
port: config.port
username: socketInfo[socket.id].nick
version: config.version
}
bot=()->
if socketInfo[socket.id] isnt undefined
return socketInfo[socket.id].bot
else
return null
emit=(array)->
io.to(socket.id).emit array...
#Eventy otrzymywane z serwera minecraftowego
war=true
bot()._client.on "map_chunk",(packet)->
cell=new Chunk()
cell.load packet.chunkData,packet.bitMap,true,true
for i in [0..255]
light=cell.getBlockLight 0, i, 0
if light isnt 0
console.log light
break
# emit ["dimension",bot().game.dimension]
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={
"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":()->
# diamond=bot().inventory.slots[36]
# ac=bot().inventory.slots[37]
# console.log diamond,ac
# bot().equip ac,"hand"
# bot().heldItem.slot=37
# console.log bot().updateHeldItem()
# console.log bot().heldItem
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)->
socketInfo[socket.id].bot.on i, ()->
if bot() isnt null
botEventMap[i] arguments...
return
)(i)
inv=""
socketInfo[socket.id].int=setInterval ()->
inv_new=JSON.stringify(bot().inventory.slots)
if inv isnt inv_new
inv=inv_new
emit ["inventory",bot().inventory.slots]
emit ["entities",bot().entities]
return
,10
socketEventMap={
"blockPlace":(pos,vec)->
block=bot().blockAt(new vec3(pos...))
console.log block
vecx=[
[1,0,0]
[-1,0,0]
[0,1,0]
[0,-1,0]
]
bot().placeBlock block,new vec3(vec...),(r)->
console.log r
return
return
"invc":(num)->
item=bot().inventory.slots[num+36]
if item isnt null
console.log item
try
bot().equip item,"hand"
else
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 socketInfo[socket.id].int
console.log "[\x1b[31m-\x1b[0m] "+socketInfo[socket.id].nick
socketInfo[socket.id].bot.end()
delete socketInfo[socket.id]
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
return