summaryrefslogtreecommitdiff
path: root/mcmap/examples/replace/main.go
blob: d289522ca1a400960d647a3543e6b5b9c133d4f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// replace replaces all iron blocks with diamond blocks.
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
		chunk.Iter(func(x, y, z int, blk *mcmap.Block) {
			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 := chunk.MarkUnused(); 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)
	}
}