summaryrefslogtreecommitdiff
path: root/mcmap
diff options
context:
space:
mode:
Diffstat (limited to 'mcmap')
-rw-r--r--mcmap/chunk.go9
-rw-r--r--mcmap/examples/emeraldfinder/main.go3
-rw-r--r--mcmap/examples/mapper/main.go2
-rw-r--r--mcmap/examples/replace/main.go2
-rw-r--r--mcmap/prechunk.go4
-rw-r--r--mcmap/region.go7
6 files changed, 17 insertions, 10 deletions
diff --git a/mcmap/chunk.go b/mcmap/chunk.go
index 0ba12f8..496fd0c 100644
--- a/mcmap/chunk.go
+++ b/mcmap/chunk.go
@@ -53,6 +53,8 @@ type Chunk struct {
modified bool
blocks []Block // Ordered YZX
biomes []Biome // Ordered XZ
+
+ reg *Region
}
// MarkModified needs to be called, if some data of the chunk was modified.
@@ -90,4 +92,11 @@ func (c *Chunk) Biome(x, z int) Biome { return c.biomes[x*16+z] }
// SetBiome sets the biome at x,z.
func (c *Chunk) SetBiome(x, z int, bio Biome) { c.biomes[x*16+z] = bio }
+// MarkUnused marks the chunk as unused. If all chunks of a superchunk are marked as unused, the superchunk will be unloaded and saved (if needed).
+//
+// You must not use the chunk any longer, after you called this function.
+//
+// If the chunk was modified, call MarkModified BEFORE.
+func (c *Chunk) MarkUnused() error { return c.reg.unloadChunk(int(c.x), int(c.z)) }
+
// TODO: func (c *Chunk) RecalcHeightMap()
diff --git a/mcmap/examples/emeraldfinder/main.go b/mcmap/examples/emeraldfinder/main.go
index 8cd1260..d92ad3c 100644
--- a/mcmap/examples/emeraldfinder/main.go
+++ b/mcmap/examples/emeraldfinder/main.go
@@ -48,7 +48,6 @@ chunkLoop:
}
}
- chunk = nil
- region.UnloadChunk(cx, cz)
+ chunk.MarkUnused()
}
}
diff --git a/mcmap/examples/mapper/main.go b/mcmap/examples/mapper/main.go
index e998ce1..2cbcefb 100644
--- a/mcmap/examples/mapper/main.go
+++ b/mcmap/examples/mapper/main.go
@@ -60,7 +60,7 @@ chunkLoop:
}
}
- if err := region.UnloadChunk(cx, cz); err != nil {
+ if err := chunk.MarkUnused(); err != nil {
fmt.Fprintf(os.Stderr, "Error while unloading chunk %d, %d: %s\n", cx, cz, err)
os.Exit(1)
}
diff --git a/mcmap/examples/replace/main.go b/mcmap/examples/replace/main.go
index 127f446..e5fff78 100644
--- a/mcmap/examples/replace/main.go
+++ b/mcmap/examples/replace/main.go
@@ -54,7 +54,7 @@ chunkLoop:
chunk.MarkModified()
}
- if err := region.UnloadChunk(cx, cz); err != nil {
+ if err := chunk.MarkUnused(); err != nil {
fmt.Fprintf(os.Stderr, "Error while unloading chunk %d, %d: %s\n", cx, cz, err)
os.Exit(1)
}
diff --git a/mcmap/prechunk.go b/mcmap/prechunk.go
index 89febe7..ae0ea2a 100644
--- a/mcmap/prechunk.go
+++ b/mcmap/prechunk.go
@@ -83,8 +83,8 @@ func (pc *preChunk) getLevelTag() (nbt.TagCompound, error) {
return lvl, nil
}
-func (pc *preChunk) toChunk() (*Chunk, error) {
- c := Chunk{ts: pc.ts}
+func (pc *preChunk) toChunk(reg *Region) (*Chunk, error) {
+ c := Chunk{ts: pc.ts, reg: reg}
lvl, err := pc.getLevelTag()
if err != nil {
diff --git a/mcmap/region.go b/mcmap/region.go
index 67966aa..0e569da 100644
--- a/mcmap/region.go
+++ b/mcmap/region.go
@@ -198,7 +198,7 @@ func (reg *Region) Chunk(x, z int) (*Chunk, error) {
}
var err error
- if chunk, err = pc.toChunk(); err != nil {
+ if chunk, err = pc.toChunk(reg); err != nil {
return nil, err
}
sc.chunks[cPos] = chunk
@@ -211,8 +211,7 @@ func (reg *Region) Chunk(x, z int) (*Chunk, error) {
return chunk, nil
}
-// UnloadChunk marks a chunk as unused. If all chunks of a superchunk are marked as unused, the superchunk will be unloaded and saved (if needed).
-func (reg *Region) UnloadChunk(x, z int) error {
+func (reg *Region) unloadChunk(x, z int) error {
scx, scz, cx, cz := chunkToSuperchunk(x, z)
scPos := XZPos{scx, scz}
cPos := XZPos{cx, cz}
@@ -264,7 +263,7 @@ func (reg *Region) AllChunks() <-chan XZPos {
return ch
}
-// Save saves modified and unloaded chunks.
+// Save saves modified and unused chunks.
func (reg *Region) Save() error {
return reg.cleanSuperchunks(true)
}