forked from huin/chunkymonkey
/
store.go
126 lines (96 loc) · 3.58 KB
/
store.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
package chunkstore
import (
"chunkymonkey/gamerules"
. "chunkymonkey/types"
"fmt"
"log"
"nbt"
)
type ChunkReadResult struct {
Reader IChunkReader
Err error
}
type IChunkStore interface {
// Serve() serves requests in the foreground.
Serve()
ReadChunk(chunkLoc ChunkXz) (result <-chan ChunkReadResult)
SupportsWrite() bool
Writer() IChunkWriter
// Submits the set chunk data for writing. The chunk writer must not be
// altered any further after calling this.
WriteChunk(writer IChunkWriter)
}
type IChunkReader interface {
// Returns the chunk location.
ChunkLoc() ChunkXz
// Returns the block IDs in the chunk.
Blocks() []byte
// Returns the block data in the chunk.
BlockData() []byte
// Returns the block light data in the chunk.
BlockLight() []byte
// Returns the sky light data in the chunk.
SkyLight() []byte
// Returns the height map data in the chunk.
HeightMap() []int
// Return a slice of the entities (items, mobs) within the chunk.
Entities() []gamerules.INonPlayerEntity
// Return a slice of the tile entities (chests, furnaces, etc.) within the
// chunk.
TileEntities() []gamerules.ITileEntity
// For low-level NBT access. Not for regular use. It's possible that this
// might return nil if the underlying system doesn't use NBT.
RootTag() nbt.ITag
}
// IChunkWriter is the interface for objects that accept chunk data and write
// it. These are created by IChunkWriteableStore for use by a chunk to store a
// snapshot of its current state into. The Set* functions make copies of the
// data passed in, so that the original data structures passed in can be
// modified upon return.
type IChunkWriter interface {
// ChunkLoc returns the chunk location.
ChunkLoc() ChunkXz
// SetChunkLoc sets the chunk location.
SetChunkLoc(loc ChunkXz)
// SetBlocks sets the block IDs in the chunk.
SetBlocks(blocks []byte)
// SetBlockData sets the block data in the chunk.
SetBlockData(blockData []byte)
// SetBlockLight sets the block light data in the chunk.
SetBlockLight(blockLight []byte)
// SetSkyLight sets the sky light data in the chunk.
SetSkyLight(skyLight []byte)
// SetHeightMap sets the height map data in the chunk.
SetHeightMap(heightMap []int)
// SetEntities sets a list of the entities (items, mobs) within the chunk.
SetEntities(entities map[EntityId]gamerules.INonPlayerEntity)
// SetTileEntities sets a list of the tile entities (chests, furnaces, etc.)
// within the chunk.
SetTileEntities(tileEntities map[BlockIndex]gamerules.ITileEntity)
}
// Given the NamedTag for a level.dat, returns an appropriate
// IChunkStoreForeground.
func ChunkStoreForLevel(worldPath string, levelData nbt.ITag, dimension DimensionId) (store IChunkStoreForeground, err error) {
versionTag, ok := levelData.Lookup("Data/version").(*nbt.Int)
if !ok {
log.Fatalln("Invalid world format! (Is it pre-anvil?)")
} else {
switch version := versionTag.Value; version {
case 19132:
log.Fatalln("Chunky Monkey only supports the Anvil world format!")
case 19133:
store, err = newChunkStoreAnvil(worldPath, dimension)
default:
err = UnknownLevelVersion(version)
}
}
return
}
type UnknownLevelVersion int32
func (err UnknownLevelVersion) Error() string {
return fmt.Sprintf("Unknown level version %d", err)
}
type NoSuchChunkError bool
func (err NoSuchChunkError) Error() string {
return "Chunk does not exist."
}