Skip to content

Commit 1b300ac

Browse files
committed
remove playerSocket map
1 parent 60ff369 commit 1b300ac

File tree

1 file changed

+41
-49
lines changed

1 file changed

+41
-49
lines changed

src/index.coffee

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ module.exports=(mode)->
1313
convert = new Convert()
1414
helmet = require "helmet"
1515

16-
playerSocket={}
1716
port=process.env.PORT or 8080
1817

1918
app.use helmet()
@@ -34,48 +33,42 @@ module.exports=(mode)->
3433
io.sockets.on "connection", (socket)->
3534
query=socket.handshake.query
3635
console.log "[\x1b[32m+\x1b[0m] #{query.nick}"
36+
heldItem=null
3737

38-
playerSocket[socket.id]=
39-
bot:mineflayer.createBot {
40-
host: config.ip
41-
port: config.port
42-
username: query.nick
43-
version: config.version
44-
}
45-
46-
bot=()->
47-
if playerSocket[socket.id] isnt undefined
48-
return playerSocket[socket.id].bot
49-
else
50-
return null
38+
bot=mineflayer.createBot {
39+
host: config.ip
40+
port: config.port
41+
username: query.nick
42+
version: config.version
43+
}
5144

5245
emit=(array)->
5346
io.to(socket.id).emit array...
5447

55-
bot()._client.on "map_chunk",(packet)->
48+
bot._client.on "map_chunk",(packet)->
5649
cell=new Chunk()
5750
cell.load packet.chunkData,packet.bitMap,true,true
5851
emit ["mapChunk", cell.sections,packet.x,packet.z,packet.biomes]
5952
return
60-
bot()._client.on "respawn",(packet)->
53+
bot._client.on "respawn",(packet)->
6154
emit ["dimension",packet.dimension.value.effects.value]
6255
return
6356
botEventMap=
6457
"heldItemChanged":(item)->
65-
playerSocket[socket.id].held=item
58+
heldItem=item
6659
return
6760
"login":()->
68-
emit ["dimension",bot().game.dimension]
61+
emit ["dimension",bot.game.dimension]
6962
return
7063
"move":()->
71-
emit ["move",bot().entity.position]
64+
emit ["move",bot.entity.position]
7265
return
7366
"health":()->
74-
emit ["hp",bot().health]
75-
emit ["food",bot().food]
67+
emit ["hp",bot.health]
68+
emit ["food",bot.food]
7669
return
7770
"spawn":()->
78-
emit ["spawn",bot().entity.yaw,bot().entity.pitch]
71+
emit ["spawn",bot.entity.yaw,bot.entity.pitch]
7972
return
8073
"kicked":(reason,loggedIn)->
8174
emit ["kicked",reason]
@@ -84,7 +77,7 @@ module.exports=(mode)->
8477
emit ["msg",convert.toHtml(msg.toAnsi())]
8578
return
8679
"experience":()->
87-
emit ["xp",bot().experience]
80+
emit ["xp",bot.experience]
8881
return
8982
"blockUpdate":(oldb,newb)->
9083
emit ["blockUpdate",[newb.position.x,newb.position.y,newb.position.z,newb.stateId]]
@@ -97,79 +90,78 @@ module.exports=(mode)->
9790
return
9891
for i of botEventMap
9992
((i)->
100-
playerSocket[socket.id].bot.on i, ()->
101-
if bot() isnt null
93+
bot.on i, ()->
94+
if bot isnt null
10295
botEventMap[i] arguments...
10396
return
10497
)(i)
10598
inv=""
106-
playerSocket[socket.id].int=setInterval ()->
107-
inv_new=JSON.stringify(bot().inventory.slots)
99+
interval=setInterval ()->
100+
inv_new=JSON.stringify(bot.inventory.slots)
108101
if inv isnt inv_new
109102
inv=inv_new
110-
emit ["inventory",bot().inventory.slots]
103+
emit ["inventory",bot.inventory.slots]
111104
entities=[]
112-
for k,v of bot().entities
105+
for k,v of bot.entities
113106
if v.type is "mob"
114107
entities.push [v.position.x,v.position.y,v.position.z]
115108
emit ["entities",entities]
116109
return
117110
,10
118111
socketEventMap=
119112
"blockPlace":(pos,vec)->
120-
block=bot().blockAt(new vec3(pos...))
113+
block=bot.blockAt(new vec3(pos...))
121114
vecx=[
122115
[1,0,0]
123116
[-1,0,0]
124117
[0,1,0]
125118
[0,-1,0]
126119
]
127-
if playerSocket[socket.id].held isnt undefined and playerSocket[socket.id].held isnt null
128-
console.log playerSocket[socket.id].held
129-
bot().placeBlock block,new vec3(vec...),(r)->
120+
if heldItem isnt undefined and heldItem isnt null
121+
console.log heldItem
122+
bot.placeBlock block,new vec3(vec...),(r)->
130123
console.log r
131124
return
132125
return
133126
"invc":(num)->
134-
item=bot().inventory.slots[num+36]
127+
item=bot.inventory.slots[num+36]
135128
if item isnt null and item isnt undefined
136-
bot().equip item,"hand"
137-
else if playerSocket[socket.id].held isnt undefined
138-
bot().unequip "hand"
129+
bot.equip item,"hand"
130+
else if heldItem isnt undefined
131+
bot.unequip "hand"
139132
return
140133
"move":(state,toggle)->
141-
bot().setControlState(state,toggle)
134+
bot.setControlState(state,toggle)
142135
return
143136
"command":(com)->
144-
bot().chat(com)
137+
bot.chat(com)
145138
return
146139
"rotate":(data)->
147-
bot().look data...
140+
bot.look data...
148141
return
149142
"disconnect":()->
150143
try
151-
clearInterval playerSocket[socket.id].int
144+
clearInterval interval
152145
console.log "[\x1b[31m-\x1b[0m] #{query.nick}"
153-
playerSocket[socket.id].bot.end()
154-
delete playerSocket[socket.id]
146+
bot.end()
155147
return
156148
"dig":(pos)->
157-
block=bot().blockAt(vec3(pos[0],pos[1]-16,pos[2]))
149+
block=bot.blockAt(vec3(pos[0],pos[1]-16,pos[2]))
158150
if block isnt null
159-
digTime=bot().digTime(block)
160-
if bot().targetDigBlock isnt null
151+
digTime=bot.digTime(block)
152+
if bot.targetDigBlock isnt null
161153
console.log "Already digging..."
162-
bot().stopDigging()
154+
bot.stopDigging()
163155
emit ["digTime",digTime,block]
164156
console.log "Start"
165-
bot().dig block,false,(xd)->
157+
bot.dig block,false,(xd)->
166158
if xd is undefined
167159
console.log "SUCCESS"
168160
else
169161
console.log "FAIL"
170162
return
171163
"stopDigging":(callback)->
172-
bot().stopDigging()
164+
bot.stopDigging()
173165
return
174166
for i of socketEventMap
175167
socket.on i,socketEventMap[i]

0 commit comments

Comments
 (0)