summaryrefslogtreecommitdiff
path: root/leveldat.go
blob: 520a10758148e9255bb61c6867cd352f360ffab0 (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
package main

import (
	"errors"
	"fmt"
	"github.com/silvasur/gonbt/nbt"
	"io"
)

func invalidLevelDat(err error) error {
	return fmt.Errorf("Invalid level.dat: %s", err)
}

func getMapCenter(leveldat io.Reader) (int, int, error) {
	lvl, _, err := nbt.ReadGzipdNamedTag(leveldat)
	if err != nil {
		return 0, 0, err
	}

	if lvl.Type != nbt.TAG_Compound {
		return 0, 0, invalidLevelDat(errors.New("Root tag has wrong type"))
	}
	root := lvl.Payload.(nbt.TagCompound)

	data, err := root.GetCompound("Data")
	if err != nil {
		return 0, 0, invalidLevelDat(err)
	}

	x, err := data.GetInt("SpawnX")
	if err != nil {
		return 0, 0, invalidLevelDat(err)
	}

	z, err := data.GetInt("SpawnZ")
	if err != nil {
		return 0, 0, invalidLevelDat(err)
	}

	return int(x), int(z), nil
}