A chat.Message can be deserialized from json array.

This commit is contained in:
Tnze
2022-05-30 11:25:56 +08:00
parent 3c078fc503
commit 6eb5f35c75
3 changed files with 46 additions and 38 deletions

View File

@ -1,9 +1,12 @@
package chat
import "encoding/json"
// HoverEvent defines an event that occurs when this component hovered over.
type HoverEvent struct {
Action string `json:"action"`
Value []HoverSub `json:"value"` // The issue i found is the fact the json is different now, it wasnt parsing properly because Message was invalid, its a listed dict.
Action string `json:"action"`
Contents json.RawMessage `json:"contents"` // Didn't handled yet
Value Message `json:"value"` // Legacy
}
type HoverSub struct {
@ -12,7 +15,7 @@ type HoverSub struct {
}
// ShowText show the text to display.
func ShowText(text []HoverSub) *HoverEvent {
func ShowText(text Message) *HoverEvent {
return &HoverEvent{
Action: "show_text",
Value: text,
@ -23,29 +26,17 @@ func ShowText(text []HoverSub) *HoverEvent {
// Item is encoded as the S-NBT format, nbt.StringifiedMessage could help.
// See: https://wiki.vg/Chat#:~:text=show_item,in%20red%20instead.
func ShowItem(item string) *HoverEvent {
T := Text(item)
return &HoverEvent{
Action: "show_item",
Value: []HoverSub{
{
Color: T.Color,
Text: T.Text,
},
},
Value: Text(item),
}
}
// ShowEntity show an entity describing by the S-NBT, nbt.StringifiedMessage could help.
// See: https://wiki.vg/Chat#:~:text=show_entity,given%20entity%20loaded.
func ShowEntity(entity string) *HoverEvent {
T := Text(entity)
return &HoverEvent{
Action: "show_entity",
Value: []HoverSub{
{
Color: T.Color,
Text: T.Color,
},
},
Value: Text(entity),
}
}