Fix DisguisedChat handling

This commit is contained in:
Tnze
2024-04-27 22:54:37 +08:00
parent d55614cdb7
commit 01d05326e0

View File

@ -1,6 +1,7 @@
package chat package chat
import ( import (
"bytes"
"errors" "errors"
"io" "io"
"strconv" "strconv"
@ -19,26 +20,35 @@ func (m Message) WriteTo(w io.Writer) (int64, error) {
return pk.NBT(&m).WriteTo(w) return pk.NBT(&m).WriteTo(w)
} }
func (m Message) MarshalNBT() ([]byte, error) { func (m Message) TagType() byte {
return nbt.TagCompound
}
func (m Message) MarshalNBT(w io.Writer) error {
if m.Translate != "" { if m.Translate != "" {
return nbt.Marshal(translateMsg(m)) return nbt.NewEncoder(w).Encode(translateMsg(m), "")
} else { } else {
return nbt.Marshal(rawMsgStruct(m)) return nbt.NewEncoder(w).Encode(rawMsgStruct(m), "")
} }
} }
func (m *Message) UnmarshalNBT(raw []byte) (err error) { func (m *Message) UnmarshalNBT(tagType byte, r nbt.DecoderReader) error {
if len(raw) == 0 { // Re-combine the tagType into the reader, and create a nbt decoder
return io.EOF tagReader := bytes.NewReader([]byte{tagType})
} decoder := nbt.NewDecoder(io.MultiReader(tagReader, r))
switch raw[0] { decoder.NetworkFormat(true) // TagType directlly followed the body
switch tagType {
case nbt.TagString: case nbt.TagString:
return nbt.Unmarshal(raw, &m.Text) // Unmarshal as jsonString _, err := decoder.Decode(&m.Text)
return err
case nbt.TagCompound: case nbt.TagCompound:
return nbt.Unmarshal(raw, (*rawMsgStruct)(m)) // Unmarshal as jsonMsg _, err := decoder.Decode((*rawMsgStruct)(m))
return err
case nbt.TagList: case nbt.TagList:
return nbt.Unmarshal(raw, &m.Extra) // Unmarshal as []Message _, err := decoder.Decode(&m.Extra)
return err
default: default:
return errors.New("unknown chat message type: '" + strconv.FormatUint(uint64(raw[0]), 16) + "'") return errors.New("unknown chat message type: '" + strconv.FormatUint(uint64(tagType), 16) + "'")
} }
} }