Example of Chunk packet. NOT TESTED
This commit is contained in:
@ -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
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user