Skip to content

Commit

Permalink
Chunk converters return error now
Browse files Browse the repository at this point in the history
  • Loading branch information
Tnze committed May 21, 2022
1 parent 5f3f66e commit 1932cbe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
5 changes: 4 additions & 1 deletion examples/frameworkServer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ func loadAllChunks(dim *server.SimpleDim, file string, rx, rz int) error {
if err := c.Load(data); err != nil {
return err
}
chunk := level.ChunkFromSave(&c)
chunk, err := level.ChunkFromSave(&c)
if err != nil {
return err
}
dim.LoadChunk(level.ChunkPos{X: rx<<5 + x, Z: rz<<5 + z}, chunk)
}
}
Expand Down
28 changes: 17 additions & 11 deletions level/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,19 @@ var biomesNames = []string{
}

// ChunkFromSave convert save.Chunk to level.Chunk.
func ChunkFromSave(c *save.Chunk) *Chunk {
func ChunkFromSave(c *save.Chunk) (*Chunk, error) {
secs := len(c.Sections)
sections := make([]Section, secs)
for _, v := range c.Sections {
i := int32(v.Y) - c.YPos
var err error
sections[i].BlockCount, sections[i].States, err = readStatesPalette(v.BlockStates.Palette, v.BlockStates.Data)
if err != nil {
panic(err)
return nil, err
}
sections[i].Biomes, err = readBiomesPalette(v.Biomes.Palette, v.Biomes.Data)
if err != nil {
panic(err)
return nil, err
}
sections[i].SkyLight = v.SkyLight
sections[i].BlockLight = v.BlockLight
Expand All @@ -220,7 +220,7 @@ func ChunkFromSave(c *save.Chunk) *Chunk {
OceanFloor: NewBitStorage(bitsForHeight, 16*16, oceanFloor),
WorldSurface: NewBitStorage(bitsForHeight, 16*16, worldSurface),
},
}
}, nil
}

func readStatesPalette(palette []save.BlockState, data []uint64) (blockCount int16, paletteData *PaletteContainer[BlocksState], err error) {
Expand Down Expand Up @@ -261,24 +261,28 @@ func readBiomesPalette(palette []string, data []uint64) (*PaletteContainer[Biome
}

// ChunkToSave convert level.Chunk to save.Chunk
func ChunkToSave(c *Chunk, dst *save.Chunk) {
func ChunkToSave(c *Chunk, dst *save.Chunk) (err error) {
secs := len(c.Sections)
sections := make([]save.Section, secs)
for i, v := range c.Sections {
s := &sections[i]
states := &s.BlockStates
biomes := &s.Biomes
s.Y = int8(int32(i) + dst.YPos)
states.Palette, states.Data = writeStatesPalette(v.States)
states.Palette, states.Data, err = writeStatesPalette(v.States)
if err != nil {
return
}
biomes.Palette, biomes.Data = writeBiomesPalette(v.Biomes)
s.SkyLight = v.SkyLight
s.BlockLight = v.BlockLight
}
dst.Sections = sections
//dst.Heightmaps.MotionBlocking = c.HeightMaps.MotionBlocking.Raw()
return
}

func writeStatesPalette(paletteData *PaletteContainer[BlocksState]) (palette []save.BlockState, data []uint64) {
func writeStatesPalette(paletteData *PaletteContainer[BlocksState]) (palette []save.BlockState, data []uint64, err error) {
rawPalette := paletteData.palette.export()
palette = make([]save.BlockState, len(rawPalette))
var buffer bytes.Buffer
Expand All @@ -287,11 +291,13 @@ func writeStatesPalette(paletteData *PaletteContainer[BlocksState]) (palette []s
palette[i].Name = b.ID()

buffer.Reset()
if err := nbt.NewEncoder(&buffer).Encode(b, ""); err != nil {
panic(err)
err = nbt.NewEncoder(&buffer).Encode(b, "")
if err != nil {
return
}
if _, err := nbt.NewDecoder(&buffer).Decode(&palette[i].Properties); err != nil {
panic(err)
_, err = nbt.NewDecoder(&buffer).Decode(&palette[i].Properties)
if err != nil {
return
}
}
data = append(data, paletteData.data.Raw()...)
Expand Down

0 comments on commit 1932cbe

Please sign in to comment.