Skip to content

Commit

Permalink
add support for EnableFeature and CustomReportDetails
Browse files Browse the repository at this point in the history
  • Loading branch information
Tnze committed Jun 15, 2024
1 parent ee23172 commit 40b32df
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 19 deletions.
15 changes: 11 additions & 4 deletions bot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ type Client struct {
Events Events

// Login plugins
LoginPlugin map[string]func(data []byte) ([]byte, error)
LoginPlugin map[string]CustomPayloadHandler

// Configuration handler
ConfigHandler

CustomReportDetails map[string]string
}

// CustomPayloadHandler is a function handling custom payload
type CustomPayloadHandler func(data []byte) ([]byte, error)

func (c *Client) Close() error {
return c.Conn.Close()
}
Expand All @@ -45,9 +50,11 @@ func (c *Client) Close() error {
// and load your Name, UUID and AccessToken to client.
func NewClient() *Client {
return &Client{
Auth: Auth{Name: "Steve"},
Events: Events{handlers: make([][]PacketHandler, packetid.ClientboundPacketIDGuard)},
ConfigHandler: NewDefaultConfigHandler(),
Auth: Auth{Name: "Steve"},
Events: Events{handlers: make([][]PacketHandler, packetid.ClientboundPacketIDGuard)},
LoginPlugin: make(map[string]CustomPayloadHandler),
ConfigHandler: NewDefaultConfigHandler(),
CustomReportDetails: make(map[string]string),
}
}

Expand Down
67 changes: 52 additions & 15 deletions bot/configuration.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package bot

import (
"fmt"
"bytes"
"io"
"unsafe"

"github.com/Tnze/go-mc/chat"
"github.com/Tnze/go-mc/data/packetid"
Expand All @@ -16,6 +15,8 @@ type ConfigHandler interface {
GetCookie(key pk.Identifier) []byte
SetCookie(key pk.Identifier, payload []byte)

EnableFeature(features []pk.Identifier)

PushResourcePack(res ResourcePack)
PopResourcePack(id pk.UUID)
PopAllResourcePack()
Expand All @@ -32,8 +33,7 @@ type ResourcePack struct {
}

type ConfigData struct {
Registries registry.NetworkCodec
FeatureFlags []string
Registries registry.NetworkCodec
}

type ConfigErr struct {
Expand All @@ -50,11 +50,10 @@ func (l ConfigErr) Unwrap() error {
}

func (c *Client) joinConfiguration(conn *net.Conn) error {
receiving := "config custom payload"
for {
var p pk.Packet
if err := conn.ReadPacket(&p); err != nil {
return ConfigErr{receiving, err}
return ConfigErr{"config custom payload", err}
}

switch packetid.ClientboundPacketID(p.ID) {
Expand All @@ -81,14 +80,24 @@ func (c *Client) joinConfiguration(conn *net.Conn) error {
return ConfigErr{"custom payload", err}
}
// TODO: Provide configuration custom data handling interface
//
// There are two types of Custom packet.
// One for Login stage, the other for config and play stage.
// The first one called "Custom Query", and the second one called "Custom Payload".
// We can know the different by their name, the "query" is one request to one response, paired.
// But the second one can be sent in any order.
//
// And the custome payload packet seems to be same in config stage and play stage.
// How do we provide API for that?

case packetid.ClientboundConfigDisconnect:
const ErrStage = "disconnect"
var reason chat.Message
err := p.Scan(&reason)
if err != nil {
return ConfigErr{"disconnect", err}
return ConfigErr{ErrStage, err}
}
return ConfigErr{"disconnect", DisconnectErr(reason)}
return ConfigErr{ErrStage, DisconnectErr(reason)}

case packetid.ClientboundConfigFinishConfiguration:
err := conn.WritePacket(pk.Marshal(
Expand All @@ -100,18 +109,19 @@ func (c *Client) joinConfiguration(conn *net.Conn) error {
return nil

case packetid.ClientboundConfigKeepAlive:
const ErrStage = "keep alive"
var keepAliveID pk.Long
err := p.Scan(&keepAliveID)
if err != nil {
return ConfigErr{"keep alive", err}
return ConfigErr{ErrStage, err}
}
// send it back
err = conn.WritePacket(pk.Marshal(
packetid.ServerboundConfigKeepAlive,
keepAliveID,
))
if err != nil {
return ConfigErr{"keep alive", err}
return ConfigErr{ErrStage, err}
}

case packetid.ClientboundConfigPing:
Expand All @@ -138,7 +148,7 @@ func (c *Client) joinConfiguration(conn *net.Conn) error {
// return ConfigErr{"registry data", err}
// }

case packetid.ClientboundConfigResourcePackPop: // TODO
case packetid.ClientboundConfigResourcePackPop:
var id pk.Option[pk.UUID, *pk.UUID]
err := p.Scan(&id)
if err != nil {
Expand Down Expand Up @@ -187,33 +197,58 @@ func (c *Client) joinConfiguration(conn *net.Conn) error {
if err != nil {
return ConfigErr{"store cookie", err}
}
// TODO: trnasfer to the server
// TODO: trnasfer to the specific server
// How does it work? Just connect the new server, and re-start at handshake?

case packetid.ClientboundConfigUpdateEnabledFeatures:
err := p.Scan(pk.Array((*[]pk.Identifier)(unsafe.Pointer(&c.ConfigData.FeatureFlags))))
features := []pk.Identifier{}
err := p.Scan(pk.Array(&features))
if err != nil {
return ConfigErr{"update enabled features", err}
}
c.ConfigHandler.EnableFeature(features)

case packetid.ClientboundConfigUpdateTags:
// TODO: Handle Tags
case packetid.ClientboundConfigSelectKnownPacks:
const ErrStage = "select known packs"
packs := []DataPack{}
err := p.Scan(pk.Array(&packs))
if err != nil {
return ConfigErr{"select known packs", err}
return ConfigErr{ErrStage, err}
}
knwonPacks := c.ConfigHandler.SelectDataPacks(packs)
err = conn.WritePacket(pk.Marshal(
packetid.ServerboundConfigSelectKnownPacks,
pk.Array(knwonPacks),
))
if err != nil {
return ConfigErr{"select known packs", err}
return ConfigErr{ErrStage, err}
}

case packetid.ClientboundConfigCustomReportDetails:
const ErrStage = "custom report details"
var length pk.VarInt
var title, description pk.String
r := bytes.NewReader(p.Data)
_, err := length.ReadFrom(r)
if err != nil {
return ConfigErr{ErrStage, err}
}
for i := 0; i < int(length); i++ {
_, err = title.ReadFrom(r)
if err != nil {
return ConfigErr{ErrStage, err}
}
_, err = description.ReadFrom(r)
if err != nil {
return ConfigErr{ErrStage, err}
}
c.CustomReportDetails[string(title)] = string(description)
}

case packetid.ClientboundConfigServerLinks:
// TODO
}
}
}
Expand Down Expand Up @@ -270,6 +305,8 @@ func (d *DefaultConfigHandler) SetCookie(key pk.Identifier, payload []byte) {
d.cookies[key] = payload
}

func (d *DefaultConfigHandler) EnableFeature(features []pk.Identifier) {}

func (d *DefaultConfigHandler) PushResourcePack(res ResourcePack) {
d.resourcesPack = append(d.resourcesPack, res)
}
Expand Down
3 changes: 3 additions & 0 deletions net/packet/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (a Ary[LEN]) ReadFrom(r io.Reader) (n int64, err error) {
} else {
n += nn
}
if Len < 0 {
return n, errors.New("array length less than zero")
}

array := reflect.ValueOf(a.Ary)
for array.Kind() == reflect.Ptr {
Expand Down

0 comments on commit 40b32df

Please sign in to comment.