Skip to content

Commit 6622155

Browse files
committed
Fix "invalid item in equip" and "must be holding block" errors
1 parent c9422f4 commit 6622155

File tree

1 file changed

+26
-45
lines changed

1 file changed

+26
-45
lines changed

src/index.coffee

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
module.exports=(mode)->
2-
#biblioteki
32
opn=require "opn"
43
fs=require "fs"
54
config=JSON.parse fs.readFileSync "#{__dirname}/server.json"
6-
http=require "http"
7-
express=require 'express'
8-
app=express()
9-
server=http.createServer(app)
5+
app=require("express")()
6+
server=require("http").createServer(app)
107
io=require("socket.io")(server)
11-
mineflayer = require 'mineflayer'
8+
mineflayer = require "mineflayer"
129
Chunk = require("prismarine-chunk")(config.version)
1310
vec3=require "vec3"
14-
Convert = require 'ansi-to-html'
11+
Convert = require "ansi-to-html"
1512
convert = new Convert()
16-
#początkowe zmienne
13+
1714
sf={}
1815
socketInfo={}
19-
port=process.env.PORT || 8080
16+
port=process.env.PORT or 8080
2017
if mode is "production"
2118
app.use express.static "#{__dirname}/client/dist"
2219
else
@@ -26,19 +23,16 @@ module.exports=(mode)->
2623
compiler = webpack devconfig
2724
app.use middleware(compiler)
2825

29-
#Konfiguracja serwera express
3026
server.listen port,()->
3127
opn "http://localhost:#{port}"
3228
console.log "Server is running on \x1b[34m*:#{port}\x1b[0m"
3329

34-
#websocket
3530
io.sockets.on "connection", (socket)->
3631
socketInfo[socket.id]={}
3732
bot=socketInfo[socket.id]
3833
socket.on "initClient",(data)->
39-
console.log "[\x1b[32m+\x1b[0m] "+data.nick
34+
console.log "[\x1b[32m+\x1b[0m] #{data.nick}"
4035

41-
#Dodawanie informacji o graczu do socketInfo
4236
socketInfo[socket.id]=data
4337
socketInfo[socket.id].bot=mineflayer.createBot {
4438
host: config.ip
@@ -56,24 +50,23 @@ module.exports=(mode)->
5650
emit=(array)->
5751
io.to(socket.id).emit array...
5852

59-
60-
#Eventy otrzymywane z serwera minecraftowego
61-
war=true
6253
bot()._client.on "map_chunk",(packet)->
6354
cell=new Chunk()
6455
cell.load packet.chunkData,packet.bitMap,true,true
65-
for i in [0..255]
66-
light=cell.getBlockLight 0, i, 0
67-
if light isnt 0
68-
console.log light
69-
break
70-
# emit ["dimension",bot().game.dimension]
56+
# for i in [0..255]
57+
# light=cell.getBlockLight 0, i, 0
58+
# if light isnt 0
59+
# console.log light
60+
# break
7161
emit ["mapChunk", cell.sections,packet.x,packet.z,packet.biomes]
7262
return
7363
bot()._client.on "respawn",(packet)->
7464
emit ["dimension",packet.dimension.value.effects.value]
7565
return
76-
botEventMap={
66+
botEventMap=
67+
"heldItemChanged":(item)->
68+
socketInfo[socket.id].held=item
69+
return
7770
"login":()->
7871
emit ["dimension",bot().game.dimension]
7972
return
@@ -85,13 +78,6 @@ module.exports=(mode)->
8578
emit ["food",bot().food]
8679
return
8780
"spawn":()->
88-
# diamond=bot().inventory.slots[36]
89-
# ac=bot().inventory.slots[37]
90-
# console.log diamond,ac
91-
# bot().equip ac,"hand"
92-
# bot().heldItem.slot=37
93-
# console.log bot().updateHeldItem()
94-
# console.log bot().heldItem
9581
emit ["spawn",bot().entity.yaw,bot().entity.pitch]
9682
return
9783
"kicked":(reason,loggedIn)->
@@ -112,7 +98,6 @@ module.exports=(mode)->
11298
"diggingAborted":(block)->
11399
emit ["diggingAborted",block]
114100
return
115-
}
116101
for i of botEventMap
117102
((i)->
118103
socketInfo[socket.id].bot.on i, ()->
@@ -133,29 +118,26 @@ module.exports=(mode)->
133118
emit ["entities",entities]
134119
return
135120
,10
136-
socketEventMap={
121+
socketEventMap=
137122
"blockPlace":(pos,vec)->
138123
block=bot().blockAt(new vec3(pos...))
139-
console.log block
140124
vecx=[
141125
[1,0,0]
142126
[-1,0,0]
143127
[0,1,0]
144128
[0,-1,0]
145129
]
146-
bot().placeBlock block,new vec3(vec...),(r)->
147-
console.log r
148-
return
149-
150-
130+
if socketInfo[socket.id].held isnt undefined socketInfo[socket.id].held isnt null
131+
console.log socketInfo[socket.id].held
132+
bot().placeBlock block,new vec3(vec...),(r)->
133+
console.log r
134+
return
151135
return
152136
"invc":(num)->
153137
item=bot().inventory.slots[num+36]
154-
if item isnt null
155-
console.log item
156-
try
157-
bot().equip item,"hand"
158-
else
138+
if item isnt null and item isnt undefined
139+
bot().equip item,"hand"
140+
else if socketInfo[socket.id].held isnt undefined
159141
bot().unequip "hand"
160142
return
161143
"move":(state,toggle)->
@@ -170,7 +152,7 @@ module.exports=(mode)->
170152
"disconnect":()->
171153
try
172154
clearInterval socketInfo[socket.id].int
173-
console.log "[\x1b[31m-\x1b[0m] "+socketInfo[socket.id].nick
155+
console.log "[\x1b[31m-\x1b[0m] #{socketInfo[socket.id].nick}"
174156
socketInfo[socket.id].bot.end()
175157
delete socketInfo[socket.id]
176158
return
@@ -192,7 +174,6 @@ module.exports=(mode)->
192174
"stopDigging":(callback)->
193175
bot().stopDigging()
194176
return
195-
}
196177
for i of socketEventMap
197178
socket.on i,socketEventMap[i]
198179
return

0 commit comments

Comments
 (0)