forked from Tnze/go-mc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chunk.go
117 lines (102 loc) · 2.61 KB
/
chunk.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package save
import (
"bytes"
"compress/gzip"
"compress/zlib"
"errors"
"io"
"github.com/maxsupermanhd/go-vmc/v762/nbt"
)
// Chunk is 16* chunk
type Chunk struct {
BlockEntities []nbt.RawMessage `nbt:"block_entities"`
BlockTicks nbt.RawMessage `nbt:"block_ticks"`
CarvingMasks map[string][]uint64
DataVersion int32
Entities []nbt.RawMessage `nbt:"entities"`
FluidTicks nbt.RawMessage `nbt:"fluid_ticks"`
Heightmaps map[string][]uint64 // keys: "WORLD_SURFACE_WG", "WORLD_SURFACE", "WORLD_SURFACE_IGNORE_SNOW", "OCEAN_FLOOR_WG", "OCEAN_FLOOR", "MOTION_BLOCKING", "MOTION_BLOCKING_NO_LEAVES"
InhabitedTime int64
IsLightOn byte `nbt:"isLightOn"`
LastUpdate int64
Lights []nbt.RawMessage
PostProcessing nbt.RawMessage
Sections []Section `nbt:"sections"`
Status string
Structures nbt.RawMessage `nbt:"structures"`
XPos int32 `nbt:"xPos"`
YPos int32 `nbt:"yPos"`
ZPos int32 `nbt:"zPos"`
}
type Section struct {
Y int8
BlockStates PaletteContainer[BlockState] `nbt:"block_states"`
Biomes PaletteContainer[BiomeState] `nbt:"biomes"`
SkyLight []byte
BlockLight []byte
}
type PaletteContainer[T any] struct {
Palette []T `nbt:"palette"`
Data []uint64 `nbt:"data"`
}
type BlockState struct {
Name string
Properties nbt.RawMessage
}
type BiomeState string
// Load read column data from []byte
func (c *Chunk) Load(data []byte) (err error) {
var r io.Reader = bytes.NewReader(data[1:])
switch data[0] {
default:
err = errors.New("unknown compression")
case 1:
r, err = gzip.NewReader(r)
case 2:
r, err = zlib.NewReader(r)
case 3:
// none compression
}
if err != nil {
return err
}
d := nbt.NewDecoder(r)
// d.DisallowUnknownFields()
_, err = d.Decode(c)
return
}
func (c *Chunk) Data(compressingType byte) ([]byte, error) {
var buff bytes.Buffer
buff.WriteByte(compressingType)
var w io.Writer
switch compressingType {
default:
return nil, errors.New("unknown compression")
case 1:
w = gzip.NewWriter(&buff)
case 2:
w = zlib.NewWriter(&buff)
case 3:
w = &buff
}
err := nbt.NewEncoder(w).Encode(c, "")
return buff.Bytes(), err
}
type Entities struct {
Pos, Motion [3]float64
Rotation [3]float32
FallDistance float32
Fire, Air int16
OnGround bool
Invulnerable bool
PortalCooldown int32
UUID [4]int32
CustomName string
CustomNameVisible bool
Silent bool
NoGravity bool
Glowing bool
TicksFrozen int32
HasVisualFire bool
Tags []string
}