fix: Added hack to find the chunk's position from the region file it is in

This commit is contained in:
Nicholas Novak 2023-11-16 00:24:09 -08:00
parent 4fd4b6112f
commit 70879ec312
2 changed files with 20 additions and 1 deletions

View File

@ -1,6 +1,9 @@
package loading package loading
import ( import (
"strconv"
"strings"
"git.nicholasnovak.io/nnovak/spatial-db/world" "git.nicholasnovak.io/nnovak/spatial-db/world"
"github.com/Tnze/go-mc/save" "github.com/Tnze/go-mc/save"
"github.com/Tnze/go-mc/save/region" "github.com/Tnze/go-mc/save/region"
@ -16,6 +19,17 @@ func LoadRegionFile(fileName string) ([]world.ChunkData, error) {
} }
defer regionFile.Close() defer regionFile.Close()
// Parse the name of the region to find its position within the world
nameParts := strings.Split(fileName, ".")
regionX, err := strconv.Atoi(nameParts[1])
if err != nil {
return nil, err
}
regionY, err := strconv.Atoi(nameParts[2])
if err != nil {
return nil, err
}
// A region file is a 32x32 grid of chunks // A region file is a 32x32 grid of chunks
chunks := []world.ChunkData{} chunks := []world.ChunkData{}
@ -37,6 +51,9 @@ func LoadRegionFile(fileName string) ([]world.ChunkData, error) {
var chunkData world.ChunkData var chunkData world.ChunkData
chunkData.FromMCAChunk(chunk) chunkData.FromMCAChunk(chunk)
chunkData.Pos.X = regionX*32 + i
chunkData.Pos.Z = regionY*32 + j
chunks = append(chunks, chunkData) chunks = append(chunks, chunkData)
} }
} }

View File

@ -58,8 +58,10 @@ var LoadSaveDirCommand = &cobra.Command{
// Save each chunk to a separate file // Save each chunk to a separate file
for _, chunk := range chunks { for _, chunk := range chunks {
chunkFilename := chunk.Pos.ToFileName()
outfile, err := os.OpenFile( outfile, err := os.OpenFile(
filepath.Join(saveOutputDir, chunk.Pos.ToFileName()), filepath.Join(saveOutputDir, chunkFilename),
os.O_WRONLY|os.O_CREATE|os.O_APPEND, os.O_WRONLY|os.O_CREATE|os.O_APPEND,
0664, 0664,
) )