frameworkServer works perfect now

This commit is contained in:
Tnze
2022-03-09 17:21:28 +08:00
parent aa8e611644
commit 0bbd279dd1
8 changed files with 73 additions and 34320 deletions

View File

@ -2,13 +2,14 @@ package level
import (
"bytes"
"fmt"
"io"
"math/bits"
"strings"
"sync"
"unsafe"
"github.com/Tnze/go-mc/data/block"
"github.com/Tnze/go-mc/level/block"
pk "github.com/Tnze/go-mc/net/packet"
"github.com/Tnze/go-mc/save"
)
@ -113,11 +114,18 @@ func ChunkFromSave(c *save.Chunk, secs int) *Chunk {
statePalette := v.BlockStates.Palette
stateRawPalette := make([]int, len(statePalette))
for i, v := range statePalette {
// TODO: Consider the properties of block, not only index the block name
stateRawPalette[i] = int(stateIDs[strings.TrimPrefix(v.Name, "minecraft:")])
if v.Name != "minecraft:air" {
b := v.Block()
if b == nil {
panic(fmt.Errorf("block not found: %#v", v))
}
if !isAir(b) {
blockCount++
}
var ok bool
stateRawPalette[i], ok = block.ToStateID(b)
if !ok {
panic(fmt.Errorf("state id not found: %#v", b))
}
}
biomesData := *(*[]uint64)((unsafe.Pointer)(&v.Biomes.Data))
@ -153,18 +161,6 @@ func ChunkFromSave(c *save.Chunk, secs int) *Chunk {
}
}
// TODO: This map should be moved to data/block.
var stateIDs = make(map[string]uint32)
func init() {
for i, v := range block.StateID {
name := block.ByID[v].Name
if _, ok := stateIDs[name]; !ok {
stateIDs[name] = i
}
}
}
func (c *Chunk) WriteTo(w io.Writer) (int64, error) {
data, err := c.Data()
if err != nil {
@ -211,8 +207,8 @@ func (s *Section) GetBlock(i int) int {
return s.States.Get(i)
}
func (s *Section) SetBlock(i int, v int) {
// TODO: Handle cave air and void air
if s.States.Get(i) != 0 {
b, _ := block.FromStateID(s.States.Get(i))
if isAir(b) {
s.blockCount--
}
if v != 0 {
@ -263,3 +259,12 @@ func (l *lightData) WriteTo(w io.Writer) (int64, error) {
pk.Array(l.BlockLight),
}.WriteTo(w)
}
func isAir(b block.Block) bool {
switch b.(type) {
case block.Air, block.CaveAir, block.VoidAir:
return true
default:
return false
}
}