Handle ping in bot/basic

This commit is contained in:
SushiLeader
2024-04-25 19:15:41 +08:00
parent 2445ff3f71
commit 032acf0d21
2 changed files with 24 additions and 0 deletions

View File

@ -40,6 +40,7 @@ func NewPlayer(c *bot.Client, settings Settings, events EventsListener) *Player
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundLogin, F: p.handleLoginPacket}, bot.PacketHandler{Priority: 0, ID: packetid.ClientboundLogin, F: p.handleLoginPacket},
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundKeepAlive, F: p.handleKeepAlivePacket}, bot.PacketHandler{Priority: 0, ID: packetid.ClientboundKeepAlive, F: p.handleKeepAlivePacket},
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundRespawn, F: p.handleRespawnPacket}, bot.PacketHandler{Priority: 0, ID: packetid.ClientboundRespawn, F: p.handleRespawnPacket},
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundPing, F: p.handlePingPacket},
) )
events.attach(p) events.attach(p)
return p return p

23
bot/basic/ping.go Normal file
View File

@ -0,0 +1,23 @@
package basic
import (
"github.com/Tnze/go-mc/data/packetid"
pk "github.com/Tnze/go-mc/net/packet"
)
func (p *Player) handlePingPacket(packet pk.Packet) error {
var pingID pk.Int
if err := packet.Scan(&pingID); err != nil {
return Error{err}
}
// Response
err := p.c.Conn.WritePacket(pk.Packet{
ID: int32(packetid.ServerboundPong),
Data: packet.Data,
})
if err != nil {
return Error{err}
}
return nil
}