add more configuration supports

This commit is contained in:
Tnze
2023-11-19 02:55:30 +08:00
parent 326761e3e8
commit 3761fdbefe
4 changed files with 58 additions and 7 deletions

View File

@ -22,6 +22,7 @@ type Client struct {
Events Events Events Events
LoginPlugin map[string]func(data []byte) ([]byte, error) LoginPlugin map[string]func(data []byte) ([]byte, error)
ConfigData
} }
func (c *Client) Close() error { func (c *Client) Close() error {

View File

@ -1,13 +1,26 @@
package bot package bot
import ( import (
"unsafe"
"github.com/Tnze/go-mc/chat" "github.com/Tnze/go-mc/chat"
"github.com/Tnze/go-mc/data/packetid" "github.com/Tnze/go-mc/data/packetid"
"github.com/Tnze/go-mc/nbt"
"github.com/Tnze/go-mc/net" "github.com/Tnze/go-mc/net"
pk "github.com/Tnze/go-mc/net/packet" pk "github.com/Tnze/go-mc/net/packet"
"github.com/Tnze/go-mc/registry"
) )
type ConfigData struct {
Registries registry.NetworkCodec
ResourcePack struct {
URL string
Hash string
Forced bool
PromptMessage *chat.Message // Optional
}
FeatureFlags []string
}
type ConfigErr struct { type ConfigErr struct {
Stage string Stage string
Err error Err error
@ -72,17 +85,54 @@ func (c *Client) joinConfiguration(conn *net.Conn) error {
} }
case packetid.ClientboundConfigPing: case packetid.ClientboundConfigPing:
var pingID pk.Int
err := p.Scan(&pingID)
if err != nil {
return ConfigErr{"ping", err}
}
// send it back
err = conn.WritePacket(pk.Marshal(
packetid.ServerboundConfigPong,
pingID,
))
if err != nil {
return ConfigErr{"pong", err}
}
case packetid.ClientboundConfigRegistryData: case packetid.ClientboundConfigRegistryData:
var registryCodec nbt.RawMessage err := p.Scan(pk.NBT(&c.ConfigData.Registries))
err := p.Scan(pk.NBT(&registryCodec))
if err != nil { if err != nil {
return ConfigErr{"registry data", err} return ConfigErr{"registry data", err}
} }
// TODO: Handle registries
case packetid.ClientboundConfigResourcePack: case packetid.ClientboundConfigResourcePack:
var Url, Hash pk.String
var Forced pk.Boolean
var PromptMessage pk.Option[chat.Message, *chat.Message]
err := p.Scan(
&Url,
&Hash,
&Forced,
&PromptMessage,
)
if err != nil {
return ConfigErr{"resource pack", err}
}
c.ConfigData.ResourcePack.URL = string(Url)
c.ConfigData.ResourcePack.Hash = string(Hash)
c.ConfigData.ResourcePack.Forced = bool(Forced)
if PromptMessage.Has {
c.ConfigData.ResourcePack.PromptMessage = &PromptMessage.Val
}
case packetid.ClientboundConfigUpdateEnabledFeatures: case packetid.ClientboundConfigUpdateEnabledFeatures:
err := p.Scan(pk.Array((*[]pk.Identifier)(unsafe.Pointer(&c.ConfigData.FeatureFlags))))
if err != nil {
return ConfigErr{"update enabled features", err}
}
case packetid.ClientboundConfigUpdateTags: case packetid.ClientboundConfigUpdateTags:
// TODO: Handle Tags
} }
} }
} }

View File

@ -88,7 +88,7 @@ func (m *Manager) handlePlayerChat(packet pk.Packet) error {
if !ok { if !ok {
return InvalidChatPacket return InvalidChatPacket
} }
ct := m.p.WorldInfo.RegistryCodec.ChatType.FindByID(chatType.ID) ct := m.c.Registries.ChatType.FindByID(chatType.ID)
if ct == nil { if ct == nil {
return InvalidChatPacket return InvalidChatPacket
} }
@ -141,7 +141,7 @@ func (m *Manager) handleDisguisedChat(packet pk.Packet) error {
return err return err
} }
ct := m.p.WorldInfo.RegistryCodec.ChatType.FindByID(chatType.ID) ct := m.c.Registries.ChatType.FindByID(chatType.ID)
if ct == nil { if ct == nil {
return InvalidChatPacket return InvalidChatPacket
} }

View File

@ -41,7 +41,7 @@ func (w *World) onPlayerSpawn(pk.Packet) error {
func (w *World) handleLevelChunkWithLightPacket(packet pk.Packet) error { func (w *World) handleLevelChunkWithLightPacket(packet pk.Packet) error {
var pos level.ChunkPos var pos level.ChunkPos
_, currentDimType := w.p.WorldInfo.RegistryCodec.DimensionType.Find(w.p.DimensionType) _, currentDimType := w.c.ConfigData.Registries.DimensionType.Find(w.p.DimensionType)
if currentDimType == nil { if currentDimType == nil {
return errors.New("dimension type " + w.p.DimensionType + " not found") return errors.New("dimension type " + w.p.DimensionType + " not found")
} }