Example of Chunk packet. NOT TESTED
This commit is contained in:
@ -186,7 +186,6 @@ func drawSection(s *save.Chunk, img *image.RGBA) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// decode section
|
// decode section
|
||||||
//bpb := int(math.Max(4, math.Ceil(math.Log2(float64(len(s.Palette))))))
|
|
||||||
|
|
||||||
// decode status
|
// decode status
|
||||||
data := *(*[]uint64)(unsafe.Pointer(&s.BlockStates)) // convert []int64 into []uint64
|
data := *(*[]uint64)(unsafe.Pointer(&s.BlockStates)) // convert []int64 into []uint64
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
package save
|
package save
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"github.com/Tnze/go-mc/data/packetid"
|
||||||
|
pk "github.com/Tnze/go-mc/net/packet"
|
||||||
"github.com/Tnze/go-mc/save/region"
|
"github.com/Tnze/go-mc/save/region"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestColumn(t *testing.T) {
|
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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
package save
|
package save
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/Tnze/go-mc/nbt"
|
"github.com/Tnze/go-mc/nbt"
|
||||||
pk "github.com/Tnze/go-mc/net/packet"
|
pk "github.com/Tnze/go-mc/net/packet"
|
||||||
"io"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type BlockState interface {
|
type BlockState interface {
|
||||||
|
Reference in New Issue
Block a user