update comments
This commit is contained in:
@ -17,6 +17,7 @@ import (
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
||||
)
|
||||
|
||||
// The Manager is used to receive and send chat messages.
|
||||
type Manager struct {
|
||||
c *bot.Client
|
||||
p *basic.Player
|
||||
@ -26,6 +27,7 @@ type Manager struct {
|
||||
sign.SignatureCache
|
||||
}
|
||||
|
||||
// New returns a new chat manager.
|
||||
func New(c *bot.Client, p *basic.Player, pl *playerlist.PlayerList, events EventsHandler) *Manager {
|
||||
m := &Manager{
|
||||
c: c,
|
||||
@ -37,7 +39,7 @@ func New(c *bot.Client, p *basic.Player, pl *playerlist.PlayerList, events Event
|
||||
if events.SystemChat != nil {
|
||||
c.Events.AddListener(bot.PacketHandler{
|
||||
Priority: 64, ID: packetid.ClientboundSystemChat,
|
||||
F: m.handleSystemMessage,
|
||||
F: m.handleSystemChat,
|
||||
})
|
||||
}
|
||||
if events.PlayerChatMessage != nil {
|
||||
@ -55,7 +57,7 @@ func New(c *bot.Client, p *basic.Player, pl *playerlist.PlayerList, events Event
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *Manager) handleSystemMessage(p pk.Packet) error {
|
||||
func (m *Manager) handleSystemChat(p pk.Packet) error {
|
||||
var msg chat.Message
|
||||
var overlay pk.Boolean
|
||||
if err := p.Scan(&msg, &overlay); err != nil {
|
||||
@ -149,7 +151,7 @@ func (m *Manager) handleDisguisedChat(packet pk.Packet) error {
|
||||
}
|
||||
|
||||
// SendMessage send chat message to server.
|
||||
// Currently only support offline-mode or "Not Secure" chat
|
||||
// Doesn't support sending message with signature currently.
|
||||
func (m *Manager) SendMessage(msg string) error {
|
||||
if len(msg) > 256 {
|
||||
return errors.New("message length greater than 256")
|
||||
|
@ -2,8 +2,28 @@ package msg
|
||||
|
||||
import "github.com/Tnze/go-mc/chat"
|
||||
|
||||
// EventsHandler is a collection of event handlers.
|
||||
// Fill the fields with your handler functions and pass this struct to [New] to create the msg manager.
|
||||
// The handler functions will be called when the corresponding event is triggered.
|
||||
// Leave the fields as nil if you don't want to handle the event.
|
||||
type EventsHandler struct {
|
||||
SystemChat func(msg chat.Message, overlay bool) error
|
||||
// SystemChat handles messages sent by gaming system.
|
||||
//
|
||||
// In vanilla client:
|
||||
// If overlay is false, the message will be displayed in the chat box.
|
||||
// If overlay is true, the message will be displayed on the top of the hot-bar.
|
||||
SystemChat func(msg chat.Message, overlay bool) error
|
||||
|
||||
// PlayerChatMessage handles messages sent by players.
|
||||
//
|
||||
// Message signing system is added in 1.19. The message and its context could be signed by the player's private key.
|
||||
// The manager tries to verify the message signature through the player's public key,
|
||||
// and return the result as validated boolean.
|
||||
PlayerChatMessage func(msg chat.Message, validated bool) error
|
||||
DisguisedChat func(msg chat.Message) error
|
||||
|
||||
// DisguisedChat handles DisguisedChat message.
|
||||
//
|
||||
// DisguisedChat message used to send system chat.
|
||||
// Now it is used to send messages from "/say" command from server console.
|
||||
DisguisedChat func(msg chat.Message) error
|
||||
}
|
||||
|
Reference in New Issue
Block a user