Tnze / go-mc

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

Write Arrays without length prefix #270

Closed jobpaardekooper closed 9 months ago

jobpaardekooper commented 9 months ago

Is there a way to write an array to a packet without prefixing the length? Currently if you use pk.Array or pk.Ary it will prefix the length when you write the packet. But for some packets the client will already know the length from the context so it does not expect a prefixed length. Any way to write such an array in a nice way?

Tnze commented 9 months ago

You should implement net.FieldEncoder in your array type. And when you do so, write elements in a loop internally.

jobpaardekooper commented 9 months ago

Thanks for the quick response. Why is this not part of the primitive types provided by this library? Basically all fields in the protocol are here in the library and can be used except for these non-length-prefixed arrays.

Tnze commented 9 months ago

Didn't found any of this, which packet are you building?

jobpaardekooper commented 9 months ago

Currently I am working on the chunk packet. That includes an array of chunk sections that is not length-prefixed. This is because the client calculates the array length based on the world height it has received before. This is not the only packet though.

Tnze commented 9 months ago

There is an implement in this file: https://github.com/Tnze/go-mc/blob/master/level/chunk.go that type Chunk has a method named WriteTo().

I'm not sure it's up to date. Hope the piece of code can answer your question.