Tnze / go-mc

Collection of Go libraries for Minecraft
https://go-mc.github.io/tutorial/
MIT License
858 stars 115 forks source link

Fix: NBT custom writer #264

Closed Edouard127 closed 1 year ago

Edouard127 commented 1 year ago

This pull request fixes issues with gzip/zlib writers, where the NBT is not written to the buffer until the writer is closed

Example:

func TestEncoder_EncodeNil(t *testing.T) {
    type Struct struct {
        Hi string
    }

    arg := Struct{
        Hi: "A",
    }

    var buf bytes.Buffer
    writer := zlib.NewWriter(&buf)
    err := NewEncoder(writer).Encode(arg, "")
    if err != nil {
        t.Fatal(err)
    }

    writer.Close() // Removing this does not write to the buffer

    fmt.Println(buf.Bytes())
}
Tnze commented 1 year ago

No, it's your responsibility to flush or close the writer.

For example, consider the case we are sending NBT through network, of course we can't close the connection every time we send a NBT message.