Skip to content

Commit

Permalink
chat/translate.go: Added way to encapsulate translations with, for ex…
Browse files Browse the repository at this point in the history
…ample, colours.
  • Loading branch information
Sandertv committed Dec 20, 2024
1 parent 34289b5 commit 02cd499
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
6 changes: 3 additions & 3 deletions server/player/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ func (chat *Chat) WriteString(s string) (n int, err error) {
return len(s), nil
}

func (chat *Chat) Writet(t Translation) {
func (chat *Chat) Writet(t Translatable, a ...any) {
chat.m.Lock()
defer chat.m.Unlock()
for _, subscriber := range chat.subscribers {
if translator, ok := subscriber.(Translator); ok {
translator.Messaget(t)
translator.Messaget(t, a...)
continue
}
subscriber.Message(t.String())
subscriber.Message(t.F(a...).String())
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/player/chat/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Subscriber interface {
}

type Translator interface {
Messaget(t Translation)
Messaget(t Translatable, a ...any)
}

// StdoutSubscriber is an implementation of Subscriber that forwards messages
Expand Down
13 changes: 10 additions & 3 deletions server/player/chat/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package chat

import (
"fmt"
"github.com/sandertv/gophertunnel/minecraft/text"
)

var JoinMessage = translate("%multiplayer.player.joined", 1, "%v joined the game")
var QuitMessage = translate("%multiplayer.player.left", 1, "%v left the game")
var MessageJoin = Translate("%multiplayer.player.joined", 1, "%v joined the game").Enc("<yellow>%v</yellow>")
var MessageQuit = Translate("%multiplayer.player.left", 1, "%v left the game").Enc("<yellow>%v</yellow>")

func translate(format string, params int, fallback string) Translatable {
func Translate(format string, params int, fallback string) Translatable {
return Translatable{format: format, params: params, fallback: fallback}
}

Expand All @@ -17,6 +18,12 @@ type Translatable struct {
fallback string
}

func (t Translatable) Enc(format string) Translatable {
t.format = text.Colourf(format, t.format)
t.fallback = text.Colourf(format, t.fallback)
return t
}

func (t Translatable) F(a ...any) Translation {
if len(a) != t.params {
panic(fmt.Sprintf("translation '%v' requires exactly %v parameters, got %v", t.format, t.params, len(a)))
Expand Down
4 changes: 2 additions & 2 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ func (p *Player) Messagef(f string, a ...any) {
p.session().SendMessage(fmt.Sprintf(f, a...))
}

func (p *Player) Messaget(t chat.Translation) {
p.session().SendTranslation(t)
func (p *Player) Messaget(t chat.Translatable, a ...any) {
p.session().SendTranslation(t.F(a...))
}

// SendPopup sends a formatted popup to the player. The popup is shown above the hotbar of the player and
Expand Down
6 changes: 2 additions & 4 deletions server/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/protocol/login"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
"github.com/sandertv/gophertunnel/minecraft/text"
"io"
"log/slog"
"net"
Expand Down Expand Up @@ -225,7 +224,7 @@ func (s *Session) Spawn(c Controllable, tx *world.Tx) {

chat.Global.Subscribe(c)
if s.joinMessage != "" {
_, _ = fmt.Fprintln(chat.Global, text.Colourf("<yellow>%v</yellow>", fmt.Sprintf(s.joinMessage, s.conn.IdentityData().DisplayName)))
chat.Global.Writet(chat.MessageJoin, s.conn.IdentityData().DisplayName)
}

go s.background()
Expand Down Expand Up @@ -256,8 +255,7 @@ func (s *Session) close(tx *world.Tx, c Controllable) {
s.chunkLoader.Close(tx)

if s.quitMessage != "" {
chat.Global.Writet(chat.QuitMessage.F(s.conn.IdentityData().DisplayName))
_, _ = fmt.Fprintln(chat.Global, text.Colourf("<yellow>%v</yellow>", fmt.Sprintf(s.quitMessage, s.conn.IdentityData().DisplayName)))
chat.Global.Writet(chat.MessageQuit, s.conn.IdentityData().DisplayName)
}
chat.Global.Unsubscribe(c)

Expand Down

0 comments on commit 02cd499

Please sign in to comment.