Example of Chunk packet. NOT TESTED

This commit is contained in:
Tnze
2021-03-31 19:08:33 +08:00
parent adf77e2661
commit 075a3d3a0a
3 changed files with 72 additions and 2 deletions

View File

@ -186,7 +186,6 @@ func drawSection(s *save.Chunk, img *image.RGBA) {
return
}
// decode section
//bpb := int(math.Max(4, math.Ceil(math.Log2(float64(len(s.Palette))))))
// decode status
data := *(*[]uint64)(unsafe.Pointer(&s.BlockStates)) // convert []int64 into []uint64

View File

@ -1,9 +1,13 @@
package save
import (
"bytes"
"github.com/Tnze/go-mc/data/packetid"
pk "github.com/Tnze/go-mc/net/packet"
"github.com/Tnze/go-mc/save/region"
"math/rand"
"testing"
"unsafe"
)
func TestColumn(t *testing.T) {
@ -51,3 +55,69 @@ func BenchmarkColumn_Load(b *testing.B) {
}
}
}
func ExampleColumn_send() {
r, err := region.Open("/path/to/r.0.0.mca")
if err != nil {
panic(err)
}
chunkPos := [2]int{0, 0}
data, err := r.ReadSector(chunkPos[0], chunkPos[1])
if err != nil {
panic(err)
}
var c Column
if err := c.Load(data); err != nil {
panic(err)
}
var buf bytes.Buffer
var PrimaryBitMask pk.VarInt
for _, v := range c.Level.Sections {
if int8(v.Y) >= 0 && int8(v.Y) < 16 {
PrimaryBitMask |= 1 << v.Y
bpb := len(v.BlockStates) * 64 / (16 * 16 * 16)
hasPalette := pk.Boolean(bpb >= 9)
paletteLength := pk.VarInt(len(v.Palette))
dataArrayLength := pk.VarInt(len(v.BlockStates))
dataArray := (*[]pk.Long)(unsafe.Pointer(&v.BlockStates))
_, err := pk.Tuple{
pk.Short(0), // Block count
pk.UnsignedByte(bpb), // Bits Per Block
pk.Opt{
Has: &hasPalette,
Field: pk.Ary{
Len: &paletteLength,
Ary: nil, // TODO: We need translate v.Palette (with type of []Block) to state ID
},
}, // Palette
pk.Ary{Len: &dataArrayLength, Ary: dataArray}, // Data Array
}.WriteTo(&buf)
if err != nil {
panic(err)
}
}
}
size := pk.VarInt(buf.Len())
bal := pk.VarInt(len(c.Level.Biomes))
_ = pk.Marshal(
packetid.WorldParticles,
pk.Int(chunkPos[0]), // Chunk X
pk.Int(chunkPos[1]), // Chunk Y
pk.Boolean(true), // Full chunk
PrimaryBitMask, // PrimaryBitMask
pk.NBT(c.Level.Heightmaps), // Heightmaps
pk.Ary{
Len: &bal, // Biomes array length
Ary: c.Level.Biomes, // Biomes
},
pk.Ary{
Len: &size, // Size
Ary: pk.ByteArray(buf.Bytes()), // Data
},
pk.VarInt(0), // Block entities array length
)
}

View File

@ -1,9 +1,10 @@
package save
import (
"io"
"github.com/Tnze/go-mc/nbt"
pk "github.com/Tnze/go-mc/net/packet"
"io"
)
type BlockState interface {