-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_server_config.go
71 lines (60 loc) · 1.73 KB
/
handler_server_config.go
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
package kite
import (
"bytes"
"fmt"
buffer2 "github.com/mworzala/kite/internal/pkg/buffer"
"github.com/mworzala/kite/pkg/proto"
"github.com/mworzala/kite/pkg/proto/binary"
"github.com/mworzala/kite/pkg/proto/packet"
)
var _ proto.Handler = (*ServerConfigHandler[any])(nil)
type ServerConfigHandler[T any] struct {
Player *Player[T]
PlayHandlerFunc func(*Player[T]) proto.Handler
}
func NewServerConfigHandler[T any](p *Player[T]) proto.Handler {
return &ServerConfigHandler[T]{
Player: p,
}
}
func (h *ServerConfigHandler[T]) HandlePacket(pp proto.Packet) (err error) {
switch pp.Id {
case packet.ServerConfigPluginMessageID:
p := new(packet.ServerPluginMessage)
if err = pp.Read(p); err != nil {
return err
}
return h.HandlePluginMessage(p)
case packet.ServerConfigFinishConfigurationID:
println("server moving to play")
if h.PlayHandlerFunc != nil {
h.Player.Server().SetState(packet.Play, h.PlayHandlerFunc(h.Player))
} else {
h.Player.Server().SetState(packet.Play, NewServerPlayHandler(h.Player))
}
return proto.Forward
default:
println("unhandled config packet from server", pp.Id)
return proto.Forward
}
}
func (h *ServerConfigHandler[T]) HandlePluginMessage(p *packet.ServerPluginMessage) error {
if p.Channel == "minecraft:brand" {
oldPayload := buffer2.NewPacketBuffer(p.Data)
oldBrand, err := binary.ReadSizedString(oldPayload, 32767)
if err != nil {
return err
}
var newPayload bytes.Buffer
err = binary.WriteSizedString(&newPayload, fmt.Sprintf("%s // %s", oldBrand, "kite"), 32767)
if err != nil {
return err
}
resp := &packet.ServerPluginMessage{
Channel: "minecraft:brand",
Data: newPayload.Bytes(),
}
return h.Player.SendPacket(resp)
}
return proto.Forward
}