-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschematic.go
51 lines (45 loc) · 1.27 KB
/
schematic.go
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
package schematic
import (
"compress/gzip"
"fmt"
"github.com/df-mc/dragonfly/server/world"
"github.com/sandertv/gophertunnel/minecraft/nbt"
"io"
"io/ioutil"
"sync"
)
// Schematic represents a parsed schematic with blocks in it. It may be used to read blocks from.
type Schematic struct {
*schematic
}
var (
mu sync.Mutex
decompressor *gzip.Reader
// Check to ensure that *schematic satisfies the world.Structure interface.
_ world.Structure = (*schematic)(nil)
)
// FromReader attempts to read a Schematic from an io.Reader passed. If successful, the schematic with all its
// data is returned.
func FromReader(r io.Reader) (Schematic, error) {
mu.Lock()
if decompressor == nil {
reader, err := gzip.NewReader(r)
if err != nil {
return Schematic{}, fmt.Errorf("decompress schematic: %w", err)
}
decompressor = reader
} else {
if err := decompressor.Reset(r); err != nil {
return Schematic{}, fmt.Errorf("decompress schematic: %w", err)
}
}
b, _ := ioutil.ReadAll(decompressor)
_ = decompressor.Close()
mu.Unlock()
m := make(map[string]interface{})
if err := nbt.UnmarshalEncoding(b, &m, nbt.BigEndian); err != nil {
return Schematic{}, fmt.Errorf("parse schematic: %w", err)
}
s := &schematic{Data: m}
return Schematic{schematic: s}, s.init()
}