summaryrefslogtreecommitdiff
path: root/mcmap/examples/replace
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2013-08-12 12:50:57 +0200
committerKevin Chabowski <kevin@kch42.de>2013-08-12 12:50:57 +0200
commita44c4ad107a197a92e9ffdde38593fd7012a309e (patch)
tree8b9f1652f1fa516dd1cfed47c4f03187fbf00117 /mcmap/examples/replace
parentb9be995232e8299d504e7898a9d6c5384664b8ee (diff)
downloadgomcmap-a44c4ad107a197a92e9ffdde38593fd7012a309e.tar.gz
gomcmap-a44c4ad107a197a92e9ffdde38593fd7012a309e.tar.bz2
gomcmap-a44c4ad107a197a92e9ffdde38593fd7012a309e.zip
Writing maps implemented
Diffstat (limited to 'mcmap/examples/replace')
-rw-r--r--mcmap/examples/replace/.gitignore1
-rw-r--r--mcmap/examples/replace/main.go66
2 files changed, 67 insertions, 0 deletions
diff --git a/mcmap/examples/replace/.gitignore b/mcmap/examples/replace/.gitignore
new file mode 100644
index 0000000..6e8b374
--- /dev/null
+++ b/mcmap/examples/replace/.gitignore
@@ -0,0 +1 @@
+replace
diff --git a/mcmap/examples/replace/main.go b/mcmap/examples/replace/main.go
new file mode 100644
index 0000000..f7bad96
--- /dev/null
+++ b/mcmap/examples/replace/main.go
@@ -0,0 +1,66 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "github.com/kch42/gomcmap/mcmap"
+ "os"
+)
+
+func main() {
+ path := flag.String("path", "", "Path to region directory")
+ flag.Parse()
+
+ if *path == "" {
+ flag.Usage()
+ os.Exit(1)
+ }
+
+ region, err := mcmap.OpenRegion(*path, true)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Could not open region: %s\n", err)
+ os.Exit(1)
+ }
+
+chunkLoop:
+ for chunkPos := range region.AllChunks() {
+ cx, cz := chunkPos.X, chunkPos.Z
+ chunk, err := region.Chunk(cx, cz)
+ switch err {
+ case nil:
+ case mcmap.NotAvailable:
+ continue chunkLoop
+ default:
+ fmt.Fprintf(os.Stderr, "Error while getting chunk (%d, %d): %s\n", cx, cz, err)
+ os.Exit(1)
+ }
+
+ modified := false
+ for y := 0; y < 256; y++ {
+ for x := 0; x < 16; x++ {
+ for z := 0; z < 16; z++ {
+ blk := chunk.Block(x, y, z)
+ if blk.ID == mcmap.BlkBlockOfIron {
+ blk.ID = mcmap.BlkBlockOfDiamond
+ modified = true
+ }
+ }
+ }
+ }
+
+ if modified {
+ fmt.Printf("Modified chunk %d, %d.\n", cx, cz)
+ chunk.MarkModified()
+ }
+
+ if err := region.UnloadChunk(cx, cz); err != nil {
+ fmt.Fprintf(os.Stderr, "Error while unloading chunk %d, %d: %s\n", cx, cz, err)
+ os.Exit(1)
+ }
+ }
+
+ if err := region.Save(); err != nil {
+ fmt.Fprintf(os.Stderr, "Error while saving: %s\n", err)
+ os.Exit(1)
+ }
+}