I am fairly new to Go, so I may be missing a workaround way of doing this, but it does not seem possible to create chat.Message
objects containing Extra
contents without modifying the chat
package.
For instance, consider this chat.Message
object with additional formatting, containing an Extra
block with its own formatting:
desc := chat.Message{
Text: "Line 1\n",
Color: "blue",
Italic: true,
Extra: []chat.jsonChat{{
Text: "Line 2",
Color: "green",
Bold: true,
}},
}
This will result in the following compiler error:
cannot refer to unexported name chat.jsonChat
If you were to replace jsonChat
with Message
, which is exported, you would get another compiler error:
cannot use []chat.Message literal (type []chat.Message) as type []chat.jsonChat in field value
This makes sense; even though Message
is identical to jsonChat
, because they are considered distinct types, this is not possible.
However, if you were to export jsonChat
by captializing it, this becomes possible:
desc := chat.JsonChat{
Text: "Line 1\n",
Color: "blue",
Italic: true,
Extra: []chat.JsonChat{{
Text: "Line 2",
Color: "green",
Bold: true,
}},
}
However, this requires modifying go-mc/chat/chatMsg.go
. I would rather not modify this package directly, since it complicates development somewhat. It surely makes Docker builds much more complicated than simply go get
-ing your package.
My use case for this is displaying formatted server messages to Minecraft clients in the server list. I am using go-mc
to create a "stub" Minecraft server, which will be shown to my players when the real Minecraft server is offline. When no players are online, I want to shut down or "hibernate" my Minecraft server. Then, if a player tries to view the server when the real Minecraft server is offline, they will see messages created in the "stub" server I am writing in Go using go-mc
. A player connecting to this "stub" server launches a request to start the real Minecraft server, which will shift the IP address to the real server and allow them to play.
In fact, using go-mc
, and the above example code (after modifying chatMsg.go
), I was able to create this stub server:
While I can create two lines by simply including a \n
in the main Text
block, I enjoy how easy color and text formatting is with go-mc
, and I would like to create lines of text with independent formatting from one another.
Is there a workaround to do this without modifying chatMsg.go
? Or can jsonChat
possibly be made public in a future release of go-mc
?
Thanks!
(P.S. When writing this issue, I noticed a bug in the way go-mc
formats messages. In my above example code, I set "Line 1" to Italic: true
. However, as you can see from the screenshot, it is not italicized. Bold and Obfuscated formatting works, but not Italic. I have set text to be italicized in server messages before, so I know it is possible to do this. Perhaps this belongs in its own issue?)