Skip to content

allow player.Handler to modify chat format #1010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/player/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Handler interface {
// HandleChat handles a message sent in the chat by a player. ctx.Cancel() may be called to cancel the
// message being sent in chat.
// The message may be changed by assigning to *message.
HandleChat(ctx *Context, message *string)
HandleChat(ctx *Context, formatter *func(username, message string) string, message *string)
// HandleFoodLoss handles the food bar of a player depleting naturally, for example because the player was
// sprinting and jumping. ctx.Cancel() may be called to cancel the food points being lost.
HandleFoodLoss(ctx *Context, from int, to *int)
Expand Down Expand Up @@ -171,7 +171,7 @@ func (NopHandler) HandleToggleSprint(*Context, bool)
func (NopHandler) HandleToggleSneak(*Context, bool) {}
func (NopHandler) HandleCommandExecution(*Context, cmd.Command, []string) {}
func (NopHandler) HandleTransfer(*Context, *net.UDPAddr) {}
func (NopHandler) HandleChat(*Context, *string) {}
func (NopHandler) HandleChat(*Context, *func(username, message string) string, *string) {}
func (NopHandler) HandleSkinChange(*Context, *skin.Skin) {}
func (NopHandler) HandleFireExtinguish(*Context, cube.Pos) {}
func (NopHandler) HandleStartBreak(*Context, cube.Pos) {}
Expand Down
7 changes: 5 additions & 2 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,15 @@ func (p *Player) RemoveBossBar() {
// Chat writes a message in the global chat (chat.Global). The message is prefixed with the name of the
// player and is formatted following the rules of fmt.Sprintln.
func (p *Player) Chat(msg ...any) {
formatter := func(username, message string) string {
return fmt.Sprintf("<%v> %v", username, message)
}
message := format(msg)
ctx := event.C(p)
if p.Handler().HandleChat(ctx, &message); ctx.Cancelled() {
if p.Handler().HandleChat(ctx, &formatter, &message); ctx.Cancelled() {
return
}
_, _ = fmt.Fprintf(chat.Global, "<%v> %v\n", p.Name(), message)
_, _ = fmt.Fprintf(chat.Global, formatter(p.Name(), message)+"\n")
}

// ExecuteCommand executes a command passed as the player. If the command could not be found, or if the usage
Expand Down