github.com/df-mc/dragonfly@v0.9.13/server/player/chat/subscriber.go (about)

     1  package chat
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/sandertv/gophertunnel/minecraft/text"
     6  	"strings"
     7  )
     8  
     9  // Subscriber represents an entity that may subscribe to a Chat. In order to do so, the Subscriber must
    10  // implement methods to send messages to it.
    11  type Subscriber interface {
    12  	// Message sends a formatted message to the subscriber. The message is formatted as it would when using
    13  	// fmt.Println.
    14  	Message(a ...any)
    15  }
    16  
    17  // StdoutSubscriber is an implementation of Subscriber that forwards messages sent to the chat to the stdout.
    18  type StdoutSubscriber struct{}
    19  
    20  // Message ...
    21  func (c StdoutSubscriber) Message(a ...any) {
    22  	s := make([]string, len(a))
    23  	for i, b := range a {
    24  		s[i] = fmt.Sprint(b)
    25  	}
    26  	t := text.ANSI(strings.Join(s, " "))
    27  	if !strings.HasSuffix(t, "\n") {
    28  		fmt.Println(t)
    29  		return
    30  	}
    31  	fmt.Print(t)
    32  }