-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchunk.go
258 lines (203 loc) · 4.69 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package anvil
/*
level
Copyright (c) 2019 beito
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
*/
import (
"fmt"
"github.com/beito123/level/block"
"github.com/beito123/nbt"
)
// NewChunk returns new Chunk
func NewChunk(x, y int, format ChunkFormat) *Chunk {
return &Chunk{
x: x,
y: y,
subChunks: make([]*SubChunk, 16),
ChunkFormat: format,
}
}
// ReadChunk returns new Chunk with CompoundTag
func ReadChunk(x, y int, b []byte) (*Chunk, error) {
stream := nbt.NewStreamBytes(nbt.BigEndian, b)
tag, err := stream.ReadTag()
if err != nil {
return nil, err
}
com, ok := tag.(*nbt.Compound)
if !ok {
return nil, fmt.Errorf("level.anvil.region: expected to be CompoundTag, but it passed different tag(%sTag)", nbt.GetTagName(tag.ID()))
}
var format ChunkFormat = &ChunkFormatV112{}
if com.Has("DataVersion") {
ver, err := com.GetInt("DataVersion")
if err != nil {
return nil, err
}
switch {
case ver >= 1519:
format = &ChunkFormatV113{}
}
}
return format.Read(com)
}
// Chunk is
type Chunk struct {
x int
y int
lastUpdate int64
inhabitedTime int64
biomes []int
subChunks []*SubChunk
ChunkFormat ChunkFormat
}
// X returns x coordinate
func (chunk *Chunk) X() int {
return chunk.x
}
// Y returns y coordinate
func (chunk *Chunk) Y() int {
return chunk.y
}
// SubChunks returns sub chunks
func (chunk *Chunk) SubChunks() []*SubChunk {
return chunk.subChunks
}
// GetSubChunk returns a sub chunk at the y index
// you can set 0-15 at y
func (chunk *Chunk) GetSubChunk(y int) (*SubChunk, bool) {
if len(chunk.subChunks) >= y {
return nil, false
}
return chunk.subChunks[y], true
}
// AtSubChunk returns a sub chunk at the y (chunk coordinate)
func (chunk *Chunk) AtSubChunk(y int) (*SubChunk, bool) {
index := y / 16
if len(chunk.subChunks) >= index {
return nil, false
}
return chunk.subChunks[index], true
}
// GetBlock gets a block at the xyz (chunk coordinate)
func (chunk *Chunk) GetBlock(x, y, z int) (*block.Block, error) {
sub, ok := chunk.AtSubChunk(y)
if !ok {
return nil, nil
}
bl, err := sub.AtBlock(x, y, z)
if err != nil {
return nil, nil
}
return bl.ToBlockData(), nil
}
// Save saves the chunk, returns CompoundTag
func (chunk *Chunk) Save() (*nbt.Compound, error) {
return nil, nil // TODO
}
// ChunkFormat is a chunk format for a version
type ChunkFormat interface {
Read(tag *nbt.Compound) (*Chunk, error)
}
// ChunkFormatV112 is a chunk format for v1.12
type ChunkFormatV112 struct {
}
func (format *ChunkFormatV112) Read(tag *nbt.Compound) (*Chunk, error) {
chunk := NewChunk(0, 0, format)
com, err := tag.GetCompound("Level")
if err != nil {
return nil, err
}
xPos, err := com.GetInt("xPos")
if err != nil {
return nil, err
}
zPos, err := com.GetInt("zPos")
if err != nil {
return nil, err
}
chunk.x = int(xPos)
chunk.y = int(zPos)
// Biomes
if com.Has("Biomes") {
biomes, err := com.GetByteArray("Biomes")
if err != nil {
return nil, err
}
chunk.biomes = make([]int, len(biomes))
for i, biome := range biomes {
chunk.biomes[i] = int(biome)
}
}
// Subchunks
sections, err := com.GetList("Sections")
if err != nil {
return nil, err
}
subchunkFormat := &SubChunkFormatV112{}
chunk.subChunks = make([]*SubChunk, 16)
for _, entry := range sections {
sec, ok := entry.(*nbt.Compound)
if !ok {
return nil, fmt.Errorf("couldn't convert to *Compound")
}
sub, err := subchunkFormat.Read(sec)
if err != nil {
return nil, err
}
chunk.subChunks[sub.Y] = sub
}
return chunk, nil
}
// ChunkFormatV113 is a chunk format for v1.13
type ChunkFormatV113 struct {
}
func (format *ChunkFormatV113) Read(tag *nbt.Compound) (*Chunk, error) {
chunk := NewChunk(0, 0, format)
com, err := tag.GetCompound("Level")
if err != nil {
return nil, err
}
xPos, err := com.GetInt("xPos")
if err != nil {
return nil, err
}
zPos, err := com.GetInt("zPos")
if err != nil {
return nil, err
}
chunk.x = int(xPos)
chunk.y = int(zPos)
// Biomes
if com.Has("Biomes") {
biomes, err := com.GetIntArray("Biomes")
if err != nil {
return nil, err
}
chunk.biomes = make([]int, len(biomes))
for i, biome := range biomes {
chunk.biomes[i] = int(biome)
}
}
// Subchunks
sections, err := com.GetList("Sections")
if err != nil {
return nil, err
}
subchunkFormat := &SubChunkFormatV113{}
chunk.subChunks = make([]*SubChunk, 16)
for _, entry := range sections {
sec, ok := entry.(*nbt.Compound)
if !ok {
return nil, fmt.Errorf("couldn't convert to *Compound")
}
sub, err := subchunkFormat.Read(sec)
if err != nil {
return nil, err
}
chunk.subChunks[sub.Y] = sub
}
return nil, nil
}