Skip to content

Commit

Permalink
use func(string, string) string as formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackjack200 committed Feb 10, 2025
1 parent 7bfd10b commit 1c8a6db
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
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, format *string, 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, *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
8 changes: 5 additions & 3 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,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) {
f := "<%v> %v\n"
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, &f, &message); ctx.Cancelled() {
if p.Handler().HandleChat(ctx, &formatter, &message); ctx.Cancelled() {
return
}
_, _ = fmt.Fprintf(chat.Global, f, 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

0 comments on commit 1c8a6db

Please sign in to comment.