update 1.19.2 bot, chat support
This commit is contained in:
@ -20,7 +20,7 @@
|
||||
// - HealthChange
|
||||
// - Death
|
||||
//
|
||||
// You must manully attach the [EventsListener] to the [Client] as needed.
|
||||
// You must manually attach the [EventsListener] to the [Client] as needed.
|
||||
package basic
|
||||
|
||||
import (
|
||||
@ -38,22 +38,23 @@ type Player struct {
|
||||
isSpawn bool
|
||||
}
|
||||
|
||||
func NewPlayer(c *bot.Client, settings Settings) *Player {
|
||||
b := &Player{c: c, Settings: settings}
|
||||
func NewPlayer(c *bot.Client, settings Settings, events EventsListener) *Player {
|
||||
p := &Player{c: c, Settings: settings}
|
||||
c.Events.AddListener(
|
||||
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundLogin, F: b.handleLoginPacket},
|
||||
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundKeepAlive, F: b.handleKeepAlivePacket},
|
||||
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundPlayerPosition, F: b.handlePlayerPosition},
|
||||
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundRespawn, F: b.handleRespawnPacket},
|
||||
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.ClientboundPlayerPosition, F: p.handlePlayerPosition},
|
||||
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundRespawn, F: p.handleRespawnPacket},
|
||||
)
|
||||
return b
|
||||
events.attach(p)
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *Player) Respawn() error {
|
||||
const PerformRespawn = 0
|
||||
|
||||
err := p.c.Conn.WritePacket(pk.Marshal(
|
||||
packetid.ServerboundClientCommand,
|
||||
int32(packetid.ServerboundClientCommand),
|
||||
pk.VarInt(PerformRespawn),
|
||||
))
|
||||
if err != nil {
|
||||
|
@ -5,35 +5,30 @@ import (
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
"github.com/Tnze/go-mc/data/packetid"
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
||||
"io"
|
||||
)
|
||||
|
||||
type EventsListener struct {
|
||||
GameStart func() error
|
||||
ChatMsg func(c *PlayerMessage) error
|
||||
SystemMsg func(c chat.Message, overlay bool) error
|
||||
Disconnect func(reason chat.Message) error
|
||||
HealthChange func(health float32) error
|
||||
Death func() error
|
||||
}
|
||||
|
||||
// Attach your event listener to the client.
|
||||
// attach your event listener to the client.
|
||||
// The functions are copied when attaching, and modify on [EventListener] doesn't affect after that.
|
||||
func (e EventsListener) Attach(c *bot.Client) {
|
||||
func (e EventsListener) attach(p *Player) {
|
||||
if e.GameStart != nil {
|
||||
attachJoinGameHandler(c, e.GameStart)
|
||||
}
|
||||
if e.ChatMsg != nil {
|
||||
attachPlayerMsg(c, e.ChatMsg)
|
||||
attachJoinGameHandler(p.c, e.GameStart)
|
||||
}
|
||||
if e.SystemMsg != nil {
|
||||
attachSystemMsg(c, e.SystemMsg)
|
||||
attachSystemMsg(p.c, e.SystemMsg)
|
||||
}
|
||||
if e.Disconnect != nil {
|
||||
attachDisconnect(c, e.Disconnect)
|
||||
attachDisconnect(p.c, e.Disconnect)
|
||||
}
|
||||
if e.HealthChange != nil || e.Death != nil {
|
||||
attachUpdateHealth(c, e.HealthChange, e.Death)
|
||||
attachUpdateHealth(p.c, e.HealthChange, e.Death)
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,60 +41,6 @@ func attachJoinGameHandler(c *bot.Client, handler func() error) {
|
||||
})
|
||||
}
|
||||
|
||||
type PlayerMessage struct {
|
||||
}
|
||||
|
||||
func (p *PlayerMessage) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
// SignedMessageHeader
|
||||
// MessageSignature
|
||||
// SignedMessageBody
|
||||
// Optional<Component>
|
||||
// FilterMask
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
type ChatType struct {
|
||||
ID int32
|
||||
Name chat.Message
|
||||
TargetName *chat.Message
|
||||
}
|
||||
|
||||
func (c *ChatType) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
var hasTargetName pk.Boolean
|
||||
n1, err := (*pk.VarInt)(&c.ID).ReadFrom(r)
|
||||
if err != nil {
|
||||
return n1, err
|
||||
}
|
||||
n2, err := c.Name.ReadFrom(r)
|
||||
if err != nil {
|
||||
return n1 + n2, err
|
||||
}
|
||||
n3, err := hasTargetName.ReadFrom(r)
|
||||
if err != nil {
|
||||
return n1 + n2 + n3, err
|
||||
}
|
||||
if hasTargetName {
|
||||
c.TargetName = new(chat.Message)
|
||||
n4, err := c.TargetName.ReadFrom(r)
|
||||
return n1 + n2 + n3 + n4, err
|
||||
}
|
||||
return n1 + n2 + n3, nil
|
||||
}
|
||||
|
||||
func attachPlayerMsg(c *bot.Client, handler func(c *PlayerMessage) error) {
|
||||
c.Events.AddListener(bot.PacketHandler{
|
||||
Priority: 64, ID: packetid.ClientboundPlayerChat,
|
||||
F: func(p pk.Packet) error {
|
||||
var message PlayerMessage
|
||||
var chatType ChatType
|
||||
if err := p.Scan(&message, &chatType); err != nil {
|
||||
return Error{err}
|
||||
}
|
||||
return handler(&message)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func attachSystemMsg(c *bot.Client, handler func(c chat.Message, overlay bool) error) {
|
||||
c.Events.AddListener(bot.PacketHandler{
|
||||
Priority: 64, ID: packetid.ClientboundSystemChat,
|
||||
|
@ -1,6 +1,7 @@
|
||||
package basic
|
||||
|
||||
import (
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
"unsafe"
|
||||
|
||||
"github.com/Tnze/go-mc/data/packetid"
|
||||
@ -46,9 +47,14 @@ type Dimension struct {
|
||||
MonsterSpawnBlockLightLimit int32 `nbt:"monster_spawn_block_light_limit"`
|
||||
}
|
||||
|
||||
type ChatType struct {
|
||||
Chat chat.Decoration `nbt:"chat"`
|
||||
Narration chat.Decoration `nbt:"narration"`
|
||||
}
|
||||
|
||||
type RegistryCodec struct {
|
||||
// What is Below? (wiki.vg)
|
||||
ChatType Registry[nbt.RawMessage] `nbt:"minecraft:chat_type"`
|
||||
ChatType Registry[ChatType] `nbt:"minecraft:chat_type"`
|
||||
DimensionType Registry[Dimension] `nbt:"minecraft:dimension_type"`
|
||||
WorldGenBiome Registry[nbt.RawMessage] `nbt:"minecraft:worldgen/biome"`
|
||||
}
|
||||
@ -71,6 +77,18 @@ func (r *Registry[E]) Find(name string) *E {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Registry[E]) FindByID(id int32) *E {
|
||||
if id >= 0 && id < int32(len(r.Value)) && r.Value[id].ID == id {
|
||||
return &r.Value[id].Element
|
||||
}
|
||||
for i := range r.Value {
|
||||
if r.Value[i].ID == id {
|
||||
return &r.Value[i].Element
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type PlayerInfo struct {
|
||||
EID int32 // The player's Entity ID (EID).
|
||||
Hardcore bool // Is hardcore
|
||||
@ -106,7 +124,7 @@ func (p *Player) handleLoginPacket(packet pk.Packet) error {
|
||||
return Error{err}
|
||||
}
|
||||
err = p.c.Conn.WritePacket(pk.Marshal( //PluginMessage packet
|
||||
packetid.ServerboundCustomPayload,
|
||||
int32(packetid.ServerboundCustomPayload),
|
||||
pk.Identifier("minecraft:brand"),
|
||||
pk.String(p.Settings.Brand),
|
||||
))
|
||||
@ -115,7 +133,7 @@ func (p *Player) handleLoginPacket(packet pk.Packet) error {
|
||||
}
|
||||
|
||||
err = p.c.Conn.WritePacket(pk.Marshal(
|
||||
packetid.ServerboundClientInformation, // Client settings
|
||||
int32(packetid.ServerboundClientInformation), // Client settings
|
||||
pk.String(p.Settings.Locale),
|
||||
pk.Byte(p.Settings.ViewDistance),
|
||||
pk.VarInt(p.Settings.ChatMode),
|
||||
|
@ -12,7 +12,7 @@ func (p Player) handleKeepAlivePacket(packet pk.Packet) error {
|
||||
}
|
||||
// Response
|
||||
err := p.c.Conn.WritePacket(pk.Packet{
|
||||
ID: packetid.ServerboundKeepAlive,
|
||||
ID: int32(packetid.ServerboundKeepAlive),
|
||||
Data: packet.Data,
|
||||
})
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user