add player package with key client-side player functionality, update protocol codecs, and refactor metadata definitions and slot usage
This commit is contained in:
42
pkg/client/handler.go
Normal file
42
pkg/client/handler.go
Normal file
@ -0,0 +1,42 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.konjactw.dev/patyhank/minego/pkg/bot"
|
||||
"git.konjactw.dev/patyhank/minego/pkg/protocol/packet/game/client"
|
||||
"github.com/Tnze/go-mc/data/packetid"
|
||||
)
|
||||
|
||||
func newPacketHandler() bot.PacketHandler {
|
||||
return &packetHandler{
|
||||
handlerMap: make(map[packetid.ClientboundPacketID][]func(ctx context.Context, p client.ClientboundPacket)),
|
||||
}
|
||||
}
|
||||
|
||||
type packetHandler struct {
|
||||
handlerMap map[packetid.ClientboundPacketID][]func(ctx context.Context, p client.ClientboundPacket)
|
||||
genericMap []func(ctx context.Context, p client.ClientboundPacket)
|
||||
}
|
||||
|
||||
func (ph *packetHandler) AddPacketHandler(id packetid.ClientboundPacketID, handler func(ctx context.Context, p client.ClientboundPacket)) {
|
||||
f := ph.handlerMap[id]
|
||||
f = append(f, handler)
|
||||
ph.handlerMap[id] = f
|
||||
}
|
||||
|
||||
func (ph *packetHandler) AddGenericPacketHandler(handler func(ctx context.Context, p client.ClientboundPacket)) {
|
||||
ph.genericMap = append(ph.genericMap, handler)
|
||||
}
|
||||
|
||||
func (ph *packetHandler) HandlePacket(ctx context.Context, p client.ClientboundPacket) {
|
||||
f := ph.handlerMap[p.PacketID()]
|
||||
if f != nil {
|
||||
for _, handler := range f {
|
||||
handler(ctx, p)
|
||||
}
|
||||
}
|
||||
for _, handler := range ph.genericMap {
|
||||
handler(ctx, p)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user