-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstate.go
68 lines (57 loc) · 1.34 KB
/
state.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
package main
import (
"encoding/json"
"os"
"path"
"github.com/sirupsen/logrus"
)
type State struct {
ChunkX int `json:"chunk_x"`
ChunkY int `json:"chunk_y"`
ChunkZ int `json:"chunk_z"`
RemovedChunks int `json:"removed_chunks"`
RetainedChunks int `json:"retained_chunks"`
ProcessedChunks int `json:"processed_chunks"`
FromX int `json:"from_x"`
FromZ int `json:"from_z"`
ToX int `json:"to_x"`
ToY int `json:"to_y"`
ToZ int `json:"to_z"`
Delay int `json:"delay"`
}
const filename = "mapcleaner.json"
// current state
var state = &State{
ChunkX: -400,
ChunkY: -400,
ChunkZ: -400,
FromX: -400,
FromZ: -400,
ToX: 400,
ToY: 400,
ToZ: 400,
}
func LoadState() error {
file_path := path.Join(wd, filename)
info, err := os.Stat(file_path)
if info == nil || !info.Mode().IsRegular() {
logrus.Warn("Creating new state config (this is normal on the first run)")
return SaveState()
}
if err != nil {
return err
}
data, err := os.ReadFile(file_path)
if err != nil {
return err
}
return json.Unmarshal(data, state)
}
func SaveState() error {
file_path := path.Join(wd, filename)
f, err := os.OpenFile(file_path, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
return err
}
return json.NewEncoder(f).Encode(state)
}