Implement multiplayer persistence and vanilla lighting

This commit is contained in:
Master290 2026-07-21 09:21:44 +03:00
parent 8f7cacf9d9
commit cae06eb97e
47 changed files with 3784 additions and 465 deletions

View file

@ -86,6 +86,13 @@ func locationIndex(localX, localZ int) int { return (localZ << 5) | localX }
// ReadChunk returns the decompressed NBT payload for the chunk, or
// ErrChunkNotFound when the chunk is absent.
func (r *RegionFile) ReadChunk(localX, localZ int) ([]byte, error) {
if localX < 0 || localX > 31 || localZ < 0 || localZ > 31 {
return nil, fmt.Errorf("world: local coordinates out of bounds")
}
r.mu.Lock()
defer r.mu.Unlock()
loc := r.offsets[locationIndex(localX, localZ)]
if loc == 0 {
return nil, ErrChunkNotFound
@ -96,9 +103,6 @@ func (r *RegionFile) ReadChunk(localX, localZ int) ([]byte, error) {
return nil, fmt.Errorf("world: invalid sector offset %d", sectorOffset)
}
r.mu.Lock()
defer r.mu.Unlock()
// 4-byte length then payload (compression byte + compressed data).
var lenBuf [4]byte
if _, err := r.f.ReadAt(lenBuf[:], int64(sectorOffset)*sectorSize); err != nil {
@ -124,7 +128,14 @@ func (r *RegionFile) ReadChunk(localX, localZ int) ([]byte, error) {
// WriteChunk stores the NBT payload for the chunk, allocating (or reusing)
// sectors and updating the offset + timestamp tables.
func (r *RegionFile) WriteChunk(localX, localZ int, nbt []byte) error {
compressed := append([]byte{compressionZlib}, zlibDeflate(nbt)...)
if localX < 0 || localX > 31 || localZ < 0 || localZ > 31 {
return fmt.Errorf("world: local coordinates out of bounds")
}
deflated, err := zlibDeflate(nbt)
if err != nil {
return err
}
compressed := append([]byte{compressionZlib}, deflated...)
// +4 for the length prefix; sectors needed to hold everything.
totalLen := 4 + len(compressed)
sectorsNeeded := (totalLen + sectorSize - 1) / sectorSize
@ -229,4 +240,4 @@ var _ = io.EOF
// (zlib helpers live in compress.go to keep this file format-focused; the
// references below are satisfied there.)
var _ = bytes.Equal
var _ = bytes.Equal